QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#480756#5439. Meet in the MiddleduankaidiWA 3795ms88752kbC++235.2kb2024-07-16 18:37:002024-07-16 18:37:00

Judging History

你现在查看的是最新测评结果

  • [2024-07-16 18:37:00]
  • 评测
  • 测评结果:WA
  • 用时:3795ms
  • 内存:88752kb
  • [2024-07-16 18:37:00]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
const int N = 2E5 + 5, M = 5E5 + 5;
int n, q, m;
vector<array<int, 2>> vec[N], adj[N];
int head[N], nex[N << 1], to[N << 1], from[N << 1], dis[N << 1], tot = 1;
i64 Ans[M];
void ad(int x, int y, int z) {
	to[++tot] = y; dis[tot] = z; from[tot] = x;
	nex[tot] = head[x]; head[x] = tot;
}
void add(int x, int y, int z) {
	ad(x, y, z); ad(y, x, z);
}
void rebuild(int x, int fa) {
	int temp = 0, len = (int)adj[x].size(), last;
	for (auto [v, w] : adj[x]) if (v != fa) {
		++temp; if (temp == 1) {	
			add(x, v, w);
			last = x;
		} else if (temp == len - (x != 1)) {
			add(last, v, w);
		} else {
			++m; add(last, m, 0);
			last = m; add(m, v, w);
		}
	}
	for (auto [v, w] : adj[x]) if (v != fa) rebuild(v, x);
}
i64 d1[N];
const int K = 20;
struct VirtualTree {
	vector<array<int, 2>> adj[N];
	void add(int x, int y, int z) {
		adj[x].push_back({y, z});
		adj[y].push_back({x, z});
	}
	int st[N][K], dfn[N], tot;
	i64 dep[N];
	int get(int x, int y) {return dfn[x] < dfn[y] ? x : y;}
	void dfs(int x, int fa) {
		st[dfn[x] = ++tot][0] = fa;
		for (auto [v, w] : adj[x]) if (v != fa) {dep[v] = dep[x] + w; dfs(v, x);}
	}
	int getlca(int u, int v) {
		if (u == v) return u;
		if ((u = dfn[u]) > (v = dfn[v])) swap(u, v);
		int d = __lg(v - u++);
		return get(st[u][d], st[v - (1 << d) + 1][d]);
	}
	void init() {
		dfs(1, 0);
		for (int j = 1; j <= __lg(n * 2); ++j)
			for (int i = 1; i + (1 << j) - 1 <= 2 * n; ++i)
				st[i][j] = get(st[i][j - 1], st[i + (1 << j - 1)][j - 1]);
		
	}
	i64 dist(int x, int y) {
		i64 val = 0;
		if (x > n) val += d1[x - n];
		if (y > n) val += d1[y - n];
		return val + dep[x] + dep[y] - 2 * dep[getlca(x, y)];
	}
	vector<array<i64, 2>> e[N];
	void conn(int x, int y, i64 w) {
		// cerr << "connect: " << x << ' ' << y << ' ' << w << '\n';
		e[x].push_back({y, w});
		e[y].push_back({x, w});
	}
	vector<int> id;
	void build(vector<int> &h) {
		sort(h.begin(), h.end(), [&](int x, int y) {return dfn[x] < dfn[y];});
		vector<int> a = h; int len = h.size();
		for (int i = 0; i + 1 < len; ++i) a.push_back(getlca(h[i], h[i + 1]));
		sort(a.begin(), a.end(), [&](int x, int y) {return dfn[x] < dfn[y];});
		a.erase(unique(a.begin(), a.end()), a.end());
		// cerr << "A: "; for (auto x : a) cerr << x << ' '; cerr << '\n';
		len = a.size();
		for (int i = 0; i + 1 < len; ++i) {
			int lca = getlca(a[i], a[i + 1]);
			conn(lca, a[i + 1], dist(lca, a[i + 1]));
		} id = a;
	}
	bool vis[N]; i64 dis[2][N];
	int dot1, dot2;
	void getdiameter() {
		int t = id[0];
		auto bfs = [&](int st, i64 *dis) {
			for (auto x : id) vis[x] = 0;
			queue<int> q; q.push(st); dis[st] = 0; vis[st] = 1;
			while (!q.empty()) {
				int u = q.front(); q.pop();
				for (auto [v, w] : e[u]) {
					if (!vis[v]) {
						dis[v] = dis[u] + w;
						vis[v] = 1;
						q.push(v);
					}
				}
			}
		} ;
		bfs(t, dis[0]);
		int s = t;
		for (auto x : id) if (dis[0][x] > dis[0][s]) s = x;
		bfs(t = s, dis[0]);
		for (auto x : id) if (dis[1][x] > dis[1][t]) t = x;
		bfs(t, dis[1]);
		dot1 = s, dot2 = t;
		// cerr << "Diameter: " << dot1 << ' ' << dot2 << '\n';
	}
	i64 query(int x) {
		return max(dist(x, dot1), dist(x, dot2));
	}
	void clear() {
		for (auto x : id) dis[0][x] = dis[1][x] = 0;
		dot1 = dot2 = 0;
		id.clear();
	}
} f;
int dsum;
int rt, dmx, sz[N];
bool vis[N << 1];
void getroot(int x, int fa) {
	sz[x] = 1;
	for (int i = head[x]; i; i = nex[i]) {
		int v = to[i];
		if (v == fa || vis[i >> 1]) continue;
		getroot(v, x); sz[x] += sz[v];
		int temp = max(sz[v], dsum - sz[v]);
		if (dmx > temp) {
			dmx = temp;
			rt = i;
		}
	}
}
vector<int> dots;
void dfs(int x, int fa) {
	if (x <= n) dots.push_back(x + n);
	for(int i = head[x]; i; i = nex[i]) {
		int v = to[i];
		if (v == fa || vis[i >> 1]) continue;
		d1[v] = d1[x] + dis[i];
		dfs(v, x);
	}
}
void calc(int x, int fa, i64 dep) {
	if (x <= n) for (auto [b, i] : vec[x]) {
		// cerr << x << ' ' << fa << ' ' << dep << ' ' << b << ' ' << f.query(b) << " query\n";
		Ans[i] = max(Ans[i], f.query(b) + dep);
	}
	for (int i = head[x]; i; i = nex[i]) {
		int v = to[i];
		if (vis[i >> 1] || v == fa) continue;
		calc(v, x, dep + dis[i]);
	}
}
void work(int u, int v, int w) {
	// cerr << u << ' ' << v << ' ' << w << " !\n";
	d1[u] = w; dfs(u, v); f.build(dots);
	f.getdiameter(); calc(v, u, 0); dots.clear(); f.clear();
}
void solve(int x, int nsum) {
	rt = 0; dmx = 1E9; dsum = nsum;
	getroot(x, 0); if (!rt) return ;
	vis[rt / 2] = 1;
	int u = from[rt], v = to[rt], w = dis[rt];
	work(u, v, w); work(v, u, w);
	solve(u, nsum - sz[u]); solve(v, sz[v]);
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cin >> n >> q;
	for (int i = 1; i < n; ++i) {
		int u, v, w; cin >> u >> v >> w;
		adj[u].push_back({v, w}); adj[v].push_back({u, w});
	}
	for (int i = 1; i < n; ++i) {
		int u, v, w; cin >> u >> v >> w;
		f.add(u, v, w);
	}
	for (int i = 1; i <= n; ++i) f.add(i, i + n, 0);
	f.init();
	for (int i = 1; i <= q; ++i) {
		int a, b; cin >> a >> b;
		vec[a].push_back({b, i});
	}
	m = n;
	rebuild(1, 0);
	solve(1, n);
	for (int i = 1; i <= q; ++i) cout << Ans[i] << '\n';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 24104kb

input:

3 4
1 2 1
2 3 2
1 2 2
2 3 1
1 1
1 2
2 1
2 2

output:

6
4
5
3

result:

ok 4 number(s): "6 4 5 3"

Test #2:

score: 0
Accepted
time: 0ms
memory: 24112kb

input:

2 1
1 2 1
1 2 1
1 1

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: 0
Accepted
time: 0ms
memory: 26372kb

input:

2 1
1 2 1
1 2 1
1 2

output:

1

result:

ok 1 number(s): "1"

Test #4:

score: -100
Wrong Answer
time: 3795ms
memory: 88752kb

input:

10000 50000
8101 5996 108427744
5996 7605 870838849
5996 5599 603303696
8101 3006 339377417
8101 6463 442516687
6463 5560 109174079
5560 4063 127596224
3006 1682 947915262
5996 1986 130416311
6463 5455 279771516
6463 2634 516688796
4063 3034 217886863
7605 5092 742375061
5599 2266 193804402
5092 140...

output:

647838384844
360216645408
387098802897
637066393138
456072330399
645842915960
641537859258
400174767683
459407279331
573316121269
674370549986
434572336505
435282975629
620172136910
553499885160
526743824759
471341504556
689550247042
549161302822
498449365451
396706495314
393003299495
701575393972
6...

result:

wrong answer 2nd numbers differ - expected: '626539793176', found: '360216645408'