Submission #447150

#TimeUsernameProblemLanguageResultExecution timeMemory
447150SirCovidThe19thTraining (IOI07_training)C++14
100 / 100
19 ms4528 KiB
#include <bits/stdc++.h> using namespace std; struct edge{ int a, b, w, lca; }; const int mx = 1005; int n, m, ans, dep[mx], tin[mx], tout[mx], cnt, par[mx], pos[mx], deg[mx], dp[mx][(1<<10)]; vector<int> adj[mx]; vector<edge> pth; void dfs(int i){ tin[i] = cnt++; for (int x : adj[i]) if (x != par[i]) dep[x] = dep[i]+1, par[x] = i, pos[x] = (1<<deg[i]++), dfs(x); tout[i] = cnt-1; }int LCA(int a, int b){ while (!(tin[a] <= tin[b] and tout[b] <= tout[a])) a = par[a]; return a; } int main(){ cin >> n >> m; for (int i = 0; i < m; i++){ int a, b, w; cin >> a >> b >> w; ans += w; if (!w) adj[a].push_back(b), adj[b].push_back(a); pth.push_back({a, b, w}); }dfs(1); for (auto &r : pth) r.lca = LCA(r.a, r.b); sort(pth.begin(), pth.end(), [](edge x, edge y){ return tin[x.lca] > tin[y.lca]; }); for (auto r : pth) if (!(r.w and (dep[r.a]+dep[r.b])%2)){ int sum = r.w, p1 = 0, p2 = 0; for (int x = r.a; x != r.lca; p1 = pos[x], x = par[x]) sum += dp[x][p1]; for (int x = r.b; x != r.lca; p2 = pos[x], x = par[x]) sum += dp[x][p2]; for (int msk = 0; msk < (1<<deg[r.lca]); msk++) if (!(msk&p1) and !(msk&p2)) dp[r.lca][msk] = max(dp[r.lca][msk], dp[r.lca][(msk|p1|p2)]+sum); }cout<<ans-dp[1][0]<<endl; }
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...