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.
Input
First line of the input contains two integers N and M.
Then following M lines each containing space-separated integers u and v, which denotes an D-line.
Constraints:
< N <=
< M <=
<= u, v < N.
Output
Output M lines, the ith line is the answer after deleting the first i edges in the input.
Sample Input
Sample Output
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.
Source
题目的意思是: 一开始有个图(n个点 m条边),然后从第0条边开始删除,(0~m条边依次删除),然后每删除一条边统计依次有多少个连通块。
做法是: 直接从最后一条边开始枚举,依次往前面添加合并边,统计连通块即可。具体看代码,应该看得懂。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 10006
#define M 100006
#define inf 1e12
int n,m;
int fa[N],ans[M];
int a[M],b[M];
void init(){
for(int i=;i<N;i++){
fa[i]=i;
}
}
int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
}
bool merge(int x,int y){
int root1=find(x);
int root2=find(y);
if(root1==root2) return false;
fa[root1]=root2;
return true;
}
int main()
{
while(scanf("%d%d",&n,&m)==){
init();
for(int i=;i<m;i++){
scanf("%d%d",&a[i],&b[i]);
}
ans[m-]=n;
int k=m-;
for(int i=m-;i>=;i--){
if(merge(a[i],b[i])){
ans[k-]=ans[k]-;
}else{
ans[k-]=ans[k];
}
k--;
}
for(int i=;i<m;i++){
printf("%d\n",ans[i]);
} } return ;
}