This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int inf = 1e9;
const ll infl = 1e18;
mt19937 rng;
using pt = pair<int,int>;
bool ccw(pt A, pt B, pt C) {
return ll(C.second-A.second) * (B.first-A.first) > ll(B.second-A.second) * (C.first-A.first);
}
bool intersect(pt A, pt B, pt C, pt D) {
return ccw(A, C, D) != ccw(B, C, D) && ccw(A, B, C) != ccw(A, B, D);
}
int main() {
int n;
cin >> n;
vector<vector<int>> adj(n);
vector<pair<int,int>> pts(n);
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
u--;v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
map<pair<int,int>, int> ix;
for (auto& [x, y] : pts)
cin >> x >> y;
for (int i = 0; i < n; i++)
ix[pts[i]]= i;
vector<int> res(n, -1);
while (true) {
vector<pt> hull;
sort(pts.begin(), pts.end());
for (int i = 0; i < (int)pts.size();i ++) {
while (hull.size() >= 2) {
if (!ccw(hull.end()[-2], hull.end()[-1], pts[i]))
hull.pop_back();
else break;
}
hull.push_back(pts[i]);
}
auto down = hull;
hull = {};
for (int i = (int)pts.size()-1; i >= 0; i--) {
while (hull.size() >= 2) {
if (!ccw(hull.end()[-2], hull.end()[-1], pts[i]))
hull.pop_back();
else break;
}
hull.push_back(pts[i]);
}
for (int i = 1; i < (int)down.size()-1; i++) {
hull.push_back(down[i]);
}
auto dfs = [&](auto& dfs, int i, int p)->void{
res[i] = ix[hull.back()];
hull.pop_back();
for (int e : adj[i]) {
if (e == p) continue;
dfs(dfs, e, i);
}
};
dfs(dfs, 0, -1);
break;
}
vector<int> byp(n);
for (int i = 0; i < n; i++)
assert(res[i] >= 0),
byp[res[i]] = i;
for (int i : byp) {
cout << i+1 << ' ';
}
cout << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |