Submission #683225

# Submission time Handle Problem Language Result Execution time Memory
683225 2023-01-18T00:31:53 Z NK_ Election Campaign (JOI15_election_campaign) C++17
100 / 100
179 ms 33608 KB
// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>

using namespace std;

#define nl '\n'

struct BIT {
	vector<int> data; int N;
	void init(int n) { N = n; data = vector<int>(N, 0); }
	void add(int p, int x) { for(++p;p<=N;p+=p&-p) data[p-1] += x; }
	int sum(int l, int r) { return sum(r+1)-sum(l); }
	int sum(int r) { int s = 0; for(;r;r-=r&-r) s += data[r-1]; return s; }
};

const int LG = 20;
const int nax = 1e5+5;

int up[nax][LG], dep[nax];
int st[nax], en[nax];
vector<int> adj[nax];
vector<int> T[nax];
int U[nax], V[nax], W[nax];
int dp[nax];
BIT DP, CHD;

int t = 0;
void gen(int u = 0, int p = -1) {
	st[u] = t++;

	up[u][0] = (p == -1 ? u : p); dep[u] = (p == -1 ? 0 : dep[p]+1);
	for(int i = 1; i < LG; i++) up[u][i] = up[up[u][i-1]][i-1];

	for(auto v : adj[u]) if (v != p) gen(v, u);	

	en[u] = t++;
}

int jmp(int u, int d) {
	for(int i = 0; i < LG; i++) if ((d >> i) & 1) u = up[u][i];
	return u;
}

int lca(int a, int b) {
	if (dep[a] < dep[b]) swap(a, b);
	a = jmp(a, dep[a] - dep[b]);
	if (a == b) return a;

	for(int i = LG - 1; i >= 0; i--) if (up[a][i] != up[b][i]) a = up[a][i], b = up[b][i];

	return up[a][0];
}

void dfs(int u = 0, int p = -1) {
	int chd = 0;
	for(auto v : adj[u]) if (v != p) {
		dfs(v, u);
		chd += dp[v];
	}

	dp[u] = chd;
	CHD.add(st[u], chd); CHD.add(en[u], -chd);

	for(auto i : T[u]) {
		int x = W[i];
		x += CHD.sum(st[u], st[U[i]]) - DP.sum(st[u], st[U[i]]);
		x += CHD.sum(st[u], st[V[i]]) - DP.sum(st[u], st[V[i]]);
		x -= CHD.sum(st[u], st[u]);
		dp[u] = max(dp[u], x);
	}

	DP.add(st[u], dp[u]); DP.add(en[u], -dp[u]);
}

