# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
503061 |
2022-01-07T04:43:12 Z |
tabr |
Training (IOI07_training) |
C++17 |
|
300 ms |
51140 KB |
#include <bits/stdc++.h>
using namespace std;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif
struct dsu {
vector<int> p;
vector<int> sz;
int n;
dsu(int _n) : n(_n) {
p.resize(n);
iota(p.begin(), p.end(), 0);
sz.assign(n, 1);
}
inline int get(int x) {
if (p[x] == x) {
return x;
} else {
return p[x] = get(p[x]);
}
}
inline bool unite(int x, int y) {
x = get(x);
y = get(y);
if (x == y) {
return false;
}
if (sz[x] > sz[y]) {
swap(x, y);
}
p[x] = y;
sz[y] += sz[x];
return true;
}
inline bool same(int x, int y) {
return (get(x) == get(y));
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
dsu uf(2 * n);
vector<vector<int>> g(n);
vector<tuple<int, int, int>> edges;
for (int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
a--, b--;
if (c == 0) {
g[a].emplace_back(b);
g[b].emplace_back(a);
uf.unite(a, b + n);
uf.unite(a + n, b);
} else {
edges.emplace_back(a, b, c);
}
}
vector<tuple<int, int, int>> new_edges;
int ans = 0;
for (auto [a, b, c] : edges) {
ans += c;
if (uf.same(a, b)) {
new_edges.emplace_back(a, b, c);
}
}
swap(edges, new_edges);
m = (int) edges.size();
vector<vector<int>> d(n);
for (int i = 0; i < m; i++) {
auto [a, b, c] = edges[i];
d[a].emplace_back(i);
d[b].emplace_back(i);
}
vector<unordered_map<int, int>> dp(n);
function<void(int, int)> dfs = [&](int v, int p) {
vector<int> ch;
unordered_map<int, vector<int>> mp;
for (int to : g[v]) {
if (to == p) {
continue;
}
dfs(to, v);
for (auto [id, cost] : dp[to]) {
if (id != -1) {
mp[id].emplace_back((int) ch.size());
}
}
ch.emplace_back(to);
}
int sz = (int) ch.size();
vector<int> f(1 << sz);
for (int i = 0; i < sz; i++) {
f[1 << i] = dp[ch[i]][-1];
}
for (auto [id, u] : mp) {
if (u.size() == 2) {
int mask = (1 << u[0]) + (1 << u[1]);
f[mask] = max(f[mask], dp[ch[u[0]]][id] + dp[ch[u[1]]][id] + get<2>(edges[id]));
}
}
for (int id : d[v]) {
auto u = mp[id];
if (u.size() == 1) {
f[1 << u[0]] = max(f[1 << u[0]], dp[ch[u[0]]][id] + get<2>(edges[id]));
mp[id].emplace_back(-1);
}
}
for (int mask = 0; mask < (1 << sz); mask++) {
for (int i = 0; i < sz; i++) {
if (mask & (1 << i)) {
f[mask] = max(f[mask], f[(1 << i)] + f[mask ^ (1 << i)]);
for (int j = i + 1; j < sz; j++) {
if (mask & (1 << j)) {
f[mask] = max(f[mask], f[(1 << i) + (1 << j)] + f[mask ^ ((1 << i) + (1 << j))]);
}
}
}
}
}
dp[v][-1] = f.back();
for (int id : d[v]) {
auto u = mp[id];
if (u.size() == 0) {
dp[v][id] = f.back();
}
}
for (auto [id, u] : mp) {
if (u.size() == 1) {
int mask = (1 << sz) - 1;
mask -= 1 << u[0];
dp[v][id] = f[mask] + dp[ch[u[0]]][id];
}
}
};
dfs(0, -1);
ans -= dp[0][-1];
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
94 ms |
14516 KB |
Output is correct |
2 |
Correct |
132 ms |
16336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
460 KB |
Output is correct |
2 |
Correct |
2 ms |
588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
1100 KB |
Output is correct |
2 |
Correct |
7 ms |
1228 KB |
Output is correct |
3 |
Correct |
23 ms |
3276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
3448 KB |
Output is correct |
2 |
Correct |
12 ms |
1804 KB |
Output is correct |
3 |
Correct |
13 ms |
2064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
1004 KB |
Output is correct |
2 |
Correct |
32 ms |
4916 KB |
Output is correct |
3 |
Execution timed out |
341 ms |
51140 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
122 ms |
17764 KB |
Output is correct |
2 |
Execution timed out |
405 ms |
50628 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |