Codeforces_761_E_(dfs)
2 seconds
256 megabytes
standard input
standard output
Dasha decided to have a rest after solving the problem. She had been ready to start her favourite activity — origami, but remembered the puzzle that she could not solve.
The tree is a non-oriented connected graph without cycles. In particular, there always are n - 1 edges in a tree with nvertices.
The puzzle is to position the vertices at the points of the Cartesian plane with integral coordinates, so that the segments between the vertices connected by edges are parallel to the coordinate axes. Also, the intersection of segments is allowed only at their ends. Distinct vertices should be placed at different points.
Help Dasha to find any suitable way to position the tree vertices on the plane.
It is guaranteed that if it is possible to position the tree vertices on the plane without violating the condition which is given above, then you can do it by using points with integral coordinates which don't exceed 1018 in absolute value.
The first line contains single integer n (1 ≤ n ≤ 30) — the number of vertices in the tree.
Each of next n - 1 lines contains two integers ui, vi (1 ≤ ui, vi ≤ n) that mean that the i-th edge of the tree connects vertices ui and vi.
It is guaranteed that the described graph is a tree.
If the puzzle doesn't have a solution then in the only line print "NO".
Otherwise, the first line should contain "YES". The next n lines should contain the pair of integers xi, yi(|xi|, |yi| ≤ 1018) — the coordinates of the point which corresponds to the i-th vertex of the tree.
If there are several solutions, print any of them.
7
1 2
1 3
2 4
2 5
3 6
3 7
YES
0 0
1 0
0 1
2 0
1 -1
-1 1
0 2
6
1 2
2 3
2 4
2 5
2 6
NO
4
1 2
2 3
3 4
YES
3 3
4 3
5 3
6 3
In the first sample one of the possible positions of tree is:
思路:边的长度取2的幂,由大减小,每次除以2(不能从1开始每次乘2,在这儿卡了好久,其实仔细一想,很容易想到如果从小到大会出现交叉)。然后就是简单dfs。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector> using namespace std;
#define LL long long struct Node
{
LL x, y;
Node() {}
Node(LL a,LL b)
{
x=a;
y=b;
} }; vector<int> eage[];
int dir[][]= {-,,,,,,,-}; int deg[];
int vis[];
int n;
LL len=;
Node coor[]; void dfs(int p,int d)
{
int di=;
for(int i=; i<eage[p].size(); i++)
{
if(vis[eage[p][i]]==)
{
if(di==d)
di=(di+)%;
vis[eage[p][i]]=;
coor[eage[p][i]].x=coor[p].x+dir[di][]*len;
coor[eage[p][i]].y=coor[p].y+dir[di][]*len;
len=len*;
//cout<<eage[p][i]<<' '<<coor[eage[p][i]].x<<' '<<coor[eage[p][i]].y<<endl;
dfs(eage[p][i],(di+)%);
di=(di+)%;
}
}
} int main()
{ scanf("%d",&n);
for(int i=; i<n-; i++)
{
int a,b;
scanf("%d%d",&a,&b);
eage[a].push_back(b);
eage[b].push_back(a);
deg[a]++;
deg[b]++; }
for(int i=;i<=n;i++)
if(deg[i]>)
{
printf("NO\n");
return ;
}
vis[]=;
dfs(,-);
printf("YES\n");
for(int i=; i<=n; i++)
//cout<<coor[i].x<<" "<<coor[i].y<<endl;
printf("%I64d %I64d\n",coor[i].x,coor[i].y);
return ;
}
Codeforces_761_E_(dfs)的更多相关文章
- BZOJ 3083: 遥远的国度 [树链剖分 DFS序 LCA]
3083: 遥远的国度 Time Limit: 10 Sec Memory Limit: 1280 MBSubmit: 3127 Solved: 795[Submit][Status][Discu ...
- BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]
1103: [POI2007]大都市meg Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2221 Solved: 1179[Submit][Sta ...
- BZOJ 4196: [Noi2015]软件包管理器 [树链剖分 DFS序]
4196: [Noi2015]软件包管理器 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1352 Solved: 780[Submit][Stat ...
- 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)
图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...
- BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]
2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 2545 Solved: 1419[Submit][Sta ...
- POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)
来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS Memory Limit: 65536 ...
- 深度优先搜索(DFS)
[算法入门] 郭志伟@SYSU:raphealguo(at)qq.com 2012/05/12 1.前言 深度优先搜索(缩写DFS)有点类似广度优先搜索,也是对一个连通图进行遍历的算法.它的思想是从一 ...
- 【BZOJ-3779】重组病毒 LinkCutTree + 线段树 + DFS序
3779: 重组病毒 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 224 Solved: 95[Submit][Status][Discuss] ...
- 【BZOJ-1146】网络管理Network DFS序 + 带修主席树
1146: [CTSC2008]网络管理Network Time Limit: 50 Sec Memory Limit: 162 MBSubmit: 3495 Solved: 1032[Submi ...
随机推荐
- 改动Centosserver主机名称
1.暂时改动server主机名称: hostname myhost. myhost为你指定的主机名称. 2.永久性的改动主机名称 Centosserver安装好之后.默认的主机名为:localhost ...
- C#高级编程五十二天----有序列表
有序列表 假设须要基于对全部集合排序,就能够使用SortedList<TKey,TValue>类.这个类依照键给元素排序.这个集合中的值和键都能够使用随意类型. 以下的样例创建了一个有序列 ...
- Eclipse插件开发中的选择监听机制(Selection Provider-Listener)
Eclipse插件开发中的选择监听机制(Selection Provider-Listener) 监听机制是eclipse插件开发或rcp应用开发中经常使用的技术,比方点击TableViewer或Tr ...
- CSS总结01
1 CSS 的作用是? 2 如何引入 CSS 样式? 3 CSS 选择器的基本类型和复合选择器分别是? 4 字体.背景.列表和链接和鼠标的属性有哪些? 5 如何理解盒子模型? 6 浮动的方式有哪些,如 ...
- 迟到的WC2019打铁祭
这是我最失败的一次考试... 具体过程就不说了,全程划水,掉线.还是自身实力不行啊. 最后文艺汇演,本人是DL24主唱&&rapper,欢迎大家交友.^_^.
- [POI2012]FES-Festival
https://www.zybuluo.com/ysner/note/1252538 题面 有一个数列\(\{a\}\).现给定多组限制,限制分成\(2\)类,第一类是\(a_x+1=a_y\),有\ ...
- MSP430:实时时钟-DS1302
/* * DS1302.h * * Created on: 2013-11-27 * Author: Allen */ #ifndef DS1302_H_ #define DS1302_H_ #inc ...
- System.Drawing.Color的几种使用方法
System.Drawing.Color cl = Color.Red; System.Drawing.Color cl = Color.FromArgb(255,0,0); ...
- [Usaco2011 Jan]道路和航线
Description Farmer John正在一个新的销售区域对他的牛奶销售方案进行调查.他想把牛奶送到T个城镇 (1 <= T <= 25,000),编号为1T.这些城镇之间通过R条 ...
- hdu1166 敌兵布阵(树状数组)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...