codeforces Plant Tree
来源:互联网 发布:显示登录成功和链接php 编辑:程序博客网 时间:2024/06/05 00:35
大神思路
http://www.mamicode.com/info-detail-1717020.html
#include <bits/stdc++.h>using namespace std;typedef long long LL;pair<LL, LL> ori;struct Point { LL x, y; LL id; bool operator < (const Point & b) const { return (x-ori.first)*(b.y-ori.second) - (y-ori.second)*(b.x-ori.first) > 0; //极角排序,根据与x轴的角度逆时针排序。角度越小在前面 }} p[1600];LL n;vector<LL> G[1600];LL sz[1600];LL ans[1600];void dfs1(LL u, LL fa) { sz[u] = 1; for(LL i = 0; i < G[u].size(); i++) { LL v = G[u][i]; if(v == fa) continue; dfs1(v, u); sz[u] += sz[v]; //得出每一个点后面有多少长度。做法是为了缩短时间 }}void dfs2(LL u,LL fa,LL s,LL e){ int pos; LL x,y; x=y=1e10; for(int i=s;i<=e;i++) if(p[i].x<x||((p[i].x==x)&&(p[i].y<y))) { x=p[i].x,y=p[i].y,pos=i; } ori.first=x,ori.second=y; swap(p[s],p[pos]); //把角度最小的点作为起点 sort(p+s+1,p+e+1); //给每次要用到的这一段排序。 ans[p[s].id] = u; //开始的坐标位置应该放第几个点。其实从头到尾输入结果坐 int cnt=0;//每次定义为0,证明是 for(LL i=0;i<G[u].size();i++) { if(G[u][i]==fa) continue; LL v=G[u][i]; dfs2(v,u,s+1+cnt,s+1+cnt+sz[v]-1); //下一个点,和下一个点结束的位置 是一个左右双闭区间 cnt+=sz[v]; }}int main(){ scanf("%I64d",&n); for(int i=0;i<n-1;i++) { LL u,v; scanf("%lld%lld",&u,&v); u--,v--; G[u].push_back(v),G[v].push_back(u); } for(LL i=0;i<n;i++) scanf("%lld%lld",&p[i].x,&p[i].y),p[i].id=i; dfs1(0,-1); dfs2(0,-1,0,n-1); for(LL i=0;i<n;i++) printf("%I64d%c",ans[i]+1,i==n-1?'\n':' ' );}
0 0
- codeforces Plant Tree
- Plant CodeForces
- Codeforces 185A Plant
- codeforces 185A Plant
- CodeForces 185A - Plant
- CodeForces - 185A Plant
- CodeForces 185A Plant
- Codeforces 185 A Plant
- codeforces 186C Plant
- codeforces plant 矩阵快速幂
- Plant
- Plant
- Plant
- Best time to plant a tree
- CodeForces Round #118 (185A) - Plant
- 【快速幂】 CodeForces 185A Plant
- CodeForces - 185A Plant 矩阵快速幂
- Codeforces 185A Plant 普通快速幂取模
- IO流文件的复制删除
- R语言常用函数总结大全
- 线线几何位置关系
- 三种常用排序算法(冒泡、选择、快速)的Java实现
- JQueryEasyUI 组件 布局 Layout组件
- codeforces Plant Tree
- 关于我
- Debug调试
- Java子类与父类之间的对象转换
- (邻接表构图-网络寻路题解
- 135. Candy
- 数据结构—树与二叉树篇I
- Hibernate 关联关系
- 136. Single Number