Submission #1064002

#TimeUsernameProblemLanguageResultExecution timeMemory
1064002BF001Railway (BOI17_railway)C++17
100 / 100
68 ms26572 KiB
#include<bits/stdc++.h> using namespace std; #define N 100005 #define log 17 #define fi first #define se second typedef pair<int, int> ii; int n, m, k, par[log + 2][N], tin[N], tout[N], tim, dp[N]; vector<ii> adj[N]; vector<int> res; void dfs(int u, int p){ tin[u] = ++tim; for (auto x : adj[u]){ int v = x.fi; if (v == p) continue; par[0][v] = u; dfs(v, u); } tout[u] = tim; } bool in(int u, int v){ return (tin[u] <= tin[v] && tout[v] <= tout[u]); } int getlca(int u, int v){ if (in(u, v)) return u; if (in(v, u)) return v; for (int k = log; k >= 0; k--){ if (par[k][u] && !in(par[k][u], v)) u = par[k][u]; } return par[0][u]; } bool cmp(int&a, int& b){ return tin[a] < tin[b]; } void dfs2(int u, int p){ for (auto x : adj[u]){ int v = x.fi, idx = x.se; if (v == p) continue; dfs2(v, u); if (dp[v] >= k) res.push_back(idx); dp[u] += dp[v]; } } signed main(){ ios_base::sync_with_stdio(0); cin.tie(NULL); cin >> n >> m >> k; for (int i = 1; i <= n - 1; i++){ int u, v; cin >> u >> v; adj[u].push_back({v, i}); adj[v].push_back({u, i}); } dfs(1, 0); for (int j = 1; j <= log; j++){ for (int i = 1; i <= n; i++) par[j][i] = par[j - 1][par[j - 1][i]]; } for (int i = 1; i <= m; i++){ int num; cin >> num; vector<int> vec; for (int j = 1; j <= num; j++){ int val; cin >> val; vec.push_back(val); } sort(vec.begin(), vec.end(), cmp); int lst = -1; dp[getlca(vec[0], vec[num - 1])]--; for (auto x : vec){ dp[x]++; if (lst != -1) dp[getlca(x, lst)]--; lst = x; } } dfs2(1, 0); /// for (int i = 1; i <= n; i++) cout << i << " : " << dp[i] << "\n"; sort(res.begin(), res.end()); cout << (int) res.size() << "\n"; for (auto x : res) cout << x << ' '; 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...