답안 #769724

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
769724 2023-06-30T06:58:05 Z raysh07 Election Campaign (JOI15_election_campaign) C++17
100 / 100
212 ms 47332 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF (int)1e18
#define f first
#define s second

mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());

struct Tree {
	vector<vector<int>> adj, lift;	
	vector<int> d, tin, tout;
	int n, timer;
	bool initialized = false;
	bool dfsed = false;

	void init(int nn){
		n = nn;
		adj.resize(n + 1);
		d.resize(n + 1);
		lift.resize(n + 1);
		tin.resize(n + 1);
		tout.resize(n + 1);
		for (int i = 1; i <= n; i++) adj[i].clear();
		for (int i = 0; i <= n; i++) lift[i].resize(20, 0);
		initialized = true;
	}

	void addEdge(int u, int v){
		if (!initialized){
			cout << "STUPID INITIALIZE\n";
			exit(0);
		}
		adj[u].push_back(v);
		adj[v].push_back(u);
	}

	void build(){
		for (int j = 1; j < 20; j++){
			for (int i = 1; i <= n; i++){
				lift[i][j] = lift[lift[i][j - 1]][j - 1];
			}
		}
	}

	void dfs(int u, int par){
		tin[u] = ++timer;
		for (int v : adj[u]){
			if (v != par){
				d[v] = d[u] + 1;
				lift[v][0] = u;
				dfs(v, u);
			}
		}
		tout[u] = ++timer;
	}

	void dfs(int root = 1){
		if (!initialized){
			cout << "STUPID INITIALIZE\n";
			exit(0);
		}
		d[root] = 0;
		timer = 0;
		dfs(root, -1);
		build();
		dfsed = true;
	}

	int lca(int a, int b){
		if (!dfsed){
			cout << "STUPID DFS\n";
			exit(0);
		}
		if (d[a] < d[b]) swap(a, b);
		int del = d[a] - d[b];
		for (int i = 0; i < 20; i++) if (del >> i & 1) a = lift[a][i];

		if (a == b) return a;

		for (int i = 19; i >= 0; i--) if (lift[a][i] != lift[b][i]){
			a = lift[a][i];
			b = lift[b][i];
		}
		return lift[a][0];
	}

	int dist(int a, int b){
		return d[a] + d[b] - 2 * d[lca(a, b)];
	}
};

struct info{
	int u, v, w;
};

int n;
const int N = 1e5 + 69;
Tree G;
vector <info> adj[N];
int dp[N][2]; //dp[i][1] = take i, dp[i][0] = not take
int f[2 * N];

void upd(int x, int v){
	for (int i = x; i <= 2 * n; i += i & (-i)) 
		f[i] += v;
} 

int query(int x){
	int res = 0;
	for (int i = x; i; i -= i & (-i)) res += f[i];

	return res;
}

int query(int l, int r){
	return query(r) - query(l - 1);
}

void dfs(int u, int par){
	for (int v : G.adj[u]){
		if (v != par){
			dfs(v, u);
			dp[u][0] += dp[v][1];
			dp[u][1] += dp[v][1];
		}
	}

	for (auto x : adj[u]){
		dp[u][1] = max(dp[u][1], dp[u][0] + x.w - query(G.tin[u], G.tin[x.u]) - query(G.tin[u], G.tin[x.v]));
	}

	upd(G.tin[u], dp[u][1] - dp[u][0]);
	upd(G.tout[u], dp[u][0] - dp[u][1]);
}

void Solve()
{
	cin >> n;

	G.init(n);
	for (int i = 1; i < n; i++){
		int u, v; cin >> u >> v;

		G.addEdge(u, v);
	}

	G.dfs();

	int m; cin >> m;
	while (m--){
		int u, v, w; cin >> u >> v >> w;

		info a1;
		a1.u = u;
		a1.v = v;
		a1.w = w;
		adj[G.lca(u, v)].push_back(a1);
	}

	dfs(1, -1);

	cout << dp[1][1] << "\n";
}

