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. CF Gym 100637G \#TheDress (水)

    题解:读懂题意按照题意模拟... 熟悉了一个库函数,strstr,memcpy #include<cstdio> #include<cstring> int main() { ...

  2. [神经网络]一步一步使用Mobile-Net完成视觉识别(二)

    1.环境配置 2.数据集获取 3.训练集获取 4.训练 5.调用测试训练结果 6.代码讲解 本文是第二篇,调用官方例子并获取数据集. 上一节里面记得我们需要配置PYTHONPATH,大家应该发现,每次 ...

  3. Problem A: 文件操作--二进制文件读入

    Problem A: 文件操作--二进制文件读入 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 1952  Solved: 524[Submit][St ...

  4. Connectivity

    6492: Connectivity 时间限制: 1 Sec  内存限制: 128 MB提交: 118  解决: 28[提交][状态][讨论版][命题人:admin] 题目描述 There are N ...

  5. spring5之SAXParseException:cvc-elt.1: 找不到元素 “beans” 的声明

    之前SSM项目一直报错,就是找不到错误  气啊 后来在网上找到了答案:燕来spring5之后就不再需要写版本号了

  6. js类型判别大合集

    1.typeof number,string,boolean,undefined,symbol,object,function 对象中除了函数为function,其他对象都判别为object, 缺陷: ...

  7. iOSAES加密的实现

    +(NSData *)AES256ParmEncryptWithKey:(NSString *)key Encrypttext:(NSData *)text  //加密 { char keyPtr[k ...

  8. Win2008 Server配置PHP环境

    Win2008 Server配置PHP环境   阅读目录 创建一个网站 配置PHP环境 配置iis的“处理应用程序映射” 在配置PHP环境之前要先配置好IIS. 传送门-> Win2008 Se ...

  9. day13-生成器

    def generator(): print(1) yield 'a' rcp = generator() print(rcp.__next__()) 只要含有yield关键字的函数都是生成器函数.y ...

  10. stm32F4中断分析-HAL库

    详细可以参考: STM32使用HAL库操作外部中断——实战操作 https://www.cnblogs.com/wt88/p/9624103.html /** ******************** ...