Codeforces Round #394 (Div. 2) E. Dasha and Puzzle(dfs)
http://codeforces.com/contest/761/problem/E
题意:
给出一棵树,现在要把这棵树上的结点放置在笛卡尔坐标上,使得每一条边与x轴平行或者与y轴平行。输出可行解,即每个点放置的坐标。
思路:
一开始我想,这个每条边的边长都是可长可短的,好像很麻烦啊。
做法还是很巧妙的,因为最多也就30个点,所以我们直接让该点连接的边未len长度,然后dfs它的子节点并使它的子节点的边长为len/2。你会发现,这样线段就不会相交了。
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cstdio>
using namespace std; const int maxn=+; int n;
vector<int> g[maxn];
int vis[maxn];
long long ans_x[maxn],ans_y[maxn]; int dx[]={,-,,};
int dy[]={-,,,}; void dfs(int u,int fa_dir,long long dir)
{
vis[u]=;
int k=;
for(int i=;i<g[u].size();i++)
{
int v=g[u][i];
if(!vis[v])
{
if(k==-fa_dir) k++;
ans_x[v]=ans_x[u]+dir*dx[k];
ans_y[v]=ans_y[u]+dir*dy[k];
dfs(v,k,dir/);
k++;
}
}
} int main()
{
while(~scanf("%d",&n))
{
bool flag=true;
for(int i=;i<=n;i++) g[i].clear();
for(int i=;i<n-;i++)
{
int u,v;
scanf("%d%d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
if(g[u].size()> || g[v].size()>) flag=false;
}
if(!flag) {puts("NO");continue;}
memset(vis,,sizeof(vis));
ans_x[]=; ans_y[]=;
dfs(,-,<<);
puts("YES");
for(int i=;i<=n;i++)
printf("%lld %lld\n",ans_x[i],ans_y[i]);
}
return ;
}
Codeforces Round #394 (Div. 2) E. Dasha and Puzzle(dfs)的更多相关文章
- 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) C.Dasha and Password(暴力)
http://codeforces.com/contest/761/problem/C 题意:给出n个串,每个串的初始光标都位于0(列)处,怎样移动光标能够在凑出密码(每个串的光标位置表示一个密码的字 ...
- Codeforces Round #394 (Div. 2) B. Dasha and friends(暴力)
http://codeforces.com/contest/761/problem/B 题意: 有一个长度为l的环形跑道,跑道上有n个障碍,现在有2个人,给出他们每过多少米碰到障碍,判断他们跑的是不是 ...
- 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) C. Dasha and Password(简单DP)
C. Dasha and Password time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- Codeforces Round #184 (Div. 2) E. Playing with String(博弈)
题目大意 两个人轮流在一个字符串上删掉一个字符,没有字符可删的人输掉游戏 删字符的规则如下: 1. 每次从一个字符串中选取一个字符,它是一个长度至少为 3 的奇回文串的中心 2. 删掉该字符,同时,他 ...
- Codeforces Round #556 (Div. 2) - C. Prefix Sum Primes(思维)
Problem Codeforces Round #556 (Div. 2) - D. Three Religions Time Limit: 1000 mSec Problem Descripti ...
- Codeforces Round #267 (Div. 2) C. George and Job(DP)补题
Codeforces Round #267 (Div. 2) C. George and Job题目链接请点击~ The new ITone 6 has been released recently ...
随机推荐
- 【BZOJ2217】[Poi2011]Lollipop 乱搞
[BZOJ2217][Poi2011]Lollipop Description 有一个长度为n的序列a1,a2,...,an.其中ai要么是1("W"),要么是2("T& ...
- 【BZOJ1040】[ZJOI2008]骑士 树形DP
[BZOJ1040][ZJOI2008]骑士 Description Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各界的赞扬.最近发生了一件可怕的事情 ...
- ios Quartz 各种绘制图形用法
摘要: CoreGraphics的功能非常强大,可以绘制各种图形:今天学习一下怎么绘制简单的点线面,记录学习. 一.导入coreGraphics.framework 二.绘制图形 1.绘制矩形 // ...
- linux 下 安装go
首先肯定是下载资源包了,链接汇总在http://www.golangtc.com/download,我用的是 http://www.golangtc.com/static/go/go1.4beta1. ...
- WCF学习 (三)深入认识WCF契约
什么是契约? 从SOA概念上讲,契约属于服务公开接口的一部分.一个服务契约,定义了服务端公开的服务方法,使用传输协议,可访问地址,传输的消息格式等内容.换句话说:契约描述了该服务的功能和作用,它告诉S ...
- Elasticsearch之settings和mappings(图文详解)
Elasticsearch之settings和mappings的意义 简单的说,就是 settings是修改分片和副本数的. mappings是修改字段和类型的. 记住,可以用url方式来操作它们,也 ...
- throw and throws in Java
throw and throws in Java - GeeksforGeeks https://www.geeksforgeeks.org/throw-throws-java/ throw and ...
- ubuntu打开txt乱码
因为不支持中文 输入命令: iconv -f gbk -t utf8 filename.txt > filename.txt.utf8
- Just a Hook---hdu1698(线段树---区间处理)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698 现有n个金属(编号1---n),每个金属的材质都是铜,有m个操作,每个操作都是把编号 L 到 R ...
- Python的Tornado框架的异步任务与AsyncHTTPClient
转载自http://www.php.cn/python-tutorials-284773.html 高性能服务器TornadoPython的web框架名目繁多,各有千秋.正如光荣属于希腊,伟大属于罗马 ...