Submission #482902

#TimeUsernameProblemLanguageResultExecution timeMemory
482902PoPularPlusPlusRailway (BOI17_railway)C++17
100 / 100
107 ms27988 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define ull unsigned long long #define pb(e) push_back(e) #define sv(a) sort(a.begin(),a.end()) #define sa(a,n) sort(a,a+n) #define mp(a,b) make_pair(a,b) #define vf first #define vs second #define ar array #define all(x) x.begin(),x.end() const int inf = 0x3f3f3f3f; const int mod = 1000000007; const double PI=3.14159265358979323846264338327950288419716939937510582097494459230; bool remender(ll a , ll b){return a%b;} const int N = 100002 , l = 25; int n , m , k; vector<pair<int,int>> adj[N]; int cnt[N],timer,tin[N], tout[N],up[N][l + 5]; vector<int> ans; void dfs(int v, int p){ tin[v] = ++timer; up[v][0] = p; for (int i = 1; i <= l; ++i) up[v][i] = up[up[v][i-1]][i-1]; for (auto &[u,ldonewjfjiwjjfosfojg] : adj[v]) { if (u != p) dfs(u, v); } tout[v] = ++timer; } bool is_ancestor(int u, int v){ return tin[u] <= tin[v] && tout[u] >= tout[v]; } int lca(int u, int v){ if (is_ancestor(u, v)) return u; if (is_ancestor(v, u)) return v; for (int i = l; i >= 0; --i) { if (!is_ancestor(up[u][i], v)) u = up[u][i]; } return up[u][0]; } bool cmp(int x , int y){ return tin[x] < tin[y]; } void dfs1(int node , int par = -1){ for(auto &[i,idx] : adj[node]){ if(i!=par){ dfs1(i,node); if(cnt[i] >= 2 * k)ans.pb(idx); cnt[node] += cnt[i]; } } } void solve(){ cin >> n >> m >> k; for(int i = 1; i < n; i++){ int u , v; cin >> u >> v; adj[u].pb(mp(v,i)); adj[v].pb(mp(u,i)); } timer = 0; dfs(1,1); memset(cnt,0,sizeof cnt); for(int i = 0; i < m; i++){ int s; cin >> s; int arr[s]; for(int j = 0; j < s; j++){ cin >> arr[j]; } sort(arr,arr+s,cmp); for(int j = 0; j < s; j++){ int lc = lca(arr[j],arr[(j + 1) % s]); cnt[lc]-=2; cnt[arr[j]]++; cnt[arr[(j + 1) % s]]++; } } dfs1(1); sv(ans); cout << ans.size() << '\n'; for(int i : ans)cout << i << ' '; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); //int t;cin >> t;while(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...