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. [神经网络]一步一步使用Mobile-Net完成视觉识别(三)

    1.环境配置 2.数据集获取 3.训练集获取 4.训练 5.调用测试训练结果 6.代码讲解 本文是第三篇,获取tfboard训练集. 前面我们拿到了所有图片对应的标注信息的xml文件,现在我们需要先把 ...

  2. c++作业:求N的阶乘。

    N的阶乘就是n.(n-1)! 5的阶乘是什么?5*4*3*2*1 #include <iostream> using namespace std; int jiecheng(int num ...

  3. ES6_Promise 对象 阮一锋

    Promise的含义 promise是异步编程的一种解决方法,比传统的回调函数和事件更合理更强大.他由社区最早提出和实现,ES6将其写进语言标准,统一了用法,原生提供了promise对象.所谓prom ...

  4. ubuntu 16.04 +anaconda3.6 +Nvidia DRIVER 390.77 +CUDA9.0 +cudnn7.0.4+tensorflow1.5.0+neural-style

    这是我第一个人工智能实验.虽然原理不是很懂,但是觉得深度学习真的很有趣.教程如下. Table of Contents 配置 时间轴 前期准备工作 anaconda3 安装 bug 1:conda:未 ...

  5. 51nod——2489 小b和灯泡(打表/平方数)

    这题打表去找因子的个数然后判奇偶也行.预处理O(n) 扫一遍判断O(n). ; i * i <= n; i++){ for(int j = i; i * j <= n; j++){ div ...

  6. 【动态规划】bzoj1705: [Usaco2007 Nov]Telephone Wire 架设电话线

    可能是一类dp的通用优化 Description 最近,Farmer John的奶牛们越来越不满于牛棚里一塌糊涂的电话服务 于是,她们要求FJ把那些老旧的电话线换成性能更好的新电话线. 新的电话线架设 ...

  7. centos 安装 python3 分类链接

    上一篇文章描述了如何安装python3,但是在后续安装pip便不断报出缺少各类模块,安装一个又需要依赖另一个,导致安装过程非常繁琐.究其原因,我是安装centos-minimal版本,有许多功能不是完 ...

  8. Vue基础指令集锦

    v-model双向绑定数据 <input type="text" v-model="msg"> {{msg}} ###v-on 事件 <div ...

  9. 19.Yii2.0框架模型删除记录

    目录 //删除记录 //http://yii.com/?r=home/del public function actionDel() { //查出要删除的记录行 // 方法一:(查一行,删一行) // ...

  10. (转)iOS平台UDID方案比较

    苹果在iOS6中禁用了[UIDevice uniqueIdentifier],在iOS7中又把mac地址的获取给堵上了.没办法,毕竟人家是老大,说不让你用,你也没办法.   在这边总结一下现有的一部分 ...