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 ...
随机推荐
- solr学习(一)安装与部署
经过测试,同步MongoDB数据到Solr的时候,Solr版本为8.4.0会出现连接不上的错误,8.3.0未经测试不知,博主测试好用的一版为8.2.0,但是官网已经下不到了,所以我会把下载链接放在文末 ...
- JavaSelenium 页面常用基本操作语法
元素定位 findElement() 方法返回一个元素, 如果没有找到,会抛出一个异常 NoElementFindException() fi ...
- 「newbee-mall新蜂商城开源啦」1000 Star Get !仓库Star数破千!记录一下
新蜂商城已经开源了 3 个多月左右的时间,在 2019 年的年末,仓库的 Star 数量冲破了 1000,整理本篇文章的时间是 2020 年 1 月 12 日,目前的 Star 数量是 1180 左右 ...
- 【笔试/面试题】中科创达——9.28(持续更新ing)
1. 线程与进程的区别 进程是具有一定独立功能的程序关于某个数据集合上的一次运行活动,是系统进行资源分配和调度的一个独立单位. 线程是进程的一个实体,是CPU调度和分派的基本单位,它是比进程更小的能独 ...
- C++中全排列函数next_permutation 用法
今天蓝桥杯刷题时发现一道字符串排序问题,突然想起next_permutation()函数和prev_permutation()函数. 就想写下next_permutation()的用法 next_pe ...
- 题解 LA3720
题目大意 多组数据,每组数据给定两个整数 \(n,m\),请求出 \(n\times m\) 的点阵(即 \((n-1)\times(m-1)\) 的方格)中有多少条非水平竖直的经过至少两个格点的不同 ...
- HTTPS中的TLS
1. SSL 与 TLS SSL:(Secure Socket Layer) 安全套接层,于 1994 年由网景公司设计,并于 1995 年发布了 3.0 版本TLS:(Transport Layer ...
- vue+bootstrap4+mybatis分页
先看效果 Springboot+Mybatis+Pagehelper分页具体实现略. Controller返回数据 @GetMapping("/findByPage") publi ...
- 前端笔记6-js2
1.break 和continue用法 break结束本次循环,如果想结束外层循环,可以通过这个label来指定要结束的循环. continue可以用来跳过当次循环,如果想跳过外次循环,也可以通过这个 ...
- KVM虚拟化基础
关于虚拟化 什么是虚拟化 在计算机技术中,虚拟化(技术)或虚拟技术(英语:Virtualization)是一种资源管理技术,是将计算机的各种实体资源(CPU.内存.磁盘空间.网络适配器等),予以抽象. ...