Submission #25432

# Submission time Handle Problem Language Result Execution time Memory
25432 2017-06-22T06:56:27 Z 윤교준(#1068) Election Campaign (JOI15_election_campaign) C++11
100 / 100
396 ms 33716 KB
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <deque>
#include <queue>
#include <set>
#include <map>
#include <unordered_map>
#include <bitset>
#include <string>
#define pb push_back
#define sz(V) ((int)(V).size())
#define allv(V) ((V).begin()),((V).end())
#define befv(V) ((V)[(sz(V)-2)])
#define sorv(V) sort(allv(V))
#define revv(V) reverse(allv(V))
#define univ(V) (V).erase(unique(allv(V)),(V).end())
#define upmin(a,b) (a)=min((a),(b))
#define upmax(a,b) (a)=max((a),(b))
#define rb(x) ((x)&(-(x)))
#define INF (1100000099)
#define INFLL (1100000000000000099ll)
#define MAXN (100005)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
int uptwo[MAXN];
struct SEG {
	int *d; int MX;
	void init(int SZ) {
		MX = uptwo[SZ];
		d = new int[MX*2]; fill(d, d+(MX*2), 0);
	}
	void upd(int X, int R) { for(X += MX; X; X /= 2) d[X] += R; }
	int get(int S, int E) {
		int r = 0;
		for(S += MX, E += MX; S <= E; S /= 2, E /= 2) {
			if(S&1) r += d[S++]; if(~E&1) r += d[E--];
		}
		return r;
	}
};

SEG seg[MAXN];
vector<int> G[MAXN];
vector<int> L[MAXN];
int d[MAXN], csum[MAXN];
int dep[MAXN], dfscnt[MAXN], prt[MAXN][17];
int HLDsz[MAXN], HLDC[MAXN], HLDI[MAXN], HLDn;
int HLDRoot[MAXN], HLDChild[MAXN];
int EA[MAXN], EB[MAXN];
int A[MAXN], B[MAXN], C[MAXN];
int N, M;

void input() {
	scanf("%d", &N);
	for(int i = 1; i < N; i++) scanf("%d%d", &EA[i], &EB[i]);
	scanf("%d", &M);
	for(int i = 1; i <= M; i++) scanf("%d%d%d", &A[i], &B[i], &C[i]);
}
int lca(int a, int b) {
	if(dep[a] != dep[b]) {
		if(dep[a] > dep[b]) swap(a, b);
		const int dt = dep[b] - dep[a];
		for(int i = 0; i < 17; i++) if(dt & (1<<i)) b = prt[b][i];
	}
	if(a == b) return a;
	for(int i = 16; ~i; i--) if(prt[a][i] != prt[b][i]) {
		a = prt[a][i]; b = prt[b][i];
	}
	return prt[a][0];
}
void dfs1(int idx, int depth) {
	dep[idx] = depth; dfscnt[idx] = 1;
	for(int v : G[idx])
		if(!dep[v]) { prt[v][0] = idx; dfs1(v, depth+1); dfscnt[idx] += dfscnt[v]; }
}
void dfs2(int idx, int c, int ci) {
	HLDC[idx] = c; HLDI[idx] = ci;
	int maxcnt = -1, hubo = -1;
	for(int v : G[idx]) if(maxcnt < dfscnt[v]) { maxcnt = dfscnt[v]; hubo = v; }
	for(int v : G[idx]) {
		if(hubo == v) dfs2(v, c, ci+1);
		else dfs2(v, ++HLDn, 1);
	}
}
void precal() {
	uptwo[0] = 1; for(int i = 1; i <= N; i++) uptwo[i] = uptwo[i/2]*2;
	for(int i = 1; i < N; i++) { G[EA[i]].pb(EB[i]); G[EB[i]].pb(EA[i]); }
	dfs1(1, 1);
	for(int i = 1; i <= N; i++) vector<int>().swap(G[i]);
	for(int i = 1; i < N; i++) if(dep[EA[i]] > dep[EB[i]]) swap(EA[i], EB[i]);
	for(int i = 1; i < N; i++) G[EA[i]].pb(EB[i]);
	for(int j = 1; j < 17; j++) for(int i = 1; i <= N; i++) prt[i][j] = prt[prt[i][j-1]][j-1];
	for(int i = 1; i <= M; i++) L[lca(A[i], B[i])].pb(i);
	dfs2(1, HLDn = 1, 1);
	for(int i = 1; i <= N; i++) upmax(HLDsz[HLDC[i]], HLDI[i]);
	for(int i = 1; i <= HLDn; i++) seg[i].init(HLDsz[i]);
	for(int i = 1; i <= N; i++) if(1 == HLDI[i]) HLDRoot[HLDC[i]] = i;
	for(int i = 1; i <= N; i++) {
		int &ret = HLDChild[i]; ret = -1;
		for(int v : G[i]) if(HLDC[i] == HLDC[v]) { ret = v; break; }
	}
}
void updHLD(int idx) {
	int r = 0; for(int v : G[idx]) if(HLDC[idx] != HLDC[v]) r += d[v];
	seg[HLDC[idx]].upd(HLDI[idx], r);
}
int f(int a, int b, int c) {
	int ret = 0;
	if(a == c) swap(a, b);
	if(b == c) {
		for(; HLDC[a] != HLDC[c]; a = prt[HLDRoot[HLDC[a]]][0]) {
			if(-1 != HLDChild[a]) ret += d[HLDChild[a]];
			ret += seg[HLDC[a]].get(1, HLDI[a]);
			ret -= d[HLDRoot[HLDC[a]]];
		}
		if(a != c) {
			if(-1 != HLDChild[a]) ret += d[HLDChild[a]];
			ret += seg[HLDC[a]].get(HLDI[c], HLDI[a]);
		} else {
			ret += csum[c];
		}
	} else {
		ret = f(a, c, c) + f(b, c, c);
		//for(int v : G[c]) ret -= d[v]; // ???
		ret -= csum[c]; // ?????
	}
	return ret;
}
void dfs3(int idx) {
	for(int v : G[idx]) dfs3(v);
	//printf("###DFS %d\n", idx);
	int &ret = d[idx]; updHLD(idx);
	for(int v : G[idx]) csum[idx] += d[v];
	//printf("CSUM %d\n", csum[idx]);
	//printf("SEG %d\n", seg[HLDC[idx]].get(HLDI[idx], HLDI[idx]));
	for(int e : L[idx]) {
		upmax(ret, f(A[e], B[e], idx) + C[e]);
		//printf("for LINE %d : %d\n", e, f(A[e], B[e], idx));
	}
	upmax(ret, csum[idx]);
	//printf("RESULT %d\n", ret);
	//puts("\n\n");
}
int main() {
	input(); precal(); dfs3(1);
	printf("%d\n", d[1]);
	return 0;
	/*for(int i = 1; i <= N; i++) printf("ANS %d : %d\n", i, d[i]);
	for(int i = 1; i <= HLDn; i++) printf("ROOT %d : %d\n", i, HLDRoot[i]);
	for(int i = 1; i <= N; i++) printf("HLD %d : %d %d %d\n", i, HLDC[i], HLDI[i], HLDChild[i]);
	for(int i = 1; i <= N; i++) {
		printf("LINE %d\n", i);
		for(int v : L[i]) printf("%d ", v); puts("");
	}
	return 0;*/
}

Compilation message

election_campaign.cpp: In function 'void input()':
election_campaign.cpp:65:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &N);
                 ^
