【构造题 贪心】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$的构造方法。那么其他点应该如何考虑?是应该在做出来的路径上分叉还是新开一条?

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

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

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

 1 #include<bits/stdc++.h>
 2 const int maxn = 1003;
 3
 4 int n;
 5 bool used[maxn];
 6 int mp[maxn][maxn];
 7 int edgeTot,edges[maxn<<1],nxt[maxn<<1],head[maxn];
 8
 9 int read()
10 {
11     char ch = getchar();
12     int num = 0;
13     bool fl = 0;
14     for (; !isdigit(ch); ch=getchar())
15         if (ch==‘-‘) fl = 1;
16     for (; isdigit(ch); ch=getchar())
17         num = (num<<1)+(num<<3)+ch-48;
18     if (fl) num = -num;
19     return num;
20 }
21 void errorDown()
22 {
23     puts("NO");
24     exit(0);
25 }
26 void addedge(int u, int v)
27 {
28     edges[++edgeTot] = v, nxt[edgeTot] = head[u], head[u] = edgeTot;
29     edges[++edgeTot] = u, nxt[edgeTot] = head[v], head[v] = edgeTot;
30 }
31 void dfs(int x, int fa)
32 {
33     for (int i=head[x]; i!=-1; i=nxt[i])
34     {
35         int v = edges[i];
36         if (v!=fa) printf("%d %d\n",x,v), dfs(v, x);
37     }
38 }
39 int main()
40 {
41     n = read();
42     memset(head, -1, sizeof head);
43     for (int i=1; i<n; i++)
44     {
45         int u = read(), v = read();
46         if (u > v) std::swap(u, v);
47         if (v!=n) errorDown();
48         mp[u][v]++;
49     }
50     for (int i=1; i<n; i++)
51         if (mp[i][n]){
52             if (i < mp[i][n]) errorDown();
53             used[i] = 1;
54             int lst = i, cnt = 0;
55             for (int j=1; j<i&&cnt<mp[i][n]-1; j++)
56                 if (!used[j]){
57                     used[j] = 1;
58                     addedge(lst, j);
59                     lst = j;
60                     cnt++;
61                 }
62             if (cnt!=mp[i][n]-1) errorDown();
63             addedge(lst, n);
64         }
65     puts("YES");
66     dfs(1, 0);
67     return 0;
68 }

链构造

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

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

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

END

原文地址:https://www.cnblogs.com/antiquality/p/9739744.html

时间: 2024-07-31 02:19:38

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

[CF1041E]Tree Reconstruction

题目大意:有一棵树,现在给你每条树边被去掉时,形成的两个联通块中点的最大的编号分别是多少,问满足条件的树存不存在,存在输出方案 题解:一条边的两个编号中较大的一个一定是$n$,否则无解. 开始构造这棵树,发现一定可以是一条链,可以钦定$n$在链的一端,把较小值(即前缀$max$)排序,每次变化是就把该位赋成变化值,否则就从前面随便取一个没用过的出来(若没有则无法构造). 为什么一定可以是链呢?发现无法构造的条件是排序后$i>max_i$,而此时也构造不出树(显然). 卡点:无 C++ Code:

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

题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=5355 题面: Cake Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1632    Accepted Submission(s): 273 Special Judge Problem Description There are s

HDU 5355 Cake (WA后AC代码,详细解析,构造题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5355 题面: Cake Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1632    Accepted Submission(s): 273 Special Judge Problem Description There are m

HDU 5402 (构造题) Travelling Salesman Problem

题意 略. 思路 比赛的时候其实已经意识到是一个构造题了. 蓝儿m,n都是偶数的时候搞崩了.sad.. m,n有一个是奇数不说了,可以走完所有. 两个都是偶数的时候,我们就去找一个最小的值且它的位置坐标和是奇数,然后就绕开这个走.其他都可以走完辣. 参考code: /* #pragma warning (disable: 4786) #pragma comment (linker, "/STACK:0x800000") */ #include <cassert> #incl

Codeforces 482 - Diverse Permutation 构造题

这是一道蛮基础的构造题. - k         +(k - 1)      -(k - 2) 1 + k ,    1 ,         k ,             2,    ................... \  /        \  /           \  / k          k-1          k-2 如图所示,先构造第一个数,就是1 + k, 然后接下来每个数字和上个数相差k , k -1 , k -2 这样下来,最后一个数字就是一个中间的数字,过程就

HDU 4961 Boring Sum 构造题

用一个数组c, c[i]表示i这个数出现的最近数字是几. 那么当加入一个6,则 c[1] = c[2] = c[3] = c[6] = 6; ==最近怎么都要开挂啊.. #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; const int N = 100005; inl

codeforces 459C - Pashmak and Buses 【构造题】

题目:codeforces 459C - Pashmak and Buses 题意:给出n个人,然后k辆车,d天时间,然后每天让n个人选择坐一辆车去上学,要去d天不能有任意两个人乘同一辆车,不能的话输出 -1 分类:数学,构造 分析:这个题目首先得分析,我开始想到的是首先用相同的放在一起,比如 7 2 3 这样构造 1 1 1 1 2 2 2 1 1 1 2 2 2 1 1 1 2 2 2 1 1 1 2 2 2 1 1 1 就是需要的天数跟每一行出现次数最多的数的出现次数相等,但是发现还有更优

CodeForces 26C Parquet 构造题

题目链接:点击打开链接 #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <math.h> #include <set> using namespace std; #define N 105 int n,m,a,b,c; char s[N][N]; set<char>myset; bool inm

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

3097: Hash Killer I Time Limit: 5 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 963  Solved: 364[Submit][Status][Discuss] Description 这天天气不错,hzhwcmhf神犇给VFleaKing出了一道题: 给你一个长度为N的字符串S,求有多少个不同的长度为L的子串. 子串的定义是S[l].S[l + 1].... S[r]这样连续的一段. 两个字符串被认为是