Submission #1291458

#TimeUsernameProblemLanguageResultExecution timeMemory
1291458hxianRailway (BOI17_railway)C++20
100 / 100
127 ms49224 KiB
// dmoj problem: https://vjudge.net/contest/765976#problem/G #include <bits/stdc++.h> #define int long long #define all(x) x.begin(), x.end() using namespace std; int n, m, k, depth[100001], val[100001], parent[100001][20], start[100001], order[100001]; int cur; vector<int> adjList[100001], special[100001]; vector<int> edges; map<pair<int, int>, int> tracks; void setdepth(int node, int last) { order[node] = cur++; depth[node] = depth[last] + 1; parent[node][0] = last; for (int u : adjList[node]) if (u != last) setdepth(u, node); } int lca(int a, int b) { for (int i = 19; i >= 0; i--) { if (depth[a] - depth[b] >= (1 << i)) a = parent[a][i]; if (depth[b] - depth[a] >= (1 << i)) b = parent[b][i]; } if (a == b) return a; for (int i = 19; i >= 0; i--) { if (parent[a][i] != parent[b][i]) { a = parent[a][i]; b = parent[b][i]; } } return parent[a][0]; } int dfs(int node, int last) { int sum = val[node]; for (int u : adjList[node]) if (u != last) sum += dfs(u, node); if (sum / 2 >= k) { edges.push_back(tracks[{min(node, last), max(node, last)}]); } return sum; } int32_t main() { cin.sync_with_stdio(0); cin.tie(0); cin >> n >> m >> k; for (int i = 1; i < n; i++) { int a, b; cin >> a >> b; adjList[a].push_back(b); adjList[b].push_back(a); tracks[{min(a, b), max(a, b)}] = i; } setdepth(1, 0); for (int i = 1; i < 20; i++) for (int j = 1; j <= n; j++) parent[j][i] = parent[parent[j][i - 1]][i - 1]; for (int i = 0; i < m; i++) { int s; cin >> s; deque<int> specials; for (int j = 0; j < s; j++) { int a; cin >> a; specials.push_back(a); special[a].push_back(i); } sort(all(specials), [](int a, int b) { return order[a] < order[b]; }); int ofall = specials[0]; for (int i = 1; i < specials.size(); i++) ofall = lca(ofall, specials[i]); specials.push_front(ofall); for (int i = 0; i < specials.size(); i++) { int l = lca(specials[i], specials[(i + 1) % specials.size()]); val[l] -= 2; ++val[specials[i]]; ++val[specials[(i + 1) % specials.size()]]; } } dfs(1, 0); cout << edges.size() << "\n"; sort(all(edges)); for (int u : edges) cout << u << " "; cout << "\n"; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...