您的位置:首页 > 其它

E. Dasha and Puzzle----DFS

2017-02-10 16:55 309 查看
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:


题目链接:http://codeforces.com/contest/761/problem/E

题目的意思是说有n个点,n-1条边,问能否在一个平面直接坐标系(笛卡尔坐标系)中构建一棵树。

这个题其实是个纯DFS,我赛时都没有看到这个题...。尴尬TAT-_-|||,先判断能不能构成一棵树,因为边都是和坐标轴平行的,所以说一个点最多链接着4个子节点,如果度超过4,则不可能构成。这个比较好理解,接下来的用代码讲比较好。放上我qscqesze学姐的链接,讲的确实比我好,细节处我会在代码中注释。

http://www.cnblogs.com/qscqesze/p/6359981.html

代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#define pii pair<int ,int>
using namespace std;
vector<int> G[100];
pair<int ,int> ans[50];
int nx[]={0,1,0,-1};
int ny[]={1,0,-1,0};
bool vis[50];
void dfs(int u,int num,int x,int y,int fromdir){
//cout<<"----"<<endl;
vis[u]=true;
ans[u]=(pii){x,y};
int dir=0,v;
for(int i=0;i<(int )G[u].size();i++){
v=G[u][i];
if(vis[v]){
continue;
}
if(dir==fromdir){//方向和父节点不重叠
dir++;
}
dfs(v,num-1,x+(1<<num)*nx[dir],y+(1<<num)*ny[dir],(dir+2)%4);//这里的加2是取反方向,1->2,1在2的左边,2在1的右边
dir++;
}
}
int main(){
int n;
while(~scanf("%d",&n)){
memset(vis,false,sizeof(vis));
for(int i=0;i<n-1;i++){
int x,y;
scanf("%d%d",&x,&y);
G[x].push_back(y);
G[y].push_back(x);
}
bool flag=true;
for(int i=0;i<n;i++){
if(G[i].size()>4){//判度
flag=false;
break;
}
}
if(!flag){
return 0*printf("NO\n");
}
dfs(1,30,0,0,-1);
printf("YES\n");
for(int i=1;i<=n;i++){
cout<<ans[i].first<<" "<<ans[i].second<<endl;
}
}
return 0;
}
网上有大犇说这个题类似于分形,可用分形去构建模型,并不觉得这是个分形。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: