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

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

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:

Codeforces Round #394 (Div. 2) E. Dasha and Puzzle

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肯定不行。

然后,第一层让边长为len,第二层边长为len/2,第三层边长为len/4……

这样弄下去就好了,这样就保证每一层都不会碰到上一层了。即缩小距离的方法。。(摘)

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <stack>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
using namespace std;
typedef long long ll;
const int N = ;
const int M = 1e5+;
int n,m,k;
int in[N];
int d[][]={{,},{,},{-,},{,-}};
vector<int>edg[N];
ll x[N],y[N];
void dfs(int u,int fa,ll xx,ll yy,ll dis,int dir){
int j=;
x[u]=xx;y[u]=yy;
for(int i=;i<edg[u].size();i++){
int v=edg[u][i];
if(v==fa)continue;
if(j+dir==)j++;
dfs(v,u,xx+d[j][]*dis,yy+d[j][]*dis,dis/,j);
j++;
}
}
int main(){
int u,v;
bool ok=true;
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d%d",&u,&v);
edg[u].pb(v);
edg[v].pb(u);
in[v]++;in[u]++;
}
for(int i=;i<=n;i++){
if(in[i]>){
ok=false;
break;
}
}
if(!ok)puts("NO");
else {
puts("YES");
dfs(,,,,<<,-);
for(int i=;i<=n;i++){
printf("%lld %lld\n",x[i],y[i]);
}
}
return ;
}

Codeforces Round #394 (Div. 2) E. Dasha and Puzzle(分形)的更多相关文章

  1. Codeforces Round #394 (Div. 2) E. Dasha and Puzzle 构造

    E. Dasha and Puzzle 题目连接: http://codeforces.com/contest/761/problem/E Description Dasha decided to h ...

  2. Codeforces Round #394 (Div. 2) E. Dasha and Puzzle

    E. Dasha and Puzzle time limit per test:2 seconds memory limit per test:256 megabytes input:standard ...

  3. Codeforces Round #394 (Div. 2) E. Dasha and Puzzle(dfs)

    http://codeforces.com/contest/761/problem/E 题意:给出一棵树,现在要把这棵树上的结点放置在笛卡尔坐标上,使得每一条边与x轴平行或者与y轴平行.输出可行解,即 ...

  4. Codeforces Round #394 (Div. 2) D. Dasha and Very Difficult Problem 贪心

    D. Dasha and Very Difficult Problem 题目连接: http://codeforces.com/contest/761/problem/D Description Da ...

  5. Codeforces Round #394 (Div. 2) C. Dasha and Password 暴力

    C. Dasha and Password 题目连接: http://codeforces.com/contest/761/problem/C Description After overcoming ...

  6. Codeforces Round #394 (Div. 2) B. Dasha and friends 暴力

    B. Dasha and friends 题目连接: http://codeforces.com/contest/761/problem/B Description Running with barr ...

  7. Codeforces Round #394 (Div. 2) A. Dasha and Stairs 水题

    A. Dasha and Stairs 题目连接: http://codeforces.com/contest/761/problem/A Description On her way to prog ...

  8. Codeforces Round #394 (Div. 2) D. Dasha and Very Difficult Problem —— 贪心

    题目链接:http://codeforces.com/contest/761/problem/D D. Dasha and Very Difficult Problem time limit per ...

  9. Codeforces Round #394 (Div. 2) C. Dasha and Password —— 枚举

    题目链接:http://codeforces.com/problemset/problem/761/C C. Dasha and Password time limit per test 2 seco ...

随机推荐

  1. [UOJ #52]【UR #4】元旦激光炮

    题目大意:交互题,给你三个有序数组,长度分别为$n\_a,n\_b,n\_c$,都不超过$10^5$.三个函数$get\_a(i),get\_b(i),get\_c(i)$,分别返回$a_i,b_i, ...

  2. cdq分治入门学习 cogs 1752 Mokia nwerc 2015-2016 G 二维偏序

    /* CDQ分治的对象是时间. 即对于一个时间段[L, R],我们取mid = (L + R) / 2. 分治的每层只考虑mid之前的修改对mid之后的查询的贡献,然后递归到[L,mid],(mid, ...

  3. [学习笔记]LCT进阶操作

    LCT总结——应用篇(附题单)(LCT) 一般都是维护链的操作.split即可搞定. 进阶操作的话,处理好辅助树和原树的关系即可搞定. 其实,最大的区别就是,splay随便转,辅助树形态变了,但是原树 ...

  4. TYVJ 1035 / codevs 2171 棋盘覆盖

    Problem Description 给定一个n * m的棋盘,已知某些各自禁止放置,求最多往棋盘上放多少长度为2宽度为1的骨牌(骨牌不重叠) Input 第一行为n,m(表示有m个删除的格子)第二 ...

  5. 【BZOJ 1146】[CTSC2008]网络管理Network

    树剖+树状数组套线段树O(nlogn^3)(我打的),有一种更加优秀的算法是O(nlogn^2)的就是直接树状数组套线段树欧拉序(并不快),或者是用主席树维护原始的树的信息,同时用树状数组套线段树维护 ...

  6. 微信小程序使用原生WebSokcet实现断线重连及数据拼接

    以前做小程序为了应急找了个插件去链接WebSokcet,文章传送门. 回过头在新项目中再次使用时出现了些许问题,不一一赘述.遂决定好好用一下原生的WebSokcet. 一.说明 1.小程序原生的Web ...

  7. [hihocoder 1050]求树的最长链

    题目链接:http://hihocoder.com/problemset/problem/1050 两种方法: 1. 两遍dfs,第一次随便找一个根,找到距离这个根最远的点,这个点必然是最长链的一端. ...

  8. 浅析 nth-child(n) 和 nth-of-type(n)

    首先看一个例子 <div> <p>第一个段落</p> <p>第二个段落</p> </div> p:nth-child(2) { ...

  9. 2、Distributed Optimization

    一.目录: Distributed dynamic programming (as applied to path-planning problems). Distributed solutions ...

  10. HDU2571--命运---DP

    http://acm.hdu.edu.cn/showproblem.php?pid=2571 #include "iostream" #include "cstdio&q ...