답안 #413376

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
413376 2021-05-28T15:26:37 Z ritul_kr_singh Election Campaign (JOI15_election_campaign) C++17
100 / 100
260 ms 41456 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define sp << ' ' <<
#define nl << '\n'

const int LIM = 1e5;

struct FenwickTree{
	vector<int> a; int n, s;
	void init(int N){ a.resize((n=N)+1); }
	void add(int i, int v){
		for(++i; i<=n; i+=i&-i) a[i] += v;
	}
	int get(int i){
		s = 0;
		for(++i; i>=1; i-=i&-i) s += a[i];
		return s;
	}
} f;

int n, m, t0[LIM], t1[LIM], s[LIM], dp[LIM], dfsTimer = 0;
array<int, 3> a[LIM];
array<int, 18> p[LIM];
vector<int> g[LIM], q[LIM];

void dfs0(int u, int par){
	p[u][0] = par;
	for(int i=0; i<17; ++i) p[u][i+1] = p[p[u][i]][i];

	t0[u] = dfsTimer++;
	for(int v : g[u]) if(v != par) dfs0(v, u);
	t1[u] = dfsTimer++;
}

bool isAncestor(int u, int v){
	return t0[u] <= t0[v] && t1[v] <= t1[u];
}

void dfs1(int u){
	for(int v : g[u]) if(v != p[u][0]) dfs1(v), s[u] += dp[v];
	dp[u] = s[u];
	for(int i : q[u]){
		dp[u] = max(dp[u], f.get(t0[a[i][0]]) + f.get(t0[a[i][1]]) + s[u] + a[i][2]);
	}
	f.add(t0[u], s[u] - dp[u]);
	f.add(t1[u], dp[u] - s[u]);
}

