Submission #462613

#TimeUsernameProblemLanguageResultExecution timeMemory
462613JovanBRailway (BOI17_railway)C++17
100 / 100
155 ms26928 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; const int MAXLOG = 18; int par[100005][MAXLOG+1]; vector <pair <int, int>> graf[100005]; int depth[100005]; int in[100005]; int out[100005]; int x[100005]; int pre[100005]; vector <int> res; int tajm; void dfs_pre(int v, int parent){ depth[v] = depth[parent] + 1; par[v][0] = parent; for(int j=1; j<MAXLOG; j++){ par[v][j] = par[par[v][j-1]][j-1]; } in[v] = ++tajm; for(auto d : graf[v]){ int c = d.first; if(c != parent) dfs_pre(c, v); } out[v] = tajm; } bool is_parent(int a, int b){ return in[a] <= in[b] && out[b] <= out[a]; } bool cmp(int a, int b){ return in[a] < in[b]; } int k; int lca(int a, int b){ if(is_parent(a, b)) return a; if(is_parent(b, a)) return b; for(int j=MAXLOG-1; j>=0; j--){ if(!is_parent(par[a][j], b)) a = par[a][j]; } return par[a][0]; } int dfs(int v){ int tren = 0; int koji = 0; for(auto c : graf[v]){ if(c.first != par[v][0]) tren += dfs(c.first); else koji = c.second; } tren += pre[v]; if(tren >= k) res.push_back(koji); return tren; } int main(){ ios_base::sync_with_stdio(false), cin.tie(0); cout.precision(10); cout << fixed; out[0] = 2e5; int n, m; cin >> n >> m >> k; for(int i=1; i<n; i++){ int a, b; cin >> a >> b; graf[a].push_back({b, i}); graf[b].push_back({a, i}); } dfs_pre(1, 0); for(int i=1; i<=m; i++){ int len; cin >> len; for(int j=1; j<=len; j++){ cin >> x[j]; } sort(x+1, x+1+len, cmp); int mn = n+5; int v = x[len]; int kolko = 0; vector <int> sad; for(int j=len; j>=1; j--){ v = lca(v, x[j]); if(out[x[j]] < mn){ pre[x[j]]++; kolko++; mn = out[x[j]]; sad.push_back(x[j]); } } for(int j=0; j<sad.size()-1; j++){ pre[lca(sad[j], sad[j+1])]--; } pre[v]--; } dfs(1); sort(res.begin(), res.end()); cout << res.size() << "\n"; for(auto c : res) cout << c << " "; return 0; }

Compilation message (stderr)

railway.cpp: In function 'int main()':
railway.cpp:101:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  101 |         for(int j=0; j<sad.size()-1; j++){
      |                      ~^~~~~~~~~~~~~
#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...