Submission #943948

#TimeUsernameProblemLanguageResultExecution timeMemory
943948vjudge1Railway (BOI17_railway)C++17
100 / 100
153 ms47984 KiB
#include <bits/stdc++.h> using namespace std; // typedefs... typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> pii; typedef pair<ll, ll> pll; // constants... const double PI = acos(-1); const ll mod = 1000000007; // 998244353 const ll INF = 1e9+5; const ll INFLL = 1e18+5; // defines... #define PB push_back #define fi first #define se second #define boost_ ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0); #define endl '\n' // functions... ll gcd(ll a, ll b){ while (b){ a %= b; swap(a, b);} return a;} ll lcm(ll a, ll b){ return (a/gcd(a, b)*b);} ll modpow(ll a,ll p,ll mod) {ll ans=1;while(p){if(p%2)ans=(ans*a)%mod;a=(a*a)%mod;p/=2;} return ans;} ll inverse_mod(ll n,ll mod) {return modpow(n,mod-2,mod);} const int N= 1e5+5,LOG= 18; int depth[N],up[N][LOG], sz[N]; vector<pii> v[N]; vector<int> col[N], minister[N]; void dfs(int pos,int pre) { sz[pos] = 1; for(auto it:v[pos]) { if(it.fi==pre) continue; depth[it.fi]=depth[pos]+1; up[it.fi][0] = pos; for(int j = 1; j < LOG; j++) { up[it.fi][j] = up[up[it.fi][j-1]][j-1]; } dfs(it.fi,pos); sz[pos] += sz[it.fi]; } return ; } int kthancestor(int pos,int k) { for(int i=LOG-1;i>=0;i--) { if(k&(1<<i)) pos=up[pos][i]; } return pos; } int get_lca(int a, int b) { if(depth[a] < depth[b]) { swap(a, b); } // 1) Get same depth. int k = depth[a] - depth[b]; a=kthancestor(a,k); // 2) if b was ancestor of a then now a==b if(a == b) { return a; } // 3) move both a and b with powers of two for(int j = LOG - 1; j >= 0; j--) { if(up[a][j] != up[b][j]) { a = up[a][j]; b = up[b][j]; } } return up[a][0]; } int n, m, k; set<int> subtree[N], ans; void dfs_seg(int pos, int pre, int ind) { for(auto it : v[pos]) { if(it.fi == pre) continue; dfs_seg(it.fi, pos, it.se); if(subtree[pos].size() < subtree[it.fi].size()) swap(subtree[pos], subtree[it.fi]); for(auto itt : subtree[it.fi]) { subtree[pos].insert(itt); } } for(auto it : col[pos]) subtree[pos].insert(it); for(auto it : minister[pos]) subtree[pos].erase(it); if(subtree[pos].size() >= k) ans.insert(ind); } void solve() { cin >> n >> m >> k; for(int i = 1; i < n; i++) { int x, y; cin >> x >> y; v[x].PB({y, i}); v[y].PB({x, i}); } dfs(1, 1); for(int i = 1; i <= m; i++) { int x, y, lc; cin >> x; vector<int> temp(x); for(int j = 0; j < x; j++) cin >> temp[j]; for(int j = 0; j < x; j++) { if(j == 0) lc = temp[j]; else lc = get_lca(lc, temp[j]); // cout << i << lc << endl; } minister[lc].PB(i); for(int j = 0; j < x; j++) { col[temp[j]].PB(i); } } dfs_seg(1, 1, 0); ans.erase(0); cout << ans.size() << endl; for(auto it : ans) cout << it << " "; cout << endl; } int main() { boost_; int t = 1; // cin >> t; for(int i=1;i<=t;i++) { // cout<<"Case "<<i<<":\n"; solve(); } }

Compilation message (stderr)

railway.cpp: In function 'void dfs_seg(int, int, int)':
railway.cpp:87:28: warning: comparison of integer expressions of different signedness: 'std::set<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   87 |     if(subtree[pos].size() >= k) ans.insert(ind);
      |        ~~~~~~~~~~~~~~~~~~~~^~~~
railway.cpp: In function 'void solve()':
railway.cpp:101:16: warning: unused variable 'y' [-Wunused-variable]
  101 |         int x, y, lc;
      |                ^
#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...