1 条题解
-
0

#include <bits/stdc++.h> using std::cin; using std::cout; typedef long long ll; typedef std::pair <ll, int> pr; const int N = 1000054, M = N / 2; int n_leaf, n_branch, V; int p[N], fc[N], nc[N], deg[N], w[N]; int o[M], ans[M]; ll deep[N], Short[N]; char s[M][12]; std::priority_queue <pr, std::vector <pr>, std::greater <pr>> pq; inline void link(int x, int px) {p[x] = px, nc[x] = fc[px], fc[px] = x, ++deg[px];} inline void down(ll &x, const ll y) {x > y ? x = y : 0;} void dfs(int x) { int y; Short[x] = LLONG_MAX; for (y = fc[x]; y; y = nc[y]) deep[y] = deep[x] + w[y], dfs(y), down(Short[x], Short[y]); if (!deg[x]) Short[x] = 0; Short[x] += w[x]; } int main() { int i, u, v, x, $; ll L; std::ios::sync_with_stdio(false), cin.tie(NULL); cin >> n_leaf >> n_branch, V = n_leaf + n_branch + 1; for (i = 0, u = n_branch + 2; i < n_leaf; ++i, ++u) cin >> s[i] >> v >> w[u], link(u, v + 1); for (i = 1; i <= n_branch; ++i) cin >> v >> w[i + 1], link(i + 1, v + 1); dfs(1); std::iota(o, o + n_leaf, n_branch + 2); std::sort(o, o + n_leaf, [] (const int x, const int y) {return deep[x] < deep[y];}); for (i = n_branch + 2; i <= V; ++i) pq.emplace(Short[i], i); for ($ = 0; $ < n_leaf; ++$) { x = o[$], L = deep[x]; for (; !pq.empty() && pq.top().first <= L; ) { u = pq.top().second, pq.pop(); if (!--deg[v = p[u]] && v != 1) pq.emplace(Short[v], v); } ans[x - n_branch - 2] = pq.size() + 1; } for (i = 0; i < n_leaf; ++i) cout << s[i] << ' ' << ans[i] << '\n'; return 0; }
- 1
信息
- ID
- 6278
- 时间
- 9000ms
- 内存
- 512MiB
- 难度
- 10
- 标签
- 递交数
- 1
- 已通过
- 1
- 上传者