Submission #796354

# Submission time Handle Problem Language Result Execution time Memory
796354 2023-07-28T10:15:41 Z vgoofficial Training (IOI07_training) C++14
10 / 100
300 ms 6868 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> visited, visited2;
vector<edge> edges;
vector<vector<int>> path;
vector<int> degree;
vector<int> lvl;
vector<pair<int, int>> parent;
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];
        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>());
    visited = vector<bool>(n);
    visited2 = vector<bool>(n);
    path = vector<vector<int>>(n);
    lvl = 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;
    }   
    visited[0]=true;
    deque<int> q;
    visited2[0]=true;
    dfs(0);
    q.push_back(0);
    while(q.size()) {
        int x = q.front();
        q.pop_front();
        path[x]=path[parent[x].first];
        path[x].push_back(x);
        for(int i: neighbors[x]) {
            if(visited[i]) continue;
            visited[i]=true;
            q.push_back(i);
        }
    }
    /*
    for(int i = 0; i < n; i++) {
        cout << i << endl;
        for(int j: path[i]) cout << j << " ";
        cout << endl;
    }
    */

    for(int j = 0; j < edges.size(); j++) {
        edge &e = edges[j];
        //cout << e.a << " " << e.b << " " << e.c << " " << path[e.a].size() << " " << path[e.b].size() << endl;
        for(int i = 1; i < min(path[e.a].size(), path[e.b].size()); i++) {
            if(path[e.a][i]!=path[e.b][i]) {
                e.lca=path[e.a][i-1];
            }
        }
        if(e.lca==-1) {
            if(path[e.a].size()<path[e.b].size()) {
                e.lca=path[e.a].back();
            } else {
                e.lca=path[e.b].back();
            }
        }
    }
    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&&path[a].size()%2!=path[b].size()%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 << "just did: " << e.a << " " << e.b << " " << e.c << " " << e.lca << " " << m1 << endl;


		for(int i = 0; i < 5; i++) {
			cout << i << " " << dp[i][0] << " " << dp[i][1] << endl;
		}
        */
       
    }
    cout << total-dp[0][0] << endl;
}  

Compilation message

training.cpp: In function 'int main()':
training.cpp:82:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<edge>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   82 |     for(int j = 0; j < edges.size(); j++) {
      |                    ~~^~~~~~~~~~~~~~
training.cpp:85:26: warning: comparison of integer expressions of different signedness: 'int' and 'const long unsigned int' [-Wsign-compare]
   85 |         for(int i = 1; i < min(path[e.a].size(), path[e.b].size()); i++) {
      |                        ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1080 ms 468 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1079 ms 6868 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1067 ms 340 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1069 ms 340 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1071 ms 724 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1073 ms 1492 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1077 ms 2464 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1067 ms 4692 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1083 ms 2388 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1076 ms 5076 KB Time limit exceeded
2 Halted 0 ms 0 KB -