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 jizz ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define pb push_back
#define ET cout << "\n"
#define MEM(i,j) memset(i,j,sizeof i)
#define F first
#define S second
#define MP make_pair
#define ALL(v) v.begin(),v.end()
#define DB(a,s,e) {for(int i=s;i<e;++i) cout << a[i] << " ";ET;}
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int INF=1e9;
vector<int> G[1005],rG[1005];
int dfn[1005],out[1005],dft,dp[1005][1024],dpp[10][1005],Fdp[1005],pa[1005];
struct mode
{
int u,v,w;
bool operator<(const mode &a)const{
return dfn[v]<dfn[a.v];
}
};
vector<mode> edge,CM[1005];
void dfs(int u,int f)
{
dfn[u]=++dft,pa[u]=f;
for(int i:G[u])
if(i!=f)
dfs(i,u),rG[u].pb(i);
out[u]=dft,vector<int>().swap(G[u]);
}
bool yee(int a,int b)
{
return dfn[a]<dfn[b];
}
bool ancestor(int u,int v)
{
return dfn[u]<=dfn[v]&&out[u]>=out[v];
}
void dfs2(int u,int f)
{
for(int i:rG[u])
dfs2(i,u);
sort(ALL(CM[u]));
int nw=0;
for(mode i:CM[u])
{
for(;nw+1<rG[u].size()&&dfn[i.v]>=dfn[rG[u][nw+1]];++nw)
for(int j=(1<<nw)-1;j>=0;--j)
dp[u][j|(1<<nw)]=max(dp[u][j|(1<<nw)],dp[u][j]+Fdp[rG[u][nw]]);
int tmp=Fdp[i.v];
if(i.u!=u) tmp+=Fdp[i.u];
for(int j=pa[i.u],ls=i.u;!ancestor(j,i.v);ls=j,j=pa[j])
tmp+=dpp[lower_bound(ALL(rG[j]),ls,yee)-rG[j].begin()][j];
for(int j=pa[i.v],ls=i.v;!ancestor(j,i.u);ls=j,j=pa[j])
tmp+=dpp[lower_bound(ALL(rG[j]),ls,yee)-rG[j].begin()][j];
if(i.u==u)
for(int j=(1<<nw)-1;j>=0;--j)
dp[u][j|(1<<nw)]=max(dp[u][j|(1<<nw)],dp[u][j]+i.w+tmp);
else
{
int lf=0;
while(lf+1<rG[u].size()&&dfn[i.u]>=dfn[rG[u][lf+1]]) ++lf;
for(int j=(1<<nw)-1;j>=0;--j)
if(j&(1<<lf))
dp[u][j|(1<<nw)]=max(dp[u][j|(1<<nw)],dp[u][j^(1<<lf)]+i.w+tmp);
}
}
for(;nw<rG[u].size();++nw)
for(int j=(1<<nw)-1;j>=0;--j)
dp[u][j|(1<<nw)]=max(dp[u][j|(1<<nw)],dp[u][j]+Fdp[rG[u][nw]]);
for(int i=(1<<rG[u].size())-1;i>0;--i)
for(int t=(Fdp[u]=max(Fdp[u],dp[u][i]),0);t<rG[u].size();++t)
if((i&(1<<t))==0)
dpp[t][u]=max(dpp[t][u],dp[u][i]);
}
int main()
{jizz
srand(time(NULL));
int n,m,u,v,w,ans=0,sum=0;
cin >> n >> m;
while(m--)
if(cin >> u >> v >> w,w)
edge.pb(mode{u,v,w});
else
G[u].pb(v),G[v].pb(u);
dfs(1,1);
for(auto &i:edge)
{
if(dfn[i.u]>dfn[i.v]) swap(i.u,i.v);
int cnt=0,lca=i.u;
for(int j=i.u;!ancestor(j,i.v);j=pa[j],lca=j)
++cnt;
for(int j=i.v;!ancestor(j,i.u);j=pa[j])
++cnt;
if(cnt&1) ans+=i.w;
else
CM[lca].pb(i),sum+=i.w;
}
dfs2(1,1);
cout << ans+sum-*max_element(Fdp+1,Fdp+n+1) << "\n";
}
Compilation message (stderr)
training.cpp: In function 'void dfs2(int, int)':
training.cpp:57:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(;nw+1<rG[u].size()&&dfn[i.v]>=dfn[rG[u][nw+1]];++nw)
~~~~^~~~~~~~~~~~~
training.cpp:72:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(lf+1<rG[u].size()&&dfn[i.u]>=dfn[rG[u][lf+1]]) ++lf;
~~~~^~~~~~~~~~~~~
training.cpp:78:9: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(;nw<rG[u].size();++nw)
~~^~~~~~~~~~~~~
training.cpp:82:46: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int t=(Fdp[u]=max(Fdp[u],dp[u][i]),0);t<rG[u].size();++t)
~^~~~~~~~~~~~~
# | 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... |