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;
#define ff first
#define ss second
int N, M;
struct ed{int a,b,c;};
vector<int> edge[1001], D[1001];
vector<ed> A[1001], E;
int dp[1001][1024], nth[1001], prt[10][1001], deg[1001], dth[1001];
void dfs(int x){
D[dth[x]].push_back(x);
for(auto &i: edge[x]){
if(prt[0][x]==i) continue;
prt[0][i] = x;
dth[i] = dth[x]+1;
dfs(i);
nth[i] = deg[x];
++deg[x];
}
}
int main(){
ios_base::sync_with_stdio(0), cin.tie(0);
cin>>N>>M;
int ans = 0;
for(int i=0; i<M; ++i){
int x,y,z; cin>>x>>y>>z;
if(z) E.push_back({z, x, y}), ans += z;
else edge[x].push_back(y), edge[y].push_back(x);
}
dth[1] = 1;
dfs(1);
for(int i=1; i<10; ++i) for(int j=1; j<=N; ++j) prt[i][j] = prt[i-1][prt[i-1][j]];
for(auto &i: E){
if((dth[i.b]+dth[i.c]) & 1) continue;
if(dth[i.b]<dth[i.c]) swap(i.b, i.c);
int x, y; tie(x, y) = tie(i.b, i.c);
for(int i=9; i>=0; --i) if(dth[prt[i][x]] >= dth[y]) x = prt[i][x];
if(x==y){
A[x].push_back(i);
continue;
}
for(int i=9; i>=0; --i) if(prt[i][x] != prt[i][y]) x = prt[i][x], y = prt[i][y];
A[prt[0][x]].push_back(i);
}
for(int d=1000; d; --d){
for(auto &x: D[d]){
for(int msk=1; msk<(1<<deg[x]); ++msk){
for(auto &i: edge[x]) if(prt[0][i]==x && ((msk>>nth[i]) & 1)) dp[x][msk] = max(dp[x][msk], dp[i][(1<<deg[i])-1]+dp[x][msk ^ (1<<nth[i])]);
for(auto &i: A[x]){
int p = i.b, q = i.c;
for(int i=9; i>=0; --i) if(dth[prt[i][p]]>dth[x]) p = prt[i][p];
for(int i=9; i>=0; --i) if(dth[prt[i][q]]>dth[x]) q = prt[i][q];
if(!(1 & (msk>>nth[p]) & (q!=x?(msk>>nth[q]):1))) continue;
int m = msk ^ ((1<<nth[p]) | (q!=x?(1<<nth[q]):0));
if(!m){
int t = i.a + dp[i.b][(1<<deg[i.b])-1];
for(int v=i.b; prt[0][v]!=x; v=prt[0][v]) t += dp[prt[0][v]][((1<<deg[prt[0][v]])-1) ^ (1<<nth[v])];
if(i.c != x){
t += dp[i.c][(1<<deg[i.c])-1];
for(int v=i.c; prt[0][v]!=x; v=prt[0][v]) t += dp[prt[0][v]][((1<<deg[prt[0][v]])-1) ^ (1<<nth[v])];
}
dp[x][msk] = max(dp[x][msk], t);
}
else dp[x][msk] = max(dp[x][msk], dp[x][m]+dp[x][msk ^ m]);
}
}
}
}
cout<<ans-dp[1][(1<<deg[1])-1];
return 0;
}
# | 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... |