题目链接 Paint Tree

给你一棵n个点的树和n个直角坐标系上的点,现在要把树上的n个点映射到直角坐标系的n个点中,要求是除了在顶点处不能有线段的相交。

我们先选一个在直角坐标系中的最左下角的点,把根结点放到这个点中,然后对剩下的点进行极角排序,按逆时顺序一个个塞进来,类似地递归处理。

这样就满足了题意。

#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b)	for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i) typedef long long LL; const int N = 1510; int X, Y; struct node{
int x, y;
int id;
friend bool operator < (const node &a, const node &b){
return (LL)(a.y - Y) * (b.x - X) < (LL)(b.y - Y) * (a.x - X);
}
} p[N]; vector <int> v[N];
int sz[N], ans[N];
int n; void dfs(int x, int fa){
sz[x] = 1;
for (auto u : v[x]){
if (u == fa) continue;
dfs(u, x);
sz[x] += sz[u];
}
} void calc(int x, int fa, int l, int r){
int t = l;
rep(i, l + 1, r){
if (p[i].y < p[t].y || (p[t].y == p[i].y && p[i].x < p[t].x))
t = i;
} if (t != l) swap(p[l], p[t]);
ans[p[l].id] = x;
X = p[l].x, Y = p[l].y;
sort(p + l + 1, p + r + 1);
int pos = l + 1; for (auto u : v[x]){
if (u == fa) continue;
calc(u, x, pos, pos + sz[u] - 1);
pos += sz[u];
}
} int main(){ scanf("%d", &n);
rep(i, 1, n - 1){
int x, y;
scanf("%d%d", &x, &y);
v[x].push_back(y);
v[y].push_back(x);
} rep(i, 1, n){
int x, y;
scanf("%d%d", &x, &y);
p[i] = {x, y, i};
} dfs(1, 0);
calc(1, 0, 1, n);
rep(i, 1, n) printf("%d\n", ans[i]); return 0;
}

Codeforces 196C Paint Tree(贪心+极角排序)的更多相关文章

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

  2. codeforces 598C C. Nearest vectors(极角排序)

    题目链接: C. Nearest vectors time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  3. Educational Codeforces Round 1 C. Nearest vectors 极角排序

    Partial Tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/598/problem/ ...

  4. Codeforces 196 C. Paint Tree

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

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

  6. [置顶] Codeforces 70D 动态凸包 (极角排序 or 水平序)

    题目链接:http://codeforces.com/problemset/problem/70/D 本题关键:在log(n)的复杂度内判断点在凸包 或 把点插入凸包 判断:平衡树log(n)内选出点 ...

  7. codeforces 1284E. New Year and Castle Construction(极角排序+扫描枚举)

    链接:https://codeforces.com/problemset/problem/1284/E 题意:平面上有n个点,问你存在多少组四个点围成的四边形 严格包围某个点P的情况.不存在三点共线. ...

  8. 【极角排序、扫描线】UVa 1606 - Amphiphilic Carbon Molecules(两亲性分子)

    Shanghai Hypercomputers, the world's largest computer chip manufacturer, has invented a new class of ...

  9. UVA 1606 Amphiphilic Carbon Molecules 两亲性分子 (极角排序或叉积,扫描法)

    任意线可以贪心移动到两点上.直接枚举O(n^3),会TLE. 所以采取扫描法,选基准点,然后根据极角或者两两做叉积比较进行排排序,然后扫一遍就好了.旋转的时候在O(1)时间推出下一种情况,总复杂度为O ...

随机推荐

  1. vue-highlightjs的使用小结

    万能的github真主,让我们强大!在vue的项目中想使用highlight.js这样的代码高亮?有人帮助我们实现了vue-highlightjs 安装 yarn add highlight.js - ...

  2. 微信JS-SDK 示例

    微信JS-SDK 示例 1.html部分 <!DOCTYPE html> <!-- saved from url=(0028){sh:$selfUrl} --> <htm ...

  3. Seven Puzzle Aizu - 0121 (搜索)

    7 パズル ime limit1000 ms Memory limit131072 kB OSLinux Source3rd PC Koshien, Final 7 パズルは 8 つの正方形のカードと ...

  4. HDU - 1465 不容易系列之一(错排)

    HDU有个网名叫做8006的男性同学,结交网友无数,最近该同学玩起了浪漫,同时给n个网友每人写了一封信,这都没什么,要命的是,他竟然把所有的信都装错了信封!注意了,是全部装错哟! 现在的问题是:请大家 ...

  5. VS2017生成.net core项目报错:The current .NET SDK does not support targeting .NET Core 2.1. Either

    今天在生成一个项目的时候,生成报错,错误如下:The current .NET SDK does not support targeting .NET Core 2.1.  Either target ...

  6. CodeForces 599E Sandy and Nuts 状压DP

    题意: 有一棵\(n(1 \leq n \leq 13)\)个节点的树,节点的标号为\(1 \sim n\),它的根节点是\(1\). 现在已知它的\(m(0 \leq m < n)\)条边,和 ...

  7. UVa 12167 & HDU 2767 强连通分量 Proving Equivalences

    题意:给出一个有向图,问最少添加几条有向边使得原图强连通. 解法:求出SCC后缩点,统计一下出度为0的点和入度为0的点,二者取最大值就是答案. 还有个特殊情况就是本身就是强连通的话,答案就是0. #i ...

  8. HDU 3394 双连通分量 桥 Railway

    第一个答案是统计图中桥的个数 如果一个点-双连通分量中边的个数大于点的个数那么这个块中所有的边都是冲突的,累加到第二个答案中去. #include <iostream> #include ...

  9. dll和lib关系及使用

    对于dll和lib两者的关系,需要理解的一个概念是编译时和运行时.   lib是编译时的东西,在lib里面包含了方法名和方法所在的dll名字,可以用dumpbin -all XXX.lib查看内容. ...

  10. 编译参数-ObjC的说明

    一些第三方库里对系统库的类加了 category , 这时,就需要使用编译参数: -ObjC ,这样第三方库中对系统类作的扩展方法才能在工程中使用. 但是使用 -Objc 后,会产生两个问题: 1 . ...