1.1003
2.
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.
3.图算法,注意两个城市之间可以有多条道路相通
4.
#include <iostream>
#include<stdio.h>
using namespace std;
const int MAX = 30000;
int n,m,k;
int parent[MAX+10];
int GetParent(int a)
{
if( parent[a]!= a)
parent[a] = GetParent(parent[a]);
return parent[a];
}
void Merge(int a,int b)
{
int p1 = GetParent(a);
int p2 = GetParent(b);
if( p1 == p2 )
return;
parent[p2] = p1;
}
int main()
{
while(true)
{
scanf("%d",&n);
if( n == 0)
break;
scanf("%d",&m);
for(int i = 1;i <=n; ++i)
{
parent[i] = i;
}
for(int i = 0;i < m; ++i)
{
int h,s;
scanf("%d",&h);
scanf("%d",&s);
Merge(h,s);
}
int num=0;
for(int i=1;i<=n;i++)
if(parent[i]==i)
num++;
printf("%d\n",num-1);
}
return 0;
}