题意:

给定一棵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的更多相关文章

  1. Codeforces 196 C. Paint Tree

    分治.选最左上的点分给根.剩下的极角排序后递归 C. Paint Tree time limit per test 2 seconds memory limit per test 256 megaby ...

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

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

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

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

  5. Codeforces Round #124 (Div. 2)

    A. Plate Game 如果可以放置一个圆的情况下,先手将圆放置在矩形正中心,那么根据对称性,先手只要放后手的对称的位置即可,也就是先手必胜,否则后手胜. B. Limit 讨论\(n,m\)的大 ...

  6. bzoj3638

    费用流+线段树 看见这个题我们马上就能想到费用流,设立源汇,分别向每个点连接容量为1费用为0的边,然后相邻的点之间连边,费用为点权,跑费用流就行了,但是很明显这样会超时,那么我们要优化一下,我们观察费 ...

  7. AtCoder Regular Contest 108

    Contest Link Official Editorial A - Sum and Product Given are integers \(S\) and \(P\) . Is there a ...

  8. 我的刷题单(8/37)(dalao珂来享受切题的快感

    P2324 [SCOI2005]骑士精神 CF724B Batch Sort CF460C Present CF482A Diverse Permutation CF425A Sereja and S ...

  9. 贪心/构造/DP 杂题选做Ⅲ

    颓!颓!颓!(bushi 前传: 贪心/构造/DP 杂题选做 贪心/构造/DP 杂题选做Ⅱ 51. CF758E Broken Tree 讲个笑话,这道题是 11.3 模拟赛的 T2,模拟赛里那道题的 ...

随机推荐

  1. open-source Julius speech-recognition engine

    http://julius.osdn.jp/en_index.php?q=index-en.html Open-Source Large Vocabulary CSR Engine Julius ht ...

  2. HSSFWorkbook

    public ActionResult excelPrint() { HSSFWorkbook workbook = new HSSFWorkbook();// 创建一个Excel文件 HSSFShe ...

  3. new和delete的基本用法

    前言 new和delete是C++中用来动态管理内存分配的运算符,其用法较为灵活.如果你对它们的几种不同用法感到困惑,混淆,那么接着看下去吧. 功能一:动态管理单变量/对象空间 下面例子使用new为单 ...

  4. LeetCode: Binary Tree Postorder Traversal [145]

    [题目] Given a binary tree, return the postorder traversal of its nodes' values. For example: Given bi ...

  5. Hibernate中的HQL语言

    一.HQL语言简介 HQL全称是Hibernate Query Language,它提供了是十分强大的功能,它是针对持久化对象,直接取得对象,而不进行update,delete和insert等操作.而 ...

  6. XShell连接不了虚拟机

    本机安装好虚拟机和centeros; 使用xshell连接: linux Could not connect to '127.0.0.1' (port 22): Connection failed. ...

  7. linux修改进程的名字

    1 修改linux进程名字的基本原理 linux进程以argv[0]作为进程的名字,因此只需要修改argv[0]处的字符串就修改了linux进程的名字. 2 直接修改argv[0]会导致的问题 如果名 ...

  8. 在MFC中使用大漠插件

    打开Class Wizard,Add Class...->MFC Class From TypeLib... File->Location->>> Finish-> ...

  9. 将css 中的16进制颜色, 转化为 rgb格式

    对dojo/_base/Color模块的注解. 源地址 https://github.com/robinxiong/dojo/blob/master/_base/Color.js function f ...

  10. js 链接传入中文参数乱码解决

    传入时,可能出现中文的参数用encodeURI进行两次转码,如: lethref="http://www.zzdblog.cn?keyword='+encodeURI(encodeURI(k ...