Submission #1093555

# Submission time Handle Problem Language Result Execution time Memory
1093555 2024-09-27T03:16:57 Z vjudge1 Railway (BOI17_railway) C++17
100 / 100
84 ms 51660 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 1e5;

vector<pair<int, int>> adj[N + 8];
int pe_id[N + 8];
int h[N + 8], tin[N + 8], tout[N + 8];
pair<int, int> euler[N * 2 + 8];
int upd[N + 8];

void add_edge(int u, int v, int id) {adj[u].push_back({v, id}); adj[v].push_back({u, id});}

int timer = 0;
void DFS_EulerTour(int u, int p) {
	int v, id;
	tin[u] = ++timer; euler[timer] = {h[u], u};
	for (pair<int, int> E : adj[u]) if (E.first != p) {
		tie(v, id) = E; h[v] = h[u] + 1; pe_id[v] = id; 
		DFS_EulerTour(v, u); euler[++timer] = {h[u], u};
	}
	tout[u] = timer;
}

pair<int, int> SpT[N * 2 + 8][20];

int lg;
void build() {
	for (lg = 0; lg <= __lg(timer); ++lg) for (int i = 1; i + (1 << lg) - 1 <= timer; ++i) 
		if (!lg) SpT[i][lg] = euler[i];
		else SpT[i][lg] = min(SpT[i][lg - 1], SpT[i + (1 << (lg - 1))][lg - 1]);
}

pair<int, int> range_min(int l, int r) {
	lg = __lg(r - l + 1);
	return min(SpT[l][lg], SpT[r + 1 - (1 << lg)][lg]);
}

int LCA(int u, int v) {
	if (tin[u] > tin[v]) swap(u, v);
	return range_min(tin[u], tin[v]).second;
}

bool custom(int u, int v) {return tin[u] < tin[v];}

void DFS(int u, int p) {
	for (pair<int, int> E : adj[u]) if (E.first != p) {
		DFS(E.first, u);
		upd[u] += upd[E.first];
	}
}

signed main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int n, q, k, u, v; cin >> n >> q >> k;
	for (int i = 1; i < n; ++i) cin >> u >> v, add_edge(u, v, i);
	DFS_EulerTour(1, 0); build();
	int sz; vector<int> nodes;
	while (q--) {
		cin >> sz; nodes.clear();
		while (sz--) cin >> u, nodes.push_back(u);
		sort(nodes.begin(), nodes.end(), custom); nodes.push_back(nodes[0]);
		for (int i = 0; i + 1 < nodes.size(); ++i) {
			++upd[nodes[i]]; ++upd[nodes[i + 1]];
			upd[LCA(nodes[i], nodes[i + 1])] -= 2;
		}
		// for (int i : nodes) cout << i << ' ';
		// cout << '\n';
	}
	DFS(1, 0); vector<int> ans; 
	for (int i = 2; i <= n; ++i) if (upd[i] >= k + k) ans.push_back(pe_id[i]);
	sort(ans.begin(), ans.end()); cout << ans.size() << '\n';
	for (int i : ans) cout << i << ' ';
}

Compilation message

