[CodeForces - 197E] E - Paint Tree
E - Paint Tree
You are given a tree with n vertexes and n points on a plane, no three points lie on one straight line.
Your task is to paint the given tree on a plane, using the given points as vertexes.
That is, you should correspond each vertex of the tree to exactly one point and each point should correspond to a vertex. If two vertexes of the tree are connected by an edge, then the corresponding points should have a segment painted between them. The segments that correspond to non-adjacent edges, should not have common points. The segments that correspond to adjacent edges should have exactly one common point.
Input
The first line contains an integer n (1 ≤ n ≤ 1500) — the number of vertexes on a tree (as well as the number of chosen points on the plane).
Each of the next n - 1 lines contains two space-separated integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi) — the numbers of tree vertexes connected by the i-th edge.
Each of the next n lines contain two space-separated integers xi and yi ( - 109 ≤ xi, yi ≤ 109) — the coordinates of the i-th point on the plane. No three points lie on one straight line.
It is guaranteed that under given constraints problem has a solution.
Output
Print n distinct space-separated integers from 1 to n: the i-th number must equal the number of the vertex to place at the i-th point (the points are numbered in the order, in which they are listed in the input).
If there are several solutions, print any of them.
Example
31 32 30 01 12 0
1 3 2
41 22 31 4-1 -23 5-3 32 0
4 2 1 3
Note
The possible solutions for the sample are given below.
题目意思就是,给你一棵已经定型的无根树,然后在二维屏幕上给出几个坐标,要求将树的每一个节点对应到二维平面上的每一个点上,要求形成的是一棵正常的树.
这题完全没思路,只知道肯定是排序+DFS,然而,就是没有想的更深一点——极角排序+分治.
我们每次递归分治的的时候,标记的总是一段区间,这个区间是有一定顺序的,他按照基准点极角排序.为什么极角排序就一定对呢?
因为每一次,你找的相当于在凸多边形上这个点的下几个点,不会穿越其他线段,这段区间也会被覆盖满(极角排序的性质很适用).最后输出就好了,效率应该是n^nlogn的。
#include<cstdio> #include<cstring> #include<algorithm> #define LL long long using namespace std; ,maxe=; int size[maxn],fa[maxn]; int n,tot,nxt[maxe],son[maxe],lnk[maxn],ans[maxn]; bool vis[maxn]; struct point{int x,y,id;}a[maxn],mainp; point operator - (point p,point q){point ret; ret.x=p.x-q.x,ret.y=p.y-q.y; return ret;} LL cross(point p,point q){return (LL)p.x*q.y-(LL)q.x*p.y;} bool cmp1(point u,point v){return u.x!=v.x?u.x<v.x:u.y<v.y;} ;} int read(){ ,f=; char ch=getchar(); '){if (ch=='-') f=-f; ch=getchar();} +ch-',ch=getchar(); return x*f; } void add(int x,int y){nxt[++tot]=lnk[x],son[tot]=y,lnk[x]=tot;} void DFS(int x){ vis[x]=,size[x]=; for (int j=lnk[x]; j; j=nxt[j]) if (!vis[son[j]]){ DFS(son[j]); size[x]+=size[son[j]]; } } void DFS(int x,int fa,int L,int R){ sort(a+L,a++R,cmp1); ans[a[L].id]=x,mainp.x=a[L].x,mainp.y=a[L].y; sort(a++L,a++R,cmp2); int now=L; for (int j=lnk[x]; j; j=nxt[j]) if (son[j]!=fa){ DFS(son[j],x,now+,now+size[son[j]]),now+=size[son[j]]; } } int main(){ n=read(),tot=; ; i<n; i++){int x=read(),y=read(); add(x,y),add(y,x);} memset(vis,,,); ; i<=n; i++) a[i].x=read(),a[i].y=read(),a[i].id=i; DFS(,,,n); ; i<=n; i++) printf("%d ",ans[i]); ; }
[CodeForces - 197E] E - Paint Tree的更多相关文章
- Codeforces 196 C. Paint Tree
分治.选最左上的点分给根.剩下的极角排序后递归 C. Paint Tree time limit per test 2 seconds memory limit per test 256 megaby ...
- Codeforces Round #124 (Div. 1) C. Paint Tree(极角排序)
C. Paint Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Codeforces 196C Paint Tree(贪心+极角排序)
题目链接 Paint Tree 给你一棵n个点的树和n个直角坐标系上的点,现在要把树上的n个点映射到直角坐标系的n个点中,要求是除了在顶点处不能有线段的相交. 我们先选一个在直角坐标系中的最左下角的点 ...
- Codeforces 461B Appleman and Tree(木dp)
题目链接:Codeforces 461B Appleman and Tree 题目大意:一棵树,以0节点为根节点,给定每一个节点的父亲节点,以及每一个点的颜色(0表示白色,1表示黑色),切断这棵树的k ...
- Codeforces 1129 E.Legendary Tree
Codeforces 1129 E.Legendary Tree 解题思路: 这题好厉害,我来复读一下官方题解,顺便补充几句. 首先,可以通过询问 \(n-1\) 次 \((S=\{1\},T=\{ ...
- Codeforces 280C Game on tree【概率DP】
Codeforces 280C Game on tree LINK 题目大意:给你一棵树,1号节点是根,每次等概率选择没有被染黑的一个节点染黑其所有子树中的节点,问染黑所有节点的期望次数 #inclu ...
- Codeforces A. Game on Tree(期望dfs)
题目描述: Game on Tree time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Codeforces Round #781(C. Tree Infection)
Codeforces Round #781 C. Tree Infection time limit per test 1 second memory limit per test 256 megab ...
- Codeforces 734E. Anton and Tree 搜索
E. Anton and Tree time limit per test: 3 seconds memory limit per test :256 megabytes input:standard ...
随机推荐
- Qt中中文字符 一劳永逸的解决方法
QT中中文字符问题,有没有一劳永逸的解决方法? 目前遇到有以下问题 1.字符串中有中文时,编译提示"常量中含有换行符" 2.在控制台窗口输出中文时无法正常显示,中文全部显示为? 目 ...
- 每日质量NPM包拖拽文件上传_react-dropzone
一.react-dropzone 官方定义: Simple HTML5-compliant drag'n'drop zone for files built with React.js. 理解: 一个 ...
- Jtest的简单使用
Jtest主要用于快速测试自己的代码是否正确 条件,导入相应的Jtest包 @Test public void test() { System.out.println(" ...
- npm i和npm install的区别
最近人用npm i来直接安装模块,但是有会报错,用npm install就不会报错,刚开始百思不得其解,它俩明明是同一个东西 后来查npm的帮助指令发现还是没区别,npm i仅仅是npm instal ...
- js常见知识点1.ajax相关
一. javascript中的typeof返回哪些数据类型? 建议回复: typeof 运算符把类型信息当作字符串返回.typeof 返回值有六种可能: number, string, boolean ...
- (转)c#反射
1. 什么是反射2. 命名空间与装配件的关系3. 运行期得到类型信息有什么用4. 如何使用反射获取类型5. 如何根据类型来动态创建对象6. 如何获取方法以及动态调用方法7. 动态创建委托 1.什么是反 ...
- idea maven环境下 java实现发送邮件验证
1.开通smtp授权 QQ邮箱-设置-账户-开启 得到一个授权码 2.下载javax.email包 maven项目中 pom文件加入: <dependency> <groupId&g ...
- .bat文件的用途
bat(batch) .bat文件是用来干什么的?我们可以通过win+r,进入cmd,用dos命令行来完成某些操作,比如ping. 我们这里可以用更简单的办法,可以把命令行写入记事本,然后修改后缀为b ...
- TStringList 常用方法与属性
/TStringList 常用方法与属性 :varList: TStringList;i: Integer;begin List := TStringList.Create;List.Add('Str ...
- English trip V1 - B 23. Nosy People 爱管闲事的人 Teacher:Parice Key: Be + Ving
In this lesson you will learn to talk about what happened. 谈论发生什么? 课上内容(Lesson) Nosy 好管闲事Noise 噪声 ...