Submission #571667

#TimeUsernameProblemLanguageResultExecution timeMemory
571667YouAreMyGalaxyMergers (JOI19_mergers)C++17
100 / 100
1077 ms135628 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 = 5e5, cbit = 18; vector<int> num[N + 1], adj[N + 1]; int n, k, tin[N + 1], tout[N + 1], depth[N + 1], dp[N + 1], timer, res, cnt[N + 1], f[N + 1][cbit + 1]; void read() { cin >> n >> k; for (int i = 1; i < n; ++ i) { int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } for (int u = 1; u <= n; ++ u) { int a; cin >> a; num[a].push_back(u); } } void preDFS(int u, int p) { tin[u] = ++timer; for (int v : adj[u]) { if (v == p) { continue; } depth[v] = depth[u] + 1; f[v][0] = u; for (int i = 1; i <= cbit; ++ i) { f[v][i] = f[f[v][i - 1]][i - 1]; if (!f[v][i]) { break; } } preDFS(v, u); } tout[u] = timer; } int LCA(int u, int v) { if (depth[u] > depth[v]) { swap(u, v); } int k = depth[v] - depth[u]; for (int i = cbit; i >= 0; -- i) { if ((k >> 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 DFS_down(int u, int p) { for (int v : adj[u]) { if (v == p) { continue; } DFS_down(v, u); dp[u] += dp[v]; } } void DFS(int u, int p, int last_zero) { if (dp[u] == 0) { if (last_zero != -1) { ++cnt[last_zero]; ++cnt[u]; } last_zero = u; } for (int v : adj[u]) { if (v == p) { continue; } DFS(v, u, last_zero); } } void solve() { preDFS(1, 0); for (int i = 1; i <= k; ++ i) { sort(num[i].begin(), num[i].end(), [&](const int &x, const int &y) { return tin[x] < tin[y]; }); vector<int> add, tmp; stack<int> st; for (int j = 1; j < num[i].size(); ++ j) { add.push_back(LCA(num[i][j], num[i][j - 1])); } move(add.begin(), add.end(), back_inserter(num[i])); sort(num[i].begin(), num[i].end(), [&](const int &x, const int &y) { return tin[x] < tin[y]; }); num[i].erase(unique(num[i].begin(), num[i].end()), num[i].end()); for (int u : num[i]) { while(!st.empty() && (tin[u] < tin[st.top()] || tin[u] > tout[st.top()])) { st.pop(); } if (!st.empty()) { ++dp[st.top()]; --dp[u]; } st.push(u); } } DFS_down(1, 0); DFS(1, 0, -1); for (int u = 1; u <= n; ++ u) { res += (cnt[u] == 1); } cout << (res + 1)/2; } 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 #" << __ << ":" << '\n'; read(); solve(); } }

Compilation message (stderr)

mergers.cpp: In function 'void solve()':
mergers.cpp:121:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  121 |         for (int j = 1; j < num[i].size(); ++ j)
      |                         ~~^~~~~~~~~~~~~~~
mergers.cpp: In function 'int main()':
mergers.cpp:159:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  159 |         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...