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. C# 如何修改Form不能修改窗体大小

    把窗体的FormBorderSytle改一下就可以了,改成FixedSingle或者Fixed3D都可以        

  2. ios网络学习------11 原生API文件上传之断点续传思路

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaHVhbmcyMDA5MzAzNTEz/font/5a6L5L2T/fontsize/400/fill/I0 ...

  3. 工作总结 2018 - 4 - 13 select标签 multiple 属性 同时选择多个选项

    <div class="col-xs-4"> @Html.DropDownList("CustomerType", (MultiSelectList ...

  4. hdu, KMP algorithm, linear string search algorithm, a nice reference provided

    reference: Rabin-Karp and Knuth-Morris-Pratt Algorithms By TheLlama– TopCoder Member https://www.top ...

  5. 【Struts2五】ValueStack以及ognl表达式二(经常使用标签)

    Ognl经常使用标签:   1.s:debug       假设把该标签放入到s:iterator中能够看到当前正在迭代的元素的状态    2.s:property       1.输出       ...

  6. 在MAC端查看win7

    在MAC端查看win7,在finder中打开网络,输入win7地址,填入用户名和密码,就可以了

  7. POJ2187 Beauty Contest (旋转卡壳算法 求直径)

    POJ2187 旋转卡壳算法如图 证明:对于直径AB 必然有某一时刻 A和B同时被卡住 所以旋转卡壳卡住的点集中必然存在直径 而卡壳过程显然是O(n)的 故可在O(n)时间内求出直径 凸包具有良好的性 ...

  8. 3-1 todolist功能开发

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. 8 种提升ASP.NET Web API性能的方法

    ASP.NET Web API 是非常棒的技术.编写 Web API 十分容易,以致于很多开发者没有在应用程序结构设计上花时间来获得很好的执行性能. 在本文中,我将介绍8项提高 ASP.NET Web ...

  10. bzoj 1828: [Usaco2010 Mar]balloc 农场分配【贪心+线段树】

    长得挺唬人的贪心,按照右端点排序,用最小值线段树的询问判断当前牛是否能放进去,能的话更新线段树,ans++ 来自https://www.cnblogs.com/rausen/p/4529245.htm ...