题目链接 Dasha and Puzzle

对于无解的情况:若存在一个点入度大于4,那么直接判断无解。

从根结点出发(假设根结点的深度为0),

深度为0的节点到深度为1的节点的这些边长度为2^30,

深度为1的节点到深度为2的节点的这些边的长度为2^29,

………………………………………………………………

以此类推。

因为结点个数最多只有30个,所以长度分配足够。

#include <bits/stdc++.h>

using namespace std;

#define REP(i,n)                for(int i(0); i <  (n); ++i)
#define rep(i,a,b) for(int i(a); i <= (b); ++i)
#define LL long long
#define PB push_back const int Q = 1000 + 10;
const int A = 30 + 1;
const LL dx[] = {0, 1, 0, -1};
const LL dy[] = {1, 0, -1, 0}; struct node{ LL x, y;} c[A];
vector <int> v[A];
int f[A][A];
LL num[Q];
int n;
int x, y;
int inn[Q];
bool vis[Q]; void dfs(int x, int cnt){
REP(i, (int)v[x].size()){
int u = v[x][i];
if (!vis[u]){
vis[u] = true;
REP(j, 4){
if (!f[x][j] && !f[u][(j + 2) % 4]){
c[u].x = c[x].x + dx[j] * num[cnt];
c[u].y = c[x].y + dy[j] * num[cnt];
f[u][(j + 2) % 4] = 1;
f[x][j] = 1;
break;
}
}
dfs(u, cnt - 1);
}
}
} int main(){ num[0] = 1; rep(i, 1, 36) num[i] = num[i - 1] * 2;
scanf("%d", &n);
rep(i, 1, n - 1){
scanf("%d%d", &x, &y);
v[x].PB(y);
v[y].PB(x);
++inn[x], ++inn[y];
} rep(i, 1, n) if (inn[i] > 4){
puts("NO");
return 0;
} memset(vis, false, sizeof vis); vis[1] = true;
c[1].x = c[1].y = 0;
dfs(1, 30); puts("YES");
rep(i, 1, n){
printf("%lld %lld\n", c[i].x, c[i].y);
} return 0; }

Codeforces 761E Dasha and Puzzle(构造)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. Codeforces 761E(DFS)

    E. Dasha and Puzzle time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  5. 【codeforces 761E】Dasha and Puzzle

    [题目链接]:http://codeforces.com/contest/761/problem/E [题意] 给你一棵树,让你在平面上选定n个坐标; 使得这棵树的连接关系以二维坐标的形式展现出来; ...

  6. Codeforces Round #394 (Div. 2) E. Dasha and Puzzle(dfs)

    http://codeforces.com/contest/761/problem/E 题意:给出一棵树,现在要把这棵树上的结点放置在笛卡尔坐标上,使得每一条边与x轴平行或者与y轴平行.输出可行解,即 ...

  7. 【AtCoder Grand Contest 012C】Tautonym Puzzle [构造]

    Tautonym Puzzle Time Limit: 50 Sec  Memory Limit: 256 MB Description 定义一个序列贡献为1,当且仅当这个序列 由两个相同的串拼接而成 ...

  8. Educational Codeforces Round 10 B. z-sort 构造

    B. z-sort 题目连接: http://www.codeforces.com/contest/652/problem/B Description A student of z-school fo ...

  9. Codeforces 707C Pythagorean Triples(构造三条边都为整数的直角三角形)

    题目链接:http://codeforces.com/contest/707/problem/C 题目大意:给你一条边,问你能否构造一个包含这条边的直角三角形且该直角三角形三条边都为整数,能则输出另外 ...

随机推荐

  1. Birthday Paradox

    Birthday Paradox Sometimes some mathematical results are hard to believe. One of the common problems ...

  2. 【File】文件操作(初识文件操作一)

    一,初识文件流 看到标题就知道接下来的所有操作对象都是面对文件进行的.那么问题来了.在java中目录是不是也属于文件呢?答案是yes.既然目录也属于文件,那么对于目录跟文件的区分就显现出来了.在接下来 ...

  3. 4 Template层-验证码

    1.验证码 在用户注册.登录页面,为了防止暴力请求,可以加入验证码功能,如果验证码错误,则不需要继续处理,可以减轻一些服务器的压力 使用验证码也是一种有效的防止crsf的方法 验证码效果如下图: 官网 ...

  4. 谋哥:转型之痒与App推广之痛

    昨天<重庆今日教育>的副主编汪熙坤老师先加我微信,谋哥的微信每天有几十个不同领域的朋友加.几句客套后,他马上就直奔主题了.为什么这么着急呢?是因为危机感,是因为感受到了互联网给传统纸媒带来 ...

  5. fastjosn在低版本丢字段问题

    简单的说: 对于java bean中有字段类似pId这种写法,特征是第一个字母小写,第二个字母大写,在eclipse中生成的getter setter方法是 getpId, setpId. 在低版本的 ...

  6. js的trim方法

    ie9以前版本,不支持string.trim()方法 所以需要自己实现. <script type="text/javascript"> String.prototyp ...

  7. diea

    http://name.vip.int ellig.top/name

  8. 【bzoj4836】[Lydsy2017年4月月赛]二元运算 分治+FFT

    题目描述 定义二元运算 opt 满足   现在给定一个长为 n 的数列 a 和一个长为 m 的数列 b ,接下来有 q 次询问.每次询问给定一个数字 c  你需要求出有多少对 (i, j) 使得 a_ ...

  9. 采花 flower

    采花 flower 题目描述 萧芸斓是 Z 国的公主,平时的一大爱好是采花. 今天天气晴朗,阳光明媚,公主清晨便去了皇宫中新建的花园采花.花园足够大,容纳 了 n 朵花,花有 c 种颜色(用整数 1- ...

  10. Codeforces Round #304 (Div. 2) A B C 水

    A. Soldier and Bananas time limit per test 1 second memory limit per test 256 megabytes input standa ...