답안 #170820

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
170820 2019-12-26T12:46:10 Z ZwariowanyMarcin Election Campaign (JOI15_election_campaign) C++14
100 / 100
290 ms 31216 KB
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define ss(x) (int) x.size()
#define pb push_back
#define ll long long
#define cat(x) cerr << #x << " = " << x << endl
#define FOR(i, n) for(int i = 0; i < n; ++i)

using namespace std;	

const int nax = 1e5 + 11;
const int LOG = 16;
const int pod = (1 << 17);

int n;
int a, b;
vector <int> v[nax];
int m, c;
int pre[nax];
int post[nax];
int h[nax];
int jump[nax][LOG + 1];
int cnt;

void dfs(int u, int p) {
	h[u] = h[p] + 1;
	jump[u][0] = p;
	pre[u] = cnt++;
	for(auto it : v[u]) 
		if(it != p)
			dfs(it, u);
	post[u] = cnt;
}

int lca(int x, int y) {
	if(h[x] < h[y])
		swap(x, y);
	for(int i = LOG; 0 <= i; --i)
		if(h[y] <= h[x] - (1 << i))
			x = jump[x][i];
	if(x == y)
		return x;
	for(int i = LOG; 0 <= i; --i) 
		if(jump[x][i] != jump[y][i]) {
			x = jump[x][i];
			y = jump[y][i];
		}
	return jump[x][0];
}

struct gao {
	int a, b, c;
};

vector <gao> V[nax];

struct seg {
	int t[2 * pod];
	
	void init() {
		for(int i = 0; i < 2 * pod; ++i)
			t[i] = 0;
	}
	
	void add(int l, int r, int c) {
		for(l += pod, r += pod; l < r; l /= 2, r /= 2) {
			if(l & 1)
				t[l++] += c;
			if(r & 1)
				t[--r] += c;
		}
	}
	
	int query(int x) {
		int sum = 0;
		x += pod;
		while(x) {
			sum += t[x];
			x /= 2;
		}
		return sum;
	}
} child, node;

int dp[nax];

void solve(int u, int p) {
	int Sum = 0;
	for(auto it : v[u])
		if(it != p) {
			solve(it, u);
			Sum += dp[it];
		}
	child.add(pre[u], post[u], Sum);
	dp[u] = Sum;
	for(auto it : V[u]) {
		int w = child.query(pre[it.a]) + child.query(pre[it.b]) - Sum - node.query(pre[it.a]) - node.query(pre[it.b]) + it.c;
		dp[u] = max(dp[u], w);
	}
	node.add(pre[u], post[u], dp[u]);
}

int main() {
	child.init();
	node.init();
	scanf("%d", &n);
	for(int i = 1; i < n; ++i) {
		scanf("%d %d", &a, &b);
		v[a].pb(b);
		v[b].pb(a);
	}
	dfs(1, 0);

	for(int j = 1; j <= LOG; ++j)
		for(int i = 1; i <= n; ++i)
			jump[i][j] = jump[jump[i][j - 1]][j - 1];
	
	scanf("%d", &m);
	
	for(int i = 1; i <= m; ++i) {
		scanf("%d %d %d", &a, &b, &c);
		V[lca(a, b)].pb({a, b, c});
	}
	
	solve(1, 0);
	
	printf("%d\n", dp[1]);
	
	
	return 0;
}

Compilation message

election_campaign.cpp: In function 'int main()':
election_campaign.cpp:108:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
election_campaign.cpp:110:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &a, &b);
   ~~~~~^~~~~~~~~~~~~~~~~
election_campaign.cpp:120:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &m);
  ~~~~~^~~~~~~~~~