signed main(){
	cin.tie(0)->sync_with_stdio(0);
	int x, y, z; cin >> n;
	for(int i=1; i<n; ++i){
		cin >> x >> y; --x, --y;
		g[x].push_back(y);
		g[y].push_back(x);
	}
	dfs0(0, 0);
	f.init(n+n);

	cin >> m;
	for(int i=0; i<m; ++i){
		cin >> x >> y >> a[i][2]; --x, --y;
		a[i][0] = x, a[i][1] = y;

		z = x;
		if(!isAncestor(x, y)){
			for(int j=17; j>=0; --j) while(!isAncestor(p[z][j], y)) z = p[z][j];
			z = p[z][0];
		}

		q[z].push_back(i);
	}

	dfs1(0);

	cout << dp[0];
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 4 ms 5028 KB Output is correct
3 Correct 5 ms 4940 KB Output is correct
4 Correct 5 ms 5196 KB Output is correct
5 Correct 129 ms 28832 KB Output is correct
6 Correct 64 ms 35908 KB Output is correct
7 Correct 123 ms 33524 KB Output is correct
8 Correct 89 ms 28264 KB Output is correct
9 Correct 129 ms 32124 KB Output is correct
10 Correct 87 ms 28228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 4940 KB Output is correct
2 Correct 4 ms 4940 KB Output is correct
3 Correct 4 ms 5324 KB Output is correct
4 Correct 140 ms 41124 KB Output is correct
5 Correct 144 ms 41108 KB Output is correct
6 Correct 157 ms 41152 KB Output is correct
7 Correct 147 ms 41120 KB Output is correct
8 Correct 173 ms 41028 KB Output is correct
9 Correct 153 ms 41112 KB Output is correct
10 Correct 157 ms 41028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 4940 KB Output is correct
2 Correct 4 ms 4940 KB Output is correct
3 Correct 4 ms 5324 KB Output is correct
4 Correct 140 ms 41124 KB Output is correct
5 Correct 144 ms 41108 KB Output is correct
6 Correct 157 ms 41152 KB Output is correct
7 Correct 147 ms 41120 KB Output is correct
8 Correct 173 ms 41028 KB Output is correct
9 Correct 153 ms 41112 KB Output is correct
10 Correct 157 ms 41028 KB Output is correct
11 Correct 15 ms 6432 KB Output is correct
12 Correct 175 ms 41412 KB Output is correct
13 Correct 156 ms 41360 KB Output is correct
14 Correct 138 ms 41456 KB Output is correct
15 Correct 155 ms 41328 KB Output is correct
16 Correct 163 ms 41424 KB Output is correct
17 Correct 161 ms 41308 KB Output is correct
18 Correct 158 ms 41436 KB Output is correct
19 Correct 133 ms 41364 KB Output is correct
20 Correct 140 ms 41388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 164 ms 33468 KB Output is correct
2 Correct 131 ms 41172 KB Output is correct
3 Correct 260 ms 38368 KB Output is correct
4 Correct 149 ms 32876 KB Output is correct
5 Correct 215 ms 37992 KB Output is correct
6 Correct 134 ms 32832 KB Output is correct
7 Correct 226 ms 37640 KB Output is correct
8 Correct 227 ms 33692 KB Output is correct
9 Correct 132 ms 41204 KB Output is correct
10 Correct 215 ms 36652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 4 ms 5028 KB Output is correct
3 Correct 5 ms 4940 KB Output is correct
4 Correct 5 ms 5196 KB Output is correct
5 Correct 129 ms 28832 KB Output is correct
6 Correct 64 ms 35908 KB Output is correct
7 Correct 123 ms 33524 KB Output is correct
8 Correct 89 ms 28264 KB Output is correct
9 Correct 129 ms 32124 KB Output is correct
10 Correct 87 ms 28228 KB Output is correct
11 Correct 4 ms 5324 KB Output is correct
12 Correct 5 ms 5324 KB Output is correct
13 Correct 5 ms 5324 KB Output is correct
14 Correct 5 ms 5328 KB Output is correct
15 Correct 5 ms 5440 KB Output is correct
16 Correct 5 ms 5292 KB Output is correct
17 Correct 4 ms 5296 KB Output is correct
18 Correct 4 ms 5324 KB Output is correct
19 Correct 4 ms 5288 KB Output is correct
20 Correct 5 ms 5288 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 4 ms 5028 KB Output is correct
3 Correct 5 ms 4940 KB Output is correct
4 Correct 5 ms 5196 KB Output is correct
5 Correct 129 ms 28832 KB Output is correct
6 Correct 64 ms 35908 KB Output is correct
7 Correct 123 ms 33524 KB Output is correct
8 Correct 89 ms 28264 KB Output is correct
9 Correct 129 ms 32124 KB Output is correct
10 Correct 87 ms 28228 KB Output is correct
11 Correct 5 ms 4940 KB Output is correct
12 Correct 4 ms 4940 KB Output is correct
13 Correct 4 ms 5324 KB Output is correct
14 Correct 140 ms 41124 KB Output is correct
15 Correct 144 ms 41108 KB Output is correct
16 Correct 157 ms 41152 KB Output is correct
17 Correct 147 ms 41120 KB Output is correct
18 Correct 173 ms 41028 KB Output is correct
19 Correct 153 ms 41112 KB Output is correct
20 Correct 157 ms 41028 KB Output is correct
21 Correct 15 ms 6432 KB Output is correct
22 Correct 175 ms 41412 KB Output is correct
23 Correct 156 ms 41360 KB Output is correct
24 Correct 138 ms 41456 KB Output is correct
25 Correct 155 ms 41328 KB Output is correct
26 Correct 163 ms 41424 KB Output is correct
27 Correct 161 ms 41308 KB Output is correct
28 Correct 158 ms 41436 KB Output is correct
29 Correct 133 ms 41364 KB Output is correct
30 Correct 140 ms 41388 KB Output is correct
31 Correct 164 ms 33468 KB Output is correct
32 Correct 131 ms 41172 KB Output is correct
33 Correct 260 ms 38368 KB Output is correct
34 Correct 149 ms 32876 KB Output is correct
35 Correct 215 ms 37992 KB Output is correct
36 Correct 134 ms 32832 KB Output is correct
37 Correct 226 ms 37640 KB Output is correct
38 Correct 227 ms 33692 KB Output is correct
39 Correct 132 ms 41204 KB Output is correct
40 Correct 215 ms 36652 KB Output is correct
41 Correct 4 ms 5324 KB Output is correct
42 Correct 5 ms 5324 KB Output is correct
43 Correct 5 ms 5324 KB Output is correct
44 Correct 5 ms 5328 KB Output is correct
45 Correct 5 ms 5440 KB Output is correct
46 Correct 5 ms 5292 KB Output is correct
47 Correct 4 ms 5296 KB Output is correct
48 Correct 4 ms 5324 KB Output is correct
49 Correct 4 ms 5288 KB Output is correct
50 Correct 5 ms 5288 KB Output is correct
51 Correct 209 ms 34212 KB Output is correct
52 Correct 163 ms 41416 KB Output is correct
53 Correct 220 ms 37176 KB Output is correct
54 Correct 146 ms 33088 KB Output is correct
55 Correct 175 ms 33820 KB Output is correct
56 Correct 155 ms 41412 KB Output is correct
57 Correct 213 ms 37900 KB Output is correct
58 Correct 161 ms 33312 KB Output is correct
59 Correct 191 ms 34056 KB Output is correct
60 Correct 156 ms 41324 KB Output is correct
61 Correct 227 ms 37964 KB Output is correct
62 Correct 138 ms 33080 KB Output is correct
63 Correct 166 ms 33608 KB Output is correct
64 Correct 151 ms 41416 KB Output is correct
65 Correct 236 ms 37696 KB Output is correct
66 Correct 142 ms 33212 KB Output is correct
67 Correct 172 ms 33684 KB Output is correct
68 Correct 154 ms 41412 KB Output is correct
69 Correct 199 ms 36572 KB Output is correct
70 Correct 137 ms 33272 KB Output is correct