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 n vertices.

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: 

直接向4个方向模拟按长度从大到小DFS即可

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<queue>
using namespace std;
int n;
vector<int> a[];
long long ansx[],ansy[];
long long p[];
const int d1[]={,,,-};
const int d2[]={,,-,};
void dfs(int x,int fa,int d,int cnt)
{
vector<int>::iterator iter;
for (iter=a[x].begin();iter!=a[x].end();iter++)
{
int y=*iter;
if (y==fa) continue;
ansx[y]=ansx[x]+d1[(d+)%]*p[cnt];
ansy[y]=ansy[x]+d2[(d+)%]*p[cnt];
dfs(y,x,(d+)%,cnt-);
d++;
}
}
int main()
{
scanf("%d",&n);
int i;
p[]=1ll;
for (i=;i<=;i++) p[i]=p[i-]<<;
for (i=;i<n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
a[x].push_back(y);
a[y].push_back(x);
}
for (i=;i<=n;i++)
if (a[i].size()>)
{
puts("NO");
return ;
}
dfs(,,,);
puts("YES");
for (i=;i<=n;i++)
printf("%lld %lld\n",ansx[i],ansy[i]);
return ;
}

Codeforces 761E(DFS)的更多相关文章

  1. codeforces 731C(DFS)

    题目链接:http://codeforces.com/contest/731/problem/C 题意:有n只袜子(1~n),k种颜色(1~k),在m天中,左脚穿下标为l,右脚穿下标为r的袜子,问最少 ...

  2. CodeForces - 95B(DFS)

    题目链接:http://codeforces.com/problemset/problem/95/B 题目大意:给你一个正整数n (1 ≤ n ≤ 10100000),求不大小于它的超级幸运数字(超级 ...

  3. CodeForces - 589J —(DFS)

    Masha has recently bought a cleaner robot, it can clean a floor without anybody's assistance. Schema ...

  4. codeforces 723D(DFS)

    题目链接:http://codeforces.com/problemset/problem/723/D 题意:n*m的矩阵中,'*'代表陆地,'.'代表水,连在一起且不沿海的水形成湖泊.问最少填多少块 ...

  5. CodeForces - 589J(DFS)

    题目链接:http://codeforces.com/problemset/problem/589/J 题目大意:一个机器人打扫一个密闭的房间,房间由一个矩形构成,'*'表示家具,'.'表示该位置为空 ...

  6. Codeforces 115A- Party(DFS)

    A. Party time limit per test 3 seconds memory limit per test 256 megabytes input standard input outp ...

  7. LeetCode Subsets II (DFS)

    题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...

  8. LeetCode Subsets (DFS)

    题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. class Sol ...

  9. HDU 2553 N皇后问题(dfs)

    N皇后问题 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description 在 ...

随机推荐

  1. LN : leetcode 413 Arithmetic Slices

    lc 413 Arithmetic Slices 413 Arithmetic Slices A sequence of number is called arithmetic if it consi ...

  2. 【转】Android Activity/Fragment Lifecycle

    原文来自:http://stormzhang.github.io/android/2014/08/08/activity-fragment-lifecycle/ 说Activity和Fragment是 ...

  3. rem手机端页面自适应布局(待修正下一篇完美布局)

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

  4. 免费公共DNS服务器IP地址大全(2017年6月24日)

    收集全球各个常用公共DNS服务器 IP地址,欢迎各位朋友评论补充! 国内常用公共DNS 114 DNS: (114.114.114.114:    114.114.115.115) 114DNS安全版 ...

  5. sosoapi的安装

    sosoapi简介及其用户手册:http://www.sosoapi.com/pass/help/manual.htm 该随笔的大概分为: 1.sosoapi的基础安装  2.sosoapi使用域名访 ...

  6. 使用python划分数据集

    无论是训练机器学习或是深度学习,第一步当然是先划分数据集啦,今天小白整理了一些划分数据集的方法,希望大佬们多多指教啊,嘻嘻~ 首先看一下数据集的样子,flower_data文件夹下有四个文件夹,每个文 ...

  7. bat2exe 就是这么简单 白研究半天VC++了

    bat2exe 就是这么简单 白研究半天VC++了 结果:bat2exe编译的执行文件会被杀毒软件查杀.

  8. PHP 下基于 php-amqp 扩展的 RabbitMQ 简单用例 (一) -- 安装 AMQP 扩展和 Direct Exchange 模式

    Windows 安装 amqp 扩展 RabbitMQ 是基于 amqp(高级消息队列协议) 协议的.使用 RabbitMQ 前必须为 PHP 安装相应的 amqp 扩展. 下载相应版本的 amqp ...

  9. Oracle 学习之:ASCII,CHR函数的作用和用法

    对于ASCII以及CHR函数的用法,Oracle给出的解释是: ASCII(x)gets the ASCII value of the character X, CHR() and ASCII() h ...

  10. JavaScript exec()方法

    exec() 方法用于检索字符串中的正则表达式的匹配.返回一个数组,其中存放匹配的结果.如果未找到匹配,则返回值为 null. var str = "我今年25岁明年26岁后年27岁千年24 ...