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

整个文件以两个-1结尾。
题目大意:输入多组数据,以 0 0结束每一组数据,-1 -1 结束程序;判断每组数据是否满足欧拉通路,不成环
(欧拉图:具有欧拉回路的无向图;欧拉回路:连通的无向图G没有奇度结点(有环);欧拉通路:经过连通的无向图G的每一条边一次且仅有一次的路径为欧拉通路__即G仅有2 个奇度结点)
解决策略:
1 常用套路,用一个数组记录每个结点的入度和出度;和这个结点存不存在,再用并查集判断无向图是否连通(根据Kruskal算法来看连通条件是,把每一条路径上的2个结点进行合并,记录变量res最后为1 表明此图是连通的,参考模板《Kruskal模板》;)
这种方法做过ny_129 树的判定,和这道题一模一样;
2 对于这道题来说,输入让我难受了好久,自己见到的题太少了,以前见过,但是忘记咋写得了。看了学长的博客后,用STL里面的set可以省掉很多麻烦,比如不知道到底有多少个结点,而且这些结点都是啥,用set<>不添加重复的元素就可以解决这个问题。只需要判断,set.size()存储的是顶点的个数,要是顶点个数等于边的个数加1,就满足生成树的条件,而且再用并查集判断有没有环。
#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)
树的判定
-
描述
-
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;
}