Submission #562775

#TimeUsernameProblemLanguageResultExecution timeMemory
562775Tien_NoobRailway (BOI17_railway)C++17
0 / 100
70 ms22740 KiB
//Make CSP great again //Vengeance #include <bits/stdc++.h> #define TASK "TESTCODE" #define Log2(x) 31 - __builtin_clz(x) using namespace std; const int N = 1e5, cbit = 16; int dp[N + 1], tin[N + 1], tout[N + 1], timer, n, m, k, x[N], y[N], f[N + 1][cbit + 1], depth[N + 1]; vector<int> adj[N + 1], res, city; void read() { cin >> n >> m >> k; for (int i = 1; i < n; ++ i) { cin >> x[i] >> y[i]; adj[x[i]].push_back(i); adj[y[i]].push_back(i); } } void preDFS(int u, int p) { tin[u] = ++timer; for (int i : adj[u]) { int v = x[i] ^ u ^ y[i]; if (v == p) { continue; } depth[v] = depth[u] + 1; f[v][0] = u; for (int j = 1; j <= cbit; ++ j) { f[v][i] = f[f[v][j - 1]][j - 1]; } preDFS(v, u); } tout[u] = ++timer; } int LCA(int u, int v) { if (depth[u] > depth[v]) { swap(u, v); } int x = depth[v] - depth[u]; for (int i = cbit; i >= 0; -- i) { if ((x >> i) & 1) { v = f[v][i]; } } if (u == v) { return u; } for (int i = cbit; i >= 0; -- i) { if (f[u][i] != f[v][i]) { u = f[u][i]; v = f[v][i]; } } return f[u][0]; } void ReadQuery() { int s; cin >> s; city.clear(); while(s--) { int u; cin >> u; city.push_back(u); } sort(city.begin(), city.end(), [&] (const int &u, const int &v) { return tin[u] < tin[v]; }); vector<int> t; for (int i = 1; i < city.size(); ++ i) { t.push_back(LCA(city[i], city[i - 1])); } move(t.begin(), t.end(), back_inserter(city)); sort(city.begin(), city.end(), [&] (const int &u, const int &v) { return tin[u] < tin[v]; }); city.erase(unique(city.begin(), city.end()), city.end()); } void DnC() { stack<int> st; for (int v : city) { while(!st.empty()) { int u = st.top(); if (tin[v] <= tout[u]) { --dp[u]; ++dp[v]; break; } else { st.pop(); } } st.push(v); } } void DFS(int u, int p) { for (int i : adj[u]) { int v = x[i] ^ y[i] ^ u; if (v == p) { continue; } DFS(v, u); dp[u] += dp[v]; if (dp[v] >= k) { res.push_back(i); } } } void solve() { preDFS(1, 0); while(m--) { ReadQuery(); DnC(); } DFS(1, 0); sort(res.begin(), res.end()); cout << res.size() << '\n'; for (int i : res) { cout << i << ' '; } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); if (fopen(TASK".INP", "r")) { freopen(TASK".INP", "r", stdin); //freopen(TASK".OUT", "w", stdout); } int t = 1; bool typetest = false; if (typetest) { cin >> t; } for (int __ = 1; __ <= t; ++ __) { //cout << "Case " << __ << ": "; read(); solve(); } }

Compilation message (stderr)

railway.cpp: In function 'void ReadQuery()':
railway.cpp:84:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   84 |     for (int i = 1; i < city.size(); ++ i)
      |                     ~~^~~~~~~~~~~~~
railway.cpp: In function 'int main()':
railway.cpp:156:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  156 |         freopen(TASK".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#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...