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. Office 佳能MP259打印EXCEL线条歪曲,字迹模糊怎么办

    这大概是墨盒的缘故,判断方法很简单,随便找一个文档,点击打印,属性   在维护选项卡中把清洗和打印头对齐做一遍(一般字迹模糊可以通过清洗解决,线条歪曲可以通过打印头对齐解决),如果你打印的结果是纸张边 ...

  2. ubuntu12.04安装NVIDIA显卡驱动和CUDA

    1.安装显卡驱动 vim /etc/modprobe.d/blacklist.conf #编辑该文件 blacklist nouveau #行末添加,禁用原来的显卡驱动 apt-get install ...

  3. Flex 页面启动事件

    事件启动顺序 容器Preinitialize=>子组件preinitialize=>子组件initialize=>childAdd=>initialize =>子组件cr ...

  4. session 生命周期

    以前看到书上session 的生命周期,知道session的生命周期是在第一次访(即打开浏览器输入地址成功访问)的时候被创建.同时HttpSessionListener接口的sessionCreate ...

  5. Oracle利用游标返回结果集的的例子(C#)...(最爱)

    引用地址:http://www.alixixi.com/program/a/2008050727634.shtml   本例在VS2005+Oracle 92010 + WindowsXp Sp2测试 ...

  6. nestedScrollview 嵌套使用recyclerview判断滑动到底部 (嵌套滑动导致 不能使用recyclerview的onscrolled监听)

    NestedScrollView scroller = (NestedScrollView) findViewById(R.id.myScroll); if (scroller != null) { ...

  7. ImportError: No module named flask.ext.login

    from flask.ext.login import current_user python 3.x中,上面代码会报错:ImportError: No module named flask.ext. ...

  8. 【168】ENVI入门系列

    参考:ENVI-IDL中国的博客 [ENVI入门系列]01.ENVI产品简介与入门 [ENVI入门系列]02.自定义坐标系(北京54.西安80.2000坐标系) [ENVI入门系列]03.基于自带定位 ...

  9. 2008提权之突破系统权限安装shift后门

    大家都知道08权限的系统权限设置很严格,且在2003系统中常用到的溢出工具都失效.面对限制IP连接的情况 我们及时拿到system权限 有账号也上不去 这种情况下只能弄shift后门 或者放大镜了.但 ...

  10. Linux 用户管理(2)

    Linux 用户管理2 添加修改和删除用户,必须是超级管理员root账号才可以进行的操作,所以当当前账号不是超级管理员root账号时,首先要先切换为root账号. 如图,ylq为普通用户,执行添加用户 ...