随笔分类 -  图论--dfs

摘要:http://acm.hdu.edu.cn/showproblem.php?pid=3720 1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 using namespace std;10 #define INF 0x3f3f3f11 mapf;12 struct node13 {14 char s[101],po[35];15 int v,k;16 }p[30];17 int pa[30],flag,w[30][30],maxz;18 void ... 阅读全文
posted @ 2013-11-03 22:36 _雨 阅读(347) 评论(0) 推荐(0)
摘要:1156求出每个联通块的黑白块数 然后再背包 二维的背包 要保证每个块都得取一个写的有些乱。。 1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 using namespace std; 8 vectored[110]; 9 int x,y,flag,g,f[110],o[110],n,q[110]; 10 int co[110],a[110],b[110],dp[110][110]; 11 int vis[110],p[110][110][2]; 12 void dfs(int u,in... 阅读全文
posted @ 2013-10-15 12:11 _雨 阅读(423) 评论(0) 推荐(0)
摘要:1137做过一样的 怎么又忘了 再一次搜超时不用回溯 1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 using namespace std; 9 #define N 1001010 int n,m;11 vectored[N];12 bool vis[N][N];13 int pa[100010],t;14 void dfs(int u)15 {16 int i;17 for(i = 0 ; i 1 ; i--)48 printf("%d ",pa[i])... 阅读全文
posted @ 2013-10-14 18:28 _雨 阅读(268) 评论(0) 推荐(0)
摘要:1124需要想那么一点点吧 一个连通块中肯定不需要伸进手不拿的情况 不是一个肯定会需要这种情况 然后注意一点 sum=0的时候 就输出0就可以了 不要再减一了 1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 using namespace std; 8 vectored[510]; 9 int a[510][55],vis[510],de[510];10 void dfs(int u)11 {12 int i;13 for(i = 0 ; i < (int)ed[u].size() ; i... 阅读全文
posted @ 2013-10-10 21:07 _雨 阅读(246) 评论(0) 推荐(0)
摘要:1742最小的是找联通块数 最大的找环 一个环算一个 其它的数各算一个 1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 using namespace std; 8 #define N 100010 9 int fa[N],vis[N];10 int minz,maxz,de[N],f[N],pp[N];11 void dfs(int u,int v)12 {13 de[u] = v;14 vis[u] = 1;15 f[v] = u;16 int i;17 if(... 阅读全文
posted @ 2013-10-10 19:45 _雨 阅读(345) 评论(0) 推荐(0)
摘要:这图最多3色就可以 搜2就行了 1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 using namespace std; 9 int vis[55][55],c[55][55],f[10],mm;10 int dis[6][2] = {{1,0},{0,1},{-1,0},{0,-1},{-1,1},{1,-1}},n;11 int flag,m;12 int judge(int x,int y)13 {14 if(x=n||y>=n)15 return 0;16 ... 阅读全文
posted @ 2013-10-07 15:06 _雨 阅读(194) 评论(0) 推荐(0)
摘要:1242简单dfs 往孩子方向搜一遍 父母方向搜一遍 输入还搞什么字符串。。 1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 using namespace std; 8 vectorfa[1010]; 9 vectorch[1010];10 int u,v,q[1010];11 int w[1010],vis1[1010],vis2[1010],o;12 void dfs1(int u)13 {14 int i;15 for(i = 0 ; i >s1)44 {45 ... 阅读全文
posted @ 2013-10-05 22:58 _雨 阅读(251) 评论(0) 推荐(0)
摘要:1033简单dfs 有一点小小的坑 就是图可能不连通 所以要从左上和右下都搜一下 加起来 从讨论里看到的讨论里看到一句好无奈的回复 “可不可以用中文呀。。。” 1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 using namespace std; 8 char s[40][40]; 9 int dis[4][2] = {0,1,1,0,-1,0,0,-1};10 int vis[40][40],n,num;11 int judge(int x,int y)12 {13 if(xn||y>n)14.. 阅读全文
posted @ 2013-10-05 21:28 _雨 阅读(197) 评论(0) 推荐(0)
摘要:1208简单dfs 对于每个数 两种情况 取还是不取 1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 using namespace std; 9 mapf;10 struct node11 {12 int a,b,c;13 }p[20];14 int n,maxz;15 int vis[60];16 int judge(node x,node y)17 {18 if(x.a==y.b||x.a==y.a||x.a==y.c)19 return 0;20 ... 阅读全文
posted @ 2013-10-05 14:37 _雨 阅读(269) 评论(0) 推荐(0)
摘要:链接dfs了 写得有点乱 1 #include 2 #include 3 #include 4 #include 5 #include 6 using namespace std; 7 #define INF 0xfffffff 8 struct node 9 { 10 int lx,ly,rx,ry,d; 11 }co[22]; 12 int kk[22][22],o[22]; 13 int num[22][22],oo[22],ans,fk[22]; 14 int n; 15 void dfs(int u,int de,int vis[]) 16 { 17 ... 阅读全文
posted @ 2013-09-03 18:17 _雨 阅读(219) 评论(0) 推荐(0)
摘要:链接想偷点懒用矩阵存 一直WA 后来看讨论说有重边改为邻接表 这题边可能走了不止一次 我设的最多两次可过 1 #include 2 #include 3 #include 4 #include 5 #include 6 using namespace std; 7 #define INF 0xfffffff 8 struct node 9 {10 int u,v,w1,w2,d,next;11 }ed[22];12 int head[12];13 int n,m,p[12][12],ans;14 int vis[12],t;15 void init()16 {17 t = 0... 阅读全文
posted @ 2013-09-03 09:35 _雨 阅读(181) 评论(0) 推荐(0)
摘要:链接这题其实是由bug的 一个串包含其它两个串的数据没有 所以就这么水了它吧 只处理两个串的关系就行了回来补点。。看了huge的博客 发现其实不是有Bug 题意没读清楚 必须首尾相连 像AGCT GC这样就不算。。降低了复杂 1 #include 2 #include 3 #include 4 #include 5 #include 6 using namespace std; 7 #define INF 0x3f3f3f 8 char s[22][22]; 9 int k[22],o[22][22],n;10 int vis[22],ans;11 void init()12 {13 ... 阅读全文
posted @ 2013-09-02 09:45 _雨 阅读(383) 评论(0) 推荐(0)
摘要:为了这题还去学了下迭代加深 回来还是不会写只好参考各大神的代码及题解了二分枚举最大可以切的块数 然后就是各种分析及优化USACO题解里写了7个优化。。问题分析抽象一下就可以发现,算法的本质是多重背包问题。 补充:这题与破锣乐队都是多个背包,不可重复放物品。区别在于破锣乐队要有顺序,此题不需要,这样此题就必须要搜索才行。 单个背包的问题我们可以用DP解决,但是对于这种问题我们只能用搜索了。 但是可以看一看这道题的数据规模:1 max{board} 那么这个rail应该舍去 By Clarkok 1 /* 2 ID: shangca2 3 LANG: C++ 4 TASK:... 阅读全文
posted @ 2013-08-28 19:36 _雨 阅读(376) 评论(0) 推荐(0)
摘要:链接把迭代加深理解错了 自己写了半天也没写对所谓迭代加深,就是在深度无上限的情况下,先预估一个深度(尽量小)进行搜索,如果没有找到解,再逐步放大深度搜索。这种方法虽然会导致重复的遍历 某些结点,但是由于搜索的复杂度是呈指数级别增加的,所以对于下一层搜索,前面的工作可以忽略不计,因而不会导致时间上的亏空。IDA*就是一个加了层数限制depth的DFS,超过了限制就不在搜索下去,如果在当前层数没有搜到目标状态,就加大层数限制depth,这里还只是一个IDA算法,并不是A*的。当然我们可以用A*的估计函数去剪枝,如果当前深度d+h()>depth的时候就可以不再搜索下去了,这样就是IDA*了。 阅读全文
posted @ 2013-08-27 22:09 _雨 阅读(654) 评论(0) 推荐(0)
摘要:链接这样的叫迭代吗。。最近多做些搜索题了要分行分列搜 判断满足条件 1 #include 2 #include 3 #include 4 #include 5 #include 6 using namespace std; 7 int o[8][8],n,m,q,p[8][8]; 8 int ans; 9 int judge(int x,int y)10 {11 int i,j,kk=0;12 if(o[x][y])13 return 0;14 for(i = x-1 ; i>= 0 ;i--)15 if(o[i][y]||p[i][y])16 ... 阅读全文
posted @ 2013-08-26 14:48 _雨 阅读(264) 评论(0) 推荐(0)
摘要:都忘了欧拉路径是什么了。。用dfs搜 标记边 刚开始直接从I-N搜 直接超时 2了 先找符合起点和终点的点搜 即度数是奇数d单dfs也超了 后来换了个姿势。。 1 /* 2 ID: shangca2 3 LANG: C++ 4 TASK: fence 5 */ 6 #include 7 #include 8 #include 9 #include10 #include11 using namespace std;12 int w[510][510],n,p[2010],flag,m,de[510],t;13 void dfs(int u,int d)14 {15 ... 阅读全文
posted @ 2013-08-20 21:59 _雨 阅读(231) 评论(0) 推荐(0)
摘要:http://poj.org/problem?id=2676一列一列的放 判断是否符合条件View Code 1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<stdlib.h> 5 using namespace std; 6 char s[20][20]; 7 int vx[20][20],vy[20][20],vis[20][20]; 8 int judge(int x,int y) 9 { 10 if(x>=0&&x< 阅读全文
posted @ 2013-01-27 20:28 _雨 阅读(197) 评论(0) 推荐(0)
摘要:http://poj.org/problem?id=1129四色定理 最多有四色 从1到四搜View Code 1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<stdlib.h> 5 using namespace std; 6 int n,w[100][100],co[100],mi,flag; 7 void dfs(int x,int v) 8 { 9 int i,j,f,k=v;10 if(flag)11 return ;12 if(x>n 阅读全文
posted @ 2013-01-27 19:44 _雨 阅读(153) 评论(0) 推荐(0)
摘要:http://poj.org/problem?id=2531把第i个节点 放在A集合中 加上没有放在A集合中的节点与之相连的数值 减去已经放在A节点中与之相连的数值View Code 1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 int num[25][25],n,va[50],vb[50],ma,sum; 6 void dfs(int x,int sum) 7 { 8 int i; 9 va[x] = 1;10 for(i = 1 阅读全文
posted @ 2013-01-26 15:27 _雨 阅读(221) 评论(0) 推荐(0)
摘要:http://poj.org/problem?id=1416乱七八糟的改,乱七八糟的错,最后过了样例 1A了 。对于每个分割开的数来说 有几种组合方式 搜下去View Code 1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #define INF 10000000 5 using namespace std; 6 int n,m,num[10],sum,g,vis[10],di,tt,mi,f; 7 char oo[10][10],o[10][10]; 8 void dfs(in 阅读全文
posted @ 2013-01-26 15:24 _雨 阅读(140) 评论(0) 推荐(0)