hdu-4496-D-City
Posted 浮生缘,半生梦
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu-4496-D-City相关的知识,希望对你有一定的参考价值。
D-City
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 3838 Accepted Submission(s): 1379
Problem Description
Luxer is a really bad guy. He destroys everything he met.
One day Luxer went to D-city. D-city has N D-points and M D-lines. Each D-line connects exactly two D-points. Luxer will destroy all the D-lines. The mayor of D-city wants to know how many connected blocks of D-city left after Luxer destroying the first K D-lines in the input.
Two points are in the same connected blocks if and only if they connect to each other directly or indirectly.
One day Luxer went to D-city. D-city has N D-points and M D-lines. Each D-line connects exactly two D-points. Luxer will destroy all the D-lines. The mayor of D-city wants to know how many connected blocks of D-city left after Luxer destroying the first K D-lines in the input.
Two points are in the same connected blocks if and only if they connect to each other directly or indirectly.
Input
First line of the input contains two integers N and M.
Then following M lines each containing 2 space-separated integers u and v, which denotes an D-line.
Constraints:
0 < N <= 10000
0 < M <= 100000
0 <= u, v < N.
Then following M lines each containing 2 space-separated integers u and v, which denotes an D-line.
Constraints:
0 < N <= 10000
0 < M <= 100000
0 <= u, v < N.
Output
Output M lines, the ith line is the answer after deleting the first i edges in the input.
Sample Input
5 10
0 1
1 2
1 3
1 4
0 2
2 3
0 4
0 3
3 4
2 4
Sample Output
1 1 1 2 2 2 2 3 4 5
Hint
The graph given in sample input is a complete graph, that each pair of vertex has an edge connecting them, so there‘s only 1 connected block at first. The first 3 lines of output are 1s because after deleting the first 3 edges of the graph, all vertexes still connected together. But after deleting the first 4 edges of the graph, vertex 1 will be disconnected with other vertex, and it became an independent connected block. Continue deleting edges the disconnected blocks increased and finally it will became the number of vertex, so the last output should always be N.题目大意:逆行并查集
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4496
递归代码:
#include <iostream> #include <cstdio> using namespace std; const int MAX=1e4+10; const int MAX1=1e5+10; int f[MAX]; int sum[MAX1]; struct ss { int a,b; }s[MAX1]; int find(int n) { if(n!=f[n]) f[n]=find(f[n]); return f[n]; } int main() { int n,m; while(cin>>n>>m) { for(int i=0;i<n;i++) f[i]=i; for(int i=0;i<m;i++) scanf("%d%d",&s[i].a,&s[i].b); sum[m]=n; for(int i=m-1;i>=0;i--) { int a1=find(s[i].a); int a2=find(s[i].b); if(a1!=a2) {f[a1]=a2;sum[i]=sum[i+1]-1;} else sum[i]=sum[i+1]; } for(int i=1;i<=m;i++) cout<<sum[i]<<endl; } }
递推代码:
#include <iostream> #include <cstdio> using namespace std; const int MAX=1e4+10; const int MAX1=1e5+10; int f[MAX]; int sum[MAX1]; struct ss { int a,b; }s[MAX1]; int find(int n) { int res=n; while(n!=f[n]) n=f[n]; int j; while(res!=n) { j=f[res]; f[res]=n; res=j; } return n; } int main() { int n,m; while(cin>>n>>m) { for(int i=0;i<n;i++) f[i]=i; for(int i=0;i<m;i++) scanf("%d%d",&s[i].a,&s[i].b); sum[m]=n; for(int i=m-1;i>=0;i--) { int a1=find(s[i].a); int a2=find(s[i].b); if(a1!=a2) {f[a1]=a2;find(a1);sum[i]=sum[i+1]-1;} else sum[i]=sum[i+1]; } for(int i=1;i<=m;i++) cout<<sum[i]<<endl; } }
以上是关于hdu-4496-D-City的主要内容,如果未能解决你的问题,请参考以下文章