Submission #525773

#TimeUsernameProblemLanguageResultExecution timeMemory
525773mjhmjh1104Mountains and Valleys (CCO20_day1problem3)C++17
5 / 25
707 ms108608 KiB
#include <cstdio> #include <vector> #include <utility> #include <algorithm> using namespace std; int n, m, dp[20][20971520]; vector<int> tree[200006]; vector<pair<int, int>> adj[200006]; int f(int x, int y) { if (dp[x][y] + 1) return dp[x][y]; if (!y) return dp[x][y] = 0; dp[x][y] = (int)1e9; for (auto &i: adj[x]) dp[x][y] = min(dp[x][y], i.second + f(i.first, y & ~(1 << i.first))); for (auto &i: tree[x]) dp[x][y] = min(dp[x][y], 1 + f(i, y & ~(1 << i))); return dp[x][y]; } int lt[5006][5006], par[200006], sz[200006]; bool cl[200006]; void dfs_lt(int x, int y, int dist = 0, int prev = -1) { lt[y][x] = dist; for (auto &i: tree[x]) if (i != prev) dfs_lt(i, y, dist + 1, x); } int dfs(int x, int prev = -1) { par[x] = prev; sz[x] = 1; for (auto &i: tree[x]) if (i != prev) { sz[x] = max(sz[x], dfs(i, x) + 1); } return sz[x]; } int dist[200006]; int dfs_dist(int x, int prev = -1) { vector<int> v; int ret = 0; dist[x] = 0; for (auto &i: tree[x]) if (i != prev) { v.push_back(dfs_dist(i, x) + 1); ret = max(ret, v.back()); dist[x] = max(dist[x], dist[i]); } sort(v.rbegin(), v.rend()); if (!v.empty()) dist[x] = max(dist[x], v[0]); if ((int)v.size() > 1) dist[x] = max(dist[x], v[0] + v[1]); return ret; } int main() { scanf("%d%d", &n, &m); while (m--) { int x, y, w; scanf("%d%d%d", &x, &y, &w); if (w == 1) { tree[x].push_back(y); tree[y].push_back(x); } else { adj[x].push_back({ y, w }); adj[y].push_back({ x, w }); } } if (n <= 20) { for (int i = 0; i < n; i++) for (int j = 0; j < 1 << n; j++) dp[i][j] = -1; int res = (int)1e9; for (int i = 0; i < n; i++) res = min(res, f(i, (1 << n) - 1 ^ 1 << i)); printf("%d", res); return 0; } for (int i = 0; i < n; i++) dfs_lt(i, i); dfs_dist(0); int zero = 2 * (n - 1) - dist[0], one = (int)1e9; if (n <= 1000) for (int i = 0; i < n; i++) for (auto &j: adj[i]) if (i < j.first) { int curr = j.second + 2 * (n - 1) - lt[i][j.first] - 1; dfs(i); int x = j.first, pv = -1; vector<int> v; int y = -1; dfs_dist(i); while (x != -1) { v.emplace_back(); for (auto &k: tree[x]) if (k != pv && k != par[x]) { v.back() = max(v.back(), sz[k]); y = max(y, dist[k] - 1); } pv = x; x = par[x]; } for (int i = 0; i < (int)v.size(); i++) for (int j = i + 1; j < (int)v.size(); j++) y = max(y, v[i] + v[j] - (j - i - 1)); curr -= y; one = min(one, curr); } printf("%d", min(zero, one)); }

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:70:66: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
   70 |         for (int i = 0; i < n; i++) res = min(res, f(i, (1 << n) - 1 ^ 1 << i));
      |                                                         ~~~~~~~~~^~~
Main.cpp:55:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   55 |     scanf("%d%d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~
Main.cpp:58:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   58 |         scanf("%d%d%d", &x, &y, &w);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...