*题意:列出了每条道路直接连通的城镇。使全省任何两个城镇间都可达,问最少还需要建设多少条道路?
*解题思路:赤裸裸的并查集
*感想:课件上的原题
*AC源码
#include<iostream>
using namespace std;
int bin[1002];
int findx(int x)
{
int r=x;
while(bin[r] !=r)
r=bin[r];
return r;
}
void merge(int x,int y)
{
int fx,fy;
fx = findx(x);
fy = findx(y);
if(fx != fy)
bin[fx] = fy;
}
int main()
{
int n,m,i,x,y,count;
while(cin>>n&&n)
{
for(i=1;i<=n;i++)
bin[i] = i;
for(cin>>m;m>0;m--)
{
cin>>x>>y;
merge(x,y);
}
for(count=-1, i=1;i<=n;i++)
if(bin[i] == i)
count ++;
cout<<count<<endl;
}
}
Problem C
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 66 Accepted Submission(s) : 31
4 2 1 3 4 3 3 3 1 2 1 3 2 3 5 2 1 2 3 5 999 0 0
1 0 2 998 <div style='font-family:Times New Roman;font-size:14px;background-color:F4FBFF;border:#B7CBFF 1px dashed;padding:6px'><div style='font-family:Arial;font-weight:bold;color:#7CA9ED;border-bottom:#B7CBFF 1px dashed'><i>Hint</i></div>Hint</div> Huge input, scanf is recommended.
1489

被折叠的 条评论
为什么被折叠?



