1 solutions
-
0
/* * __----~~~~~~~~~~~------___ * . . ~~//====...... __--~ ~~ * -. \_|// |||\\ ~~~~~~::::... /~ * ___-==_ _-~o~ \/ ||| \\ _/~~- * __---~~~.==~||\=_ -_--~/_-~|- |\\ \\ _/~ * _-~~ .=~ | \\-_ '-~7 /- / || \ / * .~ .~ | \\ -_ / /- / || \ / * / ____ / | \\ ~-_/ /|- _/ .|| \ / * |~~ ~~|--~~~~--_ \ ~==-/ | \~--===~~ .\ * ' ~-| /| |-~\~~ __--~~ * |-~~-_/ | | ~\_ _-~ /\ * / \ \__ \/~ \__ * _--~ _/ | .-~~____--~-/ ~~==. * ((->/~ '.|||' -_| ~~-/ , . _|| * -_ ~\ ~~---l__i__i__i--~~_/ * _-~-__ ~) \--______________--~~ * //.-~~~-~_--~- |-------~~~~~~~~ * //.-~~~--\ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * 神兽保佑 永无BUG */ /* * @Description: I want to be the weakest vegetable in the world! * @Author: I.Z. */ #include<bits/stdc++.h> using namespace std; #define MOD 998244353 #define speMOD 2933256077ll #define int long long #define pii pair<int,int> #define all(v) v.begin(),v.end() #define pb push_back #define REP(i,b,e) for(int i=(b);i<(int)(e);++i) #define over(x) {cout<<(x)<<endl;return;} #define lowbit(x) ((x)&(-(x))) #define cntbit(x) __builtin_popcount(x) #define rf(v) shuffle(all(v),sd); #define deal(v) sort(all(v));v.erase(unique(v.begin(),v.end()),v.end()) #define lbound(v,x) lower_bound(all(v),x)-v.begin() mt19937 sd(random_device{}()); int qpow(int a,int b,int m=MOD,int res=1){ a%=m; while(b>0)res=(b&1)?(res*a%m):(res),a=a*a%m,b>>=1; return res; } string YN(bool x,string Y="Yes",string N="No") { if(x){ return Y; } else { return N; } } int exgcd(int x,int y,int &a,int &b){ if(y==0){ a=1;b=0; return x; } int d=exgcd(y,x%y,a,b); int z=b; b=a-b*(x/y); a=z; return d; } int _x_,_y_; inline int inverse(int x,int y=MOD){ exgcd(x,y,_x_,_y_); return (_x_+y)%y; } int fac[2000005],inv[2000005]; void init(int n){ fac[0]=inv[0]=1; REP(i,1,n+1)fac[i]=fac[i-1]*i%MOD; inv[n]=qpow(fac[n],MOD-2); for(int i=n-1;i>=1;--i)inv[i]=inv[i+1]*(i+1)%MOD; } int binom(int x,int y){ return x<y||y<0? 0:fac[x]*inv[y]%MOD*inv[x-y]%MOD; } int n,m; vector<pii>v[200005]; vector<int>g[200005],s[200005]; int tp[200005],dep[200005],fa[200005],dfn[200005],sz[200005],ms[200005]; void dfs(int x,int pre,int d){ dep[x]=d;fa[x]=pre;sz[x]=1;ms[x]=-1;int mx=0; for(auto i:g[x])if(i!=pre){ dfs(i,x,d+1); sz[x]+=sz[i]; if(mx<sz[i])mx=sz[i],ms[x]=i; } } int tot; bool cmp(int x,int y){return dfn[x]<dfn[y];} void dfs2(int x,int pre,int t){ tp[x]=t;inv[tot]=x;dfn[x]=tot++; if(ms[x]!=-1)dfs2(ms[x],x,t); for(auto i:g[x])if(i!=pre&&i!=ms[x])dfs2(i,x,i); } int findfa(int x){return fa[x]==x? x:fa[x]=findfa(fa[x]);} int getlca(int x,int y){ while(tp[x]!=tp[y]){ if(dep[tp[x]]>dep[tp[y]])x=fa[tp[x]]; else y=fa[tp[y]]; } return dep[x]>dep[y]? y:x; } int dp[100005][2],spe[100005]; int st[100005][20]; int val[100005]; int query(int l,int r){ int s=__lg(r-l+1); return max(st[l][s],st[r-(1<<s)+1][s]); } int qch(int x,int y){ int res=0; // cout<<x<<' '<<y<<' '; while(tp[x]!=tp[y]){ res=max(res,query(dfn[tp[y]],dfn[y])); y=fa[tp[y]]; } if(x!=y)res=max(res,query(dfn[x]+1,dfn[y])); // cout<<res<<endl; return res; } void dfs3(int x){ for(auto i:s[x])dfs3(i); if(!s[x].size())return dp[x][spe[x]]=0,dp[x][spe[x]^1]=-1e18,void(); if(spe[x]){ dp[x][1]=0; for(auto i:s[x])dp[x][1]+=max(dp[i][1]+val[i],dp[i][0]); dp[x][0]=-1e18; }else{ dp[x][0]=0; for(auto i:s[x])dp[x][0]+=max(dp[i][1]+val[i],dp[i][0]); dp[x][1]=dp[x][0];int mx=-1e18; for(auto i:s[x])mx=max(mx,dp[i][1]-max(dp[i][1]+val[i],dp[i][0])); dp[x][1]+=mx; } } void Main() { cin>>n>>m; vector<pair<int,pii>>e; REP(i,0,m){ int x,y,z; cin>>x>>y>>z; --x,--y; v[x].pb({y,z});v[y].pb({x,z}); e.pb({z,{x,y}}); } sort(all(e)); REP(i,0,n)fa[i]=i; map<pii,int>mp; int ans=0; for(auto [_,i]:e){ int x=i.first,y=i.second; if(findfa(x)!=findfa(y)){ fa[fa[x]]=fa[y]; g[x].pb(y);g[y].pb(x); ans+=_;mp[{x,y}]=mp[{y,x}]=_; } } dfs(0,-1,0);dfs2(0,-1,0); REP(i,1,n)st[i][0]=mp[{inv[i],fa[inv[i]]}]; REP(j,0,__lg(n-1)){ REP(i,1,n-(1<<(j+1))+1)st[i][j+1]=max(st[i][j],st[i+(1<<j)][j]); } REP(i,0,n){ vector<int>t; int res=ans,S; t.pb(dfn[i]);for(auto j:v[i])t.pb(dfn[j.first]),res+=j.second; deal(t); S=t.size(); REP(j,1,S)t.pb(dfn[getlca(inv[t[j]],inv[t[j-1]])]); deal(t); REP(j,0,t.size())t[j]=inv[t[j]]; for(auto j:t)s[j].clear(),dp[j][0]=dp[j][1]=0,spe[j]=0; spe[i]=1; for(auto j:v[i])spe[j.first]=1; int lca=-1; REP(j,1,t.size())s[lca=getlca(t[j],t[j-1])].pb(t[j]),val[t[j]]=qch(lca,t[j]); dfs3(t[0]); cout<<res-dp[t[0]][1]<<endl; } } void TC() { int tc=1; while(tc--){ Main(); cout.flush(); } } signed main() { freopen("b.in","r",stdin); freopen("b.out","w",stdout); return cin.tie(0),cout.tie(0),ios::sync_with_stdio(0),TC(),0; } /* 1. CLEAR the arrays (ESPECIALLY multitests) 2. DELETE useless output */
- 1
Information
- ID
- 2937
- Time
- 4000ms
- Memory
- 1024MiB
- Difficulty
- 10
- Tags
- (None)
- # Submissions
- 1
- Accepted
- 1
- Uploaded By