文章标题

来源:互联网 发布:c语言异或符号 编辑:程序博客网 时间:2024/06/16 03:30
**1009 : Are the towns connected by roads?**

描述
The Emergency Measures Organization for an unnamed state wishes to know quickly when a hurricane strikes which counties in the state still have roads connecting all the towns of the county together. You must write a program to determine this.
输入
On the first line there is a number indicating the number of counties (no more than 10). Following that, there is data for each of the counties. For each county, the first line contains the name of the county followed by the number specifying the number of towns (no more than 10) in the county. Each subsequent line contains the names of two towns that are connected by a road. A blank line indicates that all other roads in the county have been washed out by the hurricane. You may assume that the names consist of a single word, that is, that the names do not contain any white space.
输出
for each county, in the order that the counties were input, write out one line stating either:
County (insert name) is connected.
or
County (insert name) is disconnected.
样例输入
2
One 3
First Second
Second First

Two 2
Third Fourth

样例输出
County One is disconnected.
County Two is connected.

题目的意思很简单,就是要你判断这个城市里面的所有的城镇是否可以联通、自己感觉坑的是,输入是以回车结束,所以要自己写这个输入的函数。
简单并查集:

include

include

include

using namespace std;
char name1[110][110];
int zhong,father[20],rank[20];
int Find(int a)
{
while(a!=father[a])
{
father[a]=father[father[a]];
a=father[a];
}
return a;
}
void Union(int x,int y)
{
if(x==y)return;
if(rank[x]>rank[y])
father[y]=x,rank[x]+=rank[y];
else
{
father[x]=y;
rank[y]+=rank[x];
}
}
void read()
{
char s[100],s1[100],s2[100];
int len=0,k=0;
for(int i=0;i<=20;i++)
father[i]=i,rank[i]=1;
while(1)
{
char c;
c=getchar();
if(c==’ ‘)s1[len]=’\0’,k++,len=0;
else if(c==’\n’&&k%2==0)return;
else if(c==’\n’)s2[len]=’\0’,k++,len=0;
else if(k%2==0)s1[len++]=c;
else s2[len++]=c;/**/
if(k%2==0&&len==0)
{
int f=0,wei1,wei2;
for(int i=0;i

0 0
原创粉丝点击