Submission #456395

#TimeUsernameProblemLanguageResultExecution timeMemory
456395Killer2501Railway (BOI17_railway)C++14
100 / 100
160 ms39864 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define pll pair<ll, ll> #define fi first #define se second #define pb push_back const int N = 1e5+5; const long long mod = 1e9+7; const int base = 300; ll m, n, k, a[N], ans, b[N], tong, d[N], c[N], lab[N], cnt, t, dp[N], h[N], P[N][20]; vector<pll> val[N]; vector<ll> adj[N], res; ll pw(ll k, ll n) { ll total = 1; for(; n; n >>= 1) { if(n & 1)total = total * k % mod; k = k * k % mod; } return total; } template<typename T> void read(T&x) { bool Neg=false; char c; for(c= getchar();c<'0'||c>'9';c=getchar()) if(c=='-') Neg =!Neg; x = c-'0'; for (c=getchar();c>='0'&&c<='9';c=getchar()) x=x*10+c-'0'; if(Neg&&x>0) x=-x; } ll findp(ll u) { return lab[u] < 0 ? u : lab[u] = findp(lab[u]); } bool hop(ll u, ll v) { u = findp(u); v = findp(v); if(u == v)return false; if(lab[u] > lab[v])swap(u, v); lab[u] += lab[v]; lab[v] = u; return true; } string s; void dfs(ll u, ll p) { d[u] = ++t; for(ll i : adj[u]) { ll v = a[i] + b[i] - u; if(v == p)continue; c[v] = i; P[v][0] = u; h[v] = h[u] + 1; for(int i = 1; i <= 18; i ++)P[v][i] = P[P[v][i-1]][i-1]; dfs(v, u); } } void cal(ll u, ll p) { for(ll i : adj[u]) { ll v = a[i] + b[i] - u; if(v == p)continue; cal(v, u); dp[u] += dp[v]; } //cout << u <<" "<<dp[u] << '\n'; } ll lca(ll u, ll v) { if(h[u] < h[v])swap(u, v); ll log = log2(h[u]); for(int i = log; i >= 0; i --)if(h[u] >= h[v] + (1<<i))u = P[u][i]; if(u == v)return u; for(int i = log; i >= 0; i --) { if(P[u][i] && P[u][i] != P[v][i]) { u = P[u][i]; v = P[v][i]; } } return P[u][0]; } void sol() { cin >> n >> m >> k; for(int i = 1; i < n; i ++) { cin >> a[i] >> b[i]; adj[a[i]].pb(i); adj[b[i]].pb(i); } h[1] = 1; dfs(1, 0); while(m -- > 0) { cin >> t; vector<pll> kq; for(int i = 1; i <= t; i ++) { ll u; cin >> u; ++dp[u]; kq.pb({d[u], u}); } sort(kq.begin(), kq.end()); for(int i = 0; i < t; i ++) { int j = (i+1)%t; --dp[lca(kq[i].se, kq[j].se)]; //cout << kq[i].se <<" "<<kq[j].se << " "<<lca(kq[i].se, kq[j].se) << '\n'; } } cal(1, 0); for(int i = 2; i <= n; i ++)if(dp[i] >= k)res.pb(c[i]); sort(res.begin(), res.end()); cout << res.size() << '\n'; for(ll x : res)cout << x <<" "; } int main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); #define task "testss" if(fopen(task".inp", "r")) { freopen(task".inp", "r", stdin); freopen(task".out", "w", stdout); } int ntest = 1; //cin >> ntest; while(ntest -- > 0) sol(); } /* 10 4 5 4 3 2 -1 0 0 0 0 0 */

Compilation message (stderr)

railway.cpp: In function 'int main()':
railway.cpp:138:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  138 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
railway.cpp:139:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  139 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#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...