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;
typedef long long ll;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n,m;
cin >> n >> m;
vector<array<int,3>> unpaved_edges;
vector<int> v[n+1];
for(int i=1;i<=m;i++)
{
int a,b,c;
cin >> a >> b >> c;
if(c==0)
{
v[a].push_back(b);
v[b].push_back(a);
}
else unpaved_edges.push_back({a,b,c});
}
vector<int> depth(n+1,0);
vector<int> p(n+1,0);
vector<int> ch[n+1];
vector<int> ch_id(n+1,-1);
function<void(int)> dfs=[&](int a)
{
depth[a]=depth[p[a]]+1;
for(int to:v[a])
{
if(to==p[a]) continue;
ch_id[to]=ch[a].size();
ch[a].push_back(to);
p[to]=a;
dfs(to);
}
};
dfs(1);
int req=0;
int cost_sum=0;
vector<array<int,2>> edges={{0,0}};
vector<int> cost={0};
vector<array<int,3>> opt[n+1]; //child[ren],id
vector<int> up_going[n+1];
for(auto [a,b,c]:unpaved_edges)
{
if((depth[a]-depth[b])&1) req+=c;
else
{
int id=edges.size();
edges.push_back({a,b});
cost.push_back(c);
cost_sum+=c;
int x=a,y=b;
if(depth[x]>depth[y])
{
swap(a,b);
swap(x,y);
}
int prv=0;
while(depth[x]!=depth[y])
{
prv=y;
y=p[y];
}
if(x!=y) //nice lca
{
while(p[x]!=p[y])
{
x=p[x];
y=p[y];
}
up_going[a].push_back(id);
up_going[b].push_back(id);
opt[p[x]].push_back({x,y,id});
}
else //vertical path
{
up_going[b].push_back(id);
opt[a].push_back({prv,0,id});
}
}
}
int e=(int)edges.size()-1;
const int inf=(1<<29);
vector dp(n+1,vector(e+1,int(-inf)));
auto chmax=[&](int &a,int b){a=max(a,b);};
vector<int> dp_closed(n+1,0);
function<void(int)> solve=[&](int a)
{
for(int to:ch[a]) solve(to);
int cnt=ch[a].size();
vector<int> best((1<<cnt),0);
for(int mask=0;mask<(1<<cnt);mask++)
{
for(auto [x,y,id]:opt[a])
{
int submask=(1<<ch_id[x]);
if(y!=0) submask^=(1<<ch_id[y]);
if((submask&mask)==submask) chmax(best[mask],best[mask^submask]+dp[x][id]+(y!=0?dp[y][id]:0)+cost[id]);
}
for(int i=0;i<cnt;i++) if(mask&(1<<i)) chmax(best[mask],best[mask^(1<<i)]+dp_closed[ch[a][i]]);
}
dp[a][0]=best[(1<<cnt)-1];
for(int i=0;i<cnt;i++)
{
int to=ch[a][i];
for(int j=1;j<=e;j++) chmax(dp[a][j],best[((1<<cnt)-1)^(1<<i)]+dp[to][j]);
}
for(int id:up_going[a]) chmax(dp[a][id],best[(1<<cnt)-1]);
for(int i=0;i<=e;i++) chmax(dp_closed[a],dp[a][i]);
};
solve(1);
cout << req+cost_sum-dp_closed[1] << "\n";
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... |