Submission #966244

#TimeUsernameProblemLanguageResultExecution timeMemory
966244oblantisRailway (BOI17_railway)C++17
100 / 100
86 ms27084 KiB
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") //#pragma GCC optimize("O3,unroll-loops") #include <bits/stdc++.h> #define all(v) v.begin(), v.end() #define pb push_back #define ss second #define ff first #define vt vector #define uid(a, b) uniform_int_distribution<int>(a, b)(mt) using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int inf = 2e9; const int mod = 1e9+7; const int maxn = 1e5; mt19937 mt(chrono::steady_clock::now().time_since_epoch().count()); vt<int> g[maxn]; bool was[maxn]; int in[maxn], out[maxn], up[maxn][20], dp[maxn], cnt, p[maxn]; void dfs(int v){ in[v] = cnt++; for(auto i : g[v]){ if(i == up[v][0])continue; up[i][0] = v, dp[i] = dp[v] + 1; for(int j = 1; j < 20; j++)up[i][j] = up[up[i][j - 1]][j - 1]; dfs(i); } out[v] = cnt++; } bool cmp(int x, int y){ return in[x] < in[y]; } void checkup(int v){ for(auto i : g[v]){ if(i == up[v][0])continue; checkup(i); p[v] += p[i]; } } int lca(int x, int y){ if(dp[x] > dp[y])swap(x, y); int k = dp[y] - dp[x], j = 0; while(k){ if(k & 1){ y = up[y][j]; } k >>= 1, j++; } if(y == x)return x; for(int j = 19; j >= 0; j--){ if(up[x][j] != up[y][j]){ x = up[x][j], y = up[y][j]; } } return up[x][0]; } int nw, sz; vt<int> c; void go(int v){ p[v]++; was[v] = 0; while(nw < sz && in[c[nw]] < out[v]){ nw++; p[v]--; go(c[nw - 1]); } } void solve() { int n, m, k; cin >> n >> m >> k; int u[n - 1], v[n - 1]; for(int i = 0; i < n - 1; i++){ cin >> u[i] >> v[i]; --u[i], --v[i]; g[u[i]].pb(v[i]), g[v[i]].pb(u[i]); } dfs(0); for(int i = 0; i < m; i++){ cin >> sz; c.resize(sz, 0); for(int j = 0; j < sz; j++){ cin >> c[j]; --c[j]; was[c[j]] = 1; } sort(all(c), cmp); for(int j = 1; j < sz; j++){ int LCA = lca(c[j], c[j - 1]); if(!was[LCA])c.pb(LCA), was[LCA] = 1; } sz = c.size(); sort(all(c), cmp); nw = 1; go(c[0]); p[c[0]]--; } checkup(0); vt<int> ans; for(int i = 0; i < n - 1; i++){ if(dp[u[i]] > dp[v[i]])swap(u[i], v[i]); if(p[v[i]] >= k){ ans.pb(i + 1); } } cout << ans.size() << '\n'; for(auto i : ans)cout << i << ' '; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int times = 1; //cin >> times; for(int i = 1; i <= times; i++) { solve(); } 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...