railway.cpp: In function 'int main()':
railway.cpp:64:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   64 |   for (int i = 0; i + 1 < nodes.size(); ++i) {
      |                   ~~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2652 KB Output is correct
2 Correct 6 ms 6744 KB Output is correct
3 Correct 6 ms 6744 KB Output is correct
4 Correct 2 ms 2648 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 6 ms 7260 KB Output is correct
7 Correct 5 ms 6668 KB Output is correct
8 Correct 5 ms 6668 KB Output is correct
9 Correct 5 ms 6776 KB Output is correct
10 Correct 1 ms 2652 KB Output is correct
11 Correct 2 ms 2652 KB Output is correct
12 Correct 1 ms 2648 KB Output is correct
13 Correct 2 ms 2652 KB Output is correct
14 Correct 1 ms 2652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2652 KB Output is correct
2 Correct 6 ms 6744 KB Output is correct
3 Correct 6 ms 6744 KB Output is correct
4 Correct 2 ms 2648 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 6 ms 7260 KB Output is correct
7 Correct 5 ms 6668 KB Output is correct
8 Correct 5 ms 6668 KB Output is correct
9 Correct 5 ms 6776 KB Output is correct
10 Correct 1 ms 2652 KB Output is correct
11 Correct 2 ms 2652 KB Output is correct
12 Correct 1 ms 2648 KB Output is correct
13 Correct 2 ms 2652 KB Output is correct
14 Correct 1 ms 2652 KB Output is correct
15 Correct 14 ms 7004 KB Output is correct
16 Correct 15 ms 7064 KB Output is correct
17 Correct 15 ms 7256 KB Output is correct
18 Correct 6 ms 7100 KB Output is correct
19 Correct 6 ms 6668 KB Output is correct
20 Correct 14 ms 7364 KB Output is correct
21 Correct 16 ms 7260 KB Output is correct
22 Correct 1 ms 2652 KB Output is correct
23 Correct 5 ms 6604 KB Output is correct
24 Correct 6 ms 6804 KB Output is correct
25 Correct 1 ms 2652 KB Output is correct
26 Correct 1 ms 2652 KB Output is correct
27 Correct 6 ms 7000 KB Output is correct
28 Correct 6 ms 6748 KB Output is correct
29 Correct 5 ms 6748 KB Output is correct
30 Correct 6 ms 6748 KB Output is correct
31 Correct 1 ms 2652 KB Output is correct
32 Correct 2 ms 2652 KB Output is correct
33 Correct 1 ms 2652 KB Output is correct
34 Correct 1 ms 2652 KB Output is correct
35 Correct 1 ms 2652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 73 ms 51204 KB Output is correct
2 Correct 1 ms 2648 KB Output is correct
3 Correct 71 ms 50960 KB Output is correct
4 Correct 65 ms 50256 KB Output is correct
5 Correct 84 ms 51144 KB Output is correct
6 Correct 70 ms 51500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 68 ms 46932 KB Output is correct
2 Correct 70 ms 43316 KB Output is correct
3 Correct 72 ms 43016 KB Output is correct
4 Correct 68 ms 43092 KB Output is correct
5 Correct 68 ms 42908 KB Output is correct
6 Correct 64 ms 46932 KB Output is correct
7 Correct 62 ms 46928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 68 ms 46932 KB Output is correct
2 Correct 70 ms 43316 KB Output is correct
3 Correct 72 ms 43016 KB Output is correct
4 Correct 68 ms 43092 KB Output is correct
5 Correct 68 ms 42908 KB Output is correct
6 Correct 64 ms 46932 KB Output is correct
7 Correct 62 ms 46928 KB Output is correct
8 Correct 68 ms 46904 KB Output is correct
9 Correct 67 ms 47040 KB Output is correct
10 Correct 73 ms 51404 KB Output is correct
11 Correct 67 ms 51148 KB Output is correct
12 Correct 63 ms 42832 KB Output is correct
13 Correct 60 ms 42832 KB Output is correct
14 Correct 68 ms 42836 KB Output is correct
15 Correct 73 ms 43020 KB Output is correct
16 Correct 67 ms 43092 KB Output is correct
17 Correct 68 ms 43116 KB Output is correct
18 Correct 68 ms 43148 KB Output is correct
19 Correct 69 ms 43348 KB Output is correct
20 Correct 78 ms 47156 KB Output is correct
21 Correct 67 ms 47396 KB Output is correct
22 Correct 65 ms 47224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2652 KB Output is correct
2 Correct 6 ms 6744 KB Output is correct
3 Correct 6 ms 6744 KB Output is correct
4 Correct 2 ms 2648 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 6 ms 7260 KB Output is correct
7 Correct 5 ms 6668 KB Output is correct
8 Correct 5 ms 6668 KB Output is correct
9 Correct 5 ms 6776 KB Output is correct
10 Correct 1 ms 2652 KB Output is correct
11 Correct 2 ms 2652 KB Output is correct
12 Correct 1 ms 2648 KB Output is correct
13 Correct 2 ms 2652 KB Output is correct
14 Correct 1 ms 2652 KB Output is correct
15 Correct 14 ms 7004 KB Output is correct
16 Correct 15 ms 7064 KB Output is correct
17 Correct 15 ms 7256 KB Output is correct
18 Correct 6 ms 7100 KB Output is correct
19 Correct 6 ms 6668 KB Output is correct
20 Correct 14 ms 7364 KB Output is correct
21 Correct 16 ms 7260 KB Output is correct
22 Correct 1 ms 2652 KB Output is correct
23 Correct 5 ms 6604 KB Output is correct
24 Correct 6 ms 6804 KB Output is correct
25 Correct 1 ms 2652 KB Output is correct
26 Correct 1 ms 2652 KB Output is correct
27 Correct 6 ms 7000 KB Output is correct
28 Correct 6 ms 6748 KB Output is correct
29 Correct 5 ms 6748 KB Output is correct
30 Correct 6 ms 6748 KB Output is correct
31 Correct 1 ms 2652 KB Output is correct
32 Correct 2 ms 2652 KB Output is correct
33 Correct 1 ms 2652 KB Output is correct
34 Correct 1 ms 2652 KB Output is correct
35 Correct 1 ms 2652 KB Output is correct
36 Correct 73 ms 51204 KB Output is correct
37 Correct 1 ms 2648 KB Output is correct
38 Correct 71 ms 50960 KB Output is correct
39 Correct 65 ms 50256 KB Output is correct
40 Correct 84 ms 51144 KB Output is correct
41 Correct 70 ms 51500 KB Output is correct
42 Correct 68 ms 46932 KB Output is correct
43 Correct 70 ms 43316 KB Output is correct
44 Correct 72 ms 43016 KB Output is correct
45 Correct 68 ms 43092 KB Output is correct
46 Correct 68 ms 42908 KB Output is correct
47 Correct 64 ms 46932 KB Output is correct
48 Correct 62 ms 46928 KB Output is correct
49 Correct 68 ms 46904 KB Output is correct
50 Correct 67 ms 47040 KB Output is correct
51 Correct 73 ms 51404 KB Output is correct
52 Correct 67 ms 51148 KB Output is correct
53 Correct 63 ms 42832 KB Output is correct
54 Correct 60 ms 42832 KB Output is correct
55 Correct 68 ms 42836 KB Output is correct
56 Correct 73 ms 43020 KB Output is correct
57 Correct 67 ms 43092 KB Output is correct
58 Correct 68 ms 43116 KB Output is correct
59 Correct 68 ms 43148 KB Output is correct
60 Correct 69 ms 43348 KB Output is correct
61 Correct 78 ms 47156 KB Output is correct
62 Correct 67 ms 47396 KB Output is correct
63 Correct 65 ms 47224 KB Output is correct
64 Correct 69 ms 47188 KB Output is correct
65 Correct 71 ms 43088 KB Output is correct
66 Correct 79 ms 43176 KB Output is correct
67 Correct 74 ms 42836 KB Output is correct
68 Correct 59 ms 42836 KB Output is correct
69 Correct 57 ms 42836 KB Output is correct
70 Correct 66 ms 47564 KB Output is correct
71 Correct 71 ms 46672 KB Output is correct
72 Correct 1 ms 2652 KB Output is correct
73 Correct 6 ms 6748 KB Output is correct
74 Correct 5 ms 6644 KB Output is correct
75 Correct 1 ms 2652 KB Output is correct
76 Correct 1 ms 2648 KB Output is correct
77 Correct 5 ms 7004 KB Output is correct
78 Correct 6 ms 6748 KB Output is correct
79 Correct 5 ms 6748 KB Output is correct
80 Correct 5 ms 6748 KB Output is correct
81 Correct 1 ms 2652 KB Output is correct
82 Correct 1 ms 2652 KB Output is correct
83 Correct 2 ms 2648 KB Output is correct
84 Correct 1 ms 2652 KB Output is correct
85 Correct 1 ms 2652 KB Output is correct
86 Correct 13 ms 7156 KB Output is correct
87 Correct 14 ms 7164 KB Output is correct
88 Correct 15 ms 7064 KB Output is correct
89 Correct 6 ms 7016 KB Output is correct
90 Correct 6 ms 6748 KB Output is correct
91 Correct 12 ms 7260 KB Output is correct
92 Correct 14 ms 7120 KB Output is correct
93 Correct 1 ms 2652 KB Output is correct
94 Correct 69 ms 51260 KB Output is correct
95 Correct 67 ms 50852 KB Output is correct
96 Correct 65 ms 50264 KB Output is correct
97 Correct 67 ms 51148 KB Output is correct
98 Correct 73 ms 51660 KB Output is correct
99 Correct 74 ms 43088 KB Output is correct
100 Correct 68 ms 43096 KB Output is correct
101 Correct 67 ms 43096 KB Output is correct
102 Correct 71 ms 43348 KB Output is correct
103 Correct 61 ms 47000 KB Output is correct
104 Correct 63 ms 47312 KB Output is correct
105 Correct 65 ms 46788 KB Output is correct
106 Correct 66 ms 47184 KB Output is correct
107 Correct 65 ms 46920 KB Output is correct
108 Correct 70 ms 51144 KB Output is correct
109 Correct 75 ms 51200 KB Output is correct
110 Correct 62 ms 42832 KB Output is correct
111 Correct 60 ms 42832 KB Output is correct
112 Correct 75 ms 43096 KB Output is correct
113 Correct 70 ms 42832 KB Output is correct