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 题目大意:给你一条边,问你能否构造一个包含这条边的直角三角形且该直角三角形三条边都为整数,能则输出另外 ...
随机推荐
- 大数模板Java
import java.util.*; import java.math.BigInteger; public class Main{ public static void main(String a ...
- 笔记-python-selenium,phantomjs
笔记-python-selenium,phantomjs 1. 简介 1.1. selenium selenium是一款自动化测试工具,支持多种语言 为什么爬虫要使用selenium呢 ...
- DiyCode开源项目 AboutActivity分析
1.首先看一下效果 这是手机上显示的效果: 1.1首先是一个标题栏,左侧一个左箭头,然后一个图标. 1.2然后下方是一个可以滑动的页面. 1.3分成了7个部分. 1.4DiyCode的图标. 1.5然 ...
- 用go和zk实现一个简单的分布式server
golang的zk客户端 最近打算写个简单的配置中心,考虑到实现便捷性,语言选择了go,由于其中计划用到zk,就调研了下golang的zk客户端,并实现了个简单的分布式server.最终找到了两个,地 ...
- Pre 自动换行和手动换行
pre { white-space: pre-wrap; white-space: -moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre ...
- IOS开发---菜鸟学习之路--(二十四)-iOS7View被导航栏遮挡问题的解决
(此文为复制帖,原文地址为:http://blog.sina.com.cn/s/blog_a8192bdd0101af40.html) self.navigationController.naviga ...
- linux环境搭建系列之maven
前提: jdk1.7 Linux centOS 64位 安装包从官网获取地址:http://maven.apache.org/download.cgi Jdk1.7对应apache-maven-3.3 ...
- git基础之常用操作
一.版本提交: (1)git add 文件名 (2)git commit -m "版本提交信息" 注:git分两个区:工作区+版本库 在电脑中看到的文件夹就是工作区 有一个隐藏的. ...
- URIs, URLs, and URN
首先,URI,是uniform resource identifier,统一资源标识符,用来唯一的标识一个资源.而URL是uniform resource locator,统一资源定位器,它是一种具体 ...
- linux基础(基本命令)
Linux学习 1.Linux安装.配置 Linux的操作背景介绍 Linux操作系统 开源.自由且开发源代码的类Unix操作系统 厂商较多 著名的有R ...