连接电脑 Time Limit:1000MS Memory Limit:65536K Description Description: Input Input: Output Output: Sample Input
Sample Input:
4 2
1 2
2 3
4 0
1 0
0 0 Sample Output
Sample Output:
1
3
0
Source ahstu@ICPC01 |
[Submit] [Go Back] [Status] [Discuss]
思路:代码改自1140英雄联盟阵营,加入了前两行的判断语句,然后相连的电脑置为同一个数,count既用来置数也用来计数,还有未连接的电脑,一台电脑需要一根线,count++即可,这里可能需要注意下p,q输入是反着的情况,就是防止胡乱输入两个数,而1140题则是以按由小到大输入写的。
#include<iostream>
using namespace std;
int main()
{
int n,m;
while(cin>>n>>m){
int p,q,a[201]= {0},count=1;
if(n==0&&m==0)break;
if(m==0&&n!=0){cout<<n-1<<endl;continue;}
cin>>p>>q;
a[p]=a[q]=count;
for(int i=1; i<m; i++){
cin>>p>>q;
if(a[p]!=0)a[q]=count;
else if(a[q]!=0)a[p]=count;
else {
count++;
a[p]=count;
a[q]=a[p];
}
}
for(int i=1; i<=n; i++)if(a[i]==0)count++;//这里是判断1...n,第一次写成0...n-1,wrong了一次
cout<<count-1<<endl;
}
return 0;
}