Submission #668209

#TimeUsernameProblemLanguageResultExecution timeMemory
668209ktkeremRailway (BOI17_railway)C++17
100 / 100
256 ms107340 KiB
/*#pragma GCC target ("avx2") #pragma GCC optimize ("O3") #pragma GCC optimize("Ofast") #pragma GCC optimize ("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")/**/ #include<bits/stdc++.h> /**/ //typedef int ll; typedef long long ll; typedef unsigned long long ull; typedef std::string str; //typedef vector<std::vector<ll>> vv; /*typedef __int128 vll; typedef unsigned __int128 uvll;*/ #define llll std::pair<ll , ll> #define pb push_back #define pf push_front #define halo cout << "hello\n" #define fi first #define sec second #define vv(x) std::vector<std::vector<x>> #define all(a) a.begin() , a.end() const ll limit = 1e9+7; const ll ous = 3e5 + 7; const ll dx[4] = {-1 , 0 , 1 , 0} , dy[4] = {0,1,0,-1}; ll n , m , k;std::vector<ll> ar , par , kt , fs(ous , 0) , tb(ous , 0) , lev(ous , 0) , lg2(ous , 0); std::vector<ll> dspar(ous , 0) , dssz(ous , 0) , dsval(ous , 0) , gk(ous , 0); std::vector<llll> adj[ous] , scpar; std::vector<std::vector<ll>> spar(ous , std::vector<ll>(20 , 0)); ll tmr = 0; ll fnd(ll x){ if(x == dspar[x]){ return x; } return dspar[x] = fnd(dspar[x]); } void mrg(ll x , ll y){ x = fnd(x);y = fnd(y); if(x != y){ if(dssz[y] > dssz[x]){ std::swap(x , y); } dssz[x] += dssz[y]; dspar[y] = x; } } void dfs(ll cur , ll prv){ tmr++; lev[cur] = lev[prv] - 1; ll k = tmr; tb[k] = cur; fs[cur] = ar.size(); spar[ar.size()][0] = k; ar.pb(k); for(auto j:adj[cur]){ if(j.fi != prv){ par[j.fi] = j.sec; dfs(j.fi , cur); } spar[ar.size()][0] = k; ar.pb(k); } } bool cmp(ll x , ll y){ return fs[x] < fs[y]; } void sdfs(ll crt , ll prv){ for(auto j:adj[crt]){ if(j.fi != prv){ sdfs(j.fi , crt); } } gk[par[crt]]+= kt[crt]; kt[prv]+=kt[crt]; } void solve(){ std::cin >> n >> m >> k; par.resize(n+5); kt.resize(n+5); lg2[1] = 0; for(ll i = 2;ous > i;i++){ lg2[i] = lg2[i/2] + 1; } for(ll i = 1;n>i;i++){ ll x , y;std::cin >> x >> y; adj[x].pb({y , i}); adj[y].pb({x , i}); } dfs(1 , 0); for(ll j = 1;20>j;j++){ for(ll i = 0;ar.size()>i + (1 << j);i++){ spar[i][j] = std::min(spar[i][j-1] , spar[i + (1 << j-1)][j-1]); } } //std::cout << spar[0][0] << "\n"; for(ll i = 0;m>i;i++){ ll x;std::cin >> x; std::vector<ll> o; for(ll j = 0;x>j;j++){ll y;std::cin >> y;o.pb(y);kt[y]++;} std::sort(all(o) , cmp); std::set<std::pair<llll , llll>> st; for(ll j = 0;x>j;j++){ dsval[j] = o[j]; dspar[j] = j; dssz[j] = 1; } ll alc = o[0]; for(ll j = 1;x > j;j++){ ll mn = std::min(fs[o[j-1]] , fs[o[j]]) , mx = std::max(fs[o[j-1]] , fs[o[j]]); ll tt = lg2[mx - mn + 1]; ll lc = std::min(spar[mn][tt] , spar[mx - (1ll << tt) + 1][tt]); ll pt = tb[lc]; mn = std::min(fs[alc] , fs[o[j]]) , mx = std::max(fs[alc] , fs[o[j]]); tt = lg2[mx - mn + 1]; lc = std::min(spar[mn][tt] , spar[mx - (1ll << tt) + 1][tt]); alc = tb[lc]; //std::cout << pt << " " << dsval[j] << " " << dsval[j-1] << "\n"; st.insert({{lev[pt] , pt} , {j , j-1}}); } kt[alc]--; while(st.size()){ std::pair<llll , llll> a = *st.begin(); st.erase(st.begin()); ll x = fnd(a.sec.fi) , y = fnd(a.sec.sec); if(x != a.sec.fi || y != a.sec.sec){ ll mn = std::min(fs[dsval[x]] , fs[dsval[y]]) , mx = std::max(fs[dsval[x]] , fs[dsval[y]]); ll tt = lg2[mx - mn + 1]; ll lc = std::min(spar[mn][tt] , spar[mx - (1ll << tt) + 1][tt]); ll o = tb[lc]; st.insert({{lev[o] , o} , {x , y}}); } else{ mrg(a.sec.fi , a.sec.sec); dsval[fnd(a.sec.fi)] = a.fi.sec; //std::cout << a.fi.fi << " " << a.fi.sec << " " << dsval[a.sec.fi] << " " << dsval[a.sec.sec] << "\n"; kt[a.fi.sec]--; } } } sdfs(1 , 0); std::vector<ll> ans; for(ll i = 1;n>i;i++){ if(gk[i] >= k){ ans.pb(i); //std::cout << i << "\n"; } } std::cout << ans.size() << "\n"; for(auto j:ans){ std::cout << j << " "; } std::cout <<"\n"; return;/**/ } signed main(){ std::ios_base::sync_with_stdio(false);std::cin.tie(NULL); ll t=1; //std::cin >> t; ll o = 1; while(t--){ //cout << "Case " << o++ << ":\n"; solve(); } return 0; }/**/

Compilation message (stderr)

railway.cpp:5:78: warning: "/*" within comment [-Wcomment]
    5 | #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")/**/
      |                                                                               
railway.cpp: In function 'void solve()':
railway.cpp:91:31: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'll' {aka 'long long int'} [-Wsign-compare]
   91 |         for(ll i = 0;ar.size()>i + (1 << j);i++){
      |                      ~~~~~~~~~^~~~~~~~~~~~~
railway.cpp:92:66: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   92 |             spar[i][j] = std::min(spar[i][j-1] , spar[i + (1 << j-1)][j-1]);
      |                                                                 ~^~
railway.cpp: In function 'int main()':
railway.cpp:159:8: warning: unused variable 'o' [-Wunused-variable]
  159 |     ll o = 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...