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;
int r = 0;
for (; r < n; r++)
if (adj[r].size() < 3) break;
vector<int> res(n, -1);
vector<int> subs(n, 1);
auto dfs_sz = [&](auto& dfs_sz, int i, int p=-1)->void {
for (int e : adj[i]) if (e != p) {
dfs_sz(dfs_sz, e, i);
subs[i] += subs[e];
}
};
dfs_sz(dfs_sz, r);
auto dfs = [&](auto& dfs, vector<pair<int,int>>& pts, int i, int p =-1)->void {
vector<int> ch;
for (int e : adj[i]) {
if (e != p) ch.push_back(e);
}
if (ch.empty()) {
assert(pts.empty());
return;
}
if (ch.size() == 1) {
res[ch[0]] = ix[pts.front()];
pts.erase(pts.begin());
dfs(dfs, pts, ch[0], i);
} else {
sort(pts.begin(), pts.end(), [](auto& p, auto& q)->bool {
return (ll)p.second * q.first > (ll) q.second * p.first;
});
assert(pts.size() == subs[ch[0]] + subs[ch[1]]);
vector p1(pts.begin(), pts.begin() + subs[ch[0]]);
vector p2(pts.begin() + subs[ch[0]], pts.end());
sort(p1.begin(), p1.end());
sort(p2.begin(), p2.end());
res[ch[0]] = ix[p1.front()];
res[ch[1]] = ix[p2.front()];
p1.erase(p1.begin());
p2.erase(p2.begin());
dfs(dfs, p1, ch[0], i);
dfs(dfs, p2, ch[1], i);
}
};
sort(pts.begin(), pts.end());
res[r] = ix[pts.front()];
pts.erase(pts.begin());
dfs(dfs, pts, r);
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;
}
Compilation message (stderr)
In file included from /usr/include/c++/10/cassert:44,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
from Main.cpp:1:
Main.cpp: In instantiation of 'main()::<lambda(auto:24&, std::vector<std::pair<int, int> >&, int, int)> [with auto:24 = main()::<lambda(auto:24&, std::vector<std::pair<int, int> >&, int, int)>]':
Main.cpp:85:20: required from here
Main.cpp:68:31: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} [-Wsign-compare]
68 | assert(pts.size() == subs[ch[0]] + subs[ch[1]]);
# | 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... |