bingxuan9112

Untitled

Jun 8th, 2019
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.73 KB | None | 0 0
  1. #pragma gcc Optimize("Ofast")
  2. #pragma loop_opt(on)
  3. #include <bits/stdc++.h>
  4. #define rep(i,n) for(int i=0;i<n;i++)
  5. #define debug(x) 1&&(cout<<#x<<':'<<x<<'\n')
  6.  
  7. using namespace std;
  8. typedef long long ll;
  9. const ll N = 1000;
  10.  
  11. struct Edge{
  12.     ll u,v,w;
  13.     Edge(ll u,ll v,ll w):u(u),v(v),w(w){}
  14.     friend bool operator<(Edge &a,Edge &b){return a.w<b.w;}
  15. };
  16. struct {
  17.     int par[N+1], rk[N+1];
  18.     void init(int n){memset(rk,0,sizeof rk), iota(par,par+n,0);}
  19.     int anc(int x){return (x==par[x]) ? x : (par[x]=anc(par[x]));}
  20.     bool same(int x,int y){return anc(x) == anc(y);}
  21.     void join(int x,int y){
  22.         if((x=anc(x)) == (y=anc(y))) return;
  23.         if(rk[x] < rk[y]) swap(x,y);
  24.         par[y] = x, rk[x]==rk[y]&&++rk[x];
  25.     }
  26. } dsu;
  27. ll pa[N+1][25] = {};
  28. ll pa_d[N+1][25] = {};
  29. vector<Edge> edges,p,q;
  30. vector<int> g[N+1] = {};
  31. ll d, dep[N+1] = {};
  32. void dfs(int u = 1, int fa = 0,ll w = 0){
  33.     pa[u][0] = fa, pa_d[u][0] = w, dep[u] = d;
  34.     for(auto id:g[u]){
  35.         int v = p[id].u^p[id].v^u;
  36.         if(v != fa) ++d, dfs(v,u,p[id].w), --d;
  37.     }
  38. }
  39. void LCA_init(int n){
  40.     for(int L = 1;L < 25;L++)
  41.         for(int i = 1;i <= n;i++){
  42.             pa[i][L] = pa[pa[i][L-1]][L-1];
  43.             pa_d[i][L] = max(pa_d[i][L-1], pa_d[pa[i][L-1]][L-1]);
  44.         }
  45. }
  46. ll query(ll x,ll y){
  47.     ll res = 0;
  48.     if(dep[x] != dep[y]){
  49.         if(dep[x] < dep[y]) swap(x,y);
  50.         ll dif = dep[x]-dep[y];
  51.         for(int L = 0;L < 25;L++) if((dif>>L)&1){
  52.             res = max(res,pa_d[x][L]);
  53.             x = pa[x][L];
  54.         }
  55.         assert(dep[x] == dep[y]);
  56.     }
  57.     if(x == y) return res;
  58.     for(int L = 24;L > 0;L--) if(pa[x][L]!=pa[y][L])
  59.         res = max({res,pa_d[x][L],pa_d[y][L]}),
  60.         x = pa[x][L], y = pa[y][L];
  61.     if(x != y)
  62.         //assert(pa[x][0] == pa[y][0]),
  63.         res = max({res,pa_d[x][0],pa_d[y][0]});
  64.     return res;
  65. }
  66. signed main(){
  67.     ll V,E;
  68.     scanf("%lld%lld",&V,&E);
  69.     rep(_,E){
  70.         ll u,v,w;
  71.         scanf("%lld%lld%lld",&u,&v,&w);
  72.         edges.emplace_back(u,v,w);
  73.     }
  74.  
  75.     ll mst = 0,smst = 1e18;
  76.     sort(edges.begin(),edges.end());
  77.     dsu.init(V);
  78.     for(auto &e:edges){
  79.         if(!dsu.same(e.u,e.v)){
  80.             dsu.join(e.u,e.v);
  81.             mst += e.w;
  82.             g[e.u].emplace_back(p.size());
  83.             g[e.v].emplace_back(p.size());
  84.             p.emplace_back(e);
  85.         }else q.emplace_back(e);
  86.     }
  87.     if(p.size() != V-1) return puts("-1 -1"),0;
  88.     if(q.size() == 0) return printf("%lld -1\n",mst),0;
  89.     dfs();
  90.     LCA_init(V);
  91.     smst = 1e18;
  92.     for(auto &e:q){
  93.         ll dif = e.w - query(e.u,e.v);
  94.         //assert(dif >= 0);
  95.         if(smst > mst+dif) smst = mst+dif;
  96.     }
  97.     printf("%lld %lld\n",mst,smst);
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment