Codeforces 196C Paint Tree(贪心+极角排序)
题目链接 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(贪心+极角排序)的更多相关文章
- 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 598C C. Nearest vectors(极角排序)
题目链接: C. Nearest vectors time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- Educational Codeforces Round 1 C. Nearest vectors 极角排序
Partial Tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/598/problem/ ...
- 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 70D 动态凸包 (极角排序 or 水平序)
题目链接:http://codeforces.com/problemset/problem/70/D 本题关键:在log(n)的复杂度内判断点在凸包 或 把点插入凸包 判断:平衡树log(n)内选出点 ...
- codeforces 1284E. New Year and Castle Construction(极角排序+扫描枚举)
链接:https://codeforces.com/problemset/problem/1284/E 题意:平面上有n个点,问你存在多少组四个点围成的四边形 严格包围某个点P的情况.不存在三点共线. ...
- 【极角排序、扫描线】UVa 1606 - Amphiphilic Carbon Molecules(两亲性分子)
Shanghai Hypercomputers, the world's largest computer chip manufacturer, has invented a new class of ...
- UVA 1606 Amphiphilic Carbon Molecules 两亲性分子 (极角排序或叉积,扫描法)
任意线可以贪心移动到两点上.直接枚举O(n^3),会TLE. 所以采取扫描法,选基准点,然后根据极角或者两两做叉积比较进行排排序,然后扫一遍就好了.旋转的时候在O(1)时间推出下一种情况,总复杂度为O ...
随机推荐
- python入门:用户登录,三次错误机会
#!/usr/bin/env python # -*- coding:utf-8 -*- #用户登录,三次机会重试 #主要分为两个部分,一部分是写三次循环,一部分写用户输入 #用户登录的实现,循环3次 ...
- linux SVN安装及配置教程
1.环境centos6.4 2.安装svnyum -y install subversion 3.配置 建立版本库目录mkdir /www/svndata svnserve -d -r /www/sv ...
- AD采样求平均STM32实现
iADC_read(, &u16NTC_1_Sample_Val_ARR[]); == ui8FirstSampleFlag) { ; i<; i++) { u16NTC_1_Sampl ...
- selenium2通过linkText/partialLinkText定位元素
通过linkText定位 linkText是根据链接的文本来定位,如下图,导航上全是链接 此时我想找“新闻”这个元素,那么我就可以使用linkText方式定位,语法: By.linkText(“新闻” ...
- java事物
[引用:http://www.cnblogs.com/kristain/articles/2038397.html] 一.什么是事务 事务是访问数据库的一个操作序列,数据库应用系统通过事务集来完成对数 ...
- jenkins 之 Android 打包及上传至蒲公英
准备条件 iMAC,非必须(如果是 安卓 和 苹果 可以在同一台电脑上打包则要 Mac OS 系统的电脑,如果是只是给安卓打包 windows 电脑也是可以的, window 下 需要把 ls 换成 ...
- luogu2865 [USACO06NOV]路障Roadblocks 次短路
注意:如果是这么个写法,堆数组要开成n+m的. 为什么呢?设想一下从1到2有m条长度递减的路,这岂不是要入队m次-- #include <algorithm> #include <i ...
- jquery如何获取某一个兄弟节点
$('#id').siblings() 当前元素所有的兄弟节点 $('#id').prev() 当前元素前一个兄弟节点 $('#id').prevaAll() 当前元素之前所有的兄弟节点 $('#id ...
- 浅谈我所见的CSS命名风格
在两年工作中,总结一下我所见的css命名风格. 1.单一class命名 .header { width: 500px; } .item { text-indent: 20%; } 优点:简单,渲染效率 ...
- C++ STL 的初步认知
学无止境!!! 尊重他人劳动,尊重出处:http://www.cnblogs.com/shiyangxt/archive/2008/09/11/1289493.html 我已经做了4年的MFC ...