POJ 1161
来源:互联网 发布:淘宝军粮是怎么来的 编辑:程序博客网 时间:2024/05/07 00:33
第一道并查集,47ms ,还是很慢啊,不过是绞尽脑汁,调试了n次,做出来的,思路还是挺清晰地
#include <iostream>using namespace std;const int maxn=30010;int parent[maxn];int amount[maxn];int rank[maxn];int n,m;void init(){for(int i=0;i<n;i++){parent[i]=i;amount[i]=1;rank[i]=0;}}int find(int t){if(parent[t]!=t)return(find(parent[t]));return t;}void Union(int r1,int r2){if(rank[r1]>rank[r2]){parent[r2]=r1;amount[r1]+=amount[r2];}else{parent[r1]=r2;amount[r2]+=amount[r1];if(rank[r1]==rank[r2])rank[r2]++;}}int main(){while(scanf("%d%d",&n,&m)&&!(n==0&&m==0)){init();int i,j=0,g,f,r1,r2,t;for(i=0;i<m;i++){cin>>g;if(g){cin>>f;for(j=1;j<g;j++){cin>>t;r1=find(f);r2=find(t);if(r1!=r2){Union(r1,r2);}}}}int outcome=find(0);cout<<amount[outcome]<<endl;}return 0;}
- POJ 1161
- poj 1161
- Poj 1161
- poj 1161 Walls
- POJ 1161 Walls
- POJ 1161 walls
- POJ 1161 Floyd 构图
- POJ-1161-Walls
- POJ 1161 WALLS
- POJ 1161 WALLS (FLOYD)
- [POJ 1161] Walls 建图
- poj 1161 The Suspects
- POJ 1161 Walls
- poj 1161 The Suspects
- POJ
- poj
- POJ
- POJ
- 如何向android的framework里添加新API
- 《哥伦布传》观后感
- 为什么烧写了WinCE的系统重新烧写linux时候需要先将NAND FLASH全部擦除一遍之后再重新烧写linux
- rcp(插件开发)Action的那点事情
- Samba 服务器的安装配置及案例
- POJ 1161
- 用JWidow写一个可以在桌面上拖动的小时钟
- 2012.9.23 网易有道 机试
- SWT/Jface TableViewer update() 和 refresh()
- 判断单链表是否存在环,判断两个链表是否相交问题详解 .
- 十年
- 程序员之路——一个老程序员对刚上大学的学弟学妹的忠告
- 死锁
- rcp(插件开发)通过plugin.xml-改变产品风格