Avito Code Challenge 2018 C
1 second
256 megabytes
standard input
standard output
Ramesses knows a lot about problems involving trees (undirected connected graphs without cycles)!
He created a new useful tree decomposition, but he does not know how to construct it, so he asked you for help!
The decomposition is the splitting the edges of the tree in some simple paths in such a way that each two paths have at least one common vertex. Each edge of the tree should be in exactly one path.
Help Remesses, find such a decomposition of the tree or derermine that there is no such decomposition.
The first line contains a single integer nn (2≤n≤1052≤n≤105) the number of nodes in the tree.
Each of the next n−1n − 1 lines contains two integers aiai and bibi (1≤ai,bi≤n1≤ai,bi≤n, ai≠biai≠bi) — the edges of the tree. It is guaranteed that the given edges form a tree.
If there are no decompositions, print the only line containing "No".
Otherwise in the first line print "Yes", and in the second line print the number of paths in the decomposition mm.
Each of the next mm lines should contain two integers uiui, vivi (1≤ui,vi≤n1≤ui,vi≤n, ui≠viui≠vi) denoting that one of the paths in the decomposition is the simple path between nodes uiui and vivi.
Each pair of paths in the decomposition should have at least one common vertex, and each edge of the tree should be presented in exactly one path. You can print the paths and the ends of each path in arbitrary order.
If there are multiple decompositions, print any.
4
1 2
2 3
3 4
Yes
1
1 4
6
1 2
2 3
3 4
2 5
3 6
No
5
1 2
1 3
1 4
1 5
Yes
4
1 2
1 3
1 4
1 5
The tree from the first example is shown on the picture below:
The number next to each edge corresponds to the path number in the decomposition. It is easy to see that this decomposition suits the required conditions.
The tree from the second example is shown on the picture below:
We can show that there are no valid decompositions of this tree.
The tree from the third example is shown on the picture below:
The number next to each edge corresponds to the path number in the decomposition. It is easy to see that this decomposition suits the required conditions.
题意 分成任意条路 任意两条路至少有一个交点 而且任意两条路不能有相同的边 输出 路径
解析 可以得到 所有路只有一个交点 所以最多只有一个点的度数大于等于3 才有解决方案 那个点就是交点 以它为起点枚举路径就好了
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=,maxn=1e5+;
#define ios() ios::sync_with_stdio(false),cin.tie(),cout.tie()
vector<int> g[maxn];
int vis[maxn],ans[maxn],cnt;
void dfs(int x)
{
vis[x]=;
int flagg=;
for(int i=;i<g[x].size();i++)
{
int u=g[x][i];
if(vis[u]==)
{
flagg++;
dfs(u);
}
}
if(flagg==)
ans[cnt++]=x;
}
int main()
{
ios();
int n;
cin>>n;
for(int i=;i<n-;i++)
{
int x,y;
cin>>x>>y;
g[x].push_back(y);
g[y].push_back(x);
}
int flag=,sum=;
for(int i=;i<=n;i++)
{
if(g[i].size()>=)
sum++,flag=i;
}
if(sum>=)
cout<<"No"<<endl;
else
{
memset(vis,,sizeof(vis));
cnt=;
dfs(flag);
cout<<"Yes"<<'\n'<<cnt<<endl;
for(int i=;i<cnt;i++)
{
cout<<flag<<" "<<ans[i]<<endl;
}
}
}
Avito Code Challenge 2018 C的更多相关文章
- Codeforces Avito Code Challenge 2018 D. Bookshelves
Codeforces Avito Code Challenge 2018 D. Bookshelves 题目连接: http://codeforces.com/contest/981/problem/ ...
- Codeforces - Avito Code Challenge 2018
Portal A. Antipalindrome 暴力. B. Businessmen Problems 暴力. C. Useful Decomposition 居然不是C打头的?! 将一棵树划分成若 ...
- cf掉分记——Avito Code Challenge 2018
再次作死的打了一次cf的修仙比赛感觉有点迷.. 还好掉的分不多(原本就太低没法掉了QAQ) 把会做的前三道水题记录在这.. A: Antipalindrome emmmm...直接暴力枚举 code: ...
- Avito Code Challenge 2018
第一次打CF,很菜,A了三道水题,第四题好像是是数位DP,直接放弃了.rateing从初始的1500变成了1499,还是绿名,这就很尴尬.之后觉得后面的题目也没有想象的那么难(看通过人数)过两天吧剩下 ...
- Avito Code Challenge 2018 A~E
A. Antipalindrome 还以为是什么神dp结果就是分情况讨论啊 原串是一串一样的字符的话输出0,是回文串的话输出n-1,否则直接输出原串长度 #include<iostream> ...
- [Avito Code Challenge 2018 G] Magic multisets(线段树)
题目链接:http://codeforces.com/contest/981/problem/G 题目大意: 有n个初始为空的‘魔法’可重集,向一个‘可重集’加入元素时,若该元素未出现过,则将其加入: ...
- Avito Cool Challenge 2018 E. Missing Numbers 【枚举】
传送门:http://codeforces.com/contest/1081/problem/E E. Missing Numbers time limit per test 2 seconds me ...
- Avito Cool Challenge 2018 C. Colorful Bricks 【排列组合】
传送门:http://codeforces.com/contest/1081/problem/C C. Colorful Bricks time limit per test 2 seconds me ...
- Avito Cool Challenge 2018 B. Farewell Party 【YY】
传送门:http://codeforces.com/contest/1081/problem/B B. Farewell Party time limit per test 1 second memo ...
随机推荐
- [转]Visual F# Samples and Walkthroughs
本文转自:http://msdn.microsoft.com/en-US/library/vstudio/ee241126.aspx This topic provides links to samp ...
- if判断的时候明明是null却不走null的函数体?
String phoneStr = String.valueOf(parmMap.get(phone.trim())); if(StringUtils.isBlank(phoneStr) || &qu ...
- 纯css实现的三级水平导航菜单
vscode练习使用开发纯css的三级水平导航菜单.先上图: 1.html5布局 <html> <head> <meta charset="UTF-8" ...
- 使用iconfont管理项目中的字体图标
先来说说字体图标的好处: 很容易任意地缩放: 很容易地改变颜色: 很容易地产生阴影: 可以拥有透明效果: 一般来说,有先进的浏览器支持: 可以使用CSS来装饰(可以得到CSS很好支持): 可以快速转化 ...
- SQL Server xtype的介绍
sysobjects 表 在数据库内创建的每个对象(约束.默认值.日志.规则.存储过程等)在表中占一行.只有在 tempdb 内,每个临时对象才在该表中占一行. 列名 数据类型 描述 name sys ...
- C#枚举中的位运算权限分配
什么是位运算 常用的位运算主要有与(&), 或(|)和非(~), 比如: & = ; | = ; ~ = ; 运用在权限设计中 先建立一个枚举表示所有的权限管理操作: [Flags] ...
- bat 时间 的运算与提取
比如在系统中date这个环境变量的值为 -- 星期六 年------%date:~,% 表示从左向右指针向右偏0位,然后从指针偏移到的位置开始提取4位字符,结果是2011 月------%date:~ ...
- ARM开发板如何选型-I.MX6Q开发板
拥有丰富扩展能力,供货周期长的开发平台,省事安心 处理器:迅为-i.MX6开发板恩智浦Cortex-A9 四核i.MX6Q处理器,主频1GHz,内存2G,存储16GB. 系统支持:i.MX6开发板 ...
- sql中的日期时间处理
每个数据库,不同的日期格式化: 1.mysql 2.sqlserver 使用Convert()函数: select convert(char(10),GetDate(),120) as Date 第3 ...
- WNDCLASS和WNDCLASSEX
typedef struct { UINT cbSize; UINT style; WNDPROC lpfnWndProc; int cbClsExtra; int cbWndExtra; HINST ...