election_campaign.cpp:66:58: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(int i = 1; i < N; i++) scanf("%d%d", &EA[i], &EB[i]);
                                                          ^
election_campaign.cpp:67:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &M);
                 ^
election_campaign.cpp:68:66: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(int i = 1; i <= M; i++) scanf("%d%d%d", &A[i], &B[i], &C[i]);
                                                                  ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 20772 KB Output is correct
2 Correct 0 ms 20772 KB Output is correct
3 Correct 3 ms 20772 KB Output is correct
4 Correct 0 ms 20772 KB Output is correct
5 Correct 133 ms 24732 KB Output is correct
6 Correct 69 ms 32652 KB Output is correct
7 Correct 176 ms 29576 KB Output is correct
8 Correct 109 ms 24592 KB Output is correct
9 Correct 146 ms 28284 KB Output is correct
10 Correct 89 ms 24596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 20772 KB Output is correct
2 Correct 3 ms 20772 KB Output is correct
3 Correct 3 ms 20772 KB Output is correct
4 Correct 159 ms 33708 KB Output is correct
5 Correct 153 ms 33712 KB Output is correct
6 Correct 136 ms 33708 KB Output is correct
7 Correct 179 ms 33716 KB Output is correct
8 Correct 166 ms 33712 KB Output is correct
9 Correct 123 ms 33712 KB Output is correct
10 Correct 189 ms 33708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 20772 KB Output is correct
2 Correct 3 ms 20772 KB Output is correct
3 Correct 3 ms 20772 KB Output is correct
4 Correct 159 ms 33708 KB Output is correct
5 Correct 153 ms 33712 KB Output is correct
6 Correct 136 ms 33708 KB Output is correct
7 Correct 179 ms 33716 KB Output is correct
8 Correct 166 ms 33712 KB Output is correct
9 Correct 123 ms 33712 KB Output is correct
10 Correct 189 ms 33708 KB Output is correct
11 Correct 13 ms 21036 KB Output is correct
12 Correct 169 ms 33712 KB Output is correct
13 Correct 156 ms 33708 KB Output is correct
14 Correct 136 ms 33708 KB Output is correct
15 Correct 153 ms 33712 KB Output is correct
16 Correct 146 ms 33712 KB Output is correct
17 Correct 186 ms 33712 KB Output is correct
18 Correct 159 ms 33716 KB Output is correct
19 Correct 153 ms 33716 KB Output is correct
20 Correct 186 ms 33712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 339 ms 25296 KB Output is correct
2 Correct 173 ms 33708 KB Output is correct
3 Correct 259 ms 30292 KB Output is correct
4 Correct 173 ms 25104 KB Output is correct
5 Correct 283 ms 30096 KB Output is correct
6 Correct 193 ms 25264 KB Output is correct
7 Correct 259 ms 29784 KB Output is correct
8 Correct 276 ms 25664 KB Output is correct
9 Correct 156 ms 33712 KB Output is correct
10 Correct 223 ms 29036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 20772 KB Output is correct
2 Correct 0 ms 20772 KB Output is correct
3 Correct 3 ms 20772 KB Output is correct
4 Correct 0 ms 20772 KB Output is correct
5 Correct 133 ms 24732 KB Output is correct
6 Correct 69 ms 32652 KB Output is correct
7 Correct 176 ms 29576 KB Output is correct
8 Correct 109 ms 24592 KB Output is correct
9 Correct 146 ms 28284 KB Output is correct
10 Correct 89 ms 24596 KB Output is correct
11 Correct 0 ms 20772 KB Output is correct
12 Correct 0 ms 20904 KB Output is correct
13 Correct 3 ms 20904 KB Output is correct
14 Correct 0 ms 20772 KB Output is correct
15 Correct 0 ms 20772 KB Output is correct
16 Correct 3 ms 20904 KB Output is correct
17 Correct 3 ms 20772 KB Output is correct
18 Correct 0 ms 20904 KB Output is correct
19 Correct 0 ms 20772 KB Output is correct
20 Correct 0 ms 20904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 20772 KB Output is correct
2 Correct 0 ms 20772 KB Output is correct
3 Correct 3 ms 20772 KB Output is correct
4 Correct 0 ms 20772 KB Output is correct
5 Correct 133 ms 24732 KB Output is correct
6 Correct 69 ms 32652 KB Output is correct
7 Correct 176 ms 29576 KB Output is correct
8 Correct 109 ms 24592 KB Output is correct
9 Correct 146 ms 28284 KB Output is correct
10 Correct 89 ms 24596 KB Output is correct
11 Correct 3 ms 20772 KB Output is correct
12 Correct 3 ms 20772 KB Output is correct
13 Correct 3 ms 20772 KB Output is correct
14 Correct 159 ms 33708 KB Output is correct
15 Correct 153 ms 33712 KB Output is correct
16 Correct 136 ms 33708 KB Output is correct
17 Correct 179 ms 33716 KB Output is correct
18 Correct 166 ms 33712 KB Output is correct
19 Correct 123 ms 33712 KB Output is correct
20 Correct 189 ms 33708 KB Output is correct
21 Correct 13 ms 21036 KB Output is correct
22 Correct 169 ms 33712 KB Output is correct
23 Correct 156 ms 33708 KB Output is correct
24 Correct 136 ms 33708 KB Output is correct
25 Correct 153 ms 33712 KB Output is correct
26 Correct 146 ms 33712 KB Output is correct
27 Correct 186 ms 33712 KB Output is correct
28 Correct 159 ms 33716 KB Output is correct
29 Correct 153 ms 33716 KB Output is correct
30 Correct 186 ms 33712 KB Output is correct
31 Correct 339 ms 25296 KB Output is correct
32 Correct 173 ms 33708 KB Output is correct
33 Correct 259 ms 30292 KB Output is correct
34 Correct 173 ms 25104 KB Output is correct
35 Correct 283 ms 30096 KB Output is correct
36 Correct 193 ms 25264 KB Output is correct
37 Correct 259 ms 29784 KB Output is correct
38 Correct 276 ms 25664 KB Output is correct
39 Correct 156 ms 33712 KB Output is correct
40 Correct 223 ms 29036 KB Output is correct
41 Correct 0 ms 20772 KB Output is correct
42 Correct 0 ms 20904 KB Output is correct
43 Correct 3 ms 20904 KB Output is correct
44 Correct 0 ms 20772 KB Output is correct
45 Correct 0 ms 20772 KB Output is correct
46 Correct 3 ms 20904 KB Output is correct
47 Correct 3 ms 20772 KB Output is correct
48 Correct 0 ms 20904 KB Output is correct
49 Correct 0 ms 20772 KB Output is correct
50 Correct 0 ms 20904 KB Output is correct
51 Correct 293 ms 25660 KB Output is correct
52 Correct 139 ms 33708 KB Output is correct
53 Correct 396 ms 29152 KB Output is correct
54 Correct 159 ms 25052 KB Output is correct
55 Correct 223 ms 25316 KB Output is correct
56 Correct 149 ms 33712 KB Output is correct
57 Correct 319 ms 29644 KB Output is correct
58 Correct 166 ms 25000 KB Output is correct
59 Correct 266 ms 25592 KB Output is correct
60 Correct 193 ms 33708 KB Output is correct
61 Correct 299 ms 29620 KB Output is correct
62 Correct 186 ms 25212 KB Output is correct
63 Correct 259 ms 25336 KB Output is correct
64 Correct 126 ms 33708 KB Output is correct
65 Correct 323 ms 29592 KB Output is correct
66 Correct 146 ms 25024 KB Output is correct
67 Correct 216 ms 25400 KB Output is correct
68 Correct 143 ms 33716 KB Output is correct
69 Correct 183 ms 28560 KB Output is correct
70 Correct 123 ms 25084 KB Output is correct