D-citytime limit: 1000 msmemory limit: 65535 kbthis problem will be judged on HDU. Original ID: 4496
64-bit integer Io format: % i64d Java class name: Main 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. inputfirst 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.
Outputoutput 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
Hintthe 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 1 s 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 Independent connected block. continue deleting edges the disconnected blocks increased and finally it will became the number of vertex, so the last output shocould always be n. source2013 ACM-ICPC Jilin Tonghua national invitational tournament -- Questions reproduce problem solving: and query set, reverse solution...
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define INF 0x3f3f3f3f15 using namespace std;16 const int maxn = 10010;17 int uf[maxn],n,m;18 int ans[maxn*10];19 int a[maxn*10],b[maxn*10];20 int Find(int x){21 if(x != uf[x])22 uf[x] = Find(uf[x]);23 return uf[x];24 }25 int main(){26 int i,j,k;27 while(~scanf("%d %d",&n,&m)){28 for(i = 0; i <= n; i++)29 uf[i] = i;30 for(i = 1; i <= m; i++){31 scanf("%d %d",a+i,b+i);32 }33 ans[m] = n;34 for(i = m; i; i--){35 int tx= Find(a[i]);36 int ty = Find(b[i]);37 uf[tx] = ty;38 if(tx != ty){39 ans[i-1] = ans[i]-1;40 }else ans[i-1] = ans[i];41 }42 for(i = 1; i <= m; i++){43 printf("%d\n",ans[i]);44 }45 }46 return 0;47 }View code