【构造题 贪心】cf1041E. Tree Reconstruction
比赛时候还是太慢了……要是能做快点就能上分了
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的更多相关文章
- [CF1041E]Tree Reconstruction
题目大意:有一棵树,现在给你每条树边被去掉时,形成的两个联通块中点的最大的编号分别是多少,问满足条件的树存不存在,存在输出方案 题解:一条边的两个编号中较大的一个一定是$n$,否则无解. 开始构造这棵 ...
- HDU 5355 Cake (WA后AC代码,具体解析,构造题)
题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=5355 题面: Cake Time Limit: 2000/1000 MS (Java/Others) ...
- Aizu - 2564 Tree Reconstruction 并查集
Aizu - 2564 Tree Reconstruction 题意:一个有向图,要使得能确定每一条边的权值,要求是每个点的入权和出权相等,问你最少需要确定多少条边 思路:这题好像有一个定理之类的,对 ...
- E. Tree Reconstruction 解析(思維)
Codeforce 1041 E. Tree Reconstruction 解析(思維) 今天我們來看看CF1041E 題目連結 題目 略,請直接看原題 前言 一開始完全搞錯題目意思,還以為每次會刪除 ...
- cf251.2.C (构造题的技巧)
C. Devu and Partitioning of the Array time limit per test 1 second memory limit per test 256 megabyt ...
- hdu4671 Backup Plan ——构造题
link:http://acm.hdu.edu.cn/showproblem.php?pid=4671 其实是不难的那种构造题,先排第一列,第二列从后往前选. #include <iostrea ...
- Educational Codeforces Round 7 D. Optimal Number Permutation 构造题
D. Optimal Number Permutation 题目连接: http://www.codeforces.com/contest/622/problem/D Description You ...
- Codeforces 482 - Diverse Permutation 构造题
这是一道蛮基础的构造题. - k +(k - 1) -(k - 2) 1 + k , 1 , k , 2, ....... ...
- BZOJ 3097: Hash Killer I【构造题,思维题】
3097: Hash Killer I Time Limit: 5 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 963 Solved: 36 ...
随机推荐
- layui时间控件联动:开始时间、结束时间,有效时间范围
实际开发中,经常遇到时间控件联动的情况,然后每次都网上搜代码Copy,不过每次都有点小Bug让你错不及防: 这次,在这里备份一下,以后Copy自己的得了(欢迎Copy代码,若遇到问题,请麻烦回复我一下 ...
- 03.Jquery Mobile中的按钮
一. 基础按钮 1.设置链接的data-role,使其变成按钮. <a href="index.html" data-role="button">L ...
- HttpServletRequest 和 HttpServletResponse
Servlet配置方式 全路径匹配 以 / 开始 /a /aa/bb localhost:8080/项目名称/aa/bb 路径匹配 , 前半段匹配 以 / 开始 , 但是以 * 结束 /a/* /* ...
- Spring中的注入方式 和使用的注解 详解
注解:http://www.cnblogs.com/liangxiaofeng/p/6390868.html 注入方式:http://www.cnblogs.com/java-class/p/4727 ...
- Qt5.7中使用MySQL Driver(需要把libmysql.dll文件拷贝到Qt的bin目录中。或者自己编译的时候,链接静态库)
Qt5.7中使用MySQL Driver 1.使用环境 Qt5.7的安装安装就已经带了MySQL Driver,只需要在安装的时候选择一下即可.如果没有安装,可以采取自己编译的方式.在Qt的源码包的q ...
- (转)生产环境常见的HTTP状态码列表(老男孩整理)
生产环境常见的HTTP状态码列表(老男孩整理) 原文:http://blog.51cto.com/oldboy/716294 ##################################### ...
- 表单验证插件jquery.validate.js
最常使用JavaScript的场合就是表单的验证,而jQuery作为一个优秀的JavaScript库,也提供了一个优秀的表单验证插件----Validation.Validation是历史最悠久的jQ ...
- 详解Java构造方法为什么不能覆盖,我的钻牛角尖病又犯了....
一 看Think in Java,遇到个程序 class Egg2 { protected class Yolk { public Yolk() { System.out.println(" ...
- Map和Map.Entry
Map是java中的接口,Map.Entry是Map的一个内部接口. Map.entrySet()的返回值也是返回一个Set集合,此集合的类型为Map.Entry. Map.Entry是Map声明的一 ...
- JSONModel 简单例子
// ProductModel.h // JSONModel // // Created by 张国锋 on 15/7/20. // Copyright (c) 2015年 张国锋. All righ ...