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

Input
5
4 2 3 4 5
3 3 4 5
2 4 5
1 5
0
Output
YES
1 2
2 3
3 4
4 5
Input
5
4 2 3 4 5
3 3 4 5
0
1 5
0
Output
YES
1 2
2 3
2 4
4 5
Input
3
3 2 3 1
3 3 1 2
3 1 2 3
Output
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 (并查集)的更多相关文章

  1. Is It A Tree? POJ - 1308(并查集判树)

    Problem Description A tree is a well-known data structure that is either empty (null, void, nothing) ...

  2. 【bzoj2870】最长道路tree 树的直径+并查集

    题目描述 给定一棵N个点的树,求树上一条链使得链的长度乘链上所有点中的最小权值所得的积最大. 其中链长度定义为链上点的个数. 输入 第一行N 第二行N个数分别表示1~N的点权v[i] 接下来N-1行每 ...

  3. 【51nod1253】Kundu and Tree(容斥+并查集)

    点此看题面 大致题意: 给你一棵树,每条边为黑色或红色, 求有多少个三元组\((x,y,z)\),使得路径\((x,y),(x,z),(y,z)\)上都存在至少一条红色边. 容斥 我们可以借助容斥思想 ...

  4. BZOJ 2870: 最长道路tree 树的直径+并查集

    挺好的一道题. 把所有点都离线下来,一个个往里加入就行了. #include <cstdio> #include <algorithm> #define N 100003 #d ...

  5. BZOJ 2654 tree(二分答案+并查集)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2654 [题目大意] 给你一个无向带权连通图,每条边是黑色或白色. 让你求一棵最小权的恰 ...

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

  7. Is It A Tree?(并查集)

    Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26002   Accepted: 8879 De ...

  8. CF109 C. Lucky Tree 并查集

    Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal re ...

  9. Codeforces Gym 100463E Spies 并查集

    Spies Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Desc ...

随机推荐

  1. mysql “Too many connections” 解决办法

    今天生产服务器上的MySQL出现了一个不算太陌生的错误“Too many connections”.平常碰到这个问题,我基本上是修改/etc/my.cnf的max_connections参数,然后重启 ...

  2. flask保存 文件到本地

    本篇队长介绍一下如何 把前端上传的文件保存 到 后端flask项目目录 首先讲一下上传.保存文件的思路: 第一步:前端通过post请求方式提交上传的文件 <input id="file ...

  3. Python——Flask框架——模板

    一.渲染模板 render_template 函数把Jinja2模板引擎集成到程序中 二.Jinja2变量过滤器 过滤器名 说明 safe 渲染值是不转义 capitalize 把值得首字母转换成大写 ...

  4. Lodop打印设计、维护、预览、直接打印简单介绍

    四者的区别和联系:(其中PRINT_DESIGN打印设计是提供给开发人员的,另外三个可开放给用户)PRINT_DESIGN打印设计:辅助开发人员设计,图形化拖动插入修改等,设计完成后,生成代码拷贝到程 ...

  5. Jsoup的使用

    http://caidongrong.blog.163.com/blog/static/21424025220139292525874/

  6. mysql分页查询按某类型置顶 按某类型置尾 再按优先级排序

    近段时间接到一个新需求: 第一优先级:未满的标的顺位高于已满标的顺位.第二优先级:新手标的顺位高于其他标的的顺位. 第三优先级:标的剩余可投金额少的顺位高于标的剩余可投金额多的. 我是直接通过sql语 ...

  7. Servlet篇 之 web服务器

    创建web项目,在web项目中创建html页面,然后把项目部署到web服务器里面,启动服务器之后,可以使用浏览器通过URL地址的方式,访问到web项目中的html页面了 Web服务器: 常用tomca ...

  8. Jarvis OJ A Piece Of Cake

    看图片的隐写术自闭,本来想看一看jarvisoj 的basic放松一下心情,结果一道题就做了一晚上qwq 首先看到这道题的时候想到的是凯撒密码(这其实是Google之后才知道这个名字的)枚举了26种位 ...

  9. 小程序 official-account

    只需要在页面中添加 <official-account></official-account> 需要注意的是: 1.当小程序从扫二维码场景(场景值1011)打开时 2.当小程序 ...

  10. Spring MVC 使用介绍(三)—— Controller接口控制器

    一.概述 Controller接口类图如下,其中,BaseCommandController已从Spring 4移除 基于继承Controller接口的方式已经不推荐使用,仅供学习参考 二.基于Con ...