codeforce 761E - Dasha and Puzzle(思维)

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值