Paint Tree
题意:
给定一棵n个点的树,给定平面上n个点,将n个点用线段连起来画成树的形状,使得不存在不在端点相交的线段,构造出一种情况。
解法:
首先观察我们常规画出来的树形图可知,树的子树是根据极角分开的,这样,我们每一次找到最靠左下的点,
而后对剩余点极角排序,根据子树大小和极角的连续关系将点集划分,依次递归即可。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm> #define N 1510
#define LL long long
#define p E[i].x
#define LD double
#define pi acos(-1) using namespace std; struct edge
{
int x,to;
}E[N<<]; struct node
{
LL x,y;
LD v;
int id;
void scan()
{
scanf("%I64d%I64d",&x,&y);
}
}a[N],ans[N],root; int n,totE;
int g[N],fa[N],siz[N],ansv[N]; void addedge(int x,int y)
{
E[++totE]=(edge){y,g[x]}; g[x]=totE;
E[++totE]=(edge){x,g[y]}; g[y]=totE;
} void dfs(int x)
{
siz[x]=;
for(int i=g[x];i;i=E[i].to)
if(p!=fa[x])
{
fa[p]=x;
dfs(p);
siz[x]+=siz[p];
}
} bool cmp(node a,node b)
{
LL tmp1=(a.x-root.x)*(b.y-root.y);
LL tmp2=(a.y-root.y)*(b.x-root.x);
return tmp1<tmp2;
} void solve(int x,int l,int r)
{
int t=l;
for(int i=l+;i<=r;i++)
if(a[i].x<a[t].x || (a[i].x==a[t].x && a[i].y<a[t].y))
t=i;
swap(a[t],a[l]);
root=ans[x]=a[l];
l++;
if(l>r) return;
sort(a+l,a+r+,cmp);
for(int i=g[x];i;i=E[i].to)
if(p!=fa[x])
{
solve(p,l,l+siz[p]-);
l=l+siz[p];
}
} int main()
{
while(~scanf("%d",&n))
{
for(int i=;i<=n;i++) g[i]=;
totE=;
for(int i=,x,y;i<n;i++)
{
scanf("%d%d",&x,&y);
addedge(x,y);
}
dfs();
for(int i=;i<=n;i++)
{
a[i].scan();
a[i].id=i;
}
solve(,,n);
for(int i=;i<=n;i++) ansv[ans[i].id]=i;
for(int i=;i<=n;i++) printf("%d ",ansv[i]);
printf("\n");
}
return ;
}
Paint Tree的更多相关文章
- Codeforces 196 C. Paint Tree
分治.选最左上的点分给根.剩下的极角排序后递归 C. Paint Tree time limit per test 2 seconds memory limit per test 256 megaby ...
- [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 ...
- 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 Round #124 (Div. 2)
A. Plate Game 如果可以放置一个圆的情况下,先手将圆放置在矩形正中心,那么根据对称性,先手只要放后手的对称的位置即可,也就是先手必胜,否则后手胜. B. Limit 讨论\(n,m\)的大 ...
- bzoj3638
费用流+线段树 看见这个题我们马上就能想到费用流,设立源汇,分别向每个点连接容量为1费用为0的边,然后相邻的点之间连边,费用为点权,跑费用流就行了,但是很明显这样会超时,那么我们要优化一下,我们观察费 ...
- AtCoder Regular Contest 108
Contest Link Official Editorial A - Sum and Product Given are integers \(S\) and \(P\) . Is there a ...
- 我的刷题单(8/37)(dalao珂来享受切题的快感
P2324 [SCOI2005]骑士精神 CF724B Batch Sort CF460C Present CF482A Diverse Permutation CF425A Sereja and S ...
- 贪心/构造/DP 杂题选做Ⅲ
颓!颓!颓!(bushi 前传: 贪心/构造/DP 杂题选做 贪心/构造/DP 杂题选做Ⅱ 51. CF758E Broken Tree 讲个笑话,这道题是 11.3 模拟赛的 T2,模拟赛里那道题的 ...
随机推荐
- python-pyDes-ECB加密-DES-DES3加密
网上的教程都他妹的是抄的,抄也就算了,还改抄错了,害我写了一两天都没找到原因,直接去官网看,找例子很方便 官网链接:http://twhiteman.netfirms.com/des.html 一个小 ...
- 通过路由管理视图间切换 - AngularJS路由解析
模板的视图刷新 ng-view这个指令和路由组合之后就可以将$route对应的视图放入指定的HTML中,这一过程中它会创建自己的作用域并将模板嵌套在内部. ng-view指令的优先级是1000(终极) ...
- 一套Tomcat处理多个域名请求 - Virtual Host
最近和Tomcat较上劲了... 作为Tomcat的系列之一,来尝试下如何用一套Tomcat来处理多个域名请求. 场景:基于成本考虑,多个department共用一台服务器,然后该服务器上就一套Tom ...
- poj3040(双向贪心)
Allowance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1540 Accepted: 637 Descript ...
- Openlayers中layer介绍
1.base layers & overlay layers base layer:最底层的layer,其它的图层是在他之上,最先增加的图层默认作为base layer. overlay la ...
- 2018-11-9-匿名函数&递归函数初识
1.匿名函数(lambda) 2.递归函数初识
- ubuntu搜狗拼音安装
1.官方下载deb 2.双击安装 3.终端im-config,选择fcitx 4.重启 5.输入法设置中add一下sougoupinyin
- 诡异的json包含bom头
今日项目碰到 需要调用php的一个接口 结果一直报返回的json字符串转对象 bom头报错 Exception in thread "main" com.fasterxml.j ...
- HackerRank leonardo-and-lucky-numbers —— 模线性方程的通解
题目链接:https://vjudge.net/problem/HackerRank-leonardo-and-lucky-numbers 题解: 1.根据扩展欧几里得:7*x + 4*y = gcd ...
- python 解析配置文件
settings.cfg [english] greeting = Hello [french] greeting = Bonjour [files] home = /usr/local bin = ...