Codeforces Round #124 (Div. 1) C. Paint Tree(极角排序)
2 seconds
256 megabytes
standard input
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.
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.
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.
3
1 3
2 3
0 0
1 1
2 0
1 3 2
4
1 2
2 3
1 4
-1 -2
3 5
-3 3
2 0
4 2 1 3
【分析】题意比较简单。给你一棵n个节点的树,再给你几何平面内的n个点,没有三点共线,
问你能不能用着n个点在几何平面内表现出来,线段除了顶点处无其他交点。
由于没有3点共线的情况,所以解总是存在的。
我们考虑以当前平面左下角的点p作为树根,对平面上的点以p做基准进行极角排序,则所有点与p点的连线都不会有除了p以外的交点。
现在我们已经会填树根处的点了,对于树根的每个子节点,我们都可以递归的处理以其为根的子树,
假设该子树包含x个节点,我们考虑以一根从p出发,长度不限的射线,从p的正下方开始按逆时针扫过去,
直到扫过的平面包含x个节点即可停止。此时扫过的平面即为该子树应当处理的平面。
每次处理需要先找到左下角的点,然后对当前平面的所有点进行排序,共需要处理n次,所以复杂度O(n^2*logn)。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <stack>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
typedef long long ll;
using namespace std;
const int N = ;
const int M = ;
pair<ll,ll>ori;
vector<ll>edg[N];
ll n,m,k;
ll sz[N],ans[N];
struct man{
ll x,y,id;
bool operator < (const man & b) const {
return (x-ori.first)*(b.y-ori.second) - (y-ori.second)*(b.x-ori.first) > ;
}
}p[N];
void dfs1(ll u,ll fa){
sz[u]=;
for(int i=;i<edg[u].size();i++){
ll v=edg[u][i];
if(v==fa)continue;
dfs1(v,u);
sz[u]+=sz[v];
}
}
void dfs2(ll u,ll fa,ll s,ll e){
int pos;
ll x,y;
x=y=1e10;
for(int i=s;i<=e;i++){
if(p[i].x<x||((p[i].x==x)&&p[i].y<y)){
x=p[i].x;y=p[i].y;pos=i;
}
}
ori.first=x;ori.second=y;
swap(p[s],p[pos]);
sort(p+s+,p+e+);
ans[p[s].id]=u;
int cnt=;
for(int i=;i<edg[u].size();i++){
ll v=edg[u][i];
if(v==fa)continue;
dfs2(v,u,s++cnt,s++cnt+sz[v]-);
cnt+=sz[v];
}
}
int main() {
ll T,u,v;
scanf("%lld",&n);
for(int i=;i<n;i++){
scanf("%lld%lld",&u,&v);
edg[u].pb(v);edg[v].pb(u);
}
for(int i=;i<n;i++){
scanf("%lld%lld",&p[i].x,&p[i].y);
p[i].id=i;
}
dfs1(,);
dfs2(,,,n-);
for(int i=;i<n;i++)printf("%lld ",ans[i]);printf("\n");
return ;
}
Codeforces Round #124 (Div. 1) C. Paint Tree(极角排序)的更多相关文章
- Codeforces Round #124 (Div. 2)
A. Plate Game 如果可以放置一个圆的情况下,先手将圆放置在矩形正中心,那么根据对称性,先手只要放后手的对称的位置即可,也就是先手必胜,否则后手胜. B. Limit 讨论\(n,m\)的大 ...
- Codeforces Round #592 (Div. 2) D - Paint the Tree
题目链接:https://codeforces.com/contest/1244/problem/D 题意:给你一个树,让你把树上的每个节点染成三种颜色,使得任意三个互相相邻的节点颜色都不一样(意思是 ...
- Codeforces Round #329 (Div. 2) D. Happy Tree Party 树链剖分
D. Happy Tree Party Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/593/p ...
- Codeforces Round #329 (Div. 2) D. Happy Tree Party LCA/树链剖分
D. Happy Tree Party Bogdan has a birthday today and mom gave him a tree consisting of n vertecie ...
- Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+树状数组
C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...
- Codeforces Round #200 (Div. 1)D. Water Tree dfs序
D. Water Tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/343/problem/ ...
- Codeforces Round #375 (Div. 2) F. st-Spanning Tree 生成树
F. st-Spanning Tree 题目连接: http://codeforces.com/contest/723/problem/F Description You are given an u ...
- Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+ 树状数组或线段树
C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...
- Codeforces Round #200 (Div. 1) D Water Tree 树链剖分 or dfs序
Water Tree 给出一棵树,有三种操作: 1 x:把以x为子树的节点全部置为1 2 x:把x以及他的所有祖先全部置为0 3 x:询问节点x的值 分析: 昨晚看完题,马上想到直接树链剖分,在记录时 ...
随机推荐
- [洛谷P2604][ZJOI2010]网络扩容
题目大意:给定一张有向图,每条边都有一个容量C和一个扩容费用W.这里扩容费用是指将容量扩大1所需的费用. 求: 1.在不扩容的情况下,1到N的最大流: 2.将1到N的最大流增加K所需的最小费用. 题解 ...
- IFROG线上赛做过的题目
#6 1068: 找规律 int main(){ int t,n; cin>>t; while(t--){ cin>>n; ==)printf(); else printf(& ...
- Codeforces Round #348 (VK Cup 2016 Round 2, Div. 2 Edition) D
D. Little Artem and Dance time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- POJ 1050 To the Max 二维最大子段和
To the MaxTime Limit: 1000MS Memory Limit: 10000KTotal Submissions: 52281 Accepted: 27633Description ...
- rest与restful
知乎上面摘抄的,感觉不错,分享下: https://www.zhihu.com/question/28557115 1. REST描述的是在网络中client和server的一种交互形式:RES ...
- bzoj 2425 [HAOI2010]计数 dp+组合计数
[HAOI2010]计数 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 451 Solved: 289[Submit][Status][Discus ...
- 转:增强学习(二)----- 马尔可夫决策过程MDP
1. 马尔可夫模型的几类子模型 大家应该还记得马尔科夫链(Markov Chain),了解机器学习的也都知道隐马尔可夫模型(Hidden Markov Model,HMM).它们具有的一个共同性质就是 ...
- vivo面试学习1(io和nio)
一.io流(一次从open到底层的操作) 输入和输出流 IO流 字节流 Reader.Writer 字符流 InputStream.OutputStream 字节流:可以处理所有bit为单位存储的文件 ...
- SQLSERVER数据库置疑、可疑、脱机、单用户、紧急模式等的修复
数据库出现置疑.可疑.脱机.单用户.紧急模式主要是因为数据库的日志文件除了问题,2000和2008修复方式不一样,2008的修复脚本在2000中不适用,主要是不被2000识别. 假设数据库名为:eis ...
- [bzoj1031][JSOI2007]字符加密Cipher——后缀数组
Brief Description 给定一个长度为n的字符串,你需要对其进行加密. 把字符串围成一个环 显然从任意一个位置开始都可以有一个长度为n的串 把产生的n个串按字典序排序,把这n个串的最后一个 ...