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 题目大意:给你一条边,问你能否构造一个包含这条边的直角三角形且该直角三角形三条边都为整数,能则输出另外 ...
随机推荐
- jvm探秘之三:GC初步
GC即垃圾收集器,虚拟机的必要组成部分. 不过这里说当然是,hotspot虚拟机(jvm的主要版本)的GC机制,前面说过了jvm的组成部分,那么想当然GC只需要负责方法区和堆就好了,虚拟机栈.本地方法 ...
- Eclipse主题更换方法
1.打开Eclipse的Help->Eclipse Marketplace 2.在Find里搜索Eclipse Color Theme,点击Install按钮 3.打开Window->Pr ...
- python django 路由系统
URL配置 基本格式: from django.conf.urls import url urlpatterns = [ url(正则表达式, views ...
- error LNK2001: unresolved external symbol ___CxxFrameHandler3
Q: VS2005编译的静态库, 在vc++6.0中连接出现错误 error LNK2001: unresolved external symbol ___CxxFrameHandler3 A: ...
- loj2062 [HAOI2016]地图
ref #include <algorithm> #include <iostream> #include <cstdio> #include <cmath& ...
- 设计模式之第6章-迭代器模式(Java实现)
设计模式之第6章-迭代器模式(Java实现) “我已经过时了,就不要讲了吧,现在java自带有迭代器,还有什么好讲的呢?”“虽然已经有了,但是具体细节呢?知道实现机理岂不美哉?”“好吧好吧.”(迭代器 ...
- 【 Sqrt(x) 】cpp
题目: Implement int sqrt(int x). Compute and return the square root of x. 代码: class Solution { public: ...
- C++ map 的用法归纳2
[尊重原著: http://blog.csdn.net/zcf1002797280/article/details/7847819] Map是c++的一个标准容器,它提供了很好一对一的关系,在一些程序 ...
- Strut 2 ValueStack传送带机制
源码与jar包下载(将rar改成jar,直接放在WEB_INF\lib目录中即可) 众所周知,Strut 2的Action类通过属性可以获得所有相关的值,如请求参数.Action配置参数.向其他 ...
- hihoCoder #1783 又一个重复计数
题目大意 给定一个长度为 $n$ 的字符串 $S$,定义函数 $f(S)$ 表示 $S$ 的不同回文子串的个数.对于 $1\le l \le r \le n$,定义 $S[l,r]$ 为字符串 $S$ ...