题意:

一棵树,给出每个点的后代们,问你这棵树是否存在,存在就给出这棵树

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(拓扑排序)的更多相关文章

  1. 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 ...

  2. Codeforces Round #363 Fix a Tree(树 拓扑排序)

    先做拓扑排序,再bfs处理 #include<cstdio> #include<iostream> #include<cstdlib> #include<cs ...

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

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

  4. Almost Acyclic Graph CodeForces - 915D (思维+拓扑排序判环)

    Almost Acyclic Graph CodeForces - 915D time limit per test 1 second memory limit per test 256 megaby ...

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

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

  6. Codeforces 919D Substring 【拓扑排序】+【DP】

    <题目链接> 题目大意:有一个具有n个节点,m条边的有向图,每个点对应一个小写字母,现在给出每个顶点对应的字母以及有向边的连接情况,求经过的某一条路上相同字母出现的最多次数.如果次数无限大 ...

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

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

  8. poj 2367 Genealogical tree【拓扑排序输出可行解】

    Genealogical tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3674   Accepted: 2445 ...

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

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

随机推荐

  1. 数据库并发处理 - 上的一把好"锁"

    为什么要有锁? 我们都是知道,数据库中锁的设计是解决多用户同时访问共享资源时的并发问题.在访问共享资源时,锁定义了用户访问的规则.根据加锁的范围,MySQL 中的锁可大致分成全局锁,表级锁和行锁三类. ...

  2. iOS多线程编程原理及实践

    摘要:iOS开发中,开发者不仅要做好iOS的内存管理,而且如果你的iOS涉及多线程,那你也必须了解iOS编程中对多线程的限制,iOS主线程的堆栈大小为1M,其它线程均为512KB,且这个限制开发者是无 ...

  3. Vue中echarts的基本用法

    前言:同大多数的前端框架一样,先读官网的使用方法.学会基本使用后,在实例中找到自己想要demo.拿过来改一改,一个echarts图表就形成,毕竟人家做就是为了方便使用. 我是在vue中下面直接使用的e ...

  4. springboot +fastdfs 上传文件到到云服务器

    fastdfs在云服务器的搭建和配置:https://blog.csdn.net/qq_41592652/article/details/104006289 springboot结构如下: appli ...

  5. border 边框

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  6. Frameworks.Entity.Core 4

    Project.Core\Frameworks.Entity.Core\Commons\ 1 AutoMapperExtension.cs AutoMapper扩展方法 2枚举类型扩展方法EnumEx ...

  7. Spring-事务(1)

    一,注解的方式实现事务 1.Dao层 package com.atguigu.spring.tx; public interface BookShopDao { //根据书号获取书的单价 public ...

  8. JPA基本注解的使用

    一:JPA基本注解 使用: 使用: 使用: 查看表: 二:用table来生成主键 使用: allocationSize:每次增加多少 tablel:指定使用那张表 执行两次main方法后查看表: jp ...

  9. 解决 C# GetPixel 和 SetPixel 效率问题(转)

    在对Bitmap图片操作的时候,有时需要用到获取或设置像素颜色方法:GetPixel 和 SetPixel, 如果直接对这两个方法进行操作的话速度很慢,这里我们可以通过把数据提取出来操作,然后操作完在 ...

  10. HTTP的传输编码(Transfer-Encoding:chunked)

    转载自HTTP传输编码增加了传输量,只为解决这一个问题 | 实用 HTTP,本来是将这篇文章收藏在我的有道云笔记的,但是今天复习的时候看着这个标题这尴尬,这里转载一下-... 什么是传输编码? 传输编 ...