ZOJ 1789 The Suspects
2014-04-20 10:06
274 查看
与2833差不多
基本上只是merge函数那里的优先做父亲的条件改一下便可提高效率...
基本上只是merge函数那里的优先做父亲的条件改一下便可提高效率...
//The Suspects #include<cstdio> #include<memory.h> int parent[30001]; int find(int i) { while(parent[i] >= 0) i = parent[i]; return i; } void merge(int a,int b) { int c = find(a); int d = find(b); int temp = parent[c] + parent[d]; if(c != d) { if(d < c)//d的根小于c,d做父亲。。。因为0是感染者,这样建立查找时有效率 { parent[c] = d; parent[d] = temp; } else { parent[d] = c; parent[c] = temp; } } } int main() { //freopen("input.txt","r",stdin); int n,m; int k,a,b; while(scanf("%d%d",&n,&m) && n)//这里不能scanf("%d%d",&n,&m) && n && m) 因为m为0时同样可以 { memset(parent,-1,sizeof(parent)); while(m--) { scanf("%d",&k); scanf("%d",&a); k--; while(k--) { scanf("%d",&b); merge(a,b); } } printf("%d\n",-parent[0]); } return 0; }
相关文章推荐
- zoj - 1789 - The Suspects
- ZOJ 1789 The Suspects
- zoj1789-The Suspects
- zoj 1789 The Suspects
- zoj 1789 The Suspects
- ZOJ1789(The Suspects)j简单并查集
- zoj - 1789 - The Suspects
- zoj The Suspects 并查集
- POJ 1611 / ZOJ 1789: The Suspects - 并查集
- zoj 3547 The Boss on Mars 第36届ACM大连预选赛I题
- POJ-1611 The Suspects
- ZOJ 2770 Burn the Linked Camp
- ZOJ 2771 Get Out of the Glass 很普通的计数dp
- ZOJ 3675 Trim the Nails(bfs+状态压缩搜索)
- ZOJ-1056-The Worm Turns
- ZOJ 3772 Calculate the Function 解题报告
- zoj 3772 Calculate the Function monthly 14th
- ZOJ 2770- Burn the Linked Camp 差分约束
- ZOJ 2770 Burn the Linked Camp(差分约束)
- zoj 2416 Open the Lock