Submission #647569

# Submission time Handle Problem Language Result Execution time Memory
647569 2022-10-03T07:44:20 Z Spade1 Training (IOI07_training) C++14
100 / 100
15 ms 4612 KB
#include<bits/stdc++.h>
#define pii pair<int, int>
#define pII pair<pii, pii>
#define pll pair<long long, long long>
#define ll long long
#define ld long double
#define st first
#define nd second
#define pb push_back
#define INF INT_MAX
using namespace std;

const int N = 1010, M = 5050, K = 12;

vector<int> adj[N];
int lvl[N], deg[N];
pii prt[N];
int up[N][K];
int dp[N][(1<<K)];
vector<pII> edge; //u, v, weight, lca
bool cmp(pII a, pII b) {
    return lvl[a.nd.nd] > lvl[b.nd.nd];
}

void dfs(int a, int p) {
    up[a][0] = p;
    prt[a] = {p, (1<<(deg[p]++))};
    lvl[a] = lvl[p] + 1;
    for (int i = 1; i < K; ++i) {
        up[a][i] = up[up[a][i-1]][i-1];
    }
    for (auto b : adj[a]) {
        if (b == p) continue;
        dfs(b, a);
    }
}

int lca(int a, int b) {
    if (lvl[a] < lvl[b]) swap(a, b);
    int jump = lvl[a] - lvl[b];
    for (int i = 0; i < K; ++i) {
        if (jump & (1<<i)) a = up[a][i];
    }

    if (a==b) return a;
    for (int i = K-1; i >= 0; --i) {
        if (up[a][i] != up[b][i]) {
            a = up[a][i], b = up[b][i];
        }
    }
    return up[a][0];
}

void solve() {
    for (auto i : edge) {
        int a = i.st.st, b = i.st.nd, cur = i.nd.st, l = i.nd.nd;
        if (lvl[a] % 2 != lvl[b] % 2 && cur != 0) continue;
        int mask1 = 0, mask2 = 0;
        for (; a != l; tie(a, mask1) = prt[a]) cur += dp[a][mask1];
        for (; b != l; tie(b, mask2) = prt[b]) cur += dp[b][mask2];

        int mask3 = mask1 | mask2;
        for (int i = 0; i < (1<<deg[l]); ++i) {
            if (i & mask3) continue;
            dp[l][i] = max(dp[l][i], cur + dp[l][i|mask3]);
        }
    }
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(NULL);
    int n, m, sum = 0; cin >> n >> m;
    for (int i = 0; i < m; ++i) {
        int a, b, w; cin >> a >> b >> w;
        if (!w) {
            adj[a].pb(b);
            adj[b].pb(a);
        }
        edge.pb({{a, b}, {w, 0}});
        sum += w;
    }
    dfs(1, 0);
    for (auto &i : edge) i.nd.nd = lca(i.st.st, i.st.nd);
    sort(edge.begin(), edge.end(), cmp);
    solve();
    cout << sum - dp[1][0];
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4560 KB Output is correct
2 Correct 7 ms 4592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 724 KB Output is correct
2 Correct 2 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 980 KB Output is correct
2 Correct 3 ms 872 KB Output is correct
3 Correct 4 ms 1492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2132 KB Output is correct
2 Correct 5 ms 1184 KB Output is correct
3 Correct 4 ms 1516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 724 KB Output is correct
2 Correct 3 ms 1620 KB Output is correct
3 Correct 15 ms 4612 KB Output is correct
4 Correct 3 ms 1696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 2132 KB Output is correct
2 Correct 13 ms 4564 KB Output is correct
3 Correct 6 ms 1640 KB Output is correct
4 Correct 5 ms 1492 KB Output is correct