Submission #674917

# Submission time Handle Problem Language Result Execution time Memory
674917 2022-12-26T14:53:51 Z QwertyPi Roadside Advertisements (NOI17_roadsideadverts) C++14
100 / 100
141 ms 13236 KB
#include <bits/stdc++.h>
#define pii pair<int, int>
#define fi first
#define se second
using namespace std;

const int MAXN = 5e4 + 11;
vector<pii> G[MAXN];
int tin[MAXN], tout[MAXN], timer;
int p[MAXN], d[MAXN], w[MAXN], anc[MAXN][20], dep[MAXN];
void dfs_prec(int v, int pa = -1){
	tin[v] = ++timer;
	for(auto i : G[v]){
		int u = i.fi, _w = i.se;
		if(u != pa){
			w[u] = _w; d[u] = d[v] + w[u]; dep[u] = dep[v] + 1; anc[u][0] = p[u] = v;
			for(int j = 1; j < 20; j++) anc[u][j] = anc[anc[u][j - 1]][j - 1];
			dfs_prec(u, v);
		}
	}
	tout[v] = timer;
}

int kth_anc(int u, int k){
	for(int j = 19; j >= 0; j--){
		if(k >= (1 << j)) u = anc[u][j], k -= (1 << j);
	}
	return u;
}

int lca(int u, int v){
	if(dep[u] < dep[v]) swap(u, v);
	u = kth_anc(u, dep[u] - dep[v]);
	for(int j = 19; j >= 0; j--){
		if(anc[u][j] != anc[v][j]){
			u = anc[u][j], v = anc[v][j];
		}
	}
	return u == v ? u : p[u];
}

bool is_anc(int u, int v){
	return tin[u] <= tin[v] && tout[v] <= tout[u];
}

int32_t main(){
	int n; cin >> n;
	for(int i = 0; i < n - 1; i++){
		int u, v, w; cin >> u >> v >> w;
		G[u].push_back({v, w});
		G[v].push_back({u, w});
	}
	dfs_prec(0);
	int q; cin >> q;
	for(int i = 0; i < q; i++){
		vector<int> v;
		for(int j = 0; j < 5; j++){
			int val; cin >> val; v.push_back(val);
		}
		sort(v.begin(), v.end(), [](int a, int b){
			return tin[a] < tin[b];
		});
		vector<int> path; path.push_back(v[0]);
		int ans = 0;
		for(int i = 1; i < v.size(); i++){
			while(path.size() >= 2 && is_anc(lca(path.back(), v[i]), path[path.size() - 2])){
				ans += d[path.back()] - d[path[path.size() - 2]];
				path.pop_back();
			}
			if(is_anc(path.back(), v[i])){
				path.push_back(v[i]);
			}else{
				int l = lca(path.back(), v[i]);
				ans += d[path.back()] - d[l];
				path.pop_back();
				path.push_back(l);
				path.push_back(v[i]);
			}
		}
		while(path.size() >= 2){
			ans += d[path.back()] - d[path[path.size() - 2]];
			path.pop_back();
		}
		cout << ans << endl;
	}
}

Compilation message

roadsideadverts.cpp: In function 'int32_t main()':
roadsideadverts.cpp:65:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   65 |   for(int i = 1; i < v.size(); i++){
      |                  ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 120 ms 10172 KB Output is correct
2 Correct 103 ms 13108 KB Output is correct
3 Correct 101 ms 13076 KB Output is correct
4 Correct 109 ms 13236 KB Output is correct
5 Correct 110 ms 13044 KB Output is correct
6 Correct 121 ms 13108 KB Output is correct
7 Correct 101 ms 13028 KB Output is correct
8 Correct 107 ms 13076 KB Output is correct
9 Correct 103 ms 13144 KB Output is correct
10 Correct 105 ms 13132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 58 ms 8296 KB Output is correct
2 Correct 72 ms 12040 KB Output is correct
3 Correct 70 ms 12088 KB Output is correct
4 Correct 71 ms 12232 KB Output is correct
5 Correct 67 ms 12064 KB Output is correct
6 Correct 70 ms 12048 KB Output is correct
7 Correct 69 ms 12044 KB Output is correct
8 Correct 68 ms 12136 KB Output is correct
9 Correct 67 ms 12224 KB Output is correct
10 Correct 66 ms 12124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1492 KB Output is correct
2 Correct 120 ms 10172 KB Output is correct
3 Correct 103 ms 13108 KB Output is correct
4 Correct 101 ms 13076 KB Output is correct
5 Correct 109 ms 13236 KB Output is correct
6 Correct 110 ms 13044 KB Output is correct
7 Correct 121 ms 13108 KB Output is correct
8 Correct 101 ms 13028 KB Output is correct
9 Correct 107 ms 13076 KB Output is correct
10 Correct 103 ms 13144 KB Output is correct
11 Correct 105 ms 13132 KB Output is correct
12 Correct 58 ms 8296 KB Output is correct
13 Correct 72 ms 12040 KB Output is correct
14 Correct 70 ms 12088 KB Output is correct
15 Correct 71 ms 12232 KB Output is correct
16 Correct 67 ms 12064 KB Output is correct
17 Correct 70 ms 12048 KB Output is correct
18 Correct 69 ms 12044 KB Output is correct
19 Correct 68 ms 12136 KB Output is correct
20 Correct 67 ms 12224 KB Output is correct
21 Correct 66 ms 12124 KB Output is correct
22 Correct 97 ms 11292 KB Output is correct
23 Correct 96 ms 9508 KB Output is correct
24 Correct 102 ms 12512 KB Output is correct
25 Correct 104 ms 12428 KB Output is correct
26 Correct 141 ms 12424 KB Output is correct
27 Correct 107 ms 12392 KB Output is correct
28 Correct 120 ms 12480 KB Output is correct
29 Correct 128 ms 12424 KB Output is correct
30 Correct 117 ms 12484 KB Output is correct
31 Correct 107 ms 12516 KB Output is correct