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;
#define sz(v) int(v.size())
#define ar array
typedef long long ll;
const int N = 2e5+10, MOD = 998244353;
struct P {
int x, y;
int id;
bool dead;
void read() {
cin >> x >> y;
}
bool operator < (const P& he) const {
return y < he.y;
}
};
int n, sub[N], ans[N];
vector<int> adj[N];
int dfs_sub(int c, int p) {
sub[c] = 1;
for (int nxt : adj[c]) if (nxt != p)
sub[c] += dfs_sub(nxt, c);
return sub[c];
}
void dfs(int c, int p, vector<P>& pts, int l, int r) {
int use = -1;
for (int i = l; i <= r; i++) if (!pts[i].dead)
if (use == -1 || pts[i] < pts[use])
use = i;
assert(use != -1);
ans[pts[use].id] = c; pts[use].dead = 1;
int one = -1, two = -1;
for (int nxt : adj[c]) if (nxt != p) {
if (one == -1) one = nxt;
else two = nxt;
}
if (one == -1) return;
if (two == -1) {
dfs(one, c, pts, l, r);
} else {
int cnt = 0;
for (int m = l; m <= r; m++) if (!pts[m].dead) {
cnt++;
if (cnt == sub[one])
dfs(one, c, pts, l, m), dfs(two, c, pts, m + 1, r);
}
}
}
void solve() {
cin >> n;
for (int i = 0; i < n-1; i++) {
int a, b; cin >> a >> b, --a, --b;
adj[a].push_back(b), adj[b].push_back(a);
}
int root = -1;
for (int i = 0; i < n; i++) if (sz(adj[i]) == 1)
root = i;
dfs_sub(root, -1);
vector<P> pts(n);
for (int i = 0; i < n; i++) {
pts[i].read();
pts[i].id = i, pts[i].dead = 0;
}
dfs(root, -1, pts, 0, n-1);
for (int i = 0; i < n; i++)
cout << ans[i]+1 << ' ';
cout << '\n';
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
int T = 1;
// cin >> T;
while (T--) solve();
}
# | 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... |