Submission #958031

# Submission time Handle Problem Language Result Execution time Memory
958031 2024-04-04T17:22:17 Z starchan Election Campaign (JOI15_election_campaign) C++17
100 / 100
130 ms 28476 KB
#include<bits/stdc++.h>
using namespace std;
#define in pair<int, int>
#define inn pair<int, in>
#define f first
#define s second
#define pb push_back
#define pob pop_back
#define INF (int)1e17
#define fast() ios_base::sync_with_stdio(false); cin.tie(NULL)
const int MX = 1e5+5, LOGM = 18;
vector<int> adj[MX];
vector<inn> st[MX];
int tin[MX], tout[MX], pa[LOGM][MX], dp[MX];

int timer;
void dfs(int u, int p)
{
	tin[u] = ++timer;
	pa[0][u] = p;
	for(int v: adj[u])
	{
		if(v==p)
			continue;
		dfs(v, u);
	}
	tout[u] = timer;
	return;
}

int lca(int u, int v)
{
	if(tin[u] >= tin[v])
		swap(u, v);
	if(tout[u] >= tout[v])
		return u;
	for(int i = LOGM-1; i >= 0; i--)
	{
		int k = pa[i][u];
		if(tout[k] >= tout[v])
			continue;
		u = k;
	}
	return pa[0][u];
}

int fen[MX];
void add(int x, int val)
{
	for(; x < MX; x+=(x&-x))
		fen[x]+=val;
	return;
}
int Q(int u)
{
	int ret = 0;
	for(int x = tin[u]; x; x-=(x&-x))
		ret+=fen[x];
	return ret;
}
void upd(int val, int u)
{
	add(tin[u], val);
	add(tout[u]+1, -val);
	return;
}


void DFS(int u, int p)
{
	int SUM = 0;
	for(int v: adj[u])
	{
		if(v == p)
			continue;
		DFS(v, u);
		SUM+=dp[v]; 
	}
	for(auto [C, ab]: st[u])
		dp[u] = max(dp[u], C+Q(ab.f)+Q(ab.s));
	dp[u]+=SUM;
	upd(SUM-dp[u], u);
	return;
}

