Submission #341109

#TimeUsernameProblemLanguageResultExecution timeMemory
341109ijxjdjdMergers (JOI19_mergers)C++14
0 / 100
9 ms12140 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; const int MAXN = (int)(5e5) + 5; vector<int> adj[MAXN]; int up[MAXN][21]; int timer = 0; int tin[MAXN]; int tout[MAXN]; int S[MAXN]; int last[MAXN]; int lcm[MAXN]; int cnt[MAXN]; void root(int n, int p) { tin[n] = timer++; up[n][0] = p; for (int k = 1; k <= 20; k++) { up[n][k] = up[up[n][k-1]][k-1]; } for (int e : adj[n]) { if (e != p) { root(e, n); } } tout[n] = timer++; } vector<int> dfs(int n, int p) { int paths = 0; vector<int> arr(3, 0); arr[2] = (int)(1e6); arr[1] = (int)(1e6); for (int e : adj[n]) { if (e != p) { vector<int> next = dfs(e, n); vector<int> old = arr; arr[1] = min(old[0] + next[1], old[1] + min(next[0], min(next[1], next[2])+1));; arr[1] = min(arr[1], old[2] + 1 + next[1]); arr[2] = min(old[2] + min(min(next[1], next[2])+1, next[0]), old[0] + next[2]); arr[2] = min(arr[2], old[1] + next[1]); arr[0] = min(next[1] + old[1] + 1, min(old[0], old[2]+1) + min(next[2]+1, next[0])); cnt[n] += cnt[e]; } } if (cnt[n] == 0) { if (n != p) { arr[1] = min(arr[0], arr[1]); arr[0] = (int)(1e6); } } return arr; } bool is_ancestor(int a, int b) { return (tin[a] <= tin[b] && tout[a] >= tout[b]); } int lca(int a, int b) { if (is_ancestor(a, b)) { return a; } if (is_ancestor(b, a)) { return b; } int u = a; for (int k = 20; k >= 0; k--) { if (!is_ancestor(up[u][k], b)) { u = up[u][k]; } } return up[u][0]; } int main() { string asdf = "input.in"; freopen(asdf.c_str(), "r", stdin); int N, s; cin >> N >> s; for (int i = 0; i < N-1; i++) { int a, b; cin >> a >> b; a--, b--; adj[a].push_back(b); adj[b].push_back(a); } vector<int> df; for (int i = 0; i < N; i++) { df.push_back(i); last[i] = -1; cin >> S[i]; S[i]--; } root(0, 0); sort(df.begin(), df.end(), [](const int& lhs, const int& rhs) { return tin[lhs] < tin[rhs]; } ); for (int i : df) { if (last[S[i]] == -1) { last[S[i]] = i; lcm[S[i]] = i; } else { if (is_ancestor(lcm[S[i]], i)) { cnt[i]++; cnt[lcm[S[i]]]--; } else { int lc = lca(lcm[S[i]], i); cnt[lcm[S[i]]]++; cnt[lc]-=2; cnt[i]++; lcm[S[i]] = lc; } } } vector<int> ans = dfs(0, 0); cout << min(ans[0], min(ans[1], ans[2])+1); return 0; }

Compilation message (stderr)

mergers.cpp: In function 'std::vector<int> dfs(int, int)':
mergers.cpp:29:9: warning: unused variable 'paths' [-Wunused-variable]
   29 |     int paths = 0;
      |         ^~~~~
mergers.cpp: In function 'int main()':
mergers.cpp:73:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   73 |     freopen(asdf.c_str(), "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...