Submission #589836

#TimeUsernameProblemLanguageResultExecution timeMemory
589836tranxuanbachTraining (IOI07_training)C++17
100 / 100
22 ms4692 KiB
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; #define endl '\n' #define fi first #define se second #define For(i, l, r) for (auto i = (l); i < (r); i++) #define ForE(i, l, r) for (auto i = (l); i <= (r); i++) #define FordE(i, l, r) for (auto i = (l); i >= (r); i--) #define Fora(v, a) for (auto v: (a)) #define bend(a) (a).begin(), (a).end() #define isz(a) ((signed)(a).size()) using ll = long long; using ld = long double; using pii = pair <int, int>; using vi = vector <int>; using vpii = vector <pii>; using vvi = vector <vi>; const int N = 1e3 + 5, M = 5e3 + 5; struct edge_t{ int u, v, w, t; int tu, tv, dp = 0; edge_t(){ } edge_t(int u, int v, int w): u(u), v(v), w(w){ } }; int n, m; vi adj[N]; vector <edge_t> a; int h[N], par[N], idpar[N]; void dfs_par(int u){ Fora(v, adj[u]){ if (v == par[u]){ continue; } par[v] = u; h[v] = h[u] + 1; dfs_par(v); } for (auto itr = adj[u].begin(); itr != adj[u].end(); itr++){ if (*itr == par[u]){ adj[u].erase(itr); break; } } For(i, 0, isz(adj[u])){ idpar[adj[u][i]] = i; } } int lca(int u, int v){ if (h[u] < h[v]){ swap(u, v); } while (h[u] > h[v]){ u = par[u]; } while (u != v){ u = par[u]; v = par[v]; } return u; } int nearlca(int u, int v){ if (h[u] < h[v]){ swap(u, v); } while (h[u] - 1 > h[v]){ u = par[u]; } return u; } vector <edge_t> b[N]; int dp[N][1 << 10]; int ans; void dfs_dp(int u){ Fora(v, adj[u]){ if (v == par[u]){ continue; } dfs_dp(v); } Fora(&ed, b[u]){ if (ed.u != u){ ed.dp += dp[ed.u][(1 << isz(adj[ed.u])) - 1]; int node = ed.u; while (node != u and par[node] != u){ ed.dp += dp[par[node]][(1 << isz(adj[par[node]])) - 1 - (1 << idpar[node])]; node = par[node]; } } if (ed.v != u){ ed.dp += dp[ed.v][(1 << isz(adj[ed.v])) - 1]; int node = ed.v; while (node != u and par[node] != u){ ed.dp += dp[par[node]][(1 << isz(adj[par[node]])) - 1 - (1 << idpar[node])]; node = par[node]; } } } int k = isz(adj[u]); For(msk, 0, (1 << k)){ { For(i, 0, k){ if (msk >> i & 1){ dp[u][msk] += dp[adj[u][i]][(1 << isz(adj[adj[u][i]])) - 1]; } } } Fora(&ed, b[u]){ if ((ed.tu == u or (msk >> idpar[ed.tu] & 1)) and (ed.tv == u or (msk >> idpar[ed.tv] & 1))){ int tmsk = msk; if (ed.tu != u){ tmsk ^= 1 << idpar[ed.tu]; } if (ed.tv != u){ tmsk ^= 1 << idpar[ed.tv]; } dp[u][msk] = max(dp[u][msk], ed.w + dp[u][tmsk] + ed.dp); } } } } signed main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // freopen("KEK.inp", "r", stdin); // freopen("KEK.out", "w", stdout); cin >> n >> m; ForE(i, 1, m){ int u, v, w; cin >> u >> v >> w; if (w == 0){ adj[u].emplace_back(v); adj[v].emplace_back(u); } else{ a.emplace_back(u, v, w); ans += w; } } m -= n - 1; dfs_par(1); Fora(&ed, a){ if ((h[ed.u] + h[ed.v]) & 1){ continue; } int t = lca(ed.u, ed.v); ed.tu = nearlca(ed.u, t); ed.tv = nearlca(ed.v, t); b[t].emplace_back(ed); } dfs_dp(1); ans -= dp[1][(1 << isz(adj[1])) - 1]; cout << ans << endl; } /* ==================================================+ INPUT: | --------------------------------------------------| --------------------------------------------------| ==================================================+ OUTPUT: | --------------------------------------------------| --------------------------------------------------| ==================================================+ */
#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...