Codeforces gym101755F Tree Restoration(拓扑排序)
题意:
一棵树,给出每个点的后代们,问你这棵树是否存在,存在就给出这棵树
n<=1000
思路:
对祖先->后代建立有向图,跑拓扑排序。跑的时候不断更新父亲并判断答案的存在性,同时注意一种情况:一个点他儿子是他的后代,同样也得是他父亲的后代,这样传递下去就一定是所有祖宗的后代。
代码:
代码参考网上的
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<vector>
#include<map>
#include<functional> #define fst first
#define sc second
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lc root<<1
#define rc root<<1|1
#define lowbit(x) ((x)&(-x)) using namespace std; typedef double db;
typedef long double ldb;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
typedef pair<ll,ll> PLL; const db eps = 1e-;
const int mod = 1e9+;
const int maxn = 1e6+;
const int maxm = 1e6+;
//const int inf = 0x3f3f3f3f;
const int inf = 1e9+;
const db pi = acos(-1.0); int n;
vector<int>v[maxn];
int f[maxn];
int ma[][];
int d[maxn];
int root;
bool sv(){
queue<int>q;
for(int i = ; i <= n; i++){
if(d[i]==)q.push(i);
}
if(q.size()!=)return false;
root = q.front();
while(!q.empty()){
int top = q.front();
q.pop();
for(int i = ; i < v[top].size(); i++){
int x = v[top][i];
d[x]--;
if(f[x]!=-&&!ma[f[x]][top])return false;
f[x] = top;
if(d[x]==)q.push(x);
}
} for(int i = ; i <= n; i++){
if(d[i]!=)return false;
}f[root]=;
for(int i = ; i <= n; i++){
//if(i!=root){
for(int j = ; j < v[i].size(); j++){
if(!ma[f[i]][v[i][j]]){
//printf("--%d %d %d\n",i,f[i],v[i][j]);
return false;
}
}
//}
}
return true;
}
int main() {
scanf("%d", &n);
for(int i = ; i <= n; i++){
ma[][i] = ;
f[i] = -;
int k;
scanf("%d", &k);
for(int j = ; j <= k; j++){
int x;
scanf("%d", &x);
v[i].pb(x);
d[x]++;
ma[i][x]=;
}
}
mem(f,-); if(sv()){printf("YES\n");
for(int i = ; i <= n; i++){
if(i==root)continue;
printf("%d %d\n",f[i],i);
}}
else printf("NO");
return ;
}
/*
4
2 2 3
2 3 4
1 4
0
*/
Codeforces gym101755F Tree Restoration(拓扑排序)的更多相关文章
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) E. Tree Folding 拓扑排序
E. Tree Folding 题目连接: http://codeforces.com/contest/765/problem/E Description Vanya wants to minimiz ...
- Codeforces Round #363 Fix a Tree(树 拓扑排序)
先做拓扑排序,再bfs处理 #include<cstdio> #include<iostream> #include<cstdlib> #include<cs ...
- POJ 2367:Genealogical tree(拓扑排序模板)
Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7285 Accepted: 4704 ...
- Almost Acyclic Graph CodeForces - 915D (思维+拓扑排序判环)
Almost Acyclic Graph CodeForces - 915D time limit per test 1 second memory limit per test 256 megaby ...
- poj 2367 Genealogical tree (拓扑排序)
火星人的血缘关系很奇怪,一个人可以有很多父亲,当然一个人也可以有很多孩子.有些时候分不清辈分会产生一些尴尬.所以写个程序来让n个人排序,长辈排在晚辈前面. 输入:N 代表n个人 1~n 接下来n行 第 ...
- Codeforces 919D Substring 【拓扑排序】+【DP】
<题目链接> 题目大意:有一个具有n个节点,m条边的有向图,每个点对应一个小写字母,现在给出每个顶点对应的字母以及有向边的连接情况,求经过的某一条路上相同字母出现的最多次数.如果次数无限大 ...
- POJ 2367 Genealogical tree【拓扑排序】
题意:大概意思是--有一个家族聚集在一起,现在由家族里面的人讲话,辈分高的人先讲话.现在给出n,然后再给出n行数 第i行输入的数表示的意思是第i行的子孙是哪些数,然后这些数排在i的后面. 比如样例 5 ...
- poj 2367 Genealogical tree【拓扑排序输出可行解】
Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3674 Accepted: 2445 ...
- POJ 2367:Genealogical tree(拓扑排序)
Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2738 Accepted: 1838 Spe ...
随机推荐
- 侠说java8--Stream流操作学习笔记,都在这里了
前言 首次接触到Stream的时候以为它是和InputStream.OutputStream这样的输入输出流的统称. 流和集合的前世今生 概念的差异 在开发中,我们使用最多的类库之一就是集合.集合是一 ...
- 代码注释规范-IDEA 配置 Java 类方法注释模板
1. 引言 团队开发时,业务模块分配的越清晰,代码注释管理越完善,越有利于后面维护,后面再管理也方便不少.另外也起着"文字砖"的作用,你懂的.注释不需要很详细,把代码块方法 ...
- Scala实践3
一.函数式对象 1.1 rational类的规格和创建 Rational类来源于有理数(rational number),来表示n(分子)/d(分母)的数字,同时对有理数的运算(加减乘除)建模,还具 ...
- MySQL/数据库 知识点总结
书籍推荐 <SQL基础教程(第2版)> (入门级) <高性能MySQL : 第3版> (进阶) 文字教程推荐 SQL Tutorial (SQL语句学习,英文).SQL Tut ...
- sql server 新建用户 18456
麻辣各级,今天阴沟里翻船 了,自己在家创建sqlserver新的用户名,一直报错 18456 邮件添加用户名这一套下来是没错. 重要是这样===>要重新启动一下sql server,就ok了. ...
- Python知识体系框架 思维导图
技术文档已经独立整理! 请移步个人技术文档:https://anxiangchegu.github.io/technical-doc 如需更多Java.Python.大数据体系知识,请稳移步个人技术文 ...
- 一起来学习XPATH,来看看除了正则表达式我们还能怎么抓取数据
参考学习的网站链接http://www.w3school.com.cn/xpath/xpath_intro.asp 首先理清楚一些常识 以此为例 <?xml version="1.0& ...
- 使用 LinkedBlockingQueue 实现简易版线程池
前一阵子在做联系人的导入功能,使用POI组件解析Excel文件后获取到联系人列表,校验之后批量导入.单从技术层面来说,导入操作通常情况下是一个比较耗时的操作,而且如果联系人达到几万.几十万级别,必须拆 ...
- isinstance 和type
推荐使用 isinstance 判断对象类型. isinstance 的用法: 语法: isinstance(object, classinfo) 其中,object 是变量,classinfo 是类 ...
- selenium2-Python环境搭建
一.什么是selenium? selenium主要用于web应用的自动化测试,但并不局限于此,它还支持基于所有web的管理任务自动化,且开源免费,多浏览器支持(IE,Mozilla Firefox,S ...