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 "roads.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> ii;
typedef vector<ii> vi;
typedef pair<ii,ll> iii;
const int MAX_N=2e5+10;
bool vis[MAX_N];
int pa[MAX_N];
vector<vi> G;
int n;
void init(int n){
for(int i=0;i<n;i++)
pa[i]=i;
}
int findset(int x){
if(pa[x]==x) return x;
return pa[x]=findset(pa[x]);
}
bool issameset(int i,int j){
return findset(i)==findset(j);
}
void unionset(int i,int j){
if(!issameset(i,j)){
int u=findset(i),v=findset(j);
pa[u]=v;
}
}
ll dp[MAX_N];
int path[MAX_N];
int ti;
ll knap(int u,int p){
if(dp[u]!=-1) return dp[u];
ll ans=0;
for(auto &v:G[u]){
if(v.first!=p){
ans+=knap(v.first,u);
if(!issameset(v.first,u)) ans+=v.second;
}
}
ll s=ans;
int id=-1;
for(auto &v:G[u]){
if(v.first!=p && !issameset(v.first,u)){
ll rp=s;
rp=rp-dp[v.first]-v.second;
for(auto &z:G[v.first]){
if(z.first!=u && !issameset(z.first,v.first)){
rp+=dp[z.first];
if(!issameset(z.first,v.first))
rp+=z.second;
}
}
if(ans>rp){
ans=rp;
id=v.first;
}
}
}
path[u]=id;
return dp[u]=ans;
}
void dfs(int u,int p){
for(auto &v:G[u]){
if(v.first!=p && v.first!=path[u]) dfs(v.first,u);
}
if(path[u]!=-1){
unionset(u,path[u]);
for(auto &v:G[path[u]]){
if(v.first!=u){
dfs(v.first,path[u]);
}
}
}
}
std::vector<long long> minimum_closure_costs(int N, std::vector<int> U,
std::vector<int> V,
std::vector<int> W) {
n=N;
vector<ll> res;
vector<iii> ed;
G.resize(n+1);
ll s=0;
for(int i=0;i<n-1;i++){
int a=U[i],b=V[i],w=W[i];
G[a].push_back(ii(b,w));
G[b].push_back(ii(a,w));
ed.push_back(iii(ii(a,b),w));
s+=w;
}
res.push_back(s);
init(n);
for(int i=0;i<n-1;i++){
ti=i;
memset(dp,-1,sizeof dp);
memset(path,-1,sizeof path);
res.push_back(knap(0,0));
dfs(0,0);
}
return res;
}
# | 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... |