Codeforces Round #394 (Div. 2) E. Dasha and Puzzle(分形)
2 seconds
256 megabytes
standard input
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.
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:
Codeforces Round #394 (Div. 2) E. 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.
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:
题意:
给你一棵树,让你在二维平面上摆出来,边必须平行坐标轴,且边没有交集
思路:
如果存在某点度数大于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(分形)的更多相关文章
- 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 ...
- 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 ...
- Codeforces Round #394 (Div. 2) E. Dasha and Puzzle(dfs)
http://codeforces.com/contest/761/problem/E 题意:给出一棵树,现在要把这棵树上的结点放置在笛卡尔坐标上,使得每一条边与x轴平行或者与y轴平行.输出可行解,即 ...
- 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 ...
- Codeforces Round #394 (Div. 2) C. Dasha and Password 暴力
C. Dasha and Password 题目连接: http://codeforces.com/contest/761/problem/C Description After overcoming ...
- Codeforces Round #394 (Div. 2) B. Dasha and friends 暴力
B. Dasha and friends 题目连接: http://codeforces.com/contest/761/problem/B Description Running with barr ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- [LG4890]Never·island DP
---题面--- 题解: 感到此题非常的神奇...看了大佬的题解才懂的. 首先建模: 先把所有队伍的出去回来时间都放在一个数组里,然后排序,从左到右扫一边,给每个队伍都建一个带权点,进行如下操作: ( ...
- [ZJOI2005]沼泽鳄鱼 矩阵乘法
---题面--- 题解: 乍一看还是挺懵逼的.和HH去散步很像,思路也是类似的. 复制一段我在HH去散步的题解里面写的一段话吧: 考虑f[i][j]表示i和j是否右边相连,有为1,否则为0,那么f同时 ...
- cf 442 D. Olya and Energy Drinks
cf 442 D. Olya and Energy Drinks(bfs) 题意: 给一张\(n \times m(n <= 1000,m <= 1000)\)的地图 给出一个起点和终点, ...
- [HEOI2015]最短不公共子串
四合一的题. 简单粗暴的方法: 子串匹配——SAM 子序列匹配——序列自动机 关于序列自动机:序列自动机—— [FJOI2016]所有公共子序列问题 (其实这个玩意没有什么,n+1个点,每个点的字符集 ...
- 使用记事本创建Web服务(WebService)
学习就要从最简单最直观的地方入手. 1)打开记事本,添加如下代码: <%@ WebService Language="C#" Class="myFirstWebSe ...
- tomcat发布web项目的三种方式
tomcat发布web项目的三种方式 方式一: 配置tomcat 安装目录下的conf/server.xml <Host name="loaclhost">标签里面添加 ...
- MySQL 8.0.11 中使用 grant ... identified by 时 error 1064 near 'identified by '密码'' at line 1
(1)问题: 当使用 grant 权限列表 on 数据库 to '用户名'@'访问主机' identified by '密码'; 时会出现"......near 'identifie ...
- 第一次做的jsp分页,详细代码。。。。
自己学jsp也有了一段时间,而且自己现在上的课是java web现在雪儿基础做了一个最简单的jsp页面,代码都放在一个页面,自己准备在改进,一步步来,这里的代码可能不是很完美,没事,下面接下来会有大概 ...
- POJ 2456 Aggressive cows---二分搜索法
///3.最大化最小值 /** POJ 2456 Aggressive cows Q:一排牛舍有N (2 <= N <= 100,000) 个,位置为x1,...,xN (0 <= ...
- 【Foreign】异色弧 [树状数组]
异色弧 Time Limit: 20 Sec Memory Limit: 256 MB Description Input Output 仅一行一个整数表示答案. Sample Input 8 1 ...