链接.
不懂为什么这么水的代码wa了一晚上…
把大佬的题解贴过来
题解.
题意:一共
d
d
d次询问,对于第
i
i
i次询问:(每次初始都看为无边)可以添加
i
i
i条边,保证前
i
i
i次的两点都相连(可直接可间接),求在此要求下的最大连通图。
#include<bits/stdc++.h>
using namespace std;
int n,d;
int f[1010],k[1010];
int get(int x)
{
return x==f[x]?x:f[x]=get(f[x]);
}
bool cmp(int x,int y)
{
return x>y;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>n>>d;
for(int i=1;i<=n;i++)f[i]=i,k[i]=1;
int cnt=0;
while(d--)
{
int x,y;cin>>x>>y;
int fx=get(x);
int fy=get(y);
if(fx==fy)cnt++;
else f[fy]=fx,k[fx]+=k[fy];
vector<int >v;
for(int i=1;i<=n;i++)
{
if(f[i]==i)v.push_back(k[i]);
}
sort(v.begin(),v.end(),cmp);
int ans=0;
for(int i=0;i<=cnt&&i<v.size();i++)ans+=v[i];
cout<<ans-1<<"\n";
}
return 0;
}