Submission #1096149

#TimeUsernameProblemLanguageResultExecution timeMemory
1096149TrinhKhanhDungRailway (BOI17_railway)C++14
100 / 100
64 ms25288 KiB
#include <bits/stdc++.h> #define ll long long #define fi first #define se second #define sz(x) (int)x.size() #define ALL(v) v.begin(),v.end() #define MASK(k) (1LL << (k)) #define BIT(x, i) (((x) >> (i)) & 1) #define oo (ll)1e18 + 10 #define INF (ll)1e9 #define MOD (ll)(998244353) using namespace std; template<class T1, class T2> bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;} template<class T1, class T2> bool minimize(T1 &a, T2 b){if(a > b){a = b; return true;} return false;} template<class T1, class T2> void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;} template<class T1, class T2> void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;} template<class T> void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());} const int maxN = 1e5 + 10, LOG = 17; int N, M, K; vector<pair<int, int>> adj[maxN]; int idEdge[maxN], in[maxN], out[maxN], timer, h[maxN], d[maxN][LOG], cnt[maxN]; void dfs(int u, int p = -1){ in[u] = ++timer; for(auto o: adj[u]){ int v = o.fi, id = o.se; if(v == p) continue; h[v] = h[u] + 1; d[v][0] = u; for(int i = 1; i < LOG; i++){ d[v][i] = d[d[v][i - 1]][i - 1]; } idEdge[v] = id; dfs(v, u); } out[u] = timer; } int lca(int u, int v){ if(h[u] < h[v]) swap(u, v); int delta = h[u] - h[v]; for(int i = 0; i < LOG; i++) if(BIT(delta, i)){ u = d[u][i]; } if(u == v) return u; for(int i = LOG - 1; i >= 0; i--) if(d[u][i] != d[v][i]){ u = d[u][i]; v = d[v][i]; } return d[u][0]; } void solve(){ 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}); } dfs(1); for(int p = 1; p <= M; p++){ int k; cin >> k; vector<int> index; for(int i = 1; i <= k; i++){ int x; cin >> x; index.push_back(x); } sort(ALL(index), [&](int u, int v){return in[u] < in[v];}); for(int i = 1; i < k; i++){ index.push_back(lca(index[i], index[i - 1])); } cps(index); sort(ALL(index), [&](int u, int v){return in[u] < in[v];}); stack<int> st; for(int x: index){ while(sz(st) && in[x] > out[st.top()]) st.pop(); if(sz(st)){ int y = st.top(); cnt[in[x]]++; cnt[in[y]]--; } st.push(x); } } for(int i = 1; i <= N; i++) cnt[i] += cnt[i - 1]; // cout << '\n'; vector<int> ans; for(int i = 2; i <= N; i++){ if(cnt[out[i]] - cnt[in[i] - 1] >= K) ans.push_back(idEdge[i]); } sort(ALL(ans)); cout << sz(ans) << '\n'; for(int x: ans) cout << x << ' '; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); // freopen("task.inp","r",stdin); // freopen("task.out","w",stdout); int T = 1; // cin >> T; for(; T > 0; T--) solve(); 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...