【构造题 贪心】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 ...
随机推荐
- es6入门5--class类的基本用法
在ES6之前,准确来说JavaScript语言并无类的概念,却有模拟类的做法.相比在类似java这类传统面向对象语言中通过类来生成实例,js则通过构造函数模拟类来生成实例. 这是因为在JS设计初期,作 ...
- css 文本溢出时显示省略号
.text-ellipsis { width:100px; height:60px; overflow: hidden;//隐藏滚动条 text-overflow:ellipsis; white-sp ...
- 关于Nginx对于PHP支持的问题
因为使用的mediawiki是用php写的,所以就遇上了一个问题,怎么让Nginx支持php,记得当时使用apache2的时候是安装了一个叫 “ libapache2-mod-php5 ”的mod , ...
- winform 程序隐藏窗口运行
DWPublishForm frm = new DWPublishForm(); frm.IsAutoUpdate = true; frm.ShowInTaskbar = false; frm.For ...
- 省厅报件7.0 读取mdb 生成xml 文件
using System;using System.Collections.Generic;using System.Data;using System.Data.OleDb;using System ...
- Java中的Validated验证
注意点:在使用@NotBlank时,必须与@Valid配着使用,不然不起作用(出现了很奇怪的现象,我第一次试的时候确实有这情况,但是第二次的时候这情况没了,所以这个说不准) @NotBlank 用在S ...
- C# 实现本地化日志管理
1.新建一个类库解决方案 CommnoLog 2.新建两个文件夹 2.1FileUtil.cs 代码如下 public static class FileUtil { /// <summary ...
- 【.Net MVC4 connectionString设置】获取SQL server数据库的连接字符串
第一步:创建向导文件 在桌面创建一个txt文件,并将文件后缀改成“.udl”. 第二步:选择“提供程序”tab页 双击新创建的“.udl”文件,进入后选择“提供程序”tab页,选择“Micros ...
- codevs 4888 零件分组
4888 零件分组 时间限制: 1 s 空间限制: 16000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description 现有一些棍状零件,每个零件都有 ...
- Mysql的介绍和安装注意
1.Mysql所属公司:Oracle 2.数据库类型:关系型数据库 3.开发语言:C++ 4.版本:企业收费版和社区免费版 5.搭建Mysql环境 点击下一步直到遇到选择数据库编码的时候选择utf-8 ...