제출 #1099113

#제출 시각아이디문제언어결과실행 시간메모리
1099113LeDaiKingRailway (BOI17_railway)C++14
36 / 100
65 ms26568 KiB
#include<bits/stdc++.h> using namespace std; #define ll long long #define fi first #define se second #define pb push_back #define ALL(n) n.begin(), n.end() #define mask(n) (1ll << (n)) #define ck(n, k) (((n) >> (k))&1) #define LOG 17 mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int f[100005], dfsnum[100005], lca[100005][20], hight[100005]; pair<int, int> edges[100005]; vector<int> adj[100005]; int num = 0; void dfs(int pa, int u) { // cout << pa << " " << u << endl; lca[u][0] = pa; hight[u] = hight[pa] + 1; for (auto v : adj[u]) if (v != pa) { dfs(u, v); } dfsnum[u] = ++num; } void preprocessLCA(int n) { dfs(0, 1); for (int j = 1; j <= LOG; j++) for (int i = 1; i <= n; i++) lca[i][j] = lca[lca[i][j - 1]][j - 1]; } int getLCA(int u, int v) { if (hight[u] < hight[v]) return getLCA(v, u); for (int i = LOG; i >= 0; i--) if (hight[lca[u][i]] >= hight[v]) u = lca[u][i]; if (u == v) return u; for (int i = LOG; i >= 0; i--) if (lca[u][i] != lca[v][i]) { u = lca[u][i]; v = lca[v][i]; } return lca[u][0]; } void dfs1(int pa, int u) { for (auto v : adj[u]) if (v != pa) { dfs1(u, v); f[u] += f[v]; } } void solution() { int n, m, k; cin >> n >> m >> k; for (int i = 1; i < n; i++) { int x, y; cin >> x >> y; adj[x].pb(y); adj[y].pb(x); edges[i] = {x, y}; } preprocessLCA(n); while(m--) { int num; cin >> num; vector<int> ldk(num); for (int i = 0; i < num; i++) cin >> ldk[i]; sort(ALL(ldk), [&](const int &x, const int &y) { return dfsnum[x] < dfsnum[y]; }); int pre = ldk[0]; for (int i = 1; i < (int)ldk.size(); i++) { int cur = ldk[i]; int par = getLCA(ldk[i - 1], cur); f[pre]++; f[cur]++; f[par] -= 2; pre = par; } } dfs1(0, 1); vector<int> ans; for (int i = 1; i < n; i++) { if (hight[edges[i].fi] < hight[edges[i].se]) swap(edges[i].fi, edges[i].se); if (f[edges[i].fi] >= k) { ans.pb(i); } } cout << (int)ans.size() << "\n"; for (auto v : ans) cout << v << " "; } int main() { //freopen("test.inp", "r", stdin); //freopen("test.out", "w", stdout); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); solution(); }
#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...