Codeforces_761_E_(dfs)
2 seconds
256 megabytes
standard input
standard output
Dasha decided to have a rest after solving the problem. She had been ready to start her favourite activity — origami, but remembered the puzzle that she could not solve.
The tree is a non-oriented connected graph without cycles. In particular, there always are n - 1 edges in a tree with nvertices.
The puzzle is to position the vertices at the points of the Cartesian plane with integral coordinates, so that the segments between the vertices connected by edges are parallel to the coordinate axes. Also, the intersection of segments is allowed only at their ends. Distinct vertices should be placed at different points.
Help Dasha to find any suitable way to position the tree vertices on the plane.
It is guaranteed that if it is possible to position the tree vertices on the plane without violating the condition which is given above, then you can do it by using points with integral coordinates which don't exceed 1018 in absolute value.
The first line contains single integer n (1 ≤ n ≤ 30) — the number of vertices in the tree.
Each of next n - 1 lines contains two integers ui, vi (1 ≤ ui, vi ≤ n) that mean that the i-th edge of the tree connects vertices ui and vi.
It is guaranteed that the described graph is a tree.
If the puzzle doesn't have a solution then in the only line print "NO".
Otherwise, the first line should contain "YES". The next n lines should contain the pair of integers xi, yi(|xi|, |yi| ≤ 1018) — the coordinates of the point which corresponds to the i-th vertex of the tree.
If there are several solutions, print any of them.
7
1 2
1 3
2 4
2 5
3 6
3 7
YES
0 0
1 0
0 1
2 0
1 -1
-1 1
0 2
6
1 2
2 3
2 4
2 5
2 6
NO
4
1 2
2 3
3 4
YES
3 3
4 3
5 3
6 3
In the first sample one of the possible positions of tree is:
思路:边的长度取2的幂,由大减小,每次除以2(不能从1开始每次乘2,在这儿卡了好久,其实仔细一想,很容易想到如果从小到大会出现交叉)。然后就是简单dfs。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector> using namespace std;
#define LL long long struct Node
{
LL x, y;
Node() {}
Node(LL a,LL b)
{
x=a;
y=b;
} }; vector<int> eage[];
int dir[][]= {-,,,,,,,-}; int deg[];
int vis[];
int n;
LL len=;
Node coor[]; void dfs(int p,int d)
{
int di=;
for(int i=; i<eage[p].size(); i++)
{
if(vis[eage[p][i]]==)
{
if(di==d)
di=(di+)%;
vis[eage[p][i]]=;
coor[eage[p][i]].x=coor[p].x+dir[di][]*len;
coor[eage[p][i]].y=coor[p].y+dir[di][]*len;
len=len*;
//cout<<eage[p][i]<<' '<<coor[eage[p][i]].x<<' '<<coor[eage[p][i]].y<<endl;
dfs(eage[p][i],(di+)%);
di=(di+)%;
}
}
} int main()
{ scanf("%d",&n);
for(int i=; i<n-; i++)
{
int a,b;
scanf("%d%d",&a,&b);
eage[a].push_back(b);
eage[b].push_back(a);
deg[a]++;
deg[b]++; }
for(int i=;i<=n;i++)
if(deg[i]>)
{
printf("NO\n");
return ;
}
vis[]=;
dfs(,-);
printf("YES\n");
for(int i=; i<=n; i++)
//cout<<coor[i].x<<" "<<coor[i].y<<endl;
printf("%I64d %I64d\n",coor[i].x,coor[i].y);
return ;
}
Codeforces_761_E_(dfs)的更多相关文章
- BZOJ 3083: 遥远的国度 [树链剖分 DFS序 LCA]
3083: 遥远的国度 Time Limit: 10 Sec Memory Limit: 1280 MBSubmit: 3127 Solved: 795[Submit][Status][Discu ...
- BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]
1103: [POI2007]大都市meg Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2221 Solved: 1179[Submit][Sta ...
- BZOJ 4196: [Noi2015]软件包管理器 [树链剖分 DFS序]
4196: [Noi2015]软件包管理器 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1352 Solved: 780[Submit][Stat ...
- 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)
图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...
- BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]
2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 2545 Solved: 1419[Submit][Sta ...
- POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)
来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS Memory Limit: 65536 ...
- 深度优先搜索(DFS)
[算法入门] 郭志伟@SYSU:raphealguo(at)qq.com 2012/05/12 1.前言 深度优先搜索(缩写DFS)有点类似广度优先搜索,也是对一个连通图进行遍历的算法.它的思想是从一 ...
- 【BZOJ-3779】重组病毒 LinkCutTree + 线段树 + DFS序
3779: 重组病毒 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 224 Solved: 95[Submit][Status][Discuss] ...
- 【BZOJ-1146】网络管理Network DFS序 + 带修主席树
1146: [CTSC2008]网络管理Network Time Limit: 50 Sec Memory Limit: 162 MBSubmit: 3495 Solved: 1032[Submi ...
随机推荐
- 函数绑定 bind
函数拓展-bind bind实现的是:对函数绑定作用域 更改作用域的方法:call,apply,with,eval,bind call 和 apply 的比较 相同点:1.都是在使用时候(使用即执行) ...
- Growth: 一个关于怎样成为优秀Web Developer 的 App
想了想还是决定在今天公布一个预览版.这样才干持续改进.Growth是一个关于怎样成为优秀的Web Developer的APP--结合技能树.成长路线图.进阶书单.Web七日谈以及一些小測验. 它是我对 ...
- 工作总结 default Console.WriteLine(default(Guid));
泛型代码中的默认关键字 在泛型类和泛型方法中产生的一个问题是,在预先未知以下情况时,如何将默认值分配给参数化类型 T: T 是引用类型还是值类型. 如果 T 为值类型,则它是数值还是结构. 给定参数化 ...
- android 获取手机信息工具类
package com.yqy.yqy_listviewheadview; import android.content.Context; import android.telephony.Telep ...
- Cocos2dx如何引用第三方SO文件(Android NDK)
做项目的过程中发现,引用第三方的库lib3rdsdk.so,当直接把lib3rdsdk.so放进armeabi文件夹里,会被删除掉.查网上资料都说的不全,经过实验,最简单的方法就是在jni下的andr ...
- luogu 3388 【模板】割点(割顶)
点双. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> ...
- POJ2127 Greatest Common Increasing Subsequence
POJ2127 给定两个 整数序列,求LCIS(最长公共上升子序列) dp[i][j]表示A的A[1.....i]与B[1.....j]的以B[j]为结尾的LCIS. 转移方程很简单 当A[i]!=B ...
- Java 泛型 三
一.泛型初衷 Java集合不会知道我们需要用它来保存什么类型的对象,所以他们把集合设计成能保存任何类型的对象,只要就具有很好的通用性.但这样做也带来两个问题: –集合对元素类型没有任何限制,这样可能引 ...
- 深入浅出Android makefile(1)--初探(转载)
转载:http://nfer-zhuang.iteye.com/blog/1752368 一.说明 android build system是一个非常庞大的系统,要编译Android工程.修改或新增A ...
- 【weiphp】安装中报错
问题描述:安装的第三部报错“SQLSTATE[HY000]: General error: 2030 This command is not supported in the prepared sta ...