Submission #550706

# Submission time Handle Problem Language Result Execution time Memory
550706 2022-04-18T21:20:27 Z nafis_shifat Election Campaign (JOI15_election_campaign) C++17
0 / 100
1000 ms 43516 KB
#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
using namespace std;
const int mxn=1e5+5;
const int inf=1e9;
int n;

vector<int> adj[mxn];
int level[mxn] = {};
int pa[18][mxn] = {};
int in[mxn] = {};
int T = 0;
int out[mxn];
int q;
vector<tuple<int,int,int>> con[mxn];
void dfs0(int u,int prev) {
	level[u] = level[prev]+1;
	pa[0][u] = prev;
	for(int i = 1; i < 18; i++) pa[i][u] = pa[i - 1][pa[i - 1][u]];
	for(int v : adj[u]) {
		if(v == prev) continue;
		dfs0(v,u);
	}
}


int findLCA(int u,int v) {

	if(level[u] < level[v]) swap(u,v);
	int diff = level[u] - level[v];
	for(int i = 0;i < 18;i++) if((diff>>i)&1) u = pa[i][u];
	if(u == v) return u;
	for(int i = 17; i >= 0; i--) {
		if(pa[i][u] != pa[i][v]){
			u = pa[i][u];
			v = pa[i][v];
		}
	}
	return pa[0][u];
}


ll dp[mxn] = {};
ll child_sum[mxn] = {};

ll tot[18][mxn];

ll get(int x, int dif) {
	ll sum = 0;

	for(int i = 17; i >= 0; i--) {
		if((dif >> i) & 1) {
			sum += tot[i][x];
			x = pa[i][x];
		}
	}
	return sum;

}
void dfs(int u, int prev) {

	for(int v : adj[u]) {
		if(v == prev) continue;
		dfs(v, u);
		child_sum[u] += dp[v];
	}
	dp[u] = child_sum[u];

	for(auto [a, b, c] : con[u]) {
		dp[u] = max(dp[u], get(a, level[a] - level[u]) + get(b, level[b] - level[u]) + c + child_sum[u]);
	}

	tot[0][u] = child_sum[u] - dp[u];

	for(int i = 1; i < 18; i++) for(int j = 1; j <= n; j++) {
		ll v1 = tot[i - 1][j];
		ll v2 = tot[i - 1][pa[i - 1][j]];
		if(v1 != -1 && v2 != -1) tot[i][j] = v1 + v2;
	}
}
int main() {
	cin >> n;
	for(int i = 1; i < n; i++) {
		int u, v;
		scanf("%d%d", &u, &v);
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	dfs0(1, 0);

	memset(tot, -1, sizeof tot);

	cin >> q;
	for(int i = 1; i <= q; i++) {
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		con[findLCA(a, b)].push_back({a, b, c});
	}

	dfs(1, 0);

	cout<<dp[1]<<endl;




	
}

Compilation message

election_campaign.cpp: In function 'int main()':
election_campaign.cpp:86:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   86 |   scanf("%d%d", &u, &v);
      |   ~~~~~^~~~~~~~~~~~~~~~
election_campaign.cpp:97:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   97 |   scanf("%d%d%d", &a, &b, &c);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 19156 KB Output is correct
2 Correct 8 ms 19156 KB Output is correct
3 Correct 9 ms 19096 KB Output is correct
4 Correct 29 ms 19328 KB Output is correct
5 Execution timed out 1091 ms 32256 KB Time limit exceeded
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 19156 KB Output is correct
2 Correct 9 ms 19156 KB Output is correct
3 Correct 24 ms 19412 KB Output is correct
4 Execution timed out 1084 ms 43516 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 19156 KB Output is correct
2 Correct 9 ms 19156 KB Output is correct
3 Correct 24 ms 19412 KB Output is correct
4 Execution timed out 1084 ms 43516 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1076 ms 35076 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 19156 KB Output is correct
2 Correct 8 ms 19156 KB Output is correct
3 Correct 9 ms 19096 KB Output is correct
4 Correct 29 ms 19328 KB Output is correct
5 Execution timed out 1091 ms 32256 KB Time limit exceeded
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 19156 KB Output is correct
2 Correct 8 ms 19156 KB Output is correct
3 Correct 9 ms 19096 KB Output is correct
4 Correct 29 ms 19328 KB Output is correct
5 Execution timed out 1091 ms 32256 KB Time limit exceeded
6 Halted 0 ms 0 KB -