PROBLEM A
HDU 4496
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4496
题目大意:D城有N个点,M条线,每条线连接两个点,破坏掉前i条线后,在第i行输出还有多少个联通块。
// 红果果的并查集是加上某条线后当前的联通块,这次问题是破坏后还剩多少块。转换一下思维,破坏完全部的路后,剩N块,从后往前看,每添加一条路,有多少块,果断是裸并查集。
#include<iostream>
#include<stack>
#include<stdio.h>
#include<string.h>
using namespace std;
struct edge{
int u,v;
};
int fa[10000+5];
int find(int x)
{
return fa[x]==-1?x:fa[x]=find(fa[x]);
}
void cmb(int a,int b,int &left)
{
int pa = find(a);
int pb = find(b);
if (pa!=pb)
{
fa[pb] = pa; --left;
}
}
int main()
{
int n,m;
while (cin>>n>>m)
{
memset(fa,-1,sizeof(fa));
stack<edge>qe;
stack<int>qt;
edge e;
while (m--)
{
scanf("%d%d",&e.v,&e.u);
qe.push(e);
}
while (!qe.empty())
{
qt.push(n);
e = qe.top(); qe.pop();
cmb(e.u,e.v,n);
}
while(!qt.empty())
{
printf("%d\n",qt.top());
qt.pop();
}
}
return 0;
}