Codeforces 1041 E

构造题。

给出一种操作,对于一棵树,去掉它的一条边。那么这颗树被分成两个部分,两个部分的分别的最大值就是这次操作的答案。

现在给出一棵树所有操作的结果,问能不能构造这样一颗树,可以的话输出它。

反正就是看每个数出现了几次,然后形成一条链,从这个数开始,依次减小,链向N。

这样处理每个数,就行了。 中间一旦有冲突(不能形成链了),直接NO。

#include <bits/stdc++.h>

using namespace std;

map<int,int> mp;

bool vis[1005];

struct node {
int l,r;
}; vector<node> ans;
int main() { int n; scanf("%d",&n); memset(vis,false,sizeof(vis));
int a,b;
for(int i = 1; i < n; i++) {
scanf("%d %d",&a,&b);
mp[a]++;
mp[b]++;
} if(mp[n] != n - 1) {
printf("NO\n");
return 0;
} node ad;
for(int i = 1; i < n; i++) { if(i - mp[i] + 1 < 1) {
printf("NO\n");
return 0;
}
int val = i;
if(!mp[val]) {
continue;
}
vis[val] = true; if(mp[val] == 1) {
ad.l = val;
ad.r = n;
ans.push_back(ad);
continue;
}
for(int j = 1; j < mp[i]; j++) {
int rr = val;
while(vis[val]) {
val--;
if(val < 1) {
printf("NO\n");
return 0;
}
}
vis[val] = true;
ad.l = val;
ad.r = rr;
ans.push_back(ad); }
ad.l = n;
ad.r = val;
ans.push_back(ad);
} printf("YES\n");
for(int i = 0; i < ans.size(); i++) {
printf("%d %d\n",ans[i].l,ans[i].r);
} return 0;
}

codeforces 1041 e 构造的更多相关文章

  1. codeforces 1041 E. Tree Reconstruction 和度数有关的构造树

    CF 1041E:http://codeforces.com/contest/1041/problem/E 题意: 告诉你一个树的节点个数,显然有n-1条边.已知去掉一条边后,两个集合中最大的节点值. ...

  2. Codeforces - 474D - Flowers - 构造 - 简单dp

    https://codeforces.com/problemset/problem/474/D 这道题挺好的,思路是这样. 我们要找一个01串,其中0的段要被划分为若干个连续k的0. 我们设想一个长度 ...

  3. Codeforces Global Round 8 B. Codeforces Subsequences(构造)

    题目链接:https://codeforces.com/contest/1368/problem/B 题意 构造最短的至少含有 $k$ 个 $codeforces$ 子序列的字符串. 题解 如下表: ...

  4. Codeforces 410C.Team[构造]

    C. Team time limit per test 1 second memory limit per test 256 megabytes input standard input output ...

  5. Codeforces 716C[数论][构造]

    /* CF傻逼构造题 某人要经过n回合游戏,初始分值是2,等级为1. 每次有两种操作 1.无条件,分值加上自己的等级数. 2.当目前的数字是完全平方数并且该数字开方以后是等级数加1的整数倍,那么可以将 ...

  6. Tea Party CodeForces - 808C (构造+贪心)

    Polycarp invited all his friends to the tea party to celebrate the holiday. He has ncups, one for ea ...

  7. Codeforces.578E.Walking(构造)

    题目链接 \(Description\) 给定一个长为\(n\)的足迹序列(只包含\(L,R\)两种字符),你需要\(LRLRLR...\)这样交替在\(L\)和\(R\)上走(第一步可以选择\(L\ ...

  8. New Roads CodeForces - 746G (树,构造)

    大意:构造n结点树, 高度$i$的结点有$a_i$个, 且叶子有k个. 先确定主链, 然后贪心放其余节点. #include <iostream> #include <algorit ...

  9. Subordinates CodeForces - 737C (树,构造)

    大意: 求构造一棵树, 每个节点回答它的祖先个数, 求最少打错次数. 挺简单的一个构造, 祖先个数等价于节点深度, 所以只需要确定一个最大深度然后贪心即可. 需要特判一下根的深度, 再特判一下只有一个 ...

随机推荐

  1. 洛谷 P2032 扫描

    https://www.luogu.org/problemnew/show/P2032 为啥不用STL,多方便. 定义一个大根堆,里边放一对数,这个数的大小和位置. 我们对于每次查询,判断首元素的位置 ...

  2. 【贪心】bzoj1045: [HAOI2008] 糖果传递

    很妙的贪心思考过程 Description 有n个小朋友坐成一圈,每人有ai个糖果.每人只能给左右两人传递糖果.每人每次传递一个糖果代价为1. Input 第一行一个正整数nn<=1'000'0 ...

  3. jquery如何获取手机网页触屏坐标:ontouchstart 、ontouchend、ontouchmove

    function handleTouchEvent(event) { //只跟踪一次触摸 ) { var output = document.getElementById("output&q ...

  4. CSS3的渐变-gradient

    CSS3 Gradient分为linear-gradient(线性渐变)和radial-gradient(径向渐变). CSS3的线性渐变 一.线性渐变在Mozilla下的应用 语法: -moz-li ...

  5. python学习第一天 计算机基础知识

    目录 什么是编程语言 什么是编程? 为什么要编程? 计算机5大组成分别有什么作用? qq启动的流程? 建议相关学习 课外 什么是编程语言 什么是编程语言? python和中文.英语一样,都是一门语言, ...

  6. ACM训练联盟周赛 K. Teemo's reunited

    Teemo likes to drink raspberry juice.  He even spent some of his spare time tomake the raspberry jui ...

  7. ORACLE常用修改字段脚本

    describe employees; = select column_name,data_type,nullable,data_length,data_ precision,data_scale f ...

  8. Windows 安装 composer

    在 composer.phar 同级目录下新建文件 composer.bat : C:\bin>echo @php "%~dp0composer.phar" %*>co ...

  9. hdu2087

    #include <stdio.h> #include <string.h> int main(){ int cnt,i,j,k; +],tmp[+]; int strl,tm ...

  10. POJ-1200 Crazy Search,人生第一道hash题!

                                                        Crazy Search 真是不容易啊,人生第一道hash题竟然是搜博客看题解来的. 题意:给你 ...