#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)-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)-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-Fdp[1] << "\n";
}
Compilation message
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)
~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Correct |
2 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
640 KB |
Output is correct |
2 |
Correct |
2 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
4608 KB |
Output is correct |
2 |
Correct |
10 ms |
4708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
512 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
512 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
640 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
896 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
1024 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
2176 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
896 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
8 ms |
2176 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |