码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Round #509 (Div. 2) E. Tree Reconstruction

时间:2018-09-19 22:02:36      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:nbsp   unsigned   stdin   code   数字   ace   pre   push   namespace   

题目链接:http://codeforces.com/contest/1041/problem/E

题意:给出n - 1对pair,构造一颗树,使得断开其中一条边,树两边的最大值为 a 和 b 。

题解:显示最大值出现的次数为n - 1,且i点出现的次数小于等于i。一个数字 i(< n)出现的次数为 i 到 n 的距离,可构造出一条链使得满足条件。

 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define ll long long
 4 #define LL __int128
 5 #define ull unsigned long long
 6 #define mst(a,b) memset((a),(b),sizeof(a))
 7 #define mp(a,b) make_pair(a,b)
 8 #define fi first
 9 #define se second
10 #define pi acos(-1)
11 #define pii pair<int,int>
12 #define pb push_back
13 const int INF = 0x3f3f3f3f;
14 const double eps = 1e-6;
15 const int MAXN = 1e3 + 10;
16 const int MAXM = 2e6 + 10;
17 const ll mod = 1e9 + 7;
18 
19 int du[MAXN];
20 
21 int main()
22 {
23 #ifdef local
24     freopen("data.txt", "r", stdin);
25 //    freopen("data.txt", "w", stdout);
26 #endif
27     int n;
28     scanf("%d",&n);
29     bool flag = true;
30     for(int i = 1; i < n; i++) {
31         int a,b;
32         scanf("%d%d",&a,&b);
33         du[a]++;
34         if(b != n) flag = false;
35     }
36     int now = 0;
37     for(int i = 1; i < n; i++) {
38         now += du[i];
39         if(now > i) flag = false;
40     }
41     if(!flag) {
42         puts("NO");
43         return 0;
44     }
45     puts("YES");
46     set<int>se;
47     for(int i = 1; i <= n; i++) se.insert(i);
48     int pre = -1;
49     for(int i = 1; i < n; i++) {
50         if(du[i]) {
51             if(pre != -1) printf("%d %d\n",pre,i);
52             pre = i;
53             du[i]--, se.erase(i);
54         }
55         while(du[i]) {
56             printf("%d %d\n",pre,*se.begin());
57             pre = *se.begin();
58             du[i]--, se.erase(se.begin());
59         }
60     }
61     printf("%d %d\n",pre,n);
62     return 0;
63 }

 

Codeforces Round #509 (Div. 2) E. Tree Reconstruction

标签:nbsp   unsigned   stdin   code   数字   ace   pre   push   namespace   

原文地址:https://www.cnblogs.com/scaulok/p/9676629.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!