signed main()
{
	fast();
	int n;
	cin >> n;
	int m = n-1;
	while(m--)
	{
		int u, v;
		cin >> u >> v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	pa[0][0] = 0; tin[0] = -1; tout[0] = INF;
	dfs(1, 0);
	for(int j = 1; j < LOGM; j++)
	{
		for(int i = 0; i <= n; i++)
			pa[j][i] = pa[j-1][pa[j-1][i]];
	}
	int q; cin >> q;
	while(q--)
	{
		int l, r, w;
		cin >> l >> r >> w;
		st[lca(l, r)].pb({w, {l, r}});
	}
	DFS(1, 0);
	cout << dp[1] << "\n";
	return 0;
}	
# Verdict Execution time Memory Grader output
1 Correct 2 ms 13404 KB Output is correct
2 Correct 3 ms 13404 KB Output is correct
3 Correct 3 ms 13404 KB Output is correct
4 Correct 3 ms 13656 KB Output is correct
5 Correct 48 ms 17640 KB Output is correct
6 Correct 31 ms 25432 KB Output is correct
7 Correct 51 ms 22608 KB Output is correct
8 Correct 31 ms 18012 KB Output is correct
9 Correct 44 ms 21160 KB Output is correct
10 Correct 31 ms 18012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 13404 KB Output is correct
2 Correct 3 ms 13404 KB Output is correct
3 Correct 3 ms 13660 KB Output is correct
4 Correct 56 ms 28172 KB Output is correct
5 Correct 57 ms 28124 KB Output is correct
6 Correct 56 ms 28244 KB Output is correct
7 Correct 58 ms 28244 KB Output is correct
8 Correct 56 ms 28240 KB Output is correct
9 Correct 62 ms 28244 KB Output is correct
10 Correct 64 ms 27996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 13404 KB Output is correct
2 Correct 3 ms 13404 KB Output is correct
3 Correct 3 ms 13660 KB Output is correct
4 Correct 56 ms 28172 KB Output is correct
5 Correct 57 ms 28124 KB Output is correct
6 Correct 56 ms 28244 KB Output is correct
7 Correct 58 ms 28244 KB Output is correct
8 Correct 56 ms 28240 KB Output is correct
9 Correct 62 ms 28244 KB Output is correct
10 Correct 64 ms 27996 KB Output is correct
11 Correct 7 ms 14428 KB Output is correct
12 Correct 66 ms 28216 KB Output is correct
13 Correct 72 ms 28172 KB Output is correct
14 Correct 73 ms 28240 KB Output is correct
15 Correct 60 ms 28180 KB Output is correct
16 Correct 66 ms 28476 KB Output is correct
17 Correct 58 ms 28160 KB Output is correct
18 Correct 59 ms 28152 KB Output is correct
19 Correct 59 ms 28100 KB Output is correct
20 Correct 60 ms 28240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 99 ms 19980 KB Output is correct
2 Correct 57 ms 28244 KB Output is correct
3 Correct 112 ms 25120 KB Output is correct
4 Correct 81 ms 20508 KB Output is correct
5 Correct 86 ms 24660 KB Output is correct
6 Correct 87 ms 20432 KB Output is correct
7 Correct 88 ms 24204 KB Output is correct
8 Correct 125 ms 20072 KB Output is correct
9 Correct 56 ms 28240 KB Output is correct
10 Correct 94 ms 23352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 13404 KB Output is correct
2 Correct 3 ms 13404 KB Output is correct
3 Correct 3 ms 13404 KB Output is correct
4 Correct 3 ms 13656 KB Output is correct
5 Correct 48 ms 17640 KB Output is correct
6 Correct 31 ms 25432 KB Output is correct
7 Correct 51 ms 22608 KB Output is correct
8 Correct 31 ms 18012 KB Output is correct
9 Correct 44 ms 21160 KB Output is correct
10 Correct 31 ms 18012 KB Output is correct
11 Correct 3 ms 13660 KB Output is correct
12 Correct 3 ms 13660 KB Output is correct
13 Correct 3 ms 13660 KB Output is correct
14 Correct 3 ms 13656 KB Output is correct
15 Correct 3 ms 13660 KB Output is correct
16 Correct 3 ms 13660 KB Output is correct
17 Correct 3 ms 13660 KB Output is correct
18 Correct 4 ms 13584 KB Output is correct
19 Correct 3 ms 13660 KB Output is correct
20 Correct 4 ms 13840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 13404 KB Output is correct
2 Correct 3 ms 13404 KB Output is correct
3 Correct 3 ms 13404 KB Output is correct
4 Correct 3 ms 13656 KB Output is correct
5 Correct 48 ms 17640 KB Output is correct
6 Correct 31 ms 25432 KB Output is correct
7 Correct 51 ms 22608 KB Output is correct
8 Correct 31 ms 18012 KB Output is correct
9 Correct 44 ms 21160 KB Output is correct
10 Correct 31 ms 18012 KB Output is correct
11 Correct 2 ms 13404 KB Output is correct
12 Correct 3 ms 13404 KB Output is correct
13 Correct 3 ms 13660 KB Output is correct
14 Correct 56 ms 28172 KB Output is correct
15 Correct 57 ms 28124 KB Output is correct
16 Correct 56 ms 28244 KB Output is correct
17 Correct 58 ms 28244 KB Output is correct
18 Correct 56 ms 28240 KB Output is correct
19 Correct 62 ms 28244 KB Output is correct
20 Correct 64 ms 27996 KB Output is correct
21 Correct 7 ms 14428 KB Output is correct
22 Correct 66 ms 28216 KB Output is correct
23 Correct 72 ms 28172 KB Output is correct
24 Correct 73 ms 28240 KB Output is correct
25 Correct 60 ms 28180 KB Output is correct
26 Correct 66 ms 28476 KB Output is correct
27 Correct 58 ms 28160 KB Output is correct
28 Correct 59 ms 28152 KB Output is correct
29 Correct 59 ms 28100 KB Output is correct
30 Correct 60 ms 28240 KB Output is correct
31 Correct 99 ms 19980 KB Output is correct
32 Correct 57 ms 28244 KB Output is correct
33 Correct 112 ms 25120 KB Output is correct
34 Correct 81 ms 20508 KB Output is correct
35 Correct 86 ms 24660 KB Output is correct
36 Correct 87 ms 20432 KB Output is correct
37 Correct 88 ms 24204 KB Output is correct
38 Correct 125 ms 20072 KB Output is correct
39 Correct 56 ms 28240 KB Output is correct
40 Correct 94 ms 23352 KB Output is correct
41 Correct 3 ms 13660 KB Output is correct
42 Correct 3 ms 13660 KB Output is correct
43 Correct 3 ms 13660 KB Output is correct
44 Correct 3 ms 13656 KB Output is correct
45 Correct 3 ms 13660 KB Output is correct
46 Correct 3 ms 13660 KB Output is correct
47 Correct 3 ms 13660 KB Output is correct
48 Correct 4 ms 13584 KB Output is correct
49 Correct 3 ms 13660 KB Output is correct
50 Correct 4 ms 13840 KB Output is correct
51 Correct 82 ms 20304 KB Output is correct
52 Correct 60 ms 28240 KB Output is correct
53 Correct 98 ms 23408 KB Output is correct
54 Correct 62 ms 20368 KB Output is correct
55 Correct 97 ms 19976 KB Output is correct
56 Correct 60 ms 28240 KB Output is correct
57 Correct 98 ms 24148 KB Output is correct
58 Correct 90 ms 20236 KB Output is correct
59 Correct 79 ms 20164 KB Output is correct
60 Correct 57 ms 28240 KB Output is correct
61 Correct 94 ms 24148 KB Output is correct
62 Correct 81 ms 20144 KB Output is correct
63 Correct 103 ms 19908 KB Output is correct
64 Correct 60 ms 28240 KB Output is correct
65 Correct 130 ms 24072 KB Output is correct
66 Correct 83 ms 20392 KB Output is correct
67 Correct 104 ms 20228 KB Output is correct
68 Correct 61 ms 28012 KB Output is correct
69 Correct 76 ms 22840 KB Output is correct
70 Correct 83 ms 20560 KB Output is correct