int32_t main() 
{
    auto begin = std::chrono::high_resolution_clock::now();
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
  //  cin >> t;
    for(int i = 1; i <= t; i++) 
    {
        //cout << "Case #" << i << ": ";
        Solve();
    }
    auto end = std::chrono::high_resolution_clock::now();
    auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
    cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n"; 
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 4 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2976 KB Output is correct
5 Correct 88 ms 33968 KB Output is correct
6 Correct 58 ms 40948 KB Output is correct
7 Correct 89 ms 39756 KB Output is correct
8 Correct 71 ms 33688 KB Output is correct
9 Correct 123 ms 38200 KB Output is correct
10 Correct 67 ms 33648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 3028 KB Output is correct
4 Correct 109 ms 44492 KB Output is correct
5 Correct 117 ms 46972 KB Output is correct
6 Correct 104 ms 47008 KB Output is correct
7 Correct 117 ms 46972 KB Output is correct
8 Correct 116 ms 47052 KB Output is correct
9 Correct 104 ms 46972 KB Output is correct
10 Correct 115 ms 46948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 3028 KB Output is correct
4 Correct 109 ms 44492 KB Output is correct
5 Correct 117 ms 46972 KB Output is correct
6 Correct 104 ms 47008 KB Output is correct
7 Correct 117 ms 46972 KB Output is correct
8 Correct 116 ms 47052 KB Output is correct
9 Correct 104 ms 46972 KB Output is correct
10 Correct 115 ms 46948 KB Output is correct
11 Correct 12 ms 4240 KB Output is correct
12 Correct 146 ms 47240 KB Output is correct
13 Correct 115 ms 47244 KB Output is correct
14 Correct 104 ms 47308 KB Output is correct
15 Correct 117 ms 47332 KB Output is correct
16 Correct 108 ms 47332 KB Output is correct
17 Correct 151 ms 47256 KB Output is correct
18 Correct 131 ms 47228 KB Output is correct
19 Correct 105 ms 47248 KB Output is correct
20 Correct 113 ms 47312 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 160 ms 36824 KB Output is correct
2 Correct 102 ms 46988 KB Output is correct
3 Correct 195 ms 44232 KB Output is correct
4 Correct 98 ms 38216 KB Output is correct
5 Correct 163 ms 43580 KB Output is correct
6 Correct 108 ms 37976 KB Output is correct
7 Correct 212 ms 43408 KB Output is correct
8 Correct 139 ms 39436 KB Output is correct
9 Correct 111 ms 47052 KB Output is correct
10 Correct 208 ms 42240 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 4 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2976 KB Output is correct
5 Correct 88 ms 33968 KB Output is correct
6 Correct 58 ms 40948 KB Output is correct
7 Correct 89 ms 39756 KB Output is correct
8 Correct 71 ms 33688 KB Output is correct
9 Correct 123 ms 38200 KB Output is correct
10 Correct 67 ms 33648 KB Output is correct
11 Correct 2 ms 2948 KB Output is correct
12 Correct 2 ms 3028 KB Output is correct
13 Correct 2 ms 3028 KB Output is correct
14 Correct 2 ms 3080 KB Output is correct
15 Correct 2 ms 3028 KB Output is correct
16 Correct 2 ms 3028 KB Output is correct
17 Correct 2 ms 3028 KB Output is correct
18 Correct 3 ms 3200 KB Output is correct
19 Correct 2 ms 3028 KB Output is correct
20 Correct 2 ms 3028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 4 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2976 KB Output is correct
5 Correct 88 ms 33968 KB Output is correct
6 Correct 58 ms 40948 KB Output is correct
7 Correct 89 ms 39756 KB Output is correct
8 Correct 71 ms 33688 KB Output is correct
9 Correct 123 ms 38200 KB Output is correct
10 Correct 67 ms 33648 KB Output is correct
11 Correct 1 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 3028 KB Output is correct
14 Correct 109 ms 44492 KB Output is correct
15 Correct 117 ms 46972 KB Output is correct
16 Correct 104 ms 47008 KB Output is correct
17 Correct 117 ms 46972 KB Output is correct
18 Correct 116 ms 47052 KB Output is correct
19 Correct 104 ms 46972 KB Output is correct
20 Correct 115 ms 46948 KB Output is correct
21 Correct 12 ms 4240 KB Output is correct
22 Correct 146 ms 47240 KB Output is correct
23 Correct 115 ms 47244 KB Output is correct
24 Correct 104 ms 47308 KB Output is correct
25 Correct 117 ms 47332 KB Output is correct
26 Correct 108 ms 47332 KB Output is correct
27 Correct 151 ms 47256 KB Output is correct
28 Correct 131 ms 47228 KB Output is correct
29 Correct 105 ms 47248 KB Output is correct
30 Correct 113 ms 47312 KB Output is correct
31 Correct 160 ms 36824 KB Output is correct
32 Correct 102 ms 46988 KB Output is correct
33 Correct 195 ms 44232 KB Output is correct
34 Correct 98 ms 38216 KB Output is correct
35 Correct 163 ms 43580 KB Output is correct
36 Correct 108 ms 37976 KB Output is correct
37 Correct 212 ms 43408 KB Output is correct
38 Correct 139 ms 39436 KB Output is correct
39 Correct 111 ms 47052 KB Output is correct
40 Correct 208 ms 42240 KB Output is correct
41 Correct 2 ms 2948 KB Output is correct
42 Correct 2 ms 3028 KB Output is correct
43 Correct 2 ms 3028 KB Output is correct
44 Correct 2 ms 3080 KB Output is correct
45 Correct 2 ms 3028 KB Output is correct
46 Correct 2 ms 3028 KB Output is correct
47 Correct 2 ms 3028 KB Output is correct
48 Correct 3 ms 3200 KB Output is correct
49 Correct 2 ms 3028 KB Output is correct
50 Correct 2 ms 3028 KB Output is correct
51 Correct 143 ms 39668 KB Output is correct
52 Correct 126 ms 47300 KB Output is correct
53 Correct 192 ms 42756 KB Output is correct
54 Correct 112 ms 38348 KB Output is correct
55 Correct 150 ms 39624 KB Output is correct
56 Correct 115 ms 47212 KB Output is correct
57 Correct 175 ms 43460 KB Output is correct
58 Correct 110 ms 38340 KB Output is correct
59 Correct 138 ms 39724 KB Output is correct
60 Correct 115 ms 47248 KB Output is correct
61 Correct 164 ms 43572 KB Output is correct
62 Correct 114 ms 38148 KB Output is correct
63 Correct 135 ms 39296 KB Output is correct
64 Correct 114 ms 47292 KB Output is correct
65 Correct 207 ms 43320 KB Output is correct
66 Correct 120 ms 38168 KB Output is correct
67 Correct 147 ms 39548 KB Output is correct
68 Correct 120 ms 47288 KB Output is correct
69 Correct 161 ms 42208 KB Output is correct
70 Correct 105 ms 38340 KB Output is correct