#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 1000;
struct Edge { int u, v, w; };
int N, M, num[MAXN+10];
vector<int> adj[MAXN+10], chd[MAXN+10];
vector<Edge> E, edge[MAXN+10];
int dep[MAXN+10], par[MAXN+10][20], sum;
void dfs(int now, int bef, int depth)
{
int i, j;
dep[now]=depth;
par[now][0]=bef;
for(auto nxt : adj[now])
{
if(nxt==bef) continue;
chd[now].push_back(nxt);
dfs(nxt, now, depth+1);
}
for(i=0; i<chd[now].size(); i++) num[chd[now][i]]=i;
}
int lca(int u, int v)
{
int i, j;
if(dep[u]>dep[v]) swap(u, v);
for(i=10; i>=0; i--) if(dep[u]<=dep[par[v][i]]) v=par[v][i];
if(u==v) return u;
for(i=10; i>=0; i--) if(par[u][i]!=par[v][i]) u=par[u][i], v=par[v][i];
return par[u][0];
}
int dist(int u, int v)
{
int w=lca(u, v);
return dep[u]+dep[v]-dep[w]-dep[w];
}
int dp[MAXN+10][(1<<10)+10];
void dfs2(int now)
{
int i, j;
for(auto nxt : chd[now]) dfs2(nxt);
int sz=chd[now].size(), t;
//printf("NOW %d\n", now);
vector<int> val;
for(auto &e : edge[now])
{
t=e.w;
int u=e.u, v=e.u;
while(u!=now)
{
if(u!=v) t+=dp[u][(1<<num[v])];
else t+=dp[u][0];
v=u; u=par[u][0];
}
e.u=v;
u=e.v, v=e.v;
while(u!=now)
{
if(u!=v) t+=dp[u][(1<<num[v])];
else t+=dp[u][0];
v=u; u=par[u][0];
}
e.v=v;
val.push_back(t);
}
for(i=0; i<(1<<sz); i++)
{
for(j=0; j<sz; j++)
{
if(i&(1<<j)) continue;
dp[now][i]+=dp[chd[now][j]][0];
}
}
for(i=(1<<sz)-1; i>=0; i--)
{
for(j=0; j<edge[now].size(); j++)
{
if(edge[now][j].u!=now && (i&(1<<num[edge[now][j].u]))) continue;
if(edge[now][j].v!=now && (i&(1<<num[edge[now][j].v]))) continue;
dp[now][i]=max(dp[now][i], dp[now][i|(1<<num[edge[now][j].u])|(1<<num[edge[now][j].v])]+val[j]);
}
}
}
int main()
{
int i, j;
scanf("%d%d", &N, &M);
for(i=1; i<=M; i++)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
if(w==0) adj[u].push_back(v), adj[v].push_back(u);
else E.push_back({u, v, w});
sum+=w;
}
dfs(1, 1, 1);
for(i=1; i<=10; i++) for(j=1; j<=N; j++) par[j][i]=par[par[j][i-1]][i-1];
for(auto it : E) if(dist(it.u, it.v)%2==0) edge[lca(it.u, it.v)].push_back(it);
dfs2(1);
printf("%d", sum-dp[1][0]);
}
Compilation message
training.cpp: In function 'void dfs(int, int, int)':
training.cpp:29:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0; i<chd[now].size(); i++) num[chd[now][i]]=i;
~^~~~~~~~~~~~~~~~
training.cpp:20:12: warning: unused variable 'j' [-Wunused-variable]
int i, j;
^
training.cpp: In function 'int lca(int, int)':
training.cpp:34:9: warning: unused variable 'j' [-Wunused-variable]
int i, j;
^
training.cpp: In function 'void dfs2(int)':
training.cpp:94:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(j=0; j<edge[now].size(); j++)
~^~~~~~~~~~~~~~~~~
training.cpp: In function 'int main()':
training.cpp:108:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &N, &M);
~~~~~^~~~~~~~~~~~~~~~
training.cpp:112:11: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d", &u, &v, &w);
~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
632 KB |
Output is correct |
2 |
Correct |
2 ms |
636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
4600 KB |
Output is correct |
2 |
Correct |
10 ms |
4728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
548 KB |
Output is correct |
2 |
Incorrect |
2 ms |
636 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
824 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
1016 KB |
Output is correct |
2 |
Correct |
5 ms |
888 KB |
Output is correct |
3 |
Correct |
10 ms |
1656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
2296 KB |
Output is correct |
2 |
Correct |
15 ms |
1312 KB |
Output is correct |
3 |
Correct |
7 ms |
1528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
888 KB |
Output is correct |
2 |
Correct |
5 ms |
1656 KB |
Output is correct |
3 |
Correct |
26 ms |
4600 KB |
Output is correct |
4 |
Correct |
6 ms |
1912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
2168 KB |
Output is correct |
2 |
Correct |
21 ms |
4600 KB |
Output is correct |
3 |
Correct |
13 ms |
1784 KB |
Output is correct |
4 |
Correct |
14 ms |
1528 KB |
Output is correct |