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,模拟赛里那道题的 ...
随机推荐
- [C#]使用 C# 代码实现拓扑排序 dotNet Core WEB程序使用 Nginx反向代理 C#里面获得应用程序的当前路径 关于Nginx设置端口号,在Asp.net 获取不到的,解决办法 .Net程序员 初学Ubuntu ,配置Nignix 夜深了,写了个JQuery的省市区三级级联效果
[C#]使用 C# 代码实现拓扑排序 目录 0.参考资料 1.介绍 2.原理 3.实现 4.深度优先搜索实现 回到顶部 0.参考资料 尊重他人的劳动成果,贴上参考的资料地址,本文仅作学习记录之用. ...
- nightwatch testing 注意事项
coding style 好的測試項目應該盡量不要使用pause 並且要有驗證的機制 .click('input.btn') //.pause(3000) .waitForElementVisible ...
- DM8168 unrecoverable error: OMX_ErrorBadParameter (0x80001005) [resolved]
DM8168 custom board 成功启动系统之后想先測一下8168编解码功能,把开发包里的examples跑一遍.启动完毕后.连上HDMI显示,在starting Matrix GUI app ...
- 英特尔和Red Hat合作实现Gnome桌面的Wayland支持
在发布支持XMir的Linux图形驱动程序xf86-video-intel 2.99.901后数天,英特尔宣布撤回对XMir的支持,XMir补丁不会合并到上游项目.XMir是Mir显示服务器的X11兼 ...
- 今日BBC
1.随身英语 Brain training 怎样训练大脑? link 2.地道英语 In good nick 品质好.没有损坏(主要是指古老的东西,比方古董) link 3.今日新闻 -------- ...
- POJ 2480 Longge's problem 积性函数
题目来源:id=2480" style="color:rgb(106,57,6); text-decoration:none">POJ 2480 Longge's ...
- C# 通过Hook的方法 屏蔽快捷键
#region 屏蔽Windows功能键(快捷键) public delegate int HookProc(int nCode, int wParam, IntPtr lParam ...
- PHP 5.6编译安装
yum install openssl openssl-devel libxml2-devel libxml2 bzip2 bzip2-devel curl-devel php-mcrypt libm ...
- IOS UIScrollView滚动到指定位置
[mScrollView setContentOffset:CGPointMake(0,200) animated:YES];
- 九度OJ 1119:Integer Inquiry(整数相加) (大数运算)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:679 解决:357 题目描述: One of the first users of BIT's new supercomputer was ...