This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int N = 2000 + 3;
const int M = 5000 + 3;
int n, m;
vector<int> adj[N];
vector<array<int, 3>> e, e2;
vector<int> arr, dp;
int pos[N];
void dfs(int u, int p = -1){
arr.push_back(u);
pos[u] = (int)arr.size() - 1;
for(int to: adj[u])
if(to != p)
dfs(to, u);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for(int i = 0; i < m; ++i){
int a, b, c;
cin >> a >> b >> c;
if(!c){
adj[a].push_back(b);
adj[b].push_back(a);
}
else e.push_back({a, b, c});
}
dfs(1);
for(int i = 0; i < e.size(); ++i){
auto [a, b, c] = e[i];
int new_a = pos[a];
int new_b = pos[b];
e[i] = {min(new_a, new_b), max(new_a, new_b), c};
}
int ans = 0;
for(int i = 0; i < e.size(); ++i){
auto [a, b, c] = e[i];
if((b - a) & 1) ans += c;
else e2.push_back({a, b, c});
}
sort(e2.begin(), e2.end());
dp.resize(e2.size() + 1);
dp[e2.size()] = 0;
for(int i = (int)e2.size() - 1; i >= 0; --i){
int next_idx = lower_bound(e2.begin(), e2.end(), array{e2[i][1] + 1, 0, 0}) - e2.begin();
next_idx = max(next_idx, i + 1);
dp[i] = max(dp[i + 1], e2[i][2] + dp[next_idx]);
}
ans -= dp[0];
for(int i = 0; i < e2.size(); ++i)
ans += e2[i][2];
cout << ans << "\n";
}
/*
3 3
1 3 1
1 2 0
2 3 0
*/
Compilation message (stderr)
training.cpp: In function 'int main()':
training.cpp:41:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | for(int i = 0; i < e.size(); ++i){
| ~~^~~~~~~~~~
training.cpp:49:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | for(int i = 0; i < e.size(); ++i){
| ~~^~~~~~~~~~
training.cpp:65:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | for(int i = 0; i < e2.size(); ++i)
| ~~^~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |