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>
#define god dimasi5eks
#pragma GCC optimize("O3")
#define fi first
#define se second
#define pb push_back
#define pf push_front
// #define fisier 1
using namespace std;
typedef long long ll;
const int mod = 1000000007;
const double dancila = 3.14159265359; // PI
const double eps = 1e-9;
int n, m, cost;
vector<int> v[1002];
int dp[1001][1024], tt[12][1002], lvl[1002], st[1002], dr[1002], dt, grad[1002];
pair<int, int> p[1002];
struct muchie
{
int a, b, c, lca;
};
bool cmp(muchie a, muchie b)
{
return dr[a.lca] < dr[b.lca];
}
vector<muchie> muchii;
void dfs(int dad, int nod)
{
lvl[nod] = lvl[dad] + 1;
tt[0][nod] = dad;
++dt;
st[nod] = dt;
for(int i = 1; i <= 10; ++i)
tt[i][nod] = tt[i-1][tt[i-1][nod]];
for(int i = 0; i < v[nod].size(); ++i)
{
int vecin = v[nod][i];
if(vecin == dad)
continue;
p[vecin] = {nod, (1 << grad[nod])};
++grad[nod];
dfs(nod, vecin);
}
++dt;
dr[nod] = dt;
}
int LCA(int a, int b)
{
if(lvl[a] > lvl[b])
swap(a, b);
for(int i = 10; i >= 0; --i)
if(lvl[b] - (1<<i) >= lvl[a])
b = tt[i][b];
if(a == b)
return a;
for(int i = 10; i >= 0; --i)
if(tt[i][a] != tt[i][b])
a = tt[i][a], b = tt[i][b];
return tt[0][a];
}
int main()
{
#ifdef fisier
ifstream f("input.in");
ofstream g("output.out");
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for(int i = 1; i <= m; ++i)
{
int a, b, c;
cin >> a >> b >> c;
cost += c;
if(c == 0)
{
v[a].pb(b);
v[b].pb(a);
}
muchii.pb({a, b, c, 0});
}
dfs(0, 1);
for(int i = 0; i < m; ++i)
muchii[i].lca = LCA(muchii[i].a, muchii[i].b);
sort(muchii.begin(), muchii.end(), cmp);
for(muchie i : muchii)
{
int de = lvl[i.a] % 2;
int dx = lvl[i.b] % 2;
if(i.c && (de ^ dx))
continue;
int sm = i.c;
pair<int, int> A, B;
for(A = {i.a, 0}; A.fi != i.lca; A = p[A.fi])
sm += dp[A.fi][A.se];
for(B = {i.b, 0}; B.fi != i.lca; B = p[B.fi])
sm += dp[B.fi][B.se];
for(int mask = (1 << grad[i.lca]) - 1; mask >= 0; mask--)
if(!(mask & A.se || mask & B.se))
dp[i.lca][mask] = max(dp[i.lca][mask], sm + dp[i.lca][mask | A.se | B.se]);
}
cout << cost - dp[1][0];
return 0;
}
Compilation message (stderr)
training.cpp: In function 'void dfs(int, int)':
training.cpp:46:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < v[nod].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... |