Submission #432116

#TimeUsernameProblemLanguageResultExecution timeMemory
432116SirCovidThe19thRailway (BOI17_railway)C++14
100 / 100
236 ms22980 KiB
#include <bits/stdc++.h> using namespace std; const int mx = 1e5+5, ml = 17; int n, m, k, tin[mx], tout[mx], cnt = 0, up[ml][mx], depth[mx], weight[mx]; vector<pair<int, int>> adj[mx]; vector<int> ans; void dfs(int node){ tin[node] = ++cnt; for (int l = 1; l < ml; l++) up[l][node] = up[l-1][up[l-1][node]]; for (pair<int, int> nxt : adj[node]) if (nxt.first != up[0][node]) depth[nxt.first] = depth[node]+1, up[0][nxt.first] = node, dfs(nxt.first); tout[node] = cnt; } bool isAnc(int a, int b){ return tin[a] <= tin[b] and tout[a] >= tout[b]; } int lca(int a, int b){ if (isAnc(a, b)) return a; if (isAnc(b, a)) return b; for (int l = ml-1; l >= 0; l--) if (up[l][a] != 0 and !isAnc(up[l][a], b)) a = up[l][a]; return up[0][a]; } int solve(int node){ int ret = weight[node]; for (pair<int, int> nxt : adj[node]) if (nxt.first != up[0][node]){ int val = solve(nxt.first); ret += val; if (val >= k) ans.push_back(nxt.second); } return ret; } int main(){ cin >> n >> m >> k; for (int i = 1; i < n; i++){ int a, b; cin >> a >> b; adj[a].push_back({b, i}); adj[b].push_back({a, i}); }dfs(1); for (int i = 0; i < m; i++){ int s; cin >> s; int cn[s+1]; for (int j = 0; j < s; j++) cin >> cn[j]; sort(cn, cn+s, [](int a, int b) { return tin[a] < tin[b]; } ); cn[s] = cn[0]; for (int j = 0; j < s; j++) weight[lca(cn[j], cn[j+1])]--, weight[cn[j+1]]++; }solve(1); sort(ans.begin(), ans.end()); cout<<ans.size()<<endl; for (int res : ans) cout<<res<<" "; }
#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...