# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
396669 | Sorting | Training (IOI07_training) | C++17 | 0 ms | 0 KiB |
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 long long N = 2000 + 3;
const long long M = 5000 + 3;
long long n, m;
vector<long long> adj[N];
vector<array<long long, 3>> e, e2;
vector<long long> arr, dp;
long long pos[N];
void dfs(long long u, long long p = -1){
arr.push_back(u);
pos[u] = (long long)arr.size() - 1;
for(long long to: adj[u])
if(to != p)
dfs(to, u);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for(long long i = 0; i < m; ++i){
long long 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(long long i = 0; i < e.size(); ++i){
auto [a, b, c] = e[i];
long long new_a = pos[a];
long long new_b = pos[b];
e[i] = {min(new_a, new_b), max(new_a, new_b), c};
}
long long ans = 0;
for(long long 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());
for(auto [a, b, c]: e2)
cout << a << " " << b << " " << c << endl;
dp.resize(e2.size() + 1);
dp[e2.size()] = 0;
for(long long i = (long long)e2.size() - 1; i >= 0; --i){
long long 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(long long i = 0; i < e2.size(); ++i)
ans += e2[i][2];
cout << ans << "\n";
}
/*
3 3
1 3 1
1 2 0
2 3 0
*/