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,模拟赛里那道题的 ...
随机推荐
- nodejs REPL(交互式解释器)
Node.js REPL(交互式解释器) Node.js REPL(Read Eval Print Loop:交互式解释器) 表示一个电脑的环境,类似 Window 系统的终端或 Unix/Linux ...
- STL之set具体解释(二)
首先来看看set集合容器: set集合容器实现了红黑树的平衡二叉树数据结构.在插入元素时它会自己主动调整二叉树的排列,把该元素放到适当的位置,而且 保证左右子树平衡.平衡二叉检索树採用中序遍历算法. ...
- vim tips 集锦
删除文件中的空行 :g/^$/d g 表示 global,全文件 ^ 是行开始,$ 是行结束 d 表示删除该 这里只能匹配到没有白空符的空行,假如要删除有空白符的空行,则使用: :g/^\s*$/d ...
- JavaSE 文件递归之删除&获取文件夹文件夹中全部的以.jpg的文件的绝对路径
1.递归删除文件 假设一个文件夹以下还有子文件夹,进行删除的话会 报错,这个时候要使用递归的方式来删除这个文件文件夹中的全部文件以及文件夹 package cn.itcast.digui; impor ...
- PHP获取IP
<?php $iipp = $_SERVER["REMOTE_ADDR"]; echo $iipp ; ?>
- EasyDarwin开源流媒体云平台VS调试断点提示“还没有为该文档加载任何符号”的解决办法
本文转自EasyDarwin开源团队成员Alex的博客:http://blog.csdn.net/cai6811376/article/details/52063666 近日,我们EasyDarwin ...
- Extjs-树 Ext.tree.TreePanel 动态加载数据
先上效果图 1.说明Ext.tree.Panel 控件是树形控件,大家知道树形结构在软件开发过程中的应用是很广泛的,树形控件的数据有本地数据.服务器端返回的数据两种.对于本地数据的加载,在extjs的 ...
- Recurrent neural networks are very powerful, because they combine two properties
https://www.cs.toronto.edu/~hinton/csc2535/notes/lec10new.pdf Distributed hidden state that allows t ...
- 推荐一个非常好的 IntelliJ IDEA 教程
教程地址:https://github.com/judasn/IntelliJ-IDEA-Tutorial 作者博客:http://www.youmeek.com/category/software- ...
- MongoDB 学习一
这一章,我们先介绍几个MongoDB的概念: 1.document: 它是MongoDB的基础数据单元,它大概等价于关系型数据库中的行. 2.collection: 可以想象成动态的表. 3.一个简单 ...