比赛时候还是太慢了……要是能做快点就能上分了

Monocarp has drawn a tree (an undirected connected acyclic graph) and then has given each vertex an index. All indices are distinct numbers from 11 to nn. For every edge ee of this tree, Monocarp has written two numbers: the maximum indices of the vertices of the two components formed if the edge ee (and only this edge) is erased from the tree.

Monocarp has given you a list of n−1n−1 pairs of numbers. He wants you to provide an example of a tree that will produce the said list if this tree exists. If such tree does not exist, say so.

Input

The first line contains one integer nn (2≤n≤10002≤n≤1000) — the number of vertices in the tree.

Each of the next n−1n−1 lines contains two integers aiai and bibi each (1≤ai<bi≤n1≤ai<bi≤n) — the maximal indices of vertices in the components formed if the ii-th edge is removed.

Output

If there is no such tree that can produce the given list of pairs, print "NO" (without quotes).

Otherwise print "YES" (without quotes) in the first line and the edges of the tree in the next n−1n−1 lines. Each of the last n−1n−1 lines should contain two integers xixi and yiyi (1≤xi,yi≤n1≤xi,yi≤n) — vertices connected by an edge.

Note: The numeration of edges doesn't matter for this task. Your solution will be considered correct if your tree produces the same pairs as given in the input file (possibly reordered). That means that you can print the edges of the tree you reconstructed in any order.


题目大意

现有一棵树。用$n-1$个二元组(x,y)描述树上每条边,表示:删去这条边后,两个连通块内分别最大的编号。

问是否存在一颗符合描述的树。

题目分析

菊花图构造

注意到树被分为两个连通块后,产生的二元组(x,y)中必定有一个元素为$n$.

那么,有多少个二元组$(x,y)(y=n)$就说明有多少条边满足:$[x+1,n]$这些点和$x$点分别在被割断的两块。

树有一个性质:两点间的路径唯一。那么为了割断$x$和$[x+1]...n$,这两个连通块只能有唯一路径,并且路径上的点都必须小于$x$。于是这里有了一种对于单个点$x$的构造方法。那么其他点应该如何考虑?是应该在做出来的路径上分叉还是新开一条?

构造题可以说分为两类:唯一解和多解。这种多解的题,当然选一种最简洁的构造方法。事实上这样对于单点做下去,构造菊花图的方式就是合法的。

我们对于构造菊花图的担忧主要在于,要是每次都新开一条路径,会不会浪费了一些点?换而言之,原先的路径上能不能够共用一些点?

然而,共用路径上的边无论如何只会贡献一种二元组。如果共用,为了达到给定的二元组数量,也只能在原先路径上插上一条可独立的完整路径————也就是说相当于没有共用。

 #include<bits/stdc++.h>
const int maxn = ; int n;
bool used[maxn];
int mp[maxn][maxn];
int edgeTot,edges[maxn<<],nxt[maxn<<],head[maxn]; int read()
{
char ch = getchar();
int num = ;
bool fl = ;
for (; !isdigit(ch); ch=getchar())
if (ch=='-') fl = ;
for (; isdigit(ch); ch=getchar())
num = (num<<)+(num<<)+ch-;
if (fl) num = -num;
return num;
}
void errorDown()
{
puts("NO");
exit();
}
void addedge(int u, int v)
{
edges[++edgeTot] = v, nxt[edgeTot] = head[u], head[u] = edgeTot;
edges[++edgeTot] = u, nxt[edgeTot] = head[v], head[v] = edgeTot;
}
void dfs(int x, int fa)
{
for (int i=head[x]; i!=-; i=nxt[i])
{
int v = edges[i];
if (v!=fa) printf("%d %d\n",x,v), dfs(v, x);
}
}
int main()
{
n = read();
memset(head, -, sizeof head);
for (int i=; i<n; i++)
{
int u = read(), v = read();
if (u > v) std::swap(u, v);
if (v!=n) errorDown();
mp[u][v]++;
}
for (int i=; i<n; i++)
if (mp[i][n]){
if (i < mp[i][n]) errorDown();
used[i] = ;
int lst = i, cnt = ;
for (int j=; j<i&&cnt<mp[i][n]-; j++)
if (!used[j]){
used[j] = ;
addedge(lst, j);
lst = j;
cnt++;
}
if (cnt!=mp[i][n]-) errorDown();
addedge(lst, n);
}
puts("YES");
dfs(, );
return ;
}

链构造

上一个做法的最后一段话并不是说不能构造出合法链。事实上可发现,刻意把小的节点接在大节点后也是可以的。

这里介绍一种非常巧妙的链构造:将$a_i$视作前缀最大值,由此构造一条链。

