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 ...
随机推荐
- 使用jquery清除select中的所有option
html代码 <select id="search"> <option>baidu</option> <option>sogou&l ...
- 示例vue 的keep-alive缓存功能的实现
本篇文章主要介绍了vue 的keep-alive缓存功能的实现,写的十分的全面细致,具有一定的参考价值,对此有需要的朋友可以参考学习下.如有不足之处,欢迎批评指正. Vue 实现组件信息的缓存 当我们 ...
- W3CPLUS DEMO一些有意思的效果备份
时间轴轮播图: http://www.w3cplus.com/w3cplusDemo/demos/timeline.html css3各种图标效果: http://www.w3cplus.com/w3 ...
- python2和python3,字典和json
Python2的标准数据类型有: Numbers (数字) String (字符串) List (列表) Tuple (元组) Dictionary (字典) Python3的标准数据类型有: Num ...
- HDU 4738 双连通分量 Caocao's Bridges
求权值最小的桥,考虑几种特殊情况: 图本身不连通,那么就不用派人去了 图的边双连通分量只有一个,答案是-1 桥的最小权值是0,但是也要派一个人过去 #include <iostream> ...
- MongoDB学习-->命令行增删改查&JAVA驱动操作Mongodb
MongoDB 是一个基于分布式文件存储的数据库. 由 C++ 语言编写.旨在为 WEB 应用提供可扩展的高性能数据存储解决方案. MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关 ...
- JS实现——贪吃蛇
把以下代码保存成Snake.html文件,使用Google或360浏览器打开 <!DOCTYPE HTML> <html> <head> <meta char ...
- Jquery+Ajax+asp.net+sqlserver-编写的通用邮件管理(源码)
开始 邮件管理通常用在各个内部系统中,为了方便快捷的使用现有的代码开发一个邮件管理系统而诞生的. 准备条件 这是我的设计表结构,大家一看就懂了 --邮件接收表CREATE TABLE [dbo]. ...
- Leetcode25--->Reverse Nodes in k-Group(以k个节点为段,反转单链表)
题目: 给定一个单链表,一次反转k个节点,最终返回翻转后的链表的头节点:如果链表不足k个,则不变 举例: Given this linked list: 1->2->3->4-> ...
- ACM-ICPC 2018 沈阳赛区网络预赛 J树分块
J. Ka Chang Given a rooted tree ( the root is node 11 ) of NN nodes. Initially, each node has zero p ...