题目地址:点击打开链接
题意:给你一棵树,问你能不能放在二维坐标系中,边必须平行坐标轴,且边没有交集。
思路:什么情况不能放呢,很容易可以想到一个点他有大于四条边时肯定是放不了了。那如果所有点的边数小于等于
4,怎么进行放呢,为了不让边相交,我们可以将边按点的遍历降序放,比如第一个点的边放的边长len,第二个点的
为len/2,那么即使在同一水平线或垂直线的两个点,不断向对方方向放边,永远也不可能碰到一起,因为
len<len/2+len/2/2+len/2/2/2+....
要注意的是,边放的顺序要注意下,不能跟这条边所在点的来的方向反向。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 35;
vector<int> g[maxn];
int n, du[maxn], X[maxn], Y[maxn];
bool vis[maxn];
void dfs(int u, int x, int y, int len, int dir)
{
int next[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};
dir = (dir+3)%4;
for(int i = 0; i < g[u].size(); i++)
{
int v = g[u][i];
if(!vis[v])
{
vis[v] = 1;
int tx = x+next[dir][0]*len;
int ty = y+next[dir][1]*len;
X[v] = tx, Y[v] = ty;
dfs(v, tx, ty, len/2, dir);
dir = (dir+1)%4;
}
}
}
int main(void)
{
while(cin >> n)
{
memset(du, 0, sizeof(du));
memset(vis, 0, sizeof(vis));
for(int i = 0; i < maxn; i++)
g[i].size();
bool ok = 1;
for(int i = 1; i < n; i++)
{
int u, v;
scanf("%d%d", &u, &v);
g[u].push_back(v);
g[v].push_back(u);
du[u]++, du[v]++;
if(du[u] > 4 || du[v] > 4) ok = 0;
}
if(!ok) puts("NO");
else
{
X[1] = Y[1] = 0;
vis[1] = 1;
dfs(1, 0, 0, 1<<30, 0);
puts("YES");
for(int i = 1; i <= n; i++)
printf("%d %d\n", X[i], Y[i]);
}
}
return 0;
}
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.
![](https://i-blog.csdnimg.cn/blog_migrate/fc83ac8c2830b8c877d9c4957f62c9f7.png)
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.
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.
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.
7 1 2 1 3 2 4 2 5 3 6 3 7
YES 0 0 1 0 0 1 2 0 1 -1 -1 1 0 2
6 1 2 2 3 2 4 2 5 2 6
NO
4 1 2 2 3 3 4
YES 3 3 4 3 5 3 6 3
In the first sample one of the possible positions of tree is: