Codeforces Round #625 (Div. 1, based on Technocup 2020 Final Round) E. Treeland and Viruses
首先建出虚树
然后就相当于多源最短路,权值为第一关键字轮数和第二关键字下标
然后dijkstra即可
建虚树过程是在维护右链,注意要把所有用到的点都初始化一遍。
#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define pii pair<int, int>
#define pli pair<ll, int>
#define lp p << 1
#define rp p << 1 | 1
#define mid ((l + r) / 2)
#define lowbit(i) ((i) & (-i))
#define ll long long
#define ull unsigned long long
#define db double
#define rep(i,a,b) for(int i=a;i<b;i++)
#define per(i,a,b) for(int i=b-1;i>=a;i--)
#define Edg int ccnt=1,head[N],to[E],ne[E];void addd(int u,int v){to[++ccnt]=v;ne[ccnt]=head[u];head[u]=ccnt;}void add(int u,int v){addd(u,v);addd(v,u);}
#define Edgc int ccnt=1,head[N],to[E],ne[E],c[E];void addd(int u,int v,int w){to[++ccnt]=v;ne[ccnt]=head[u];c[ccnt]=w;head[u]=ccnt;}void add(int u,int v,int w){addd(u,v,w);addd(v,u,w);}
#define es(u,i,v) for(int i=head[u],v=to[i];i;i=ne[i],v=to[i])
const int MOD = 1e9 + 7;
void M(int &x) {if (x >= MOD)x -= MOD; if (x < 0)x += MOD;}
int qp(int a, int b = MOD - 2) {int ans = 1; for (; b; a = 1LL * a * a % MOD, b >>= 1)if (b & 1)ans = 1LL * ans * a % MOD; return ans % MOD;}
template<class T>T gcd(T a, T b) { while (b) { a %= b; std::swap(a, b); } return a; }
template<class T>bool chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; }
template<class T>bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
char buf[1 << 21], *p1 = buf, *p2 = buf;
inline char getc() {
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++;
}
inline int _() {
int x = 0, f = 1; char ch = getc();
while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getc(); }
while (ch >= '0' && ch <= '9') { x = x * 10ll + ch - 48; ch = getc(); }
return x * f;
}
const int N = 2e5 + 7, E = 4e5 + 7;
int n, q;
int dep[N], in[N], dfn, fa[N][22];
int Lca(int u, int v) {
if (dep[u] < dep[v]) std::swap(u, v);
int dif = dep[u] - dep[v];
per (i, 0, 20) if (dif >> i & 1) u = fa[u][i];
if (u == v) return u;
per (i, 0, 20) if (fa[u][i] != fa[v][i]) u = fa[u][i], v = fa[v][i];
return fa[u][0];
}
namespace XX {
Edg
int key[N], speed[N], query[N], k, m, point[E], cnt, st[N], top;
int used[N], tol;
bool cmp(int x, int y) { return in[x] < in[y]; }
void vir() {
std::sort(point + 1, point + 1 + cnt, cmp);
cnt = std::unique(point + 1, point + 1 + cnt) - point - 1;
st[top = 1] = 1;
used[tol = 1] = 1;
rep (i, 1, cnt + 1) {
if (point[i] == 1) continue;
used[++tol] = point[i];
int lca = Lca(point[i], st[top]);
if (lca != st[top]) {
while (top > 1 && in[st[top - 1]] >= in[lca])
add(st[top], st[top - 1]), top--;
if (st[top] != lca)
add(lca, st[top]), st[top] = lca, used[++tol] = lca;
}
st[++top] = point[i];
}
rep (i, 1, top) add(st[i], st[i + 1]);
}
struct Node {
int round, index, d;
Node() {}
Node(int d, int x): d(d), index(x) {
round = d ? (d - 1) / speed[index] : -1;
}
bool operator < (const Node &p) const {
return round == p.round ? index < p.index : round < p.round;
}
} dis[N];
bool done[N];
void dijkstra() {
#define pa pair<Node, int>
std::priority_queue<std::pa, std::vector<std::pa>, std::greater<std::pa> > que;
rep (i, 1, tol + 1) dis[used[i]].round = dis[used[i]].index = N, done[used[i]] = 0;
rep (i, 1, k + 1) {
int u = key[i];
dis[u] = Node(0, i), que.push(std::pa(dis[u], u));
}
while (!que.empty()) {
int u = que.top().se; que.pop();
if (done[u]) continue;
done[u] = 1;
es (u, i, v) {
Node nxt = Node(dis[u].d + std::abs(dep[u] - dep[v]), dis[u].index);
if (nxt < dis[v]) dis[v] = nxt, que.push(std::pa(dis[v], v));
}
}
rep (i, 1, m + 1) {
printf("%d%c", dis[query[i]].index, " \n"[i == m]);
}
rep (i, 1, tol + 1) head[used[i]] = 0;
ccnt = 1;
}
void solve() {
cnt = 0;
k = _(), m = _();
rep (i, 1, k + 1) {
key[i] = _(), speed[i] = _();
point[++cnt] = key[i];
}
rep (i, 1, m + 1) query[i] = _(), point[++cnt] = query[i];
vir();
dijkstra();
}
}
Edg
void dfs(int u, int f) {
in[u] = ++dfn;
dep[u] = dep[f] + 1;
fa[u][0] = f;
rep (i, 1, 20) if (fa[u][i - 1]) fa[u][i] = fa[fa[u][i - 1]][i - 1];
es (u, i, v) if (v != f) dfs(v, u);
}
int main() {
#ifdef LOCAL
freopen("ans.out", "w", stdout);
#endif
n = _();
rep (i, 1, n) {
int u = _(), v = _();
add(u, v);
}
dfs(1, 0);
q = _();
while (q--) XX::solve();
return 0;
}
/*
10
2 4
7 8
6 3
9 2
6 8
7 1
9 8
9 10
5 4
10
1 8
8 1
10 2 3 4 1 9 8 5
8 1
9 2
3 1
10 4
8 5
5 3
6 2
4 2
2 4
9
8 5
7 1
9 2
5 1
2 5
10 3
4 4
6 1
1 1
2 8 6 4 10
4 2
6 5
4 3
1 2
3 1
8 1
8 2
7 5
8 3
6 2
5 3
3 5
9 3
10 1
4 1
6 1
3 4
1 2
7 3
8 1
7 1 8 6
2 7
5 5
4 4
7 3 9 8 6 4 2
4 7
4 2
9 2
2 2
3 3
2 9 10 7 5 6 3
6 9
4 4
5 4
9 1
2 3
7 5
10 2
6 3 9 10 4 8 2 5 7
4 8
4 4
6 1
3 3
2 5
3 4 10 8 6 1 9 7
*/

浙公网安备 33010602011771号