codeforce 761E

来源:互联网 发布:淘宝直播申请入口 编辑:程序博客网 时间:2024/05/14 19:57
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.


题解: 给你一棵树,要你在坐标系上把这棵树表示出来,要求是树的边必须跟坐标系平行。

首先若有一个节点度的值超过4明显不可能,然后设深度为0的根节点发出的点的边长为L==5000000000000000(或其他合理的值也可以,反正保证求解过程不能超过10^18),那么深度为1的节点发出的边长为L/2,类似递推下去就不会发生重叠。然后每个点都按照左,上,右,下四个方向进行遍历。遇到不合理的方向即跳过。

AC代码:

#include<stdio.h>#include<iostream>#include<vector>using namespace std;typedef long long int ll;int n, degree[110];ll X[110], Y[110], L[110], F[110];vector<int> g[110];ll x[4]={-1, 0, 1, 0};ll y[4]={0, 1, 0, -1};void dfs(int root, int f){int Size=g[root].size();for(int i=0, k=0; i<Size; i++){int s=g[root][i];if(s==f)continue;if(k==F[root]){k++;}X[s]=X[root]+x[k]*(L[root]);Y[s]=Y[root]+y[k]*(L[root]);F[s]=(k+2)%4;L[s]=L[root]/2;dfs(s, root);k++;}}int main(){int i, j, k, u, v;scanf("%d", &n);for(i=1; i<=n-1; i++){scanf("%d%d", &u, &v);degree[u]++;degree[v]++;g[u].push_back(v);g[v].push_back(u);}for(i=1; i<=n; i++){if(degree[i]>4){printf("NO");return 0;}}L[1]=5000000000000000;X[1]=0;Y[1]=0;F[1]=-1;dfs(1, -1);printf("YES\n");for(i=1; i<=n; i++){cout<<X[i]<<' '<<Y[i]<<'\n' ;}return 0;}



0 0