Submission #797006

#TimeUsernameProblemLanguageResultExecution timeMemory
797006vgoofficialTraining (IOI07_training)C++14
100 / 100
9 ms4692 KiB
#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 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...