分治。选最左上的点分给根。剩下的极角排序后递归

C. Paint Tree
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 ≤ nui ≠ 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 thei-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.

Sample test(s)
input
3
1 3
2 3
0 0
1 1
2 0
output
1 3 2
input
4
1 2
2 3
1 4
-1 -2
3 5
-3 3
2 0
output
4 2 1 3
Note

The possible solutions for the sample are given below.


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector> using namespace std; const int maxn=1520; int n,X,Y; struct PO
{
int x,y,d;
bool operator<(const PO &o) const
{
if(x-X>=0&&o.x-X<=0) return 1;
if(x-X<=0&&o.x-X>=0) return 0;
return (y-Y)*(long long)(o.x-X)<(o.y-Y)*(long long)(x-X);
}
}p[maxn]; vector<int> g[maxn]; bool vis[maxn];
int sz[maxn],o[maxn]; int dfs(int u)
{
vis[u]=true;
sz[u]=1;
int ret=0;
for(int i=0,j=g[u].size();i<j;i++)
{
int v=g[u][i];
if(vis[v]) continue;
ret+=dfs(v);
}
sz[u]+=ret;
return sz[u];
} void calc(int u,int l,int r)
{
vis[u]=true;
int t=l;
for(int i=l+1;i<=r;i++)
{
if((p[i].y<p[t].y)||(p[t].y==p[i].y&&p[i].x<p[t].x))
t=i;
}
if(t!=l) swap(p[l],p[t]);
o[p[l].d]=u;
X=p[l].x; Y=p[l].y;
sort(p+l+1,p+r+1);
int pos=l+1;
for(int i=0,j=g[u].size();i<j;i++)
{
int v=g[u][i];
if(vis[v]) continue;
calc(v,pos,pos+sz[v]-1);
pos+=sz[v];
}
}
int main()
{
scanf("%d",&n);
for(int i=0;i<n-1;i++)
{
int x,y;
scanf("%d%d",&x,&y);
g[x].push_back(y); g[y].push_back(x);
}
for(int i=1;i<=n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
p[i]=(PO){x,y,i};
}
dfs(1);
memset(vis,0,sizeof(vis));
calc(1,1,n);
for(int i=1;i<=n;i++)
printf("%d ",o[i]);
putchar(10);
return 0;
}

Codeforces 196 C. Paint Tree的更多相关文章

  1. [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 ...

  2. 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 ...

  3. Codeforces 196C Paint Tree(贪心+极角排序)

    题目链接 Paint Tree 给你一棵n个点的树和n个直角坐标系上的点,现在要把树上的n个点映射到直角坐标系的n个点中,要求是除了在顶点处不能有线段的相交. 我们先选一个在直角坐标系中的最左下角的点 ...

  4. Codeforces 461B Appleman and Tree(木dp)

    题目链接:Codeforces 461B Appleman and Tree 题目大意:一棵树,以0节点为根节点,给定每一个节点的父亲节点,以及每一个点的颜色(0表示白色,1表示黑色),切断这棵树的k ...

  5. Codeforces 1129 E.Legendary Tree

    Codeforces 1129 E.Legendary Tree 解题思路: 这题好厉害,我来复读一下官方题解,顺便补充几句. 首先,可以通过询问 \(n-1​\) 次 \((S=\{1\},T=\{ ...

  6. Codeforces 280C Game on tree【概率DP】

    Codeforces 280C Game on tree LINK 题目大意:给你一棵树,1号节点是根,每次等概率选择没有被染黑的一个节点染黑其所有子树中的节点,问染黑所有节点的期望次数 #inclu ...

  7. [Codeforces #196] Tutorial

    Link: Codeforces #196 传送门 A: 枚举 #include <bits/stdc++.h> using namespace std; #define X first ...

  8. Codeforces A. Game on Tree(期望dfs)

    题目描述: Game on Tree time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  9. Codeforces Round #781(C. Tree Infection)

    Codeforces Round #781 C. Tree Infection time limit per test 1 second memory limit per test 256 megab ...

随机推荐

  1. EasyUI - ComboBox 下拉组件

    效果: html代码: <input id ="comb" name ="comb"/> JS代码: $(function () { $('#com ...

  2. EasyUI - DataGrid 组建 - [ 组件加载和分页 ]

    效果: 原理:通过POST传递到数据后台字段. 此时上传的参数,page:当前页数,rows:每页显示的页数. 有此两项参数,计算取出数据条数. 通过后台接受参数,进行处理并返回抽取的数据. html ...

  3. Swift - 跳跃吃苹果游戏开发(SpriteKit游戏开发)

    下面通过一个样例演示如何实现飞行道具的生成,以及道具碰撞拾取. 样例说明: 1,屏幕从右到左不断地生成苹果飞过来(苹果高度随机) 2,点击屏幕可以让熊猫跳跃 3,熊猫碰到苹果,苹果消失 运行效果: 样 ...

  4. Ubuntu_开启root 登陆

    默认的安装完ubuntu ,root 用户没有开启 1.使用安装时的用户,先给root用户设置密码 设置root密码 sudo passwd root 之后会提示输入新的密码 切换到root用户 su ...

  5. python 网络爬虫(二) BFS不断抓URL并放到文件中

    上一篇的python 网络爬虫(一) 简单demo 还不能叫爬虫,只能说基础吧,因为它没有自动化抓链接的功能. 本篇追加如下功能: [1]广度优先搜索不断抓URL,直到队列为空 [2]把所有的URL写 ...

  6. hdu 1387 Team Queue (链表)

    题目大意: 不同的人在不同的队伍里,插入链表的时候假设这个链表里有他的队友,就把它放到最后一个队友的最后.假设没有队友,就把它放到整个链表的最后面. 出链表的时候把第一个人拿出来. 思路分析: 要模拟 ...

  7. Delphi 10.1 柏林更新内容简译

    新的 SDKTransform.exe 支持转换 Object-C 或 C++ 头文件到Delphi. 修改了对话框的接口,分成了同步和异步两种: http://blog.qdac.cc/?p=380 ...

  8. 让AllocateHwnd接受一般函数地址作参数(105篇博客)

    http://www.xuebuyuan.com/1889769.html Classes单元的AllocateHWnd函数是需要传入一个处理消息的类的方法的作为参数的,原型: function Al ...

  9. 一劳永逸解决UAC问题(修改QMAKE_LFLAGS_EXE的设置)

    如果你的程序跑在一个开启了UAC保护的系统中,而你的程序又没有"盾牌"的话,程序总是会受到各种阻挠的,比如读写文件,写注册表等. 有了"盾牌"的话就不会出现一些 ...

  10. 测试关闭mojo utf-8

    [root@wx03 ~]# cat test.pl use Mojolicious::Lite; use JSON qw/encode_json decode_json/; use Encode; ...