Submission #688514

#TimeUsernameProblemLanguageResultExecution timeMemory
688514stevancvRailway (BOI17_railway)C++14
100 / 100
142 ms37548 KiB
#include <bits/stdc++.h> #define ll long long #define ld long double #define sp ' ' #define en '\n' #define smin(a, b) a = min(a, b) #define smax(a, b) a = max(a, b) using namespace std; const int N = 1e5 + 2; const int inf = 1e9; vector<array<int, 2>> g[N], ch[N]; vector<int> a[N], st[N], ans; int par[N][17], in[N], out[N], tsz, k; struct Bit { int bit[N]; void Add(int x, int v) { for (; x < N; x += x & (-x)) bit[x] += v; } int Get(int x) { int ans = 0; for (; x > 0; x -= x & (-x)) ans += bit[x]; return ans; } }f; void Dfs(int s, int e) { in[s] = ++tsz; par[s][0] = e; for (int i = 1; i < 17; i++) par[s][i] = par[par[s][i - 1]][i - 1]; for (auto u : g[s]) { if (u[0] == e) continue; Dfs(u[0], s); } out[s] = tsz; } bool Ancestor(int u, int v) { return in[u] <= in[v] && out[v] <= out[u]; } int Lca(int u, int v) { if (Ancestor(u, v)) return u; if (Ancestor(v, u)) return v; for (int i = 16; i >= 0; i--) { if (par[u][i] && !Ancestor(par[u][i], v)) u = par[u][i]; } return par[u][0]; } void Solve(int s, int e) { for (int u : st[s]) f.Add(in[a[u][0]], 1); for (auto u : ch[s]) { f.Add(in[a[u[0]][u[1]]], -1); if (u[1] < a[u[0]].size() - 1) f.Add(in[a[u[0]][u[1] + 1]], 1); } for (auto u : g[s]) { if (u[0] == e) continue; if (f.Get(out[u[0]]) - f.Get(in[u[0]] - 1) >= k) ans.push_back(u[1]); Solve(u[0], s); } for (int u : st[s]) f.Add(in[a[u][0]], -1); } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, s; cin >> n >> s >> k; for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; g[u].push_back({v, i}); g[v].push_back({u, i}); } Dfs(1, 0); for (int i = 1; i <= s; i++) { int sz; cin >> sz; a[i].resize(sz); for (int j = 0; j < sz; j++) cin >> a[i][j]; sort(a[i].begin(), a[i].end(), [&] (int ii, int jj) { return in[ii] < in[jj]; }); int lca = a[i][0]; for (int j = 1; j < sz; j++) lca = Lca(lca, a[i][j]); st[lca].push_back(i); for (int j = 0; j < sz; j++) ch[a[i][j]].push_back({i, j}); } Solve(1, 0); sort(ans.begin(), ans.end()); cout << ans.size() << en; for (int u : ans) cout << u << sp; cout << en; return 0; }

Compilation message (stderr)

railway.cpp: In function 'void Solve(int, int)':
railway.cpp:50:18: warning: comparison of integer expressions of different signedness: 'std::array<int, 2>::value_type' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   50 |         if (u[1] < a[u[0]].size() - 1) f.Add(in[a[u[0]][u[1] + 1]], 1);
#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...