election_campaign.cpp:123:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d %d", &a, &b, &c);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 7284 KB Output is correct
2 Correct 8 ms 7032 KB Output is correct
3 Correct 8 ms 7160 KB Output is correct
4 Correct 8 ms 7292 KB Output is correct
5 Correct 111 ms 19900 KB Output is correct
6 Correct 88 ms 27384 KB Output is correct
7 Correct 125 ms 24696 KB Output is correct
8 Correct 78 ms 20088 KB Output is correct
9 Correct 120 ms 23164 KB Output is correct
10 Correct 79 ms 20216 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 7160 KB Output is correct
2 Correct 8 ms 7288 KB Output is correct
3 Correct 9 ms 7292 KB Output is correct
4 Correct 182 ms 30840 KB Output is correct
5 Correct 186 ms 30712 KB Output is correct
6 Correct 167 ms 30840 KB Output is correct
7 Correct 206 ms 30712 KB Output is correct
8 Correct 192 ms 30704 KB Output is correct
9 Correct 181 ms 30716 KB Output is correct
10 Correct 179 ms 30712 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 7160 KB Output is correct
2 Correct 8 ms 7288 KB Output is correct
3 Correct 9 ms 7292 KB Output is correct
4 Correct 182 ms 30840 KB Output is correct
5 Correct 186 ms 30712 KB Output is correct
6 Correct 167 ms 30840 KB Output is correct
7 Correct 206 ms 30712 KB Output is correct
8 Correct 192 ms 30704 KB Output is correct
9 Correct 181 ms 30716 KB Output is correct
10 Correct 179 ms 30712 KB Output is correct
11 Correct 23 ms 8056 KB Output is correct
12 Correct 189 ms 31068 KB Output is correct
13 Correct 185 ms 30968 KB Output is correct
14 Correct 177 ms 30968 KB Output is correct
15 Correct 191 ms 30968 KB Output is correct
16 Correct 192 ms 31092 KB Output is correct
17 Correct 192 ms 31100 KB Output is correct
18 Correct 193 ms 31216 KB Output is correct
19 Correct 173 ms 30968 KB Output is correct
20 Correct 228 ms 31176 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 225 ms 22544 KB Output is correct
2 Correct 166 ms 30712 KB Output is correct
3 Correct 269 ms 27768 KB Output is correct
4 Correct 144 ms 22956 KB Output is correct
5 Correct 231 ms 27428 KB Output is correct
6 Correct 153 ms 22892 KB Output is correct
7 Correct 283 ms 27044 KB Output is correct
8 Correct 221 ms 22984 KB Output is correct
9 Correct 165 ms 30712 KB Output is correct
10 Correct 267 ms 25992 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 7284 KB Output is correct
2 Correct 8 ms 7032 KB Output is correct
3 Correct 8 ms 7160 KB Output is correct
4 Correct 8 ms 7292 KB Output is correct
5 Correct 111 ms 19900 KB Output is correct
6 Correct 88 ms 27384 KB Output is correct
7 Correct 125 ms 24696 KB Output is correct
8 Correct 78 ms 20088 KB Output is correct
9 Correct 120 ms 23164 KB Output is correct
10 Correct 79 ms 20216 KB Output is correct
11 Correct 9 ms 7288 KB Output is correct
12 Correct 11 ms 7288 KB Output is correct
13 Correct 9 ms 7288 KB Output is correct
14 Correct 9 ms 7288 KB Output is correct
15 Correct 9 ms 7288 KB Output is correct
16 Correct 10 ms 7288 KB Output is correct
17 Correct 9 ms 7288 KB Output is correct
18 Correct 9 ms 7288 KB Output is correct
19 Correct 9 ms 7288 KB Output is correct
20 Correct 9 ms 7280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 7284 KB Output is correct
2 Correct 8 ms 7032 KB Output is correct
3 Correct 8 ms 7160 KB Output is correct
4 Correct 8 ms 7292 KB Output is correct
5 Correct 111 ms 19900 KB Output is correct
6 Correct 88 ms 27384 KB Output is correct
7 Correct 125 ms 24696 KB Output is correct
8 Correct 78 ms 20088 KB Output is correct
9 Correct 120 ms 23164 KB Output is correct
10 Correct 79 ms 20216 KB Output is correct
11 Correct 8 ms 7160 KB Output is correct
12 Correct 8 ms 7288 KB Output is correct
13 Correct 9 ms 7292 KB Output is correct
14 Correct 182 ms 30840 KB Output is correct
15 Correct 186 ms 30712 KB Output is correct
16 Correct 167 ms 30840 KB Output is correct
17 Correct 206 ms 30712 KB Output is correct
18 Correct 192 ms 30704 KB Output is correct
19 Correct 181 ms 30716 KB Output is correct
20 Correct 179 ms 30712 KB Output is correct
21 Correct 23 ms 8056 KB Output is correct
22 Correct 189 ms 31068 KB Output is correct
23 Correct 185 ms 30968 KB Output is correct
24 Correct 177 ms 30968 KB Output is correct
25 Correct 191 ms 30968 KB Output is correct
26 Correct 192 ms 31092 KB Output is correct
27 Correct 192 ms 31100 KB Output is correct
28 Correct 193 ms 31216 KB Output is correct
29 Correct 173 ms 30968 KB Output is correct
30 Correct 228 ms 31176 KB Output is correct
31 Correct 225 ms 22544 KB Output is correct
32 Correct 166 ms 30712 KB Output is correct
33 Correct 269 ms 27768 KB Output is correct
34 Correct 144 ms 22956 KB Output is correct
35 Correct 231 ms 27428 KB Output is correct
36 Correct 153 ms 22892 KB Output is correct
37 Correct 283 ms 27044 KB Output is correct
38 Correct 221 ms 22984 KB Output is correct
39 Correct 165 ms 30712 KB Output is correct
40 Correct 267 ms 25992 KB Output is correct
41 Correct 9 ms 7288 KB Output is correct
42 Correct 11 ms 7288 KB Output is correct
43 Correct 9 ms 7288 KB Output is correct
44 Correct 9 ms 7288 KB Output is correct
45 Correct 9 ms 7288 KB Output is correct
46 Correct 10 ms 7288 KB Output is correct
47 Correct 9 ms 7288 KB Output is correct
48 Correct 9 ms 7288 KB Output is correct
49 Correct 9 ms 7288 KB Output is correct
50 Correct 9 ms 7280 KB Output is correct
51 Correct 205 ms 23100 KB Output is correct
52 Correct 190 ms 30968 KB Output is correct
53 Correct 267 ms 26284 KB Output is correct
54 Correct 146 ms 23220 KB Output is correct
55 Correct 190 ms 23004 KB Output is correct
56 Correct 195 ms 31188 KB Output is correct
57 Correct 223 ms 27000 KB Output is correct
58 Correct 153 ms 23216 KB Output is correct
59 Correct 200 ms 23236 KB Output is correct
60 Correct 179 ms 30968 KB Output is correct
61 Correct 222 ms 27128 KB Output is correct
62 Correct 156 ms 23028 KB Output is correct
63 Correct 200 ms 22744 KB Output is correct
64 Correct 182 ms 30968 KB Output is correct
65 Correct 290 ms 26880 KB Output is correct
66 Correct 153 ms 23172 KB Output is correct
67 Correct 209 ms 22792 KB Output is correct
68 Correct 181 ms 31068 KB Output is correct
69 Correct 223 ms 26000 KB Output is correct
70 Correct 152 ms 23388 KB Output is correct