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 ...
随机推荐
- Windows远程桌面连接复制文件失败或非常慢
环境搭建过程中经常会遇到需要将文件从本机传到云服务器或者企业内部服务器上的场景,此时如果文件过大的话要传个半天或者直接告诉你复制失败,让人好生无奈 ~ ~. windows环境下,可以将本地磁盘映 ...
- vue学习之遇见的问题
1.本地图片加载不出来 错误原因:图片放置位置不对: 解决方法:需要将图片放在static文件夹里
- CCF|路径解析|Java
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in=ne ...
- CentOS 6.4 linux下编译安装MySQL5.6.14
CentOS 6.4下通过yum安装的MySQL是5.1版的,比较老,所以就想通过源代码安装高版本的5.6.14. 正文: 一:卸载旧版本 使用下面的命令检查是否安装有MySQL Server rpm ...
- jQuery 价格显示 前面位数与后面两位显示不同样式(一大一小)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 数组,寻找第K大的数
时间复杂度 O(n) def partition(data,left,right): if (len(data)<=0 or left<0 or right>=len(data)): ...
- 19Web服务
Web服务 Web服务 Micosoft.Net平台架构中的分布式系统主要包括两部分:用ASP.Net技术构建服务器端动态网页,以及Web服务(Web Service或XML Web Service) ...
- vue中的组件传值
组件关系可以分为父子组件通信.兄弟组件通信.跨级组件通信. 父传子 - props 子传父 - $emit 跨级可以用bus 父子双向 v-model 父链(this.$parent this.$ch ...
- 关于nested exception is org.apache.ibatis.binding.BindingException:Parameter '***' not found报错解决
几天晚上遇到的奇怪的问题 传入的参数名一直没有变 但是从mapper到xml似乎有一个找不到参数的报错,实际上只要在Mapper接口形参前加“@Param(“形参名称”)”就可以了
- selenium抓取动态网页数据
1.selenium抓取动态网页数据基础介绍 1.1 什么是AJAX AJAX(Asynchronouse JavaScript And XML:异步JavaScript和XML)通过在后台与服务器进 ...