#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;
vector<int> temp(1, 0);
deque<vector<int>> q;
q.push_back(temp);
while(q.size()) {
vector<int> t = q.front();
q.pop_front();
for(int i: t) {
path[t.back()].push_back(i);
}
for(int i: neighbors[t.back()]) {
if(visited[i]) continue;
visited[i]=true;
vector<int> s = t;
s.push_back(i);
q.push_back(s);
}
}
/*
for(int i = 0; i < n; i++) {
cout << i << endl;
for(int j: path[i]) cout << j << " ";
cout << endl;
}
*/
visited2[0]=true;
dfs(0);
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:85:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<edge>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
85 | for(int j = 0; j < edges.size(); j++) {
| ~~^~~~~~~~~~~~~~
training.cpp:88:26: warning: comparison of integer expressions of different signedness: 'int' and 'const long unsigned int' [-Wsign-compare]
88 | for(int i = 1; i < min(path[e.a].size(), path[e.b].size()); i++) {
| ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1075 ms |
468 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1068 ms |
6484 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 |
1074 ms |
340 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1065 ms |
724 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1069 ms |
1492 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1067 ms |
2388 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1079 ms |
4692 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1042 ms |
2388 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1062 ms |
5032 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |