HDU - 4496 D-City

https://vjudge.net/problem/HDU-4496
倒着来,初始就有n个独立的街区,每增加一条路就少一条
注意数据范围

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
const int N=10010,M=1e5+10;
int n,m;
int p[N],res[M];
int find(int x)
{
	if(x!=p[x]) p[x]=find(p[x]);
	return p[x];
}
int main()
{
	ios::sync_with_stdio(false);
	while(cin>>n>>m)
	{
		for(int i=0;i<n;i++) p[i]=i;
		vector<pair<int,int>> q;
		int a,b;
		for(int i=0;i<m;i++)
		{	
			cin>>a>>b;
			q.push_back({a,b});
		}
		int t=n;
		for(int i=m-1;i>=0;i--)
		{
			res[i]=t;
			a=find(q[i].first);
			b=find(q[i].second);
			if(a!=b)
			{
				t--;
				p[a]=b;
			}
		}
		for(int i=0;i<m;i++)
			cout<<res[i]<<"\n";
	}
	return 0;
}
posted @ 2021-08-29 10:43  斯文~  阅读(24)  评论(0)    收藏  举报

你好!