POJ——1308Is It A Tree?(模拟拓扑排序判断有向图是否为树)
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 28399 | Accepted: 9684 |
Description
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 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
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?(模拟拓扑排序判断有向图是否为树)的更多相关文章
- POJ 2367:Genealogical tree(拓扑排序模板)
Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7285 Accepted: 4704 ...
- POJ 2367:Genealogical tree(拓扑排序)
Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2738 Accepted: 1838 Spe ...
- hdoj 4324 Triangle LOVE【拓扑排序判断是否存在环】
Triangle LOVE Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- LeetCode 210. Course Schedule II(拓扑排序-求有向图中是否存在环)
和LeetCode 207. Course Schedule(拓扑排序-求有向图中是否存在环)类似. 注意到.在for (auto p: prerequistites)中特判了输入中可能出现的平行边或 ...
- 拓扑排序 判断给定图是否存在合法拓扑序列 自家oj1393
//拓扑排序判断是否有环 #include<cstdio> #include<algorithm> #include<string.h> #include<m ...
- [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 ...
- poj 2367 Genealogical tree (拓扑排序)
火星人的血缘关系很奇怪,一个人可以有很多父亲,当然一个人也可以有很多孩子.有些时候分不清辈分会产生一些尴尬.所以写个程序来让n个人排序,长辈排在晚辈前面. 输入:N 代表n个人 1~n 接下来n行 第 ...
- POJ 2367 Genealogical tree【拓扑排序】
题意:大概意思是--有一个家族聚集在一起,现在由家族里面的人讲话,辈分高的人先讲话.现在给出n,然后再给出n行数 第i行输入的数表示的意思是第i行的子孙是哪些数,然后这些数排在i的后面. 比如样例 5 ...
- ACM: poj 1094 Sorting It All Out - 拓扑排序
poj 1094 Sorting It All Out Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%lld & ...
随机推荐
- JPA + EclipseLink + SAP云平台 = 运行在云端的数据库应用
JPA(Java Persistence API)的实现Provider有Hibernate,OpenJPA和EclipseLink等等. 本文介绍如何通过JPA + Eclipse连接SAP云平台上 ...
- python 基础之while无限循环
用户登录程序 username = "chenxi" passwed = "testki" counter = 0 while counter < 3: ...
- IntelliJ IDEA java设置程序运行时内存
Run/Edit Configurations Configuration/VM options 例如:设置运行内存为:-Xmx3m -Xms3m
- Mybatis中关于OGNL表达式冲突
注意设计表字段不能用bor xor and band eq neq lt gt lte gte shl shr ushr
- Cordova 本地项目创建方法
l 创建项目 需要在终端上输入:cordova create [目录][项目ID][APP名称] 运行:cordova create hello com.example.hello hello 将在 ...
- ValidForm验证表单
在做项目时,要求熟悉项目中验证表单的插件,所以学习一下validForm这个插件 http://validform.rjboy.cn/document.html#validformObject
- Java基础面试操作题: File IO 文件过滤器FileFilter 练习 把一个文件夹下的.java文件复制到另一个文件夹下的.txt文件
package com.swift; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File ...
- 当c++遇上音乐
运用到的函数 #include <windows.h> Beep( f, t ); Sleep( t ); eep() 函数可以让蜂鸣器发出频率为f赫兹,音长大约为 2t 毫秒的音.(注意 ...
- 【js】window.onscroll 无效问题
body 设置为height:100% 导致window.onscroll 无效
- nrf528xx bootloader 模块介绍(转载)
转载https://www.cnblogs.com/rfnets/p/8205521.html 1. bootloader 的基本功能: 启动应用 几个应用之间切换 初始化外设 nordic nrf5 ...