Tree Restoration Gym - 101755F (并查集)
There is a tree of n vertices. For each vertex a list of all its successors is known (not only direct ones). It is required to restore the tree or to say there is no such tree.
Input
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of vertices in the tree.
Each of the next n lines contains an integer ci (0 ≤ ci ≤ n) — the number of successors of vertex i, and then ci distinct integers aij (1 ≤ aij ≤ n) — the indices of successors of vertex i.
Output
If the answer does not exist, output «NO».
Otherwise, in the first line output «YES», and then output n - 1 lines containing two integers each — indices of parent and child. Pairs (parent, child) can be output in any order.
Examples
5
4 2 3 4 5
3 3 4 5
2 4 5
1 5
0
YES
1 2
2 3
3 4
4 5
5
4 2 3 4 5
3 3 4 5
0
1 5
0
YES
1 2
2 3
2 4
4 5
3
3 2 3 1
3 3 1 2
3 1 2 3
NO 细节太多,大致先按子孙的数量排序。。。
还是请看我的代码吧,写这篇博客,主要是提醒一下坑点。
1.有环
2.建出来好几课树
3.自己不能当自己的儿子。
4.每个儿子都不能漏
数据一组:
7
5 2 3 4 5 6
2 4 5
2 6 7
0
0
0
0
还要注意并查集不能压缩路径
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define ls (t<<1)
#define rs ((t<<1)+1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = ;
const int inf = 2.1e9;
const ll Inf = ;
const int mod = ;
const double eps = 1e-;
const double pi = acos(-);
bool vis[maxn];
int num[maxn];
int f[maxn];
struct node
{
int id,sz;
}a[maxn];
vector<int>v[maxn];
int n,m; bool cmp(node a,node b){
return a.sz<b.sz;
} int mx=;
int getf(int x,int t){
if(t==mx){return -;}
if(x==f[x])return x;
return getf(f[x],t+);
} int get_num(int x,int t){
if(t==mx){return -;}
num[x]++;
if(x==f[x]){return x;}
return get_num(f[x],t+);
} vector<int>tt;
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&m);f[i]=i;
for(int j=;j<=m;j++){
int x;
scanf("%d",&x);
v[i].push_back(x);
a[i].sz++;
}
a[i].id=i;
} sort(a+,a++n,cmp);
bool flag=true;
for(int i=;i<=n;i++){
int siz = a[i].sz;
int id=a[i].id;
for(int j=;j<siz;j++){
vis[v[id][j]]=true;
}
vis[id]=true;
for(int j=;j<siz;j++){
int t1=getf(v[id][j],);
if(id==v[id][j]||t1==-||!vis[t1]){flag=false;break;}
if(t1==v[id][j]){tt.push_back(t1);}
}
int sz=tt.size();
for(int j=;j<sz;j++){
f[tt[j]]=id;
}
tt.clear();
if(!flag){break;}
memset(vis,,sizeof(vis));
} int rec=;
for(int i=;i<=n;i++){
if(f[i]==i){rec++;}
get_num(i,);
}
for(int i=;i<=n;i++){
if(num[i]!=v[i].size()+){flag=false;}
}
if(rec!=){flag=false;} if(flag){
printf("YES\n");
for(int i=;i<=n;i++){
if(f[i]!=i){printf("%d %d\n",f[i],i);}
}
}
else {
printf("NO\n");
} return ;
}
Tree Restoration Gym - 101755F (并查集)的更多相关文章
- Is It A Tree? POJ - 1308(并查集判树)
Problem Description A tree is a well-known data structure that is either empty (null, void, nothing) ...
- 【bzoj2870】最长道路tree 树的直径+并查集
题目描述 给定一棵N个点的树,求树上一条链使得链的长度乘链上所有点中的最小权值所得的积最大. 其中链长度定义为链上点的个数. 输入 第一行N 第二行N个数分别表示1~N的点权v[i] 接下来N-1行每 ...
- 【51nod1253】Kundu and Tree(容斥+并查集)
点此看题面 大致题意: 给你一棵树,每条边为黑色或红色, 求有多少个三元组\((x,y,z)\),使得路径\((x,y),(x,z),(y,z)\)上都存在至少一条红色边. 容斥 我们可以借助容斥思想 ...
- BZOJ 2870: 最长道路tree 树的直径+并查集
挺好的一道题. 把所有点都离线下来,一个个往里加入就行了. #include <cstdio> #include <algorithm> #define N 100003 #d ...
- BZOJ 2654 tree(二分答案+并查集)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2654 [题目大意] 给你一个无向带权连通图,每条边是黑色或白色. 让你求一棵最小权的恰 ...
- Hdu.1325.Is It A Tree?(并查集)
Is It A Tree? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- Is It A Tree?(并查集)
Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26002 Accepted: 8879 De ...
- CF109 C. Lucky Tree 并查集
Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal re ...
- Codeforces Gym 100463E Spies 并查集
Spies Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Desc ...
随机推荐
- QTP自动化测试-点滴-步骤
1 添加 test 2 设置 整个测试项目的 setting -数据表位置 3 添加 引用 方法文件 4 添加 action 5 添加 action 对应的 repository 控件库 6 录制.整 ...
- Delphi (Library Path Browsing Path)
首先要明白的一个概念是dcu文件 *.dcu是*.pas的编译后单元文件(Delphi Compiled Unit), 编译器把它和库文件连接起来就构成了可执行文件*.exe 或*.dll等,相当于C ...
- python 读取csv 数据并画图分析
数据源 : https://pan.baidu.com/s/1eR593Uy 密码: yqjh python环境 python3 #encoding: utf-8 import csv impo ...
- java.lang.Exception: No tests found matching [{ExactMatcher:fDisplayName=fun2], {ExactMatcher:fDisplayName=fun2(cn.itcast.demo2.fun1)], {LeadingIdentifierMatcher:fClassName=cn.itcast.demo2.fun1,fLeadi
Junit报的错误, 在测试方法前面没有添加注解@Test
- JS实现控制HTML5背景音乐播放暂停
首先在网页中嵌入背景音乐,html5代码为: <script src="http://wuover.qiniudn.com/jquery.js"></script ...
- linux-shell系列1-ip
#!/bin/sh # Ping网段所有IP ip1=192.168.30.ip2=192.168.31.ip3=192.168.232.for i in `seq 1 255`do ping -c ...
- Flask 构建微电影视频网站(一)
Flask构建电影视频网站 Python MTV模型 Flask微内核 Flask扩展插件配置及使用方法 根据业务开发网站前后台功能 Flask结合MySQL数据库 你将可以独立开发网站 独立部署运维 ...
- BZOJ 4196 软件包管理器
树链剖分 建树之后,安装软件就是让跟节点到安装的节点路径所有点权+1,卸载软件就是让一个节点和他的子数-1 要求变化数量的话直接求和相减就行啦(绝对值) 注意一点,一开始的lazyatag应该是-1, ...
- 【XSY2772】数列 特征多项式 数学
题目描述 给你一个数列: \[ f_n=\begin{cases} a^n&1\leq n\leq k\\ \sum_{i=1}^k(a-1)f_{n-i}&n>k \end{c ...
- bzoj 2653: middle (主席树+二分)
2653: middle Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 2522 Solved: 1434[Submit][Status][Disc ...