Submission #1095835

#TimeUsernameProblemLanguageResultExecution timeMemory
1095835anhthiRailway (BOI17_railway)C++14
100 / 100
77 ms28368 KiB
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define ll long long #define mp(x, y) make_pair(x, y) #define sz(v) ((int) (v).size()) #define all(v) (v).begin(), (v).end() #define MASK(i) (1LL << (i)) #define BIT(x, y) (((x) >> (y)) & 1) #define pb push_back #define max_rng *max_element #define min_rng *min_element #define rep(i, n) for(int i = 1, _n = (n); i <= _n; ++i) #define forn(i, a, b) for(int i = (a), _b = (b); i <= _b; ++i) #define ford(i, a, b) for(int i = (a), _b = (b); i >= _b; --i) template <class X, class Y> inline bool maximize(X &x, Y y) { return (x < y ? x = y, true : false); } template <class X, class Y> inline bool minimize(X &x, Y y) { return (x > y ? x = y, true : false); } template <class X> inline void compress(vector<X> &a) { sort(all(a)); a.resize(unique(all(a)) - a.begin()); } int ctz(ll x) { return __builtin_ctzll(x); } int lg(ll x) { return 63 - __builtin_clzll(x); } int popcount(ll x) { return __builtin_popcount(x); } mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); ll rand(ll l, ll r) { return l + abs((ll) rng()) % (r - l + 1); } const ll oo = (ll) 1e17; const int inf = (ll) 2e9 + 7, mod = (ll) 1e9 + 7; const int mxn = 1e5 + 5, LOG = 20; void add(int &x, int y) { x += y; if (x >= mod) x -= mod; } void sub(int &x, int y) { x -= y; if (x < 0) x += mod; } int n, m, k; vector<pair<int, int>> g[mxn]; int timer = 0; int in[mxn], out[mxn]; int h[mxn]; int par[mxn][LOG + 5]; int cnt[mxn], pf[mxn]; void dfs(int u, int p) { par[u][0] = p; h[u] = h[p] + 1; forn(i, 1, LOG) { par[u][i] = par[par[u][i-1]][i-1]; } in[u] = ++timer; for (pair<int, int> i : g[u]) if (i.first != p) { dfs(i.first, u); } out[u] = timer; } int lift(int u, int k) { for (; k > 0; k -= k & -k) u = par[u][ctz(k)]; return u; } int lca(int u, int v) { if (h[u] < h[v]) swap(u, v); u = lift(u, h[u] - h[v]); if (u == v) return u; ford(i, lg(h[u]), 0) if (par[u][i] != par[v][i]) { u = par[u][i]; v = par[v][i]; } return par[u][0]; } bool isIn(int root, int u) { return in[root] <= in[u] && in[u] <= out[root]; } bool compare(int &u, int &v) { return in[u] < in[v]; } void fix(int u = 1, int p = 0) { for (pair<int, int> i : g[u]) if (i.first != p) { fix(i.first, u); pf[u] += pf[i.first]; cnt[i.second] = pf[i.first]; } } signed main(void) { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> k; rep(i, n - 1) { int u, v; cin >> u >> v; g[u].pb(mp(v, i)); g[v].pb(mp(u, i)); } dfs(1, 0); rep(_, m) { int k; cin >> k; vector<int> req(k); for (int &i : req) cin >> i; sort(all(req), compare); forn(i, 0, k - 2) req.pb(lca(req[i], req[i+1])); compress(req); sort(all(req), compare); stack<int> st; st.push(req[0]); forn(i, 1, sz(req) - 1) { int u = req[i]; while (!st.empty() && !isIn(st.top(), u)) st.pop(); if (!st.empty()) { pf[st.top()]--; pf[u]++; } st.push(u); } } fix(); vector<int> res; forn(i, 1, n - 1) if (cnt[i] >= k) res.pb(i); cout << sz(res) << '\n'; for (int v : res) cout << v << ' '; 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...