Submission #797006

# Submission time Handle Problem Language Result Execution time Memory
797006 2023-07-29T04:04:39 Z vgoofficial Training (IOI07_training) C++14
100 / 100
9 ms 4692 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct edge {
    int a,b,c,lca;
    edge(int a, int b, int c) {
        this->a=a;
        this->b=b;
        this->c=c;
        this->lca=-1;
    }
};
vector<vector<int>> neighbors;
vector<bool> visited2;
vector<edge> edges;
vector<vector<int>> path;
vector<int> degree;
vector<int> lvl;
vector<pair<int, int>> parent;
vector<int> dist;
int dg=0;
void dfs(int node) {
    for(int i: neighbors[node]) {
        if(visited2[i]) continue;
        visited2[i]=true;
        parent[i].first=node;
        parent[i].second=1<<lvl[node];
        dist[i]=dist[node]+1;
        lvl[node]++;
        dfs(i);
    }
    degree[node]=dg;
    dg++;
}
int main() {
    ios_base::sync_with_stdio(false); 
    cin.tie(0);
    int n,m;
    int total = 0;
    cin >> n >> m;
    neighbors = vector<vector<int>>(n, vector<int>());
    visited2 = vector<bool>(n);
    path = vector<vector<int>>(n);
    lvl = vector<int>(n);
    dist = vector<int>(n);
    degree = vector<int>(n);
    parent = vector<pair<int, int>>(n);
    for(int i = 0; i < m; i++) {
        int a,b,c;
        cin >> a >> b >> c;
        a--;
        b--;
        edges.push_back(edge(a,b,c));
        if(c==0) {
            neighbors[a].push_back(b);
            neighbors[b].push_back(a);
        }
        total+=c;
    }   
    visited2[0]=true;
    dfs(0);
    //cout << "got here" << endl;
    /*
    for(int i = 0; i < n; i++) {
        cout << i << endl;
        for(int j: path[i]) cout << j << " ";
        cout << endl;
    }


    cout << "DIST" << endl;
    for(int i = 0; i < n; i++) {
        cout << i << " " << dist[i] << endl;
    }
        */
    for(edge &e: edges) {
        int a = e.a, b = e.b;
        while(dist[a]>dist[b]) {
            a=parent[a].first;
        }
        while(dist[b]>dist[a]) {
            b=parent[b].first;
        }
        while(a!=b) {
            a=parent[a].first;
            b=parent[b].first;
        }
        e.lca=a;
    }
    sort(begin(edges), end(edges), [](const edge a, const edge b) -> bool{
        return degree[a.lca]<degree[b.lca];
    });
        /*
    for(edge e: edges) {
        cout << e.a << " " << e.b << " " << e.c << " " << e.lca << endl;
    }

    cout << "PARENT" << endl;
    for(int i = 0; i < 5; i++) {
        cout << i << " " << parent[i].first << " " << parent[i].second << endl;
    }
    */
   //cout << "line 113" << endl;

    vector<vector<int>> dp(n, vector<int>(1<<10));
    for(edge e: edges) {
        int a = e.a, b = e.b, current = e.c, lca = e.lca, m1=0, m2=0;
        if(current!=0&&dist[a]%2!=dist[b]%2) continue;
        //cout << a << " " << b << " " << current << " " << lca << " " << m1 << " " << m2 << endl;
        while(a!=lca) {
            current+=dp[a][m1];
            m1=parent[a].second;    
            a=parent[a].first;
            //cout << "a " << a << endl;
        }
        while(b!=lca) {
            current+=dp[b][m2];
            m2=parent[b].second;
            b=parent[b].first;
            //if(b!=0) 
            //cout << "b " << b << endl;
        }
        //cout << "line 129" << endl;
        m1|=m2;
        for(int i = (1<<lvl[lca])-1; i >= 0; i--) {
            if(m1&i) continue;
            dp[lca][i]=max(dp[lca][i], current+dp[lca][i|m1]);
        }
       
    }
    cout << total-dp[0][0] << endl;
}  
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 316 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 5 ms 4692 KB Output is correct
2 Correct 5 ms 4564 KB Output is correct
# 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 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 708 KB Output is correct
2 Correct 1 ms 736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1492 KB Output is correct
2 Correct 1 ms 1492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2388 KB Output is correct
2 Correct 2 ms 2464 KB Output is correct
3 Correct 3 ms 2388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4556 KB Output is correct
2 Correct 6 ms 4564 KB Output is correct
3 Correct 4 ms 4572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2388 KB Output is correct
2 Correct 3 ms 2384 KB Output is correct
3 Correct 9 ms 4644 KB Output is correct
4 Correct 3 ms 2388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4564 KB Output is correct
2 Correct 9 ms 4664 KB Output is correct
3 Correct 7 ms 4564 KB Output is correct
4 Correct 5 ms 4564 KB Output is correct