1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
#include<bits/stdc++.h> #define re register typedef long long ll; template<class T> inline void read(T &x) { x=0; char ch=getchar(),t=0; while(ch<'0'||ch>'9') t|=ch=='-',ch=getchar(); while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch^48),ch=getchar(); if(t) x=-x; } template<class T,class ...T1> inline void read(T &x,T1 &...x1){ read(x),read(x1...); } template<class T> inline void write(T x) { if(x<0) putchar('-'),x=-x; if(x>9) write(x/10); putchar(x%10+'0'); } template<> inline void write(bool x){ putchar(x?'1':'0'); } template<> inline void write(char c){ putchar(c); } template<> inline void write(char *s){ while(*s!='\0') putchar(*s++); } template<> inline void write(const char *s){ while(*s!='\0') putchar(*s++); } template<class T,class ...T1> inline void write(T x,T1 ...x1){ write(x),write(x1...); } template<class T> inline bool checkMax(T &x,T y){ return x<y?x=y,1:0; } template<class T> inline bool checkMin(T &x,T y){ return x>y?x=y,1:0; } const int MAXN=2e5+10; struct Vec { ll x,y; inline Vec(ll u,ll v):x(u),y(v){}; Vec operator-(const Vec v) const { return Vec(x-v.x,y-v.y); } }O=Vec(-1,-1); const ll cross(Vec u,Vec v){ return u.x*v.y-u.y*v.x; } struct Node {int x,y,id;}Mp[MAXN]; struct G { int next,to; }Edge[MAXN<<1]; int Head[MAXN],Total; int N,Rt,In[MAXN],Siz[MAXN],ans[MAXN],Son[MAXN]; inline void addEdge(int u,int v) { Edge[++Total]=(G){Head[u],v};Head[u]=Total;++In[u]; Edge[++Total]=(G){Head[v],u};Head[v]=Total;++In[v]; } inline bool cmp(Node &u,Node &v) { return cross(Vec(u.x,u.y)-O,Vec(v.x,v.y)-O)>0; } void dfsTree(int x,int last) { Siz[x]=1; for(int e=Head[x],v;e;e=Edge[e].next) { if((v=Edge[e].to)==last) continue; dfsTree(v,x); Siz[x]+=Siz[v]; if(Siz[v]>Siz[Son[x]]) Son[x]=v; } } void solve(int u,std::vector<Node>& a); void Dac(int l,int r,std::vector<Node>&a,std::vector<int>&b) { if(l+1>=r) { if(!a.size()) return ; int u=b[l],v; for(int e=Head[u];e;e=Edge[e].next) { v=Edge[e].to; if(v!=b[r]&&Siz[v]<Siz[u]) break; } O=Vec(Mp[u].x,Mp[u].y); std::nth_element(a.begin(),a.begin(),a.end(),cmp); solve(v,a);return ; } int mid=(Siz[b[l]]-Siz[b[r]])>>1,sl=0,Mid; for(int i=l;i<=r;++i){ sl+=Siz[b[i]]-Siz[b[i+1]]; if(i>l&&sl>mid){Mid=i;break;} } sl=Siz[b[l]]-Siz[b[Mid]]-1; std::vector<Node> L; O=Vec(Mp[b[l]].x,Mp[b[l]].y); std::nth_element(a.begin(),a.begin()+sl,a.end(),cmp); O=Vec(Mp[b[r]].x,Mp[b[r]].y); std::nth_element(a.begin()+sl,a.begin()+sl,a.end(),cmp); for(int i=0;i<sl;++i) { if(cmp(a[i],a[sl])) L.push_back(a[i]); else a.push_back(a[i]); } Mp[b[Mid]]=a[sl]; O=Vec(a[sl].x,a[sl].y); a.erase(a.begin(),a.begin()+sl+1); if((sl-=L.size())>0) { nth_element(a.begin(),a.begin()+sl-1,a.end(),cmp); L.insert(L.end(),a.begin(),a.begin()+sl); a.erase(a.begin(),a.begin()+sl); } Dac(l,Mid,L,b);Dac(Mid,r,a,b); } void solve(int u,std::vector<Node>& a) { Mp[u]=*a.begin();a.erase(a.begin()); O=Vec(Mp[u].x,Mp[u].y); if(!a.size()) return ; std::nth_element(a.begin(),a.end()-1,a.end(),cmp); std::vector<int>b; while(u) b.push_back(u),u=Son[u]; Mp[b.back()]=a.back(); a.pop_back(); Dac(0,b.size()-1,a,b); } int main() { read(N);std::vector<Node>a; for(int i=2,u,v;i<=N;++i) read(u,v),addEdge(u,v); for(int i=1,u,v;i<=N;++i) read(u,v),a.push_back((Node){u,v,i}); for(int i=1;i<=N;++i) if(In[i]<=1){ Rt=i;break; } dfsTree(Rt,0); std::nth_element(a.begin(),a.begin(),a.end(),cmp); solve(Rt,a); for(int i=1;i<=N;++i) ans[Mp[i].id]=i; for(int i=1;i<=N;++i) write(ans[i],' '); }
|