Submission #961127

# Submission time Handle Problem Language Result Execution time Memory
961127 2024-04-11T14:17:33 Z Pring Election Campaign (JOI15_election_campaign) C++17
100 / 100
84 ms 44656 KB
#include <bits/stdc++.h>
#pragma GCC optimize("O3","unroll-loops")
#pragma GCC target("avx2","popcnt","sse4","abm")
using namespace std;

#ifdef MIKU
string dbmc = "\033[1;38;2;57;197;187m", dbrs = "\033[0m";
#define debug(x...) cout << dbmc << "[" << #x << "]: ", dout(x)
void dout() { cout << dbrs << endl; }
template <typename T, typename ...U>
void dout(T t, U ...u) { cout << t << (sizeof...(u) ? ", " : ""); dout(u...); }
#else 
#define debug(...) 39
#endif

#define fs first
#define sc second
#define mp make_pair
#define FOR(i, j, k) for (int i = j, Z = k; i < Z; i++)
using ll = long long;
typedef pair<int, int> pii;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;

#include<cstdio>
inline char readchar(){
	const int S = 1<<16;
	static char buf[S], *p = buf, *q = buf;
	return p == q and (q = (p = buf) + fread(buf, 1, S, stdin)) == buf ? EOF : *p++;
}

inline int R(){
	int ans = 0; char c = readchar();
	while((c < '0' or c > '9') and c != EOF) c = readchar();
	// if(c == '-') minus = true, c = readchar();
	while(c >= '0' and c <= '9') ans = ans * 10 + (c ^ '0'), c = readchar();
	// return minus ? -ans : ans;
	return ans;
}

const int MXN = 100005;
int n, m, a[MXN], b[MXN], c[MXN];
vector<int> lenid[MXN];
int lca[MXN], dfn[MXN];
ll dp[MXN];

struct BIT {
	int n;
	ll val[MXN];
	void init(int _n) {
		n = _n;
		fill(val + 1, val + n + 1, 0);
	}
	void modify(int id, int v) {
		for (; id <= n; id += (id & -id)) val[id] += v;
	}
	ll query(int id) {
		ll ans = 0;
		for (; id > 0; id -= (id & -id)) ans += val[id];
		return ans;
	}
	ll query(int l, int r) {
		return query(r) - query(l - 1);
	}
} B;

namespace TREE {
	int p[MXN], d[MXN], sz[MXN], son[MXN], top[MXN], C = 1;
	vector<int> edge[MXN];
	void ADD_EDGE(int x, int y) {
		edge[x].push_back(y);
		edge[y].push_back(x);
	}
	void DFS(int id, int par, int dep) {
		p[id] = par;
		d[id] = dep;
		sz[id] = 1;
		son[id] = -1;
		for (auto &i : edge[id]) {
			if (i == par) continue;
			DFS(i, id, dep + 1);
			sz[id] += sz[i];
			if (son[id] == -1 || sz[son[id]] < sz[i]) son[id] = i;
		}
	}
	void HLD(int id, int par, int _t) {
		top[id] = _t;
		dfn[id] = C++;
		if (son[id] != -1) HLD(son[id], id, _t);
		for (auto &i : edge[id]) {
			if (i == par) continue;
			if (i == son[id]) continue;
			HLD(i, id, i);
		}
	}
	void BUILD() {
		DFS(1, 0, 0);
		HLD(1, 0, 1);
	}
	int LCA(int x, int y) {
		while (top[x] != top[y]) {
			if (d[top[x]] < d[top[y]]) swap(x, y);
			x = p[top[x]];
		}
		return (d[x] > d[y] ? y : x);
	}
}

ll JUMP(int rt, int x) {
	using namespace TREE;
	ll ans = 0;
	while (top[x] != top[rt]) {
		ans += B.query(dfn[top[x]], dfn[x]);
		x = p[top[x]];
	}
	return ans + B.query(dfn[rt] + 1, dfn[x]);
}

void DP(int id, int par) {
	ll sum = 0;
	for (auto &i : TREE::edge[id]) {
		if (i == par) continue;
		DP(i, id);
		sum += dp[i];
	}
	dp[id] = sum;
	for (auto &e : lenid[id]) {
		int l = JUMP(id, a[e]), r = JUMP(id, b[e]);
		ll now = sum + c[e] - l - r;
		dp[id] = max(dp[id], now);
	}
	B.modify(dfn[id], dp[id] - sum);
}

