Submission #958030

# Submission time Handle Problem Language Result Execution time Memory
958030 2024-04-04T17:20:07 Z starchan Election Campaign (JOI15_election_campaign) C++17
100 / 100
128 ms 29448 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;
}

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

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;
		auto [x, y] = lca(l, r);
		st[x].pb({w, {l, r}});
	}
	DFS(1, 0);
	cout << dp[1] << "\n";
	return 0;
}	
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13404 KB Output is correct
2 Correct 2 ms 13404 KB Output is correct
3 Correct 3 ms 13404 KB Output is correct
4 Correct 3 ms 13580 KB Output is correct
5 Correct 57 ms 18204 KB Output is correct
6 Correct 40 ms 25636 KB Output is correct
7 Correct 55 ms 23124 KB Output is correct
8 Correct 30 ms 18524 KB Output is correct
9 Correct 48 ms 21588 KB Output is correct
10 Correct 41 ms 18440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13400 KB Output is correct
2 Correct 3 ms 13404 KB Output is correct
3 Correct 3 ms 13660 KB Output is correct
4 Correct 63 ms 29012 KB Output is correct
5 Correct 61 ms 29008 KB Output is correct
6 Correct 61 ms 29148 KB Output is correct
7 Correct 65 ms 29012 KB Output is correct
8 Correct 65 ms 29000 KB Output is correct
9 Correct 67 ms 29160 KB Output is correct
10 Correct 58 ms 29016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13400 KB Output is correct
2 Correct 3 ms 13404 KB Output is correct
3 Correct 3 ms 13660 KB Output is correct
4 Correct 63 ms 29012 KB Output is correct
5 Correct 61 ms 29008 KB Output is correct
6 Correct 61 ms 29148 KB Output is correct
7 Correct 65 ms 29012 KB Output is correct
8 Correct 65 ms 29000 KB Output is correct
9 Correct 67 ms 29160 KB Output is correct
10 Correct 58 ms 29016 KB Output is correct
11 Correct 8 ms 14356 KB Output is correct
12 Correct 61 ms 29272 KB Output is correct
13 Correct 65 ms 29240 KB Output is correct
14 Correct 71 ms 29264 KB Output is correct
15 Correct 65 ms 29412 KB Output is correct
16 Correct 72 ms 29392 KB Output is correct
17 Correct 62 ms 29200 KB Output is correct
18 Correct 61 ms 29412 KB Output is correct
19 Correct 62 ms 29448 KB Output is correct
20 Correct 63 ms 29268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 98 ms 20744 KB Output is correct
2 Correct 62 ms 29060 KB Output is correct
3 Correct 89 ms 26088 KB Output is correct
4 Correct 98 ms 21420 KB Output is correct
5 Correct 84 ms 25500 KB Output is correct
6 Correct 90 ms 21304 KB Output is correct
7 Correct 92 ms 25248 KB Output is correct
8 Correct 81 ms 21072 KB Output is correct
9 Correct 57 ms 28992 KB Output is correct
10 Correct 128 ms 24124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13404 KB Output is correct
2 Correct 2 ms 13404 KB Output is correct
3 Correct 3 ms 13404 KB Output is correct
4 Correct 3 ms 13580 KB Output is correct
5 Correct 57 ms 18204 KB Output is correct
6 Correct 40 ms 25636 KB Output is correct
7 Correct 55 ms 23124 KB Output is correct
8 Correct 30 ms 18524 KB Output is correct
9 Correct 48 ms 21588 KB Output is correct
10 Correct 41 ms 18440 KB Output is correct
11 Correct 4 ms 13664 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 13660 KB Output is correct
15 Correct 4 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 13660 KB Output is correct
19 Correct 4 ms 13660 KB Output is correct
20 Correct 3 ms 13660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13404 KB Output is correct
2 Correct 2 ms 13404 KB Output is correct
3 Correct 3 ms 13404 KB Output is correct
4 Correct 3 ms 13580 KB Output is correct
5 Correct 57 ms 18204 KB Output is correct
6 Correct 40 ms 25636 KB Output is correct
7 Correct 55 ms 23124 KB Output is correct
8 Correct 30 ms 18524 KB Output is correct
9 Correct 48 ms 21588 KB Output is correct
10 Correct 41 ms 18440 KB Output is correct
11 Correct 3 ms 13400 KB Output is correct
12 Correct 3 ms 13404 KB Output is correct
13 Correct 3 ms 13660 KB Output is correct
14 Correct 63 ms 29012 KB Output is correct
15 Correct 61 ms 29008 KB Output is correct
16 Correct 61 ms 29148 KB Output is correct
17 Correct 65 ms 29012 KB Output is correct
18 Correct 65 ms 29000 KB Output is correct
19 Correct 67 ms 29160 KB Output is correct
20 Correct 58 ms 29016 KB Output is correct
21 Correct 8 ms 14356 KB Output is correct
22 Correct 61 ms 29272 KB Output is correct
23 Correct 65 ms 29240 KB Output is correct
24 Correct 71 ms 29264 KB Output is correct
25 Correct 65 ms 29412 KB Output is correct
26 Correct 72 ms 29392 KB Output is correct
27 Correct 62 ms 29200 KB Output is correct
28 Correct 61 ms 29412 KB Output is correct
29 Correct 62 ms 29448 KB Output is correct
30 Correct 63 ms 29268 KB Output is correct
31 Correct 98 ms 20744 KB Output is correct
32 Correct 62 ms 29060 KB Output is correct
33 Correct 89 ms 26088 KB Output is correct
34 Correct 98 ms 21420 KB Output is correct
35 Correct 84 ms 25500 KB Output is correct
36 Correct 90 ms 21304 KB Output is correct
37 Correct 92 ms 25248 KB Output is correct
38 Correct 81 ms 21072 KB Output is correct
39 Correct 57 ms 28992 KB Output is correct
40 Correct 128 ms 24124 KB Output is correct
41 Correct 4 ms 13664 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 13660 KB Output is correct
45 Correct 4 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 13660 KB Output is correct
49 Correct 4 ms 13660 KB Output is correct
50 Correct 3 ms 13660 KB Output is correct
51 Correct 87 ms 21436 KB Output is correct
52 Correct 62 ms 29268 KB Output is correct
53 Correct 122 ms 24668 KB Output is correct
54 Correct 63 ms 21644 KB Output is correct
55 Correct 109 ms 21084 KB Output is correct
56 Correct 61 ms 29268 KB Output is correct
57 Correct 94 ms 25352 KB Output is correct
58 Correct 92 ms 21516 KB Output is correct
59 Correct 85 ms 21328 KB Output is correct
60 Correct 61 ms 29276 KB Output is correct
61 Correct 91 ms 25364 KB Output is correct
62 Correct 88 ms 21492 KB Output is correct
63 Correct 95 ms 21056 KB Output is correct
64 Correct 61 ms 29288 KB Output is correct
65 Correct 95 ms 25224 KB Output is correct
66 Correct 67 ms 21648 KB Output is correct
67 Correct 120 ms 21332 KB Output is correct
68 Correct 67 ms 29264 KB Output is correct
69 Correct 78 ms 24144 KB Output is correct
70 Correct 84 ms 21560 KB Output is correct