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 ...
随机推荐
- AngularJS从入门到精通
第一 AngularJS的四大特性 1. MVC 例如:使用angularjs向模板传递数据 <!doctype html> <html> <head> <m ...
- Windows 7 SP1 x64 LSP
NALapi.dll napinsp.dll pnrpnsp.dll mswsock.dll winrnr.dll
- webpack配置之代码优化
前面的话 前面介绍了webpack的基本配置,本文将详细介绍webpack中关于代码优化的配置 打包公共代码 CommonsChunkPlugin 插件,是一个可选的用于建立一个独立文件(又称作 ch ...
- 手写事务管理器 也是spring实现事务管理的原理
- [BZOJ 2705] [SDOI 2012] Longge的问题
Description Longge的数学成绩非常好,并且他非常乐于挑战高难度的数学问题.现在问题来了:给定一个整数 \(N\),你需要求出 \(\sum gcd(i, N)(1\le i \le N ...
- 洛谷P1057传球游戏题解
题目 这个题表面上看并不像DP,但是当我们看到方案数时,我们可能会想到什么??? 对,分类加法原理,在每一轮中,每一个点的方案数都要加上这个点左边的方案与右边的方案. 因此我们可以枚举,设一个DP数组 ...
- 【XSY2519】神经元 prufer序列 DP
题目描述 有\(n\)点,每个点有度数限制,\(\forall i(1\leq i\leq n)\),让你选出\(i\)个点,再构造一棵生成树,要求每个点的度数不超过度数限制.问你有多少种方案. \( ...
- Java实现月份递减
问题:从当前月份开始,往前3年的所有月份 返回map类型,key是String,value是Date,map倒序排列 public static Map<String, Date> get ...
- MT【245】小概率事件
(2011年AAA测试)将一枚均匀的硬币连续抛掷$n$次,以$P_n$ 表示未出现连续3次正面的概率.求$\{P_n\}$.并讨论$\{P_n\}$单调性和极限. 分类讨论:第$n$次反面则未出现连续 ...
- Hdoj 1050.Moving Tables 题解
Problem Description The famous ACM (Advanced Computer Maker) Company has rented a floor of a buildin ...