답안 #26464

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
26464 2017-07-01T05:37:08 Z rubabredwan Election Campaign (JOI15_election_campaign) C++14
10 / 100
246 ms 45332 KB
/*  Bismillahir Rahmanir Rahim  */

#include <bits/stdc++.h>

#define debug(x) cout << #x << ' ' << x << endl

using namespace std;

const int N = 200005;

struct data{
	int u, v, c;
	data () {}
	data (int _u, int _v, int _c){
		u = _u, v = _v, c = _c;
	}
};

struct BIT{
	int t[N], n;
	void init(int _n){
		n = _n;
		memset(t, 0, sizeof(t));
	}
	void update(int pos, int val){
		while(pos <= n){
			t[pos] += val;
			pos += pos & -pos;
		}
	}
	int get(int pos){
		int ret = 0;
		while(pos){
			ret += t[pos];
			pos -= pos & -pos;
		}
		return ret;
	}
	int get(int l, int r){
		return get(r) - get(l - 1);
	}
};

int n;
int m; 
int a[N], b[N], c[N];
int dp[N];
vector <data> cnd[N];
vector <int> g[N];
int dep[N];
int pa[N][18];
int in[N], out[N], t;
BIT t1, t2;

void dfs(int at, int par){
	in[at] = ++t;
	pa[at][0] = par;
	for(int i = 1; i <= 16; ++i) pa[at][i] = pa[ pa[at][i-1] ][ i-1 ];
	for(auto u : g[at]){
		if(u != par){
			dep[u] = dep[at] + 1;
			dfs(u, at);
		}
	}
	out[at] = ++t;
}

int lca(int a, int b){
	if(dep[a] < dep[b]) swap(a, b);
	for(int i = 16; i >= 0; --i) if(dep[a] - (1 << i) >= dep[b]) a = pa[a][i];
	if(a == b) return a;
	for(int i = 16; i >= 0; --i) if(pa[a][i] != pa[b][i]) a = pa[a][i], b = pa[b][i];
	return pa[a][0];
}

void solve(int at, int par){
	int child_sum = 0;
	for(auto u : g[at]){
		if(u == par) continue;
		solve(u, at);
		dp[at] = max(dp[at], dp[u]);
		child_sum += dp[u];
	}
	for(auto f : cnd[at]){
		int u = f.u, v = f.v, c = f.c;
		int sum = child_sum;
		if(u == at){
			sum += t2.get(in[v]) - t1.get(in[v]);
		}
		else{
			sum += t2.get(in[u]) - t1.get(in[u]);
			sum += t2.get(in[v]) - t1.get(in[v]);
		}
		dp[at] = max(dp[at], c + sum);
	}
	t1.update(in[at], +dp[at]);
	t1.update(out[at], -dp[at]);
	t2.update(in[at], +child_sum);
	t2.update(out[at], -child_sum);
}

int main(){
	int x, y;
	scanf("%d", &n);
	for(int i = 1; i < n; ++i){
		scanf("%d %d", &x, &y);
		g[x].push_back(y);
		g[y].push_back(x);
	}
	dfs(1, 0);
	scanf("%d", &m);
	for(int i = 1; i <= m; ++i){
		scanf("%d %d %d", &a[i], &b[i], &c[i]);
		int lc = lca(a[i], b[i]);
		if(b[i] == lc) swap(a[i], b[i]);
		cnd[lc].push_back(data(a[i], b[i], c[i]));
	}
	t1.init(2 * n);
	t2.init(2 * n);
	solve(1, 0);
	printf("%d\n", dp[1]);
	return 0;
}

Compilation message

election_campaign.cpp: In function 'int main()':
election_campaign.cpp:104:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
                 ^
election_campaign.cpp:106:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &x, &y);
                         ^
election_campaign.cpp:111:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &m);
                 ^
election_campaign.cpp:113:41: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d %d", &a[i], &b[i], &c[i]);
                                         ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 32492 KB Output is correct
2 Correct 0 ms 32492 KB Output is correct
3 Correct 3 ms 32492 KB Output is correct
4 Incorrect 6 ms 32492 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 32492 KB Output is correct
2 Correct 0 ms 32492 KB Output is correct
3 Correct 0 ms 32492 KB Output is correct
4 Correct 236 ms 45328 KB Output is correct
5 Correct 209 ms 45328 KB Output is correct
6 Correct 163 ms 45324 KB Output is correct
7 Correct 229 ms 45324 KB Output is correct
8 Correct 246 ms 45328 KB Output is correct
9 Correct 183 ms 45328 KB Output is correct
10 Correct 243 ms 45324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 32492 KB Output is correct
2 Correct 0 ms 32492 KB Output is correct
3 Correct 0 ms 32492 KB Output is correct
4 Correct 236 ms 45328 KB Output is correct
5 Correct 209 ms 45328 KB Output is correct
6 Correct 163 ms 45324 KB Output is correct
7 Correct 229 ms 45324 KB Output is correct
8 Correct 246 ms 45328 KB Output is correct
9 Correct 183 ms 45328 KB Output is correct
10 Correct 243 ms 45324 KB Output is correct
11 Correct 26 ms 33020 KB Output is correct
12 Correct 206 ms 45328 KB Output is correct
13 Correct 179 ms 45328 KB Output is correct
14 Correct 143 ms 45332 KB Output is correct
15 Correct 139 ms 45328 KB Output is correct
16 Correct 213 ms 45324 KB Output is correct
17 Correct 226 ms 45332 KB Output is correct
18 Correct 213 ms 45324 KB Output is correct
19 Correct 233 ms 45324 KB Output is correct
20 Correct 159 ms 45332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 173 ms 37532 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 32492 KB Output is correct
2 Correct 0 ms 32492 KB Output is correct
3 Correct 3 ms 32492 KB Output is correct
4 Incorrect 6 ms 32492 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 32492 KB Output is correct
2 Correct 0 ms 32492 KB Output is correct
3 Correct 3 ms 32492 KB Output is correct
4 Incorrect 6 ms 32492 KB Output isn't correct
5 Halted 0 ms 0 KB -