hdu 4496(并查集)
来源:互联网 发布:linux多线程编程 书籍 编辑:程序博客网 时间:2024/06/06 04:01
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4496、
思路:简单并查集应用,从后往前算就可以了。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 #define MAXN 10100 7 #define MAXM 1001000 8 9 struct Edge{10 int u,v;11 }edge[MAXM];12 13 int n,m,ans[MAXM];14 int parent[MAXN];15 int Find(int x)16 {17 if(x==parent[x]){18 return parent[x];19 }20 parent[x]=Find(parent[x]);21 return parent[x];22 }23 24 void Union(int u,int v)25 {26 int r1=Find(u),r2=Find(v);27 if(r1==r2)return ;28 parent[r1]=r2;29 }30 31 int main()32 {33 int k;34 while(~scanf("%d%d",&n,&m)){35 for(int i=0;i<n;i++)parent[i]=i;36 for(int i=0;i<m;i++){37 scanf("%d%d",&edge[i].u,&edge[i].v);38 }39 ans[m-1]=n;40 k=m-1;41 for(int i=m-1;i>=0;i--){42 int u=edge[i].u,v=edge[i].v;43 if(Find(u)!=Find(v)){44 Union(u,v);45 ans[k-1]=ans[k]-1;46 }else 47 ans[k-1]=ans[k];48 k--;49 }50 for(int i=0;i<m;i++){51 printf("%d\n",ans[i]);52 }53 }54 return 0;55 }56 57 58 59 60
0 0
- hdu 4496 并查集
- hdu 4496(并查集)
- HDU 4496 并查集
- hdu-4496-并查集
- [并查集] HDU 4496
- HDU 4496 D-City(并查集)
- hdu 4496 D-City 并查集
- HDU 4496 D-CITY(并查集)
- 【并查集】-HDU-4496-D-City
- HDU 4496 D-City(并查集)
- hdu 4496 D-City(并查集)
- hdu 4496 并查集的变形
- 【HDU】4496 D-City 并查集
- HDU-4496-逆向并查集
- hdu 4496 D-City 并查集
- hdu 4496 D-City(并查集)
- 并查集--HDU - 4496 D-City
- hdu 4496 D-City 并查集
- hdu 1520(简单树形dp)
- hdu 1513(dp+滚动数组)
- hdu 4240在(最大流)
- 大端法、小端法、网络字节序
- hdu 4722(记忆化搜索)
- hdu 4496(并查集)
- hdu 4685(匹配+强连通分量)
- uva 11381(神奇的构图、最小费用最大流)
- hdu 4291(矩阵+暴力求循环节)
- sgu 326(经典网络流构图)
- zoj 3370(二分+二分图染色)
- hdu 3760(2次bfs求最短路)
- UVALive 6044(双连通分量的应用)
- UESTC 1511(差分约束)