제출 #1097056

#제출 시각아이디문제언어결과실행 시간메모리
1097056YasuaFullApRailway (BOI17_railway)C++14
100 / 100
107 ms27592 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define pi pair<int,int> #define pii pair<int,pair<int,int>> #define fi first #define se second const int N = 1e5 + 5; const int LG = 20; int p[N][LG + 2], tin[N], tout[N], timer, n, m, k, inc[N], de[N], dep[N], val[N], a[N]; vector<pi> adj[N]; void dfs1(int u, int par){ for(int k = 1; k < LG; k++) p[u][k] = p[p[u][k - 1]][k - 1]; tin[u] = ++timer; for(auto x:adj[u]){ if(x.fi == par) continue; int v = x.fi; p[v][0] = u; dep[v] = dep[u] + 1; dfs1(v, u); } tout[u] = timer; } bool ances(int u, int v){ return tin[u] <= tin[v] && tout[u] > tout[v]; } int lca(int u, int v){ if(u == v) return u; if(dep[u] < dep[v]) swap(u, v); for(int k = LG - 1; k >= 0; k--) if(dep[p[u][k]] >= dep[v]) u = p[u][k]; if(u == v) return u; for(int k = LG - 1; k >= 0; k--) if(p[u][k] != p[v][k]) u = p[u][k], v = p[v][k]; return p[u][0]; } void dfs2(int u, int par){ for(auto x:adj[u]){ int id = x.se; int v = x.fi; if(v == par) continue; dfs2(v, u); inc[u] += inc[v]; val[id] = inc[v]; // cout << u << ' ' << v << ' ' << inc[v] << '\n'; } inc[u] -= de[u]; } void UP(int u, int v){ inc[v]++; de[u]++; } bool cmd(int u, int v){ return tin[u] < tin[v]; } signed main(){ cin.tie(0)->sync_with_stdio(false); // freopen("IN.INP","r",stdin); // freopen("OU.OUT","w",stdout); cin >> n >> m >> k; for(int i = 1; i < n; i++){ int u, v; cin >> u >> v; adj[u].push_back({v, i}); adj[v].push_back({u, i}); } dep[1] = 1; dfs1(1, 0); // cout << lca(2, 5) << '\n'; for(int k = 1; k <= m; k++){ int x; cin >> x; for(int i = 1; i <= x; i++) cin >> a[i]; sort(a + 1, a + x + 1, cmd); int _lca = a[1]; for(int i = 2; i <= x; i++){ // cout << a[i] << ' '; if(ances(a[i - 1], a[i])){ UP(a[i - 1], a[i]); }else{ int tmp = lca(_lca, a[i]); if(tmp == _lca){ UP(lca(a[i - 1], a[i]), a[i]); }else{ UP(tmp, _lca); UP(tmp, a[i]); _lca = tmp; } } } // cout << '\n'; // for(int i = 1; i <= n; i++) cout <<i << ' '<< inc[i] << ' ' << de[i] << '\n'; } dfs2(1, 0); std::vector<int> vec; for(int i = 1; i < n; i++) if(val[i] >= k) vec.push_back(i); cout << vec.size() << '\n'; for(auto x:vec) cout << x << ' '; }
#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...