题意:给定一个N个顶点全连通图,给定M个边weight=1,剩下的边weight=2,问移去一些edge后可以使得剩下的图是连通的且weight和最小,求最小的weight和。
一眼瞄上去是最小生成的树,从图中找出一棵树, such that cover所有的点且边的weight和最小。但是最小生成树需要枚举所有的边,复杂度是N^2,而且没有利用到这一题中weight只有1和2两个值的条件。
对于n个点的全连通图,如果weight权重相同都是x,那么最小生成树的sum of weight=x*(n-1)。所以并不需要枚举所有的边。
我们可以先对black strand连接的node做并查集,先check由black strand连起来的联通分量有多少。这些联通分量之间只能通过red strand连接。对于剩下没有被black strand cover的node,可以把它们放在一个联通分量里,再通过一个red strand和black strand组成的联通分量连接。
举个例子1: a graph with 4 nodes, two black strands: 1---2, 3---4,black strand组成的联通分量有两个,这两个component再用red strand连接。
举个例子2:a graph with 4 nodes, one black strand: 1---2, black strand组成的联通分量有1个,剩下的node 3, 4 位于由red strands组成的联通分量里,这两个component再用一个red strand连接。
另外,并查集union完成最后check联通分量的个数时,不可以直接check不同的p[i]的值,因为可能有的node i对应的component merge 了其他node之后,p[i]没有及时update,所以要通过find(i)的值确定联通分量的个数。
#include<iostream>
#include<stdio.h>
#include<cstdio>
#include<string>
#include<cmath>
#include<stdlib.h>
#include<algorithm>
#include<string.h>
#include<cstring>
#include<vector>
#include<queue>
#include<map>
#include<set>
using namespace std;
const int maxn=100010;
int T;
int N;
int M;
//vector<int> mp[maxn]; //node i connect to black line
long long ans;
//pair<int,int>black_strand[maxn];
//set<pair<int,int> >black;
set<int>blackset;//nodes that are included in black strand
int C[maxn];
int D[maxn];
int p[maxn];
int init()
{
for(int i=0;i<=N;++i)
{
p[i]=i;
}
}
int find(int x)
{
if(x==p[x])
{
return x;
}
else
{
return p[x]=find(p[x]);
}
}
void Union(int x,int y)
{
p[find(x)]=find(y);
}
void Kruskal()
{
init();
int idx=0;
while(idx<M)
{
while(find(C[idx])==find(D[idx]))
{
// cout<<" already connected "<<C[idx]<<" "<<D[idx]<<endl;
idx++;
if(idx>=M)
{
break;
}
}
if(idx<M)
{
Union(C[idx],D[idx]);
// cout<<" merge edge "<<C[idx]<<" "<<D[idx]<<endl;
ans+=1;
idx++;
}
else
{
break;
}
}
//check number of groups in connected by black strands only
//compute the number of components in the graph
set<int>group=set<int>();
for(int i=1;i<=N;i++)
{
if(blackset.find(i)==blackset.end())
{
continue;
}
// cout<<i<<" "<<p[i]<<endl;
int tmp=find(i);
group.insert(tmp);
// if(group.find(p[i])==group.end()) the value of p[i] may not be equivalent to the ancestor
// {
// group.insert(p[i]);
// }
}
// cout<<"group size "<<group.size()<<endl;
ans+=(group.size()-1)*2;
if(N-blackset.size()>0)
{
ans+=(N-blackset.size()-1)*2+2;
}
// cout<<"1st ans "<<ans<<endl;
//put all red stands into one set
// for(int i=1;i<=N;i++)
// {
// for(int j=i+1;j<=N;j++)
// {
// pair<int,int>tmp=make_pair(i,j);
// if(black.find(tmp)!=black.end())
// {
// continue;
// }
// if(find(i)!=find(j))
// {
// Union(i,j);
// ans+=2;
//
// }
// }
//
// }
// cout<<"2st ans "<<ans<<endl;
}
int main()
{
freopen("input.txt","r",stdin);
cin>>T;
for(int ca=1;ca<=T;ca++)
{
// memset(mp,0,sizeof(mp));
memset(C,0,sizeof(C));
memset(D,0,sizeof(D));
// memset(black_strand,0,sizeof(black_strand));
memset(p,0,sizeof(p));
blackset.clear();
// black.clear();
ans=0;
cin>>N>>M;
for(int i=0;i<M;i++)
{
// int x=0;
// int y=0;
// cin>>x>>y;
// C[i]=min(x,y);
// D[i]=max(x,y);
cin>>C[i]>>D[i];
// black_strand[i]=make_pair(C[i],D[i]);
// black.insert(make_pair(C[i],D[i]));
blackset.insert(C[i]);
blackset.insert(D[i]);
}
Kruskal();
printf("Case #%d: %lld\n",ca,ans);
}
return 0;
}