제출 #447254

#제출 시각아이디문제언어결과실행 시간메모리
447254ak2006Railway (BOI17_railway)C++14
0 / 100
108 ms27708 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; using vb = vector<bool>; using vvb = vector<vb>; using vi = vector<int>; using vvi = vector<vi>; using vl = vector<ll>; using vvl = vector<vl>; using vc = vector<char>; using vvc = vector<vc>; using vs = vector<string>; const ll mod = 1e9 + 7,inf = 1e18; #define pb push_back #define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); void setIO() { fast; } int n = 1e5 + 5,m,k,TIMER; vvi adj(n),anc(n,vi(21)); vi pref(n),in(n),out(n); void dfs(int u,int p) { in[u] = ++TIMER; anc[u][0] = p; for (int i = 1;i<=20;i++)anc[u][i] = anc[anc[u][i - 1]][i - 1]; for (int v:adj[u]){ if (v == p)continue; dfs(v,u); } out[u] = ++TIMER; } void dfs2(int u,int p) { for (int v:adj[u]){ if (v == p)continue; dfs2(v,u); pref[u] += pref[v]; } } bool cmp(int a,int b) { return in[a] <= in[b]; } bool is_ancestor(int u,int v) { return in[u] <= in[v] && out[u] >= out[v]; } int LCA(int u,int v) { if (in[u] > in[v])swap(u,v); if (is_ancestor(u,v))return u; for (int i = 20;i>=0;i--) if (!is_ancestor(anc[v][i],u))v = anc[v][i]; return anc[v][0]; } int main() { setIO(); cin>>n>>m>>k; for (int i = 1;i<n;i++){int u,v;cin>>u>>v;adj[u].pb(v),adj[v].pb(u);} dfs(1,1); while (m--){ int num; cin>>num; vi vals(num); for (int i = 0;i<num;i++)cin>>vals[i]; sort(vals.begin(),vals.end(),cmp); // for (int i = 0;i<num - 1;i++){ // cout<<vals[i]<<" "<<vals[i + 1]<<" "<<LCA(vals[i],vals[i + 1])<<'\n'; // } // cout<<'\n'; for (int i = 0;i<num;i++){ int lca = LCA(vals[i],vals[(i + 1)%num]); pref[vals[i]]++; pref[vals[(i + 1)%num]]++; pref[lca] -= 2; } } dfs2(1,1); vi all; for (int i = 1;i<=n;i++)if (pref[i] >= 2 * k)all.pb(i); cout<<all.size()<<'\n'; for (auto it:all)cout<<it<<" "; 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...