Submission #210689

#TimeUsernameProblemLanguageResultExecution timeMemory
210689super_j6Railway (BOI17_railway)C++14
100 / 100
460 ms31608 KiB
#include <iostream> #include <cstdio> #include <algorithm> #include <vector> #include <set> using namespace std; #define endl '\n' #define pi pair<int, int> #define f first #define s second struct segTree{ int l, r; segTree *left, *right; int val = 0; segTree(int a, int b){ l = a; r = b; if(l != r){ int mid = (l + r) / 2; left = new segTree(l, mid); right = new segTree(mid + 1, r); } } void add(int x, int v){ if(x < l || r < x) return; if(l == r){ val += v; return; } left->add(x, v); right->add(x, v); val = left->val + right->val; } int qry(int a, int b){ if(b < l || r < a) return 0; if(a <= l && r <= b) return val; return left->qry(a, b) + right->qry(a, b); } }; const int maxn = 100000, w = 18; int n, m, k; int l[maxn], r[maxn], d[maxn], dp[maxn]; int p[w][maxn]; vector<pi> graph[maxn]; segTree tre(0, maxn); vector<int> ans; auto cmp = [&](int x, int y){ return (pi){r[x], -l[x]} < (pi){r[y], -l[y]}; }; set<int, decltype(cmp)> a(cmp); vector<pi> b; int dfs(int c){ r[c] = l[c]; for(pi i : graph[c]){ if(i.f == p[0][c]) continue; d[i.f] = d[c] + 1; l[i.f] = r[c] + 1; p[0][i.f] = c; r[c] = dfs(i.f); } return r[c]; } int lca(int x, int y){ if(d[x] < d[y]) swap(x, y); for(int i = w - 1; i >= 0; i--) if(p[i][x] != -1 && d[p[i][x]] >= d[y]) x = p[i][x]; for(int i = w - 1; i >= 0; i--){ if(p[i][x] != p[i][y]){ x = p[i][x]; y = p[i][y]; } } return x == y ? x : p[0][x]; } void dfs2(int c){ for(pi i : graph[c]){ if(i.f == p[0][c]) continue; dfs2(i.f); if(dp[i.f] >= k) ans.push_back(i.s); dp[c] += dp[i.f]; } } int main(){ ios::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m >> k; for(int i = 1; i < n; i++){ int u, v; cin >> u >> v; u--, v--; graph[u].push_back({v, i}); graph[v].push_back({u, i}); } p[0][0] = -1; dfs(0); for(int i = 1; i < w; i++) for(int j = 0; j < n; j++){ p[i][j] = p[i - 1][j] == -1 ? -1 : p[i - 1][p[i - 1][j]]; } for(int i = 0; i < m; i++){ int s; cin >> s; b.clear(); for(int j = 0; j < s; j++){ int x; cin >> x; x--; a.insert(x); } while(!a.empty()){ if(!b.empty()) a.insert(lca(b.back().f, *a.begin())); b.push_back({*a.begin(), 0}); a.erase(a.begin()); b.back().s = -tre.qry(l[b.back().f], r[b.back().f]) + 1; tre.add(l[b.back().f], b.back().s); dp[b.back().f] += b.back().s; } dp[b.back().f]--; for(pi i : b){ tre.add(l[i.f], -i.s); } } dfs2(0); sort(ans.begin(), ans.end()); cout << ans.size() << endl; if(!ans.empty()) cout << ans[0]; for(int i = 1; i < ans.size(); i++) cout << " " << ans[i]; cout << endl; return 0; }

Compilation message (stderr)

railway.cpp: In function 'int main()':
railway.cpp:147:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 1; i < ans.size(); i++) cout << " " << ans[i];
                 ~~^~~~~~~~~~~~
#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...