D-city
Time limit:2000/1000 MS (java/others) Memory limit:65535/65535 K (java/others)
Total submission (s): 2134 Accepted Submission (s): 751
Problem Description
Luxer is a really. 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 and D-points. Luxer'll destroy all the d-lines. The mayor of D-city wants to know what many connected blocks of d-city left after Luxer destroying the first K D-lines in t He input.
The points is in the same connected blocks if and only if they connect to each other directly or indirectly.
Input
First line of the input contains, 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.
Output
Output M lines, the ith line was 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 was a complete graph, which each pair of vertex have an edge connecting them, so there's only 1 connected block at first.
The first 3 lines of output is 1s because after deleting the first 3 edges of the graph, all vertexes still connected tog Ether.
But after deleting the first 4 edges of the graph, vertex 1 'll be disconnected with other vertex, and it became an INDEP Endent connected block.
Continue deleting edges the disconnected blocks increased and finally it'll became the number of vertex, so the last out Put should always is N.
Test instructions: There are several sets (blocks) after the output delete before I edge
Code:
#include <cstdio>#include <cstring>using namespace STD;Const intM =1e6+ -;Const intN =1e5+ -;structnode{intx, y;} E[M];intN, M, Fat[n], ans[m];intFiintx) {if(x = fat[x]) fat[x] = Fi (fat[x]);returnFAT[X];}voidF () {ans[m] = n; for(inti = m1; I >=0; --i) {intx = Fi (e[i].x), y = fi (E[I].Y);if(x! = y) {Ans[i] = ans[i+1]-1; Fat[x] = y; }ElseAns[i] = ans[i+1]; }}intMain () { while(scanf("%d%d", &n, &m) = =2){ for(inti =0; I <= N; + + i) {fat[i] = i; } for(inti =0; I < m; ++i) {scanf("%d%d", &e[i].x, &E[I].Y); }memset(ANS,0,sizeof(ans)); f (); for(inti =1; I <= m; ++i)printf("%d\n", Ans[i]); }return 0;}
Hdoj 4496 d-city "and check Set"