int main() {
	cin.tie(0)->sync_with_stdio(0);
	
	int N; cin >> N;
	for(int i = 0; i < N-1; i++) {
		int u, v; cin >> u >> v; --u, --v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}

	gen();

	DP.init(2*N); CHD.init(2*N);

	int Q; cin >> Q;

	for(int i = 0; i < Q; i++) {
		int u, v, w; cin >> u >> v >> w; --u, --v;
		int l = lca(u, v);
		U[i] = u, V[i] = v, W[i] = w;
		T[l].push_back(i);
	}

	dfs();
	
	cout << dp[0] << nl;

    return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 2 ms 5028 KB Output is correct
4 Correct 3 ms 5160 KB Output is correct
5 Correct 95 ms 20452 KB Output is correct
6 Correct 51 ms 29644 KB Output is correct
7 Correct 85 ms 26384 KB Output is correct
8 Correct 67 ms 20744 KB Output is correct
9 Correct 112 ms 24468 KB Output is correct
10 Correct 75 ms 20772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 5204 KB Output is correct
4 Correct 112 ms 33160 KB Output is correct
5 Correct 111 ms 33136 KB Output is correct
6 Correct 107 ms 33252 KB Output is correct
7 Correct 142 ms 33188 KB Output is correct
8 Correct 110 ms 33160 KB Output is correct
9 Correct 116 ms 33180 KB Output is correct
10 Correct 117 ms 33152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 5204 KB Output is correct
4 Correct 112 ms 33160 KB Output is correct
5 Correct 111 ms 33136 KB Output is correct
6 Correct 107 ms 33252 KB Output is correct
7 Correct 142 ms 33188 KB Output is correct
8 Correct 110 ms 33160 KB Output is correct
9 Correct 116 ms 33180 KB Output is correct
10 Correct 117 ms 33152 KB Output is correct
11 Correct 11 ms 5972 KB Output is correct
12 Correct 118 ms 33472 KB Output is correct
13 Correct 114 ms 33376 KB Output is correct
14 Correct 97 ms 33512 KB Output is correct
15 Correct 113 ms 33468 KB Output is correct
16 Correct 103 ms 33572 KB Output is correct
17 Correct 122 ms 33380 KB Output is correct
18 Correct 121 ms 33388 KB Output is correct
19 Correct 98 ms 33444 KB Output is correct
20 Correct 113 ms 33608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 138 ms 23400 KB Output is correct
2 Correct 105 ms 33244 KB Output is correct
3 Correct 172 ms 29464 KB Output is correct
4 Correct 105 ms 23880 KB Output is correct
5 Correct 147 ms 29004 KB Output is correct
6 Correct 117 ms 23876 KB Output is correct
7 Correct 173 ms 28692 KB Output is correct
8 Correct 145 ms 23876 KB Output is correct
9 Correct 96 ms 33244 KB Output is correct
10 Correct 167 ms 27620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 2 ms 5028 KB Output is correct
4 Correct 3 ms 5160 KB Output is correct
5 Correct 95 ms 20452 KB Output is correct
6 Correct 51 ms 29644 KB Output is correct
7 Correct 85 ms 26384 KB Output is correct
8 Correct 67 ms 20744 KB Output is correct
9 Correct 112 ms 24468 KB Output is correct
10 Correct 75 ms 20772 KB Output is correct
11 Correct 3 ms 5204 KB Output is correct
12 Correct 3 ms 5296 KB Output is correct
13 Correct 4 ms 5296 KB Output is correct
14 Correct 3 ms 5204 KB Output is correct
15 Correct 4 ms 5204 KB Output is correct
16 Correct 4 ms 5204 KB Output is correct
17 Correct 3 ms 5204 KB Output is correct
18 Correct 3 ms 5204 KB Output is correct
19 Correct 4 ms 5204 KB Output is correct
20 Correct 3 ms 5332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 2 ms 5028 KB Output is correct
4 Correct 3 ms 5160 KB Output is correct
5 Correct 95 ms 20452 KB Output is correct
6 Correct 51 ms 29644 KB Output is correct
7 Correct 85 ms 26384 KB Output is correct
8 Correct 67 ms 20744 KB Output is correct
9 Correct 112 ms 24468 KB Output is correct
10 Correct 75 ms 20772 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 5204 KB Output is correct
14 Correct 112 ms 33160 KB Output is correct
15 Correct 111 ms 33136 KB Output is correct
16 Correct 107 ms 33252 KB Output is correct
17 Correct 142 ms 33188 KB Output is correct
18 Correct 110 ms 33160 KB Output is correct
19 Correct 116 ms 33180 KB Output is correct
20 Correct 117 ms 33152 KB Output is correct
21 Correct 11 ms 5972 KB Output is correct
22 Correct 118 ms 33472 KB Output is correct
23 Correct 114 ms 33376 KB Output is correct
24 Correct 97 ms 33512 KB Output is correct
25 Correct 113 ms 33468 KB Output is correct
26 Correct 103 ms 33572 KB Output is correct
27 Correct 122 ms 33380 KB Output is correct
28 Correct 121 ms 33388 KB Output is correct
29 Correct 98 ms 33444 KB Output is correct
30 Correct 113 ms 33608 KB Output is correct
31 Correct 138 ms 23400 KB Output is correct
32 Correct 105 ms 33244 KB Output is correct
33 Correct 172 ms 29464 KB Output is correct
34 Correct 105 ms 23880 KB Output is correct
35 Correct 147 ms 29004 KB Output is correct
36 Correct 117 ms 23876 KB Output is correct
37 Correct 173 ms 28692 KB Output is correct
38 Correct 145 ms 23876 KB Output is correct
39 Correct 96 ms 33244 KB Output is correct
40 Correct 167 ms 27620 KB Output is correct
41 Correct 3 ms 5204 KB Output is correct
42 Correct 3 ms 5296 KB Output is correct
43 Correct 4 ms 5296 KB Output is correct
44 Correct 3 ms 5204 KB Output is correct
45 Correct 4 ms 5204 KB Output is correct
46 Correct 4 ms 5204 KB Output is correct
47 Correct 3 ms 5204 KB Output is correct
48 Correct 3 ms 5204 KB Output is correct
49 Correct 4 ms 5204 KB Output is correct
50 Correct 3 ms 5332 KB Output is correct
51 Correct 175 ms 24140 KB Output is correct
52 Correct 105 ms 33372 KB Output is correct
53 Correct 179 ms 28052 KB Output is correct
54 Correct 105 ms 24068 KB Output is correct
55 Correct 138 ms 23704 KB Output is correct
56 Correct 103 ms 33484 KB Output is correct
57 Correct 163 ms 28972 KB Output is correct
58 Correct 114 ms 24136 KB Output is correct
59 Correct 154 ms 24104 KB Output is correct
60 Correct 115 ms 33492 KB Output is correct
61 Correct 159 ms 28984 KB Output is correct
62 Correct 109 ms 23976 KB Output is correct
63 Correct 137 ms 23764 KB Output is correct
64 Correct 105 ms 33436 KB Output is correct
65 Correct 172 ms 28872 KB Output is correct
66 Correct 104 ms 24244 KB Output is correct
67 Correct 134 ms 23748 KB Output is correct
68 Correct 104 ms 33436 KB Output is correct
69 Correct 150 ms 27448 KB Output is correct
70 Correct 105 ms 24016 KB Output is correct