Is It A Tree?
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 28399   Accepted: 9684

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.

刚开始看到这题以为跟HDU上那个迷宫题目一样,后来发现是有向图,而那个迷宫是无向图。但是道理差不多。

判断一个有向图是否为树:无环;n个结点最多有n-1条边,不然就会有环;只有一个入度为0的结点,不存在入度大于1的结点

根据以上信息就可以判断一个有向图是否存在环,然后假设他是一个树对其进行拓扑排序。排序的点放入一个set中(一开始用queue就WA。估计是重复了什么吧。加上这题排序的顺序没用,set确实更适合于此题的记录个数因为不会重复)就这样搜搜搜就过了。这题看discuss是用并查集用的比较多,有时间用并查集做做。此题数据据说比较水,可能这代码也有问题。但是DISCUSS里那几个特例都是可以过的。

代码:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#define INF 0x3f3f3f3f
#define MM(x) memset(x,0,sizeof(x))
using namespace std;
typedef long long LL;
const int N=100010;
vector<int>edge[N];//ÁÚ½Ó±í
map<int,int>deg;
int main(void)
{
int x,y,i,j,q=1;
int flag=1;
while (~scanf("%d%d",&x,&y))
{
if(x==-1&&x==y)
{
break;
}
else if(x==0&&y==0)
{
map<int,int>::iterator it;
queue<int> Q;
set<int>tp;
for (it=deg.begin(); it!=deg.end(); it++)
{
if(it->second==0)
{
Q.push(it->first);
tp.insert(it->first);
break;
}
}
while (!Q.empty())
{
int now=Q.front();
Q.pop();
for (i=0; i<edge[now].size(); i++)
{
int v=edge[now][i];
deg[v]--;
if(deg[v]==0)
{
tp.insert(v);
Q.push(v);
}
}
}
//cout<<deg.size()<<" "<<endl;
if(tp.size()==deg.size()&&flag)
printf("Case %d is a tree.\n",q++);
else
printf("Case %d is not a tree.\n",q++);
deg.clear();
for (i=0; i<N; i++)
edge[i].clear();
flag=1;
tp.clear();
while (!Q.empty())
Q.pop();
}
else
{
if(deg.find(x)==deg.end())
deg[x]=0;
deg[y]++;
if(deg[y]>=2)
flag=0;
edge[x].push_back(y);
}
}
return 0;
}

POJ——1308Is It A Tree?(模拟拓扑排序判断有向图是否为树)的更多相关文章

  1. POJ 2367:Genealogical tree(拓扑排序模板)

    Genealogical tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7285   Accepted: 4704 ...

  2. POJ 2367:Genealogical tree(拓扑排序)

    Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2738 Accepted: 1838 Spe ...

  3. hdoj 4324 Triangle LOVE【拓扑排序判断是否存在环】

    Triangle LOVE Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  4. LeetCode 210. Course Schedule II(拓扑排序-求有向图中是否存在环)

    和LeetCode 207. Course Schedule(拓扑排序-求有向图中是否存在环)类似. 注意到.在for (auto p: prerequistites)中特判了输入中可能出现的平行边或 ...

  5. 拓扑排序 判断给定图是否存在合法拓扑序列 自家oj1393

    //拓扑排序判断是否有环 #include<cstdio> #include<algorithm> #include<string.h> #include<m ...

  6. [ACM_模拟] POJ 1094 Sorting It All Out (拓扑排序+Floyd算法 判断关系是否矛盾或统一)

    Description An ascending sorted sequence of distinct values is one in which some form of a less-than ...

  7. poj 2367 Genealogical tree (拓扑排序)

    火星人的血缘关系很奇怪,一个人可以有很多父亲,当然一个人也可以有很多孩子.有些时候分不清辈分会产生一些尴尬.所以写个程序来让n个人排序,长辈排在晚辈前面. 输入:N 代表n个人 1~n 接下来n行 第 ...

  8. POJ 2367 Genealogical tree【拓扑排序】

    题意:大概意思是--有一个家族聚集在一起,现在由家族里面的人讲话,辈分高的人先讲话.现在给出n,然后再给出n行数 第i行输入的数表示的意思是第i行的子孙是哪些数,然后这些数排在i的后面. 比如样例 5 ...

  9. ACM: poj 1094 Sorting It All Out - 拓扑排序

    poj 1094 Sorting It All Out Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & ...

随机推荐

  1. JDBC + SAP云平台 = 运行在云端的数据库应用

    在前一篇文章JPA + EclipseLink + SAP云平台 = 运行在云端的数据库应用我介绍了如何通过JPA和EclipseLink操作部署在SAP云平台上的HANA数据库实例. 在这篇文章里, ...

  2. UI EventSystem事件监听

    Unity5.0 EventSystem事件系统的详细说明 一.EventSystem对象的说明 当我们在场景中创建任一UI对象后,Hierarchy面板中都可以看到系统自动创建了对象EventSys ...

  3. input输入大于0的小数和整数

    <input onkeyup="num(this)"onbeforepaste="num(this)"> <script src='jquer ...

  4. UVA12904 Load Balancing(中途相遇法)

    虽然这题可以用暴力n^3过,但是还有有种n^2的方法的,枚举b,对于b,分别枚举a和c,得到对于这个b的最优解,然后从所以b中选一个最优的. 要保证字典序最小,只要从小往大枚举就好了 感谢moonfl ...

  5. python之道13

    看代码分析结果 func_list = [] for i in range(10): func_list.append(lambda :i) v1 = func_list[0]() v2 = func ...

  6. runtime实践之Method Swizzling

    利用 Objective-C 的 Runtime 特性,我们可以给语言做扩展,帮助解决项目开发中的一些设计和技术问题.这一篇,我们来探索一些利用 Objective-C Runtime 的黑色技巧.这 ...

  7. java解析多层嵌套json字符串

    java分别解析下面两个json字符串 package jansonDemo; import com.alibaba.fastjson.JSON; import com.alibaba.fastjso ...

  8. CentOS 7.4 基于LNMP搭建wordpress

    之前有好多次搭建wordpress的经历,有在Ubuntu系统上,有在CentOS7.2系统上,但都是搭完还是稀里糊涂的,因为好多都是教程上照着敲的.这次好好出个教程,以便以后方便查看. 准备工作:C ...

  9. Linux虚拟机里用X11协议打开图形界面的eclipse

    1.下载工具包 XLaunch(安装到win)https://xming.en.softonic.com/ Eclipse IDE for C/C++ Developers(虚拟机里解压到 /data ...

  10. 有关Kali的方法

    Kali 找回系统登陆密码的方式:https://jingyan.baidu.com/article/47a29f24560e77c0142399e3.html