具体的证明和代码见  题解 CF1041E 【Tree Reconstruction】

END

【构造题 贪心】cf1041E. Tree Reconstruction的更多相关文章

  1. [CF1041E]Tree Reconstruction

    题目大意:有一棵树,现在给你每条树边被去掉时,形成的两个联通块中点的最大的编号分别是多少,问满足条件的树存不存在,存在输出方案 题解:一条边的两个编号中较大的一个一定是$n$,否则无解. 开始构造这棵 ...

  2. HDU 5355 Cake (WA后AC代码,具体解析,构造题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=5355 题面: Cake Time Limit: 2000/1000 MS (Java/Others) ...

  3. Aizu - 2564 Tree Reconstruction 并查集

    Aizu - 2564 Tree Reconstruction 题意:一个有向图,要使得能确定每一条边的权值,要求是每个点的入权和出权相等,问你最少需要确定多少条边 思路:这题好像有一个定理之类的,对 ...

  4. E. Tree Reconstruction 解析(思維)

    Codeforce 1041 E. Tree Reconstruction 解析(思維) 今天我們來看看CF1041E 題目連結 題目 略,請直接看原題 前言 一開始完全搞錯題目意思,還以為每次會刪除 ...

  5. cf251.2.C (构造题的技巧)

    C. Devu and Partitioning of the Array time limit per test 1 second memory limit per test 256 megabyt ...

  6. hdu4671 Backup Plan ——构造题

    link:http://acm.hdu.edu.cn/showproblem.php?pid=4671 其实是不难的那种构造题,先排第一列,第二列从后往前选. #include <iostrea ...

  7. Educational Codeforces Round 7 D. Optimal Number Permutation 构造题

    D. Optimal Number Permutation 题目连接: http://www.codeforces.com/contest/622/problem/D Description You ...

  8. Codeforces 482 - Diverse Permutation 构造题

    这是一道蛮基础的构造题. - k         +(k - 1)      -(k - 2) 1 + k ,    1 ,         k ,             2,    ....... ...

  9. BZOJ 3097: Hash Killer I【构造题,思维题】

    3097: Hash Killer I Time Limit: 5 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 963  Solved: 36 ...

随机推荐

  1. MySQL的slave_exec_mode参数作用

    主从复制中常会遇到的问题就是1062主键重复 如果在读写分离的架构中,slave同步失败会对业务造成很大的影响的 因此,很有必要对主从复制做些监控,做些自动化的处理.涉及到MySQL的一个参数slav ...

  2. 洛谷2444(Trie图上dfs判环)

    要点 并没问具体方案,说明很可能不是构造. 思考不断读入这个文本串,然后中间不出现某些文法的串.啊,这就是个自动机. 将不合法串使用ac自动机构成一个Trie图,我们需要的字符串就是在这个自动机上无限 ...

  3. 074 Search a 2D Matrix 搜索二维矩阵

    编写一个高效的算法来搜索 m x n 矩阵中的一个目标值.该矩阵具有以下特性:    每行中的整数从左到右排序.    每行的第一个整数大于前一行的最后一个整数.例如,以下矩阵:[  [1,   3, ...

  4. STM32之ADC(内部基准电压,参考电压)

    转 STM32内部参照电压VREFIN的使用 https://blog.csdn.net/uncle_guo/article/details/50625660 每个STM32芯片都有一个内部的参照电压 ...

  5. E. Mike and Foam 容斥原理

    http://codeforces.com/problemset/problem/548/E 这题是询问id,如果这个id不在,就插入这个id,然后求a[id1] ,  a[id2]互质的对数. 询问 ...

  6. P2737 [USACO4.1]麦香牛块Beef McNuggets 数学题 + 放缩思想

    https://www.luogu.org/problem/show?pid=2737#sub 先说一个结论:对于两个数p, q,且gcd(p, q) = 1(这个很重要,是条件来的).他们不能组合成 ...

  7. Codeforces Beta Round #96 (Div. 2) E. Logo Turtle dp

    http://codeforces.com/contest/133/problem/E 题目就是给定一段序列,要求那个乌龟要走完整段序列,其中T就是掉头,F就是向前一步,然后开始在原点,起始方向随意, ...

  8. JAVA基础之基本类型包装类、System类、Math类、Arrays类及大数据运算

    个人理解: 为了方便运算及调用一些方法,我们需要将基本类型的数值转换为对象:不过转换的时候需要特别注意好它们的类型到底是什么,需要调用方法的类名是哪个!特别注意是Byte常量池的相关问题(==):gc ...

  9. 【转】pom.xml讲解

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  10. java http的get,post请求

    初学可用F12查看任意网页帮助理解 package httpTest: import java.io.BufferedReader; import java.io.IOException;import ...