图论——hdu1272小希的迷宫(欧拉回路、通路);ny_129 树的判定(数据结构)

小希的迷宫

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 59103    Accepted Submission(s): 18602


Problem 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 45 6 0 08 1 7 3 6 2 8 9 7 57 4 7 8 7 6 0 03 8 6 8 6 45 3 5 6 5 2 0 0-1 -1
 

Sample Output

 
YesYesNo
 

Author
Gardon
 

Source
 

Recommend
lxj   |   We have carefully selected several similar problems for you:   1856  1325  1198  1875  1879 

    题目大意:输入多组数据,以 0 0结束每一组数据,-1 -1 结束程序;判断每组数据是否满足欧拉通路,不成环

(欧拉图:具有欧拉回路的无向图;欧拉回路:连通的无向图G没有奇度结点(有环);欧拉通路:经过连通的无向图G的每一条边一次且仅有一次的路径为欧拉通路__即G仅有2 个奇度结点)

    解决策略:

1 常用套路,用一个数组记录每个结点的入度和出度;和这个结点存不存在,再用并查集判断无向图是否连通(根据Kruskal算法来看连通条件是,把每一条路径上的2个结点进行合并,记录变量res最后为1 表明此图是连通的,参考模板《Kruskal模板》;)

这种方法做过ny_129 树的判定,和这道题一模一样;

2  对于这道题来说,输入让我难受了好久,自己见到的题太少了,以前见过,但是忘记咋写得了。看了学长的博客后,用STL里面的set可以省掉很多麻烦,比如不知道到底有多少个结点,而且这些结点都是啥,用set<>不添加重复的元素就可以解决这个问题。只需要判断,set.size()存储的是顶点的个数,要是顶点个数等于边的个数加1,就满足生成树的条件,而且再用并查集判断有没有环。

set的做法:

#include<stdio.h>
#include<string.h>
#include<set>
using namespace std;
#define N 100005
int par[N];
bool circle;
set<int>s;
int find(int x)//并查集_查找+压缩路径 
{
	int i=x;
	while(!par[x]) return x;
	while(par[i])  i=par[i];
	int j=x,t;
	while(par[j])
	{
		t=par[j];
		par[j]=i;
		j=t;
	}
	return i;
}
void Union(int a,int b)//并查集_合并 
{
	int nx=find(a);
	int ny=find(b);
	if(nx!=ny)
	{
		par[nx]=ny;
	}
	else
	circle=true;
}
int main()
{
	int a,b,sum;
	while(~scanf("%d%d",&a,&b)&&a!=-1&&b!=-1)
	{
		memset(par,0,sizeof(par));
		sum=1,circle=false;
		if(a==0&&b==0)
		{
			printf("Yes\n");
			continue;
		}
		s.insert(a);//注意每次都要添加元素 
		s.insert(b);
		if(!circle)
		Union(a,b);
		while(1)
		{
			scanf("%d%d",&a,&b);
			if(a==0&&b==0)
				break;
			s.insert(a);
			s.insert(b);
			if(!circle)
			Union(a,b);
			sum++;
		}
		if(!circle&&sum+1==s.size())
			printf("Yes\n");
		else
			printf("No\n");	
		s.clear();//结束之后清空 set所有元素 
	}
	return 0;
}

常用办法解决ny_129 数的判定(这道题的输入,输出都很让人难受 T_T)

树的判定

时间限制: 1000 ms  |  内存限制: 65535 KB
难度: 4
描述

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.

输入
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.

The number of test cases will not more than 20,and the number of the node will not exceed 10000.
The inputs will be ended by a pair of -1.
输出
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).
样例输入
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
样例输出
Case 1 is a tree.
Case 2 is a tree.
Case 3 is not a tree.

#include <stdio.h>  
#include <string.h>  
  
#define maxn 10010  
  
int pre[maxn];  
bool vis[maxn];  
  
int unionFind(int k){  
    int a = k, b;  
    while(pre[k] != -1) k = pre[k];  
    while(a != k){  
        b = pre[a];  
        pre[a] = k;  
        a = b;  
    }  
    return k;  
}  
  
int main() {  
    // freopen("stdin.txt", "r", stdin);  
    memset(pre, -1, sizeof(pre));  
    int u, v, cas = 1, ok = 1, count = 0;  
    while(scanf("%d%d", &u, &v) != EOF) {  
        if(u < 0) break;  
        if(!(u | v)) {  
            printf("Case %d ", cas++);  
            if(count > 1) ok = 0;  
            if(ok) printf("is a tree.\n");  
            else printf("is not a tree.\n");  
            memset(pre, -1, sizeof(pre));  
            memset(vis, 0, sizeof(vis));  
            count = 0; ok = 1; continue;  
        }  
        if(!ok) continue;  
  
        if(!vis[u]) { 
            vis[u] = 1; ++count;  
        }  
        if(!vis[v]) {  
            vis[v] = 1; ++count;  
        }  
        if(pre[v] != -1 || u == v) {  
            ok = 0; continue;  
        }  
        u = unionFind(u);  
        if(u == v) {  
            ok = 0; continue;  
        }  
        pre[v] = u; --count;  
    }  
    return 0;  
}  




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值