题目链接 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. poj 2533Longest Ordered Subsequence

    Longest Ordered Subsequence Description A numeric sequence of ai is ordered if a1 < a2 < - < ...

  2. BZOJ 3420: Poi2013 Triumphal arch

    二分答案 第二个人不会走回头路 那么F[i]表示在i的子树内(不包括i)所需要的额外步数 F[1]==0表示mid可行 k可能为0 #include<cstdio> #include< ...

  3. js 监听后退事件及跳转页面

    //直接跳转 window.location.href="b.html"; //返回上一级页面 window.history.back(-1); //返回下一级页面 window. ...

  4. day38--MySQL基础二

    1.数据库连表 1.1, 一对多 使用外键做约束.注意:外键列的数据类型要一致. 命令的方式创建外键CREATE table part1( nid int not null auto_incremen ...

  5. [转]9个基于Java的搜索引擎框架

    9个基于Java的搜索引擎框架 在这个信息相当繁杂的互联网时代,我们已经学会了如何利用搜索引擎这个强大的利器来找寻目标信息,比如你会在Google上搜索情人节如何讨女朋友欢心,你也会在百度上寻找正规的 ...

  6. ogre3D学习基础1 -- 核心对象与脚本技术

    一.核心对象介绍1.命名空间 Ogre3d使用了C++的特性--命名空间,可以防止命名混淆.使用方法也简单,using namespace Ogre;或者直接在使用时加上“Ogre::”的前缀,如Og ...

  7. [python][oldboy][函数篇][1]名称空间

    名称空间:存储名字的空间,分为三种,内置空间,全局空间,局部空间 名称可以是:变量名,函数名,类名等 当遇到一个名字时,首先在自己空间找,再到自己外的空间找 比如 test.py print f # ...

  8. java读取文件(更新jdk7及jdk8)

    以字节的方式读取: InputStream inputStream = new FileInputStream(file); int temp = -1; StringBuilder sb = new ...

  9. AsyncSocket

    如果需要在项目中像QQ微信一样做到即时通讯,必须使用socket通讯,本人也是刚学习,分享一下,有什么不对的地方希望大家指正 ios原生的socket用起来不是很直观,所以我用的是AsyncSocke ...

  10. 做一个APP

    前言 有点零乱,但是我想写下来慢慢整理,搭建一个好点的工程-模式MVC, 会包括一些第三方库,动画库,第三方库管理关联,自定义常用控件的管理和关联 1.预编译文件的创建 在build setting ...