답안 #931857

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
931857 2024-02-22T12:51:31 Z parlimoos Election Campaign (JOI15_election_campaign) C++14
100 / 100
134 ms 38084 KB
//Be Name KHODA
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
#define pb push_back
#define pp pop_back
#define lb lower_bound
#define ub upper_bound
#define cl clear
#define bg begin
#define arr(x) array<int , x>
#define endl '\n'

int n , m;
vector<int> tr[100000];
int times[100000][2] , bl[100000][20];
int seg[400001][3] , is[100000];
int dp[100000];
vector<arr(3)> pths[100000];

void buildSeg(int l = 0 , int r = n - 1 , int i = 1){
	seg[i][0] = l , seg[i][1] = r;
	if(l == r) is[l] = i;
	else{
		int c = (l + r - 1) >>  1;
		buildSeg(l , c , i << 1) , buildSeg(c + 1 , r , (i << 1) | 1);
	}
}
void updSeg(int l , int r , int i , int val){
	if(seg[i][0] >= l and seg[i][1] <= r) seg[i][2] += val;
	else if(seg[i][1] >= l and seg[i][0] <= r) updSeg(l , r , i << 1 , val) , updSeg(l , r , (i << 1) | 1 , val);
}
int getSeg(int i){
	int res = seg[i][2];
	while(i > 1) i >>= 1 , res += seg[i][2];
	return res;
}
int timer = -1;
void dfs1(int v = 0 , int p = -1){
	times[v][0] = ++timer;
	bl[v][0] = p;
	for(int i = 1 ; i < 20 ; i++){
		if(bl[v][i - 1] == -1) bl[v][i] = -1;
		else bl[v][i] = bl[bl[v][i - 1]][i - 1];
	}
	for(int u : tr[v]) if(u != p) dfs1(u , v);
	times[v][1] = timer;
}
int lca(int v1 , int v2){
	if(times[v1][0] <= times[v2][0] and times[v1][1] >= times[v2][0]) return v1;
	if(times[v2][0] <= times[v1][0] and times[v2][1] >= times[v1][0]) return v2;
	int u = v2;
	for(int i = 19 ; i >= 0 ; i--){
		if(bl[u][i] == -1) continue;
		if(times[bl[u][i]][0] <= times[v1][0] and times[bl[u][i]][1] >= times[v1][0]) continue;
		u = bl[u][i];
	}
	return bl[u][0];
}
void dfs2(int v = 0){
	int sz = 0;
	for(int u : tr[v]){
		if(u == bl[v][0]) continue;
		dfs2(u) , sz += dp[u];
	}
	updSeg(times[v][0] , times[v][0] , 1 , sz);
	for(int u : tr[v]){
		if(u == bl[v][0]) continue;
		updSeg(times[u][0] , times[u][1] , 1 , sz - dp[u]);
	}
	dp[v] = sz;
	for(auto pth : pths[v]){
		int u1 = pth[0] , u2 = pth[1] , c = pth[2];
		if(u1 == v and u2 == v) dp[v] = max(dp[v] , sz + c);
		else if(u1 == v) dp[v] = max(dp[v] , getSeg(is[times[u2][0]]) + c);
		else{
			int d = getSeg(is[times[u1][0]]) + getSeg(is[times[u2][0]]) - sz + c;
			dp[v] = max(dp[v] , d);
		}
	}
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n;
	for(int i = 1 ; i < n ; i++){
		int v , u;
		cin >> v >> u;
		v-- , u--;
		tr[v].pb(u) , tr[u].pb(v);
	}
	dfs1() , buildSeg();
	cin >> m;
	for(int i = 0 ; i < m ; i++){
		int v , u , c;
		cin >> v >> u >> c;
		v-- , u--;
		if(times[v][0] > times[u][0]) swap(v , u);
		int d = lca(v , u);
		pths[d].pb({v , u , c});
	}
	dfs2();
	cout << dp[0];
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10844 KB Output is correct
2 Correct 2 ms 10844 KB Output is correct
3 Correct 3 ms 11040 KB Output is correct
4 Correct 3 ms 10844 KB Output is correct
5 Correct 76 ms 23900 KB Output is correct
6 Correct 53 ms 34396 KB Output is correct
7 Correct 77 ms 30544 KB Output is correct
8 Correct 55 ms 23900 KB Output is correct
9 Correct 74 ms 28496 KB Output is correct
10 Correct 57 ms 23892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10844 KB Output is correct
2 Correct 2 ms 11036 KB Output is correct
3 Correct 3 ms 11100 KB Output is correct
4 Correct 81 ms 37716 KB Output is correct
5 Correct 80 ms 37716 KB Output is correct
6 Correct 80 ms 37800 KB Output is correct
7 Correct 82 ms 37568 KB Output is correct
8 Correct 84 ms 37792 KB Output is correct
9 Correct 80 ms 37716 KB Output is correct
10 Correct 80 ms 37712 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10844 KB Output is correct
2 Correct 2 ms 11036 KB Output is correct
3 Correct 3 ms 11100 KB Output is correct
4 Correct 81 ms 37716 KB Output is correct
5 Correct 80 ms 37716 KB Output is correct
6 Correct 80 ms 37800 KB Output is correct
7 Correct 82 ms 37568 KB Output is correct
8 Correct 84 ms 37792 KB Output is correct
9 Correct 80 ms 37716 KB Output is correct
10 Correct 80 ms 37712 KB Output is correct
11 Correct 7 ms 11868 KB Output is correct
12 Correct 89 ms 37836 KB Output is correct
13 Correct 83 ms 37880 KB Output is correct
14 Correct 82 ms 37972 KB Output is correct
15 Correct 83 ms 37972 KB Output is correct
16 Correct 84 ms 38004 KB Output is correct
17 Correct 84 ms 37968 KB Output is correct
18 Correct 84 ms 37908 KB Output is correct
19 Correct 82 ms 38084 KB Output is correct
20 Correct 82 ms 37972 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 100 ms 26376 KB Output is correct
2 Correct 79 ms 37788 KB Output is correct
3 Correct 125 ms 33360 KB Output is correct
4 Correct 87 ms 26884 KB Output is correct
5 Correct 112 ms 32592 KB Output is correct
6 Correct 84 ms 26940 KB Output is correct
7 Correct 120 ms 32260 KB Output is correct
8 Correct 103 ms 26708 KB Output is correct
9 Correct 79 ms 37568 KB Output is correct
10 Correct 122 ms 31108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10844 KB Output is correct
2 Correct 2 ms 10844 KB Output is correct
3 Correct 3 ms 11040 KB Output is correct
4 Correct 3 ms 10844 KB Output is correct
5 Correct 76 ms 23900 KB Output is correct
6 Correct 53 ms 34396 KB Output is correct
7 Correct 77 ms 30544 KB Output is correct
8 Correct 55 ms 23900 KB Output is correct
9 Correct 74 ms 28496 KB Output is correct
10 Correct 57 ms 23892 KB Output is correct
11 Correct 3 ms 11352 KB Output is correct
12 Correct 3 ms 11100 KB Output is correct
13 Correct 3 ms 11100 KB Output is correct
14 Correct 3 ms 11100 KB Output is correct
15 Correct 3 ms 11100 KB Output is correct
16 Correct 4 ms 11100 KB Output is correct
17 Correct 3 ms 11100 KB Output is correct
18 Correct 3 ms 11100 KB Output is correct
19 Correct 4 ms 11100 KB Output is correct
20 Correct 3 ms 11100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10844 KB Output is correct
2 Correct 2 ms 10844 KB Output is correct
3 Correct 3 ms 11040 KB Output is correct
4 Correct 3 ms 10844 KB Output is correct
5 Correct 76 ms 23900 KB Output is correct
6 Correct 53 ms 34396 KB Output is correct
7 Correct 77 ms 30544 KB Output is correct
8 Correct 55 ms 23900 KB Output is correct
9 Correct 74 ms 28496 KB Output is correct
10 Correct 57 ms 23892 KB Output is correct
11 Correct 2 ms 10844 KB Output is correct
12 Correct 2 ms 11036 KB Output is correct
13 Correct 3 ms 11100 KB Output is correct
14 Correct 81 ms 37716 KB Output is correct
15 Correct 80 ms 37716 KB Output is correct
16 Correct 80 ms 37800 KB Output is correct
17 Correct 82 ms 37568 KB Output is correct
18 Correct 84 ms 37792 KB Output is correct
19 Correct 80 ms 37716 KB Output is correct
20 Correct 80 ms 37712 KB Output is correct
21 Correct 7 ms 11868 KB Output is correct
22 Correct 89 ms 37836 KB Output is correct
23 Correct 83 ms 37880 KB Output is correct
24 Correct 82 ms 37972 KB Output is correct
25 Correct 83 ms 37972 KB Output is correct
26 Correct 84 ms 38004 KB Output is correct
27 Correct 84 ms 37968 KB Output is correct
28 Correct 84 ms 37908 KB Output is correct
29 Correct 82 ms 38084 KB Output is correct
30 Correct 82 ms 37972 KB Output is correct
31 Correct 100 ms 26376 KB Output is correct
32 Correct 79 ms 37788 KB Output is correct
33 Correct 125 ms 33360 KB Output is correct
34 Correct 87 ms 26884 KB Output is correct
35 Correct 112 ms 32592 KB Output is correct
36 Correct 84 ms 26940 KB Output is correct
37 Correct 120 ms 32260 KB Output is correct
38 Correct 103 ms 26708 KB Output is correct
39 Correct 79 ms 37568 KB Output is correct
40 Correct 122 ms 31108 KB Output is correct
41 Correct 3 ms 11352 KB Output is correct
42 Correct 3 ms 11100 KB Output is correct
43 Correct 3 ms 11100 KB Output is correct
44 Correct 3 ms 11100 KB Output is correct
45 Correct 3 ms 11100 KB Output is correct
46 Correct 4 ms 11100 KB Output is correct
47 Correct 3 ms 11100 KB Output is correct
48 Correct 3 ms 11100 KB Output is correct
49 Correct 4 ms 11100 KB Output is correct
50 Correct 3 ms 11100 KB Output is correct
51 Correct 125 ms 26860 KB Output is correct
52 Correct 86 ms 38024 KB Output is correct
53 Correct 134 ms 31572 KB Output is correct
54 Correct 85 ms 27024 KB Output is correct
55 Correct 114 ms 26632 KB Output is correct
56 Correct 83 ms 38072 KB Output is correct
57 Correct 116 ms 32340 KB Output is correct
58 Correct 94 ms 27024 KB Output is correct
59 Correct 101 ms 26960 KB Output is correct
60 Correct 90 ms 37972 KB Output is correct
61 Correct 120 ms 32564 KB Output is correct
62 Correct 86 ms 27008 KB Output is correct
63 Correct 104 ms 26684 KB Output is correct
64 Correct 83 ms 37812 KB Output is correct
65 Correct 120 ms 32260 KB Output is correct
66 Correct 85 ms 27184 KB Output is correct
67 Correct 99 ms 26960 KB Output is correct
68 Correct 85 ms 38064 KB Output is correct
69 Correct 111 ms 30688 KB Output is correct
70 Correct 88 ms 27192 KB Output is correct