#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
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)
~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
640 KB |
Output is correct |
2 |
Correct |
5 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
4608 KB |
Output is correct |
2 |
Correct |
11 ms |
4608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
640 KB |
Output is correct |
2 |
Correct |
5 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
896 KB |
Output is correct |
2 |
Correct |
5 ms |
768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
1152 KB |
Output is correct |
2 |
Correct |
6 ms |
1024 KB |
Output is correct |
3 |
Correct |
8 ms |
1664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
2304 KB |
Output is correct |
2 |
Correct |
10 ms |
1152 KB |
Output is correct |
3 |
Correct |
8 ms |
1620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
768 KB |
Output is correct |
2 |
Correct |
7 ms |
1792 KB |
Output is correct |
3 |
Correct |
21 ms |
4608 KB |
Output is correct |
4 |
Correct |
8 ms |
1792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
2220 KB |
Output is correct |
2 |
Correct |
18 ms |
4608 KB |
Output is correct |
3 |
Correct |
11 ms |
1664 KB |
Output is correct |
4 |
Correct |
10 ms |
1460 KB |
Output is correct |