Codeforces 761E Dasha and Puzzle(构造)
题目链接 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(构造)的更多相关文章
- 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
E. Dasha and Puzzle time limit per test:2 seconds memory limit per test:256 megabytes input:standard ...
- Codeforces 761E(DFS)
E. Dasha and Puzzle time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 【codeforces 761E】Dasha and Puzzle
[题目链接]:http://codeforces.com/contest/761/problem/E [题意] 给你一棵树,让你在平面上选定n个坐标; 使得这棵树的连接关系以二维坐标的形式展现出来; ...
- Codeforces Round #394 (Div. 2) E. Dasha and Puzzle(dfs)
http://codeforces.com/contest/761/problem/E 题意:给出一棵树,现在要把这棵树上的结点放置在笛卡尔坐标上,使得每一条边与x轴平行或者与y轴平行.输出可行解,即 ...
- 【AtCoder Grand Contest 012C】Tautonym Puzzle [构造]
Tautonym Puzzle Time Limit: 50 Sec Memory Limit: 256 MB Description 定义一个序列贡献为1,当且仅当这个序列 由两个相同的串拼接而成 ...
- 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 ...
- Codeforces 707C Pythagorean Triples(构造三条边都为整数的直角三角形)
题目链接:http://codeforces.com/contest/707/problem/C 题目大意:给你一条边,问你能否构造一个包含这条边的直角三角形且该直角三角形三条边都为整数,能则输出另外 ...
随机推荐
- 笔记-数据库-redis
笔记-数据库-redis 1. redis简介 Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库.缓存和消息中间件. 它支持多种类型的数据结构,如 stri ...
- Kali 中文家目录改英文目录
中文版Kali装好之后,家目录会中文显示,不便操作 root@kali:~# ls -l drwxr-xr-x root root .0K 7月 : 公共 drwxr-xr-x root root . ...
- as API一些容易忘记的属性和方法
1.在flash动画里的一些动态文本会随着动画的执行,有抖动,解决问题的方法: tt为动画里的动态文本,tt.transform.matrix=null;
- 【Clone Graph】cpp
题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. ...
- 【数据结构与算法】Fibonacci Sequence
学计算机的对 Fibonacci 都并不陌生,在课堂上一讲到递归几乎都会提到 Fibonacci 数列.不久前,我对 Fibonacci 产生了一些兴趣,就在这里把自己的想法给记录下来. 递推公式: ...
- Leetcode 560.和为k的子数组
和为k的子数组 给定一个整数数组和一个整数 k,你需要找到该数组中和为 k 的连续的子数组的个数. 示例 1 : 输入:nums = [1,1,1], k = 2 输出: 2 , [1,1] 与 [1 ...
- Mysql Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
Mysql update error: Error Code: 1175. You are using safe update mode and you tried to update a table ...
- Android简单的BaseExpandableList使用
1.Activity package com.example.administrator.mystudent.ExpandableListView; import android.app.Expand ...
- Chrome DevTools & performance optimization
Chrome DevTools & performance ptimization https://www.bing.com https://developers.google.com/web ...
- i++ 和++i 的理解 以防面试
根本原理: //模拟 a++ function afterAdd(){ var temp = a; a = a+1; return temp; } //模拟++a; function beforeAd ...