B - 小希的迷宫
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走。但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就是说如果有一个通道连通了房间A和B,那么既可以通过它从房间A走到房间B,也可以通过它从房间B走到房间A,为了提高难度,小希希望任意两个房间有且仅有一条路径可以相通(除非走了回头路)。小希现在把她的设计图给你,让你帮忙判断她的设计图是否符合她的设计思路。比如下面的例子,前两个是符合条件的,但是最后一个却有两种方法从5到达8。
Input
输入包含多组数据,每组数据是一个以0 0结尾的整数对列表,表示了一条通道连接的两个房间的编号。房间的编号至少为1,且不超过100000。每两组数据之间有一个空行。
整个文件以两个-1结尾。
Output
对于输入的每一组数据,输出仅包括一行。如果该迷宫符合小希的思路,那么输出"Yes",否则输出"No"。
Sample Input
6 8 5 3 5 2 6 4 5 6 0 0 8 1 7 3 6 2 8 9 7 5 7 4 7 8 7 6 0 0 3 8 6 8 6 4 5 3 5 6 5 2 0 0 -1 -1
Sample Output
Yes Yes No
思路:要找符合条件的,则要判断是否构成回路,并且判断有且仅有一棵树。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<map>
#include<iostream>
#include<algorithm>
using namespace std;
int flag;
int bin[100005];
int vis[100005]; //标记数组
int findx(int x) //找祖先
{
int r=x;
while(bin[r]!=r)
{
r=bin[r];
}
return r;
}
void unionxy(int x,int y) //连接两个点
{
int fx,fy;
fx=findx(x);
fy=findx(y);
if(fx!=fy)
{
bin[fx]=fy;
}
else //构成了回路,不是一棵树
{
flag=0;
}
}
int main()
{
int i,a,b;
while(~scanf("%d %d",&a,&b))
{
if(a==-1&&b==-1)
break;
if(a==0&&b==0)
{
cout<<"Yes"<<endl;
continue;
}
for(i=0; i<100005; i++)
{
bin[i]=i;
vis[i]=0; //把每个点先初始化为零
}
unionxy(a,b);
flag=1;
vis[a]=1; //把出现过的点标记
vis[b]=1;
while(scanf("%d %d",&a,&b)&&a&&b)
{
unionxy(a,b);
vis[a]=1; //把出现过的点标记
vis[b]=1;
}
if(flag==0)
{
cout<<"No"<<endl;
continue;
}
else
{
int sum=0;
for(i=0; i<100005; i++)
{
if(vis[i]==1&&bin[i]==i) //这个点出现过并且祖先是他自己则它是祖先
sum++;
}
if(sum==1) //一棵数只能有一个祖先,如果出现大于1则不止一个祖先即不止一棵树
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
}
}
D - Is It A Tree?
Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u
Description
A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directed edges between nodes satisfying the following properties.
There is exactly one node, called the root, to which no directed edges point.
Every node except the root has exactly one edge pointing to it.
There is a unique sequence of directed edges from the root to each node.
For example, consider the illustrations below, in which nodes are represented by circles and edges are represented by lines with arrowheads. The first two of these are trees, but the last is not.

In this problem you will be given several descriptions of collections of nodes connected by directed edges. For each of these you are to determine if the collection satisfies the definition of a tree or not.
Input
The input will consist of a sequence of descriptions (test cases) followed by a pair of negative integers. Each test case will consist of a sequence of edge descriptions followed by a pair of zeroes Each edge description will consist of a pair of integers; the first integer identifies the node from which the edge begins, and the second integer identifies the node to which the edge is directed. Node numbers will always be greater than zero.
Output
For each test case display the line "Case k is a tree." or the line "Case k is not a tree.", where k corresponds to the test case number (they are sequentially numbered starting with 1).
Sample Input
6 8 5 3 5 2 6 4 5 6 0 0 8 1 7 3 6 2 8 9 7 5 7 4 7 8 7 6 0 0 3 8 6 8 6 4 5 3 5 6 5 2 0 0 -1 -1
Sample Output
Case 1 is a tree. Case 2 is a tree. Case 3 is not a tree.
思路:判断是否是一棵树。和上一道题几乎是一样的,不过要注意的是,存在自环不是一棵树。上一道题自环输出的是yes。
不是一棵树的条件:自环,形成回路,森林。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<map>
#include<iostream>
#include<algorithm>
using namespace std;
int flag;
int bin[100005];
int vis[100005];
int findx(int x)
{
int r=x;
while(bin[r]!=r)
{
r=bin[r];
}
return r;
}
void unionxy(int x,int y)
{
if(x==y) //如果形成自环也不行
flag=0;
int fx,fy;
fx=findx(x);
fy=findx(y);
if(fx!=fy)
{
bin[fx]=fy;
}
else
{
flag=0;
}
}
int main()
{
int i,a,b;
int t=1;
while(~scanf("%d %d",&a,&b))
{
if(a==-1&&b==-1)
break;
if(a==0&&b==0)
{
printf("Case %d is a tree.\n",t++);
continue;
}
for(i=0; i<100005; i++)
{
bin[i]=i;
vis[i]=0;
}
unionxy(a,b);
flag=1;
if(a==b) //判断自环的情况
{
flag=0;
}
vis[a]=1;
vis[b]=1;
while(scanf("%d %d",&a,&b)&&a&&b)
{
unionxy(a,b);
vis[a]=1;
vis[b]=1;
}
if(flag==0)
{
printf("Case %d is not a tree.\n",t++);
continue;
}
else
{
int sum=0;
for(i=0; i<100005; i++)
{
if(vis[i]==1&&bin[i]==i)
sum++;
}
if(sum==1)
printf("Case %d is a tree.\n",t++);
else
printf("Case %d is not a tree.\n",t++);
}
}
}
本文探讨了如何通过编程判断给定的迷宫设计或节点集合是否构成一棵树。使用并查集算法验证是否存在唯一路径连接任意两点,避免回路和自环,确保树结构的有效性。
11万+

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