void miku() {
	int x, y;
	// cin >> n;
	n = R();
	FOR(i, 1, n) {
		// cin >> x >> y;
		x = R();
		y = R();
		TREE::ADD_EDGE(x, y);
	}
	TREE::BUILD();
	// cin >> m;
	m = R();
	FOR(i, 0, m) {
		// cin >> a[i] >> b[i] >> c[i];
		a[i] = R();
		b[i] = R();
		c[i] = R();
		lca[i] = TREE::LCA(a[i], b[i]);
		lenid[lca[i]].push_back(i);
	}
	B.init(n);
	DP(1, 0);
	// cout << dp[1] << '\n';
	printf("%lld\n", dp[1]);
}

int32_t main() {
	// cin.tie(0) -> sync_with_stdio(false);
	// cin.exceptions(cin.failbit);
	miku();
	return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 2 ms 9304 KB Output is correct
2 Correct 2 ms 9304 KB Output is correct
3 Correct 2 ms 9308 KB Output is correct
4 Correct 2 ms 9308 KB Output is correct
5 Correct 24 ms 12828 KB Output is correct
6 Correct 34 ms 42316 KB Output is correct
7 Correct 37 ms 32084 KB Output is correct
8 Correct 17 ms 13148 KB Output is correct
9 Correct 34 ms 25944 KB Output is correct
10 Correct 18 ms 13044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9308 KB Output is correct
2 Correct 3 ms 9308 KB Output is correct
3 Correct 3 ms 9560 KB Output is correct
4 Correct 56 ms 44416 KB Output is correct
5 Correct 53 ms 44564 KB Output is correct
6 Correct 52 ms 44656 KB Output is correct
7 Correct 66 ms 44400 KB Output is correct
8 Correct 56 ms 44624 KB Output is correct
9 Correct 64 ms 44652 KB Output is correct
10 Correct 60 ms 44372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9308 KB Output is correct
2 Correct 3 ms 9308 KB Output is correct
3 Correct 3 ms 9560 KB Output is correct
4 Correct 56 ms 44416 KB Output is correct
5 Correct 53 ms 44564 KB Output is correct
6 Correct 52 ms 44656 KB Output is correct
7 Correct 66 ms 44400 KB Output is correct
8 Correct 56 ms 44624 KB Output is correct
9 Correct 64 ms 44652 KB Output is correct
10 Correct 60 ms 44372 KB Output is correct
11 Correct 4 ms 10276 KB Output is correct
12 Correct 58 ms 44528 KB Output is correct
13 Correct 55 ms 44376 KB Output is correct
14 Correct 53 ms 44644 KB Output is correct
15 Correct 59 ms 44372 KB Output is correct
16 Correct 53 ms 44628 KB Output is correct
17 Correct 56 ms 44468 KB Output is correct
18 Correct 55 ms 44596 KB Output is correct
19 Correct 58 ms 44596 KB Output is correct
20 Correct 60 ms 44616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 72 ms 14288 KB Output is correct
2 Correct 56 ms 44552 KB Output is correct
3 Correct 65 ms 33104 KB Output is correct
4 Correct 36 ms 14808 KB Output is correct
5 Correct 69 ms 31356 KB Output is correct
6 Correct 43 ms 14808 KB Output is correct
7 Correct 66 ms 30808 KB Output is correct
8 Correct 66 ms 14896 KB Output is correct
9 Correct 55 ms 44636 KB Output is correct
10 Correct 68 ms 27372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9304 KB Output is correct
2 Correct 2 ms 9304 KB Output is correct
3 Correct 2 ms 9308 KB Output is correct
4 Correct 2 ms 9308 KB Output is correct
5 Correct 24 ms 12828 KB Output is correct
6 Correct 34 ms 42316 KB Output is correct
7 Correct 37 ms 32084 KB Output is correct
8 Correct 17 ms 13148 KB Output is correct
9 Correct 34 ms 25944 KB Output is correct
10 Correct 18 ms 13044 KB Output is correct
11 Correct 2 ms 9308 KB Output is correct
12 Correct 2 ms 9820 KB Output is correct
13 Correct 2 ms 9564 KB Output is correct
14 Correct 2 ms 9308 KB Output is correct
15 Correct 2 ms 9308 KB Output is correct
16 Correct 2 ms 9564 KB Output is correct
17 Correct 2 ms 9308 KB Output is correct
18 Correct 2 ms 9564 KB Output is correct
19 Correct 2 ms 9308 KB Output is correct
20 Correct 2 ms 9820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9304 KB Output is correct
2 Correct 2 ms 9304 KB Output is correct
3 Correct 2 ms 9308 KB Output is correct
4 Correct 2 ms 9308 KB Output is correct
5 Correct 24 ms 12828 KB Output is correct
6 Correct 34 ms 42316 KB Output is correct
7 Correct 37 ms 32084 KB Output is correct
8 Correct 17 ms 13148 KB Output is correct
9 Correct 34 ms 25944 KB Output is correct
10 Correct 18 ms 13044 KB Output is correct
11 Correct 2 ms 9308 KB Output is correct
12 Correct 3 ms 9308 KB Output is correct
13 Correct 3 ms 9560 KB Output is correct
14 Correct 56 ms 44416 KB Output is correct
15 Correct 53 ms 44564 KB Output is correct
16 Correct 52 ms 44656 KB Output is correct
17 Correct 66 ms 44400 KB Output is correct
18 Correct 56 ms 44624 KB Output is correct
19 Correct 64 ms 44652 KB Output is correct
20 Correct 60 ms 44372 KB Output is correct
21 Correct 4 ms 10276 KB Output is correct
22 Correct 58 ms 44528 KB Output is correct
23 Correct 55 ms 44376 KB Output is correct
24 Correct 53 ms 44644 KB Output is correct
25 Correct 59 ms 44372 KB Output is correct
26 Correct 53 ms 44628 KB Output is correct
27 Correct 56 ms 44468 KB Output is correct
28 Correct 55 ms 44596 KB Output is correct
29 Correct 58 ms 44596 KB Output is correct
30 Correct 60 ms 44616 KB Output is correct
31 Correct 72 ms 14288 KB Output is correct
32 Correct 56 ms 44552 KB Output is correct
33 Correct 65 ms 33104 KB Output is correct
34 Correct 36 ms 14808 KB Output is correct
35 Correct 69 ms 31356 KB Output is correct
36 Correct 43 ms 14808 KB Output is correct
37 Correct 66 ms 30808 KB Output is correct
38 Correct 66 ms 14896 KB Output is correct
39 Correct 55 ms 44636 KB Output is correct
40 Correct 68 ms 27372 KB Output is correct
41 Correct 2 ms 9308 KB Output is correct
42 Correct 2 ms 9820 KB Output is correct
43 Correct 2 ms 9564 KB Output is correct
44 Correct 2 ms 9308 KB Output is correct
45 Correct 2 ms 9308 KB Output is correct
46 Correct 2 ms 9564 KB Output is correct
47 Correct 2 ms 9308 KB Output is correct
48 Correct 2 ms 9564 KB Output is correct
49 Correct 2 ms 9308 KB Output is correct
50 Correct 2 ms 9820 KB Output is correct
51 Correct 61 ms 14672 KB Output is correct
52 Correct 55 ms 44536 KB Output is correct
53 Correct 62 ms 27864 KB Output is correct
54 Correct 39 ms 14684 KB Output is correct
55 Correct 73 ms 14304 KB Output is correct
56 Correct 55 ms 44628 KB Output is correct
57 Correct 62 ms 29564 KB Output is correct
58 Correct 48 ms 14792 KB Output is correct
59 Correct 65 ms 14876 KB Output is correct
60 Correct 54 ms 44464 KB Output is correct
61 Correct 63 ms 29948 KB Output is correct
62 Correct 40 ms 14752 KB Output is correct
63 Correct 77 ms 14316 KB Output is correct
64 Correct 60 ms 44368 KB Output is correct
65 Correct 63 ms 30044 KB Output is correct
66 Correct 36 ms 14640 KB Output is correct
67 Correct 84 ms 14472 KB Output is correct
68 Correct 56 ms 44560 KB Output is correct
69 Correct 68 ms 25388 KB Output is correct
70 Correct 34 ms 14680 KB Output is correct