E. Dasha and Puzzle
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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 uivi (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.

Output

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 xiyi(|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.

Examples
input
7
1 2
1 3
2 4
2 5
3 6
3 7
output
YES
0 0
1 0
0 1
2 0
1 -1
-1 1
0 2
input
6
1 2
2 3
2 4
2 5
2 6
output
NO
input
4
1 2
2 3
3 4
output
YES
3 3
4 3
5 3
6 3
Note

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)的更多相关文章

  1. BZOJ 3083: 遥远的国度 [树链剖分 DFS序 LCA]

    3083: 遥远的国度 Time Limit: 10 Sec  Memory Limit: 1280 MBSubmit: 3127  Solved: 795[Submit][Status][Discu ...

  2. BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2221  Solved: 1179[Submit][Sta ...

  3. BZOJ 4196: [Noi2015]软件包管理器 [树链剖分 DFS序]

    4196: [Noi2015]软件包管理器 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1352  Solved: 780[Submit][Stat ...

  4. 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)

    图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...

  5. BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]

    2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 2545  Solved: 1419[Submit][Sta ...

  6. POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)

    来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS   Memory Limit: 65536 ...

  7. 深度优先搜索(DFS)

    [算法入门] 郭志伟@SYSU:raphealguo(at)qq.com 2012/05/12 1.前言 深度优先搜索(缩写DFS)有点类似广度优先搜索,也是对一个连通图进行遍历的算法.它的思想是从一 ...

  8. 【BZOJ-3779】重组病毒 LinkCutTree + 线段树 + DFS序

    3779: 重组病毒 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 224  Solved: 95[Submit][Status][Discuss] ...

  9. 【BZOJ-1146】网络管理Network DFS序 + 带修主席树

    1146: [CTSC2008]网络管理Network Time Limit: 50 Sec  Memory Limit: 162 MBSubmit: 3495  Solved: 1032[Submi ...

随机推荐

  1. AutoCAD如何编辑块,打散块

    选中块之后,点击最右侧的最后一个工具"分解"即可.  

  2. redis 主从 及集群

    一.redis 主从架构 搭建redis 主从   (可以用一台主机,也可以两台主机) 环境准备: 一台服务器:192.168.206.6 操作系统:CentOS7.5 redis 版本: redis ...

  3. MySQL-数据表锁定

    MySQL允许客户端会话明确获取表锁,以防止其他会话在特定时间段内访问表.客户端会话只能为自己获取或释放表锁.它不能获取或释放其他会话的表锁. 创建一个数据表: CREATE DATABASE IF ...

  4. JavaScript基础 -- ECMAscript

    ECMAScript是一种由Ecma国际(前身为欧洲计算机制造商协会,英文名称是European Computer Manufacturers Association)通过ECMA-262标准化的脚本 ...

  5. Spring Boot Controller

    接上篇文章.HelloWorld程序中我们已经创建了一个HellController,里面包括了响应JSON的方法.本文针对Controller再做一下解说. 回想上篇文章,我们在Controller ...

  6. YTU 2535: C++复数运算符重载(+与<<)

    2535: C++复数运算符重载(+与<<) 时间限制: 1 Sec  内存限制: 128 MB 提交: 867  解决: 532 题目描述 定义一个复数类Complex,重载运算符&qu ...

  7. AT2004 Anticube

    https://www.zybuluo.com/ysner/note/1304774 题面 给定\(n\)个数\(s_i\),要求从中选出最多的数,满足任意两个数之积都不是完全立方数. \(n\leq ...

  8. bzoj 1615: [Usaco2008 Mar]The Loathesome Hay Baler麻烦的干草打包机【bfs】

    直接bfs即可,注意开double,还有驱动和终点的齿轮都在序列里,要把它们找出来= = #include<iostream> #include<cstdio> #includ ...

  9. 1642: [Usaco2007 Nov]Milking Time 挤奶时间(dp)

    1642: [Usaco2007 Nov]Milking Time 挤奶时间 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 935  Solved: 55 ...

  10. P4727 [HNOI2009]图的同构记数

    传送门 如果我们把选出子图看成选出边,进而看成对边黑白染色,那么就是上一题的弱化版了,直接复制过来然后令\(m=2\)即可 不过直接交上去会T,于是加了几发大力优化 不知为何华丽的被小号抢了rank2 ...