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 ...
随机推荐
- 【5min+】 秋名山的竞速。 ValueTask 和 Task
系列介绍 简介 [五分钟的dotnet]是一个利用您的碎片化时间来学习和丰富.net知识的博文系列.它所包含了.net体系中可能会涉及到的方方面面,比如C#的小细节,AspnetCore,微服务中的. ...
- Codeforces - A. Watermelon
A. Watermelon time limit per test 1 second memory limit per test 64 megabytes input standard input o ...
- 秦皇岛 I 题
有 n 个数字,你可以挑选其中任意个数字代表一个背包的体积,其余的数字表示的物品的体积,所挑选的背包必选完全装满,询问最终的方案数 思路分析 : 定义dp[i] 表示挑选 i 状态下的物品的方案数,f ...
- 线段树 or 并查集 (多一个时间截点)
There is a company that has N employees(numbered from 1 to N),every employee in the company has a im ...
- Hive 这些基础知识,你忘记了吗?
Hive 其实是一个客户端,类似于navcat.plsql 这种,不同的是Hive 是读取 HDFS 上的数据,作为离线查询使用,离线就意味着速度很慢,有可能跑一个任务需要几个小时甚至更长时间都有可能 ...
- Akka Java 文档 -- 容错
[转自: http://blog.csdn.net/zjw10wei321/article/details/46911825] 容错 实际中的故障处理 容错案例图解 容错案例所有源码 创建新的监管策略 ...
- 字典 pop
1.pop(key) 删除键值对,返回value2.若字典中没有这个key,则返回None,也可以自定义3.可用作if条件判断 来源: rest framework 框架 Serializer que ...
- pymysql 增
1.重点:数据库插入数据需要进行---------事物提交 其它:事物回滚 import pymysql # 注册用户 print("=========注册用户===========&quo ...
- ORM _meta
import os if __name__ == '__main__': os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'XadminDemon.se ...
- windows下配置tomcat的虚拟路径编译器为IDEA
在tomcat中配置好后发现运行项目还是无效 我们需要打开tomcat的配置,把下方的红色区域勾选上即可生效 具体原因如下 那是由于默认时IDEA的热部署机制,会在以下路径: [C:\Users\系统 ...