結果

問題 No.922 東北きりきざむたん
ユーザー 👑 tute7627tute7627
提出日時 2019-11-08 22:20:31
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 233 ms / 2,000 ms
コード長 5,131 bytes
コンパイル時間 2,238 ms
コンパイル使用メモリ 188,300 KB
実行使用メモリ 51,104 KB
最終ジャッジ日時 2023-10-13 03:58:54
合計ジャッジ時間 6,680 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,348 KB
testcase_01 AC 3 ms
4,348 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 2 ms
4,352 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 3 ms
4,348 KB
testcase_06 AC 3 ms
4,420 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 64 ms
26,800 KB
testcase_10 AC 31 ms
10,792 KB
testcase_11 AC 54 ms
21,724 KB
testcase_12 AC 43 ms
27,640 KB
testcase_13 AC 16 ms
10,208 KB
testcase_14 AC 102 ms
39,472 KB
testcase_15 AC 40 ms
29,140 KB
testcase_16 AC 173 ms
44,400 KB
testcase_17 AC 177 ms
44,468 KB
testcase_18 AC 170 ms
44,304 KB
testcase_19 AC 174 ms
44,448 KB
testcase_20 AC 175 ms
44,384 KB
testcase_21 AC 197 ms
44,204 KB
testcase_22 AC 197 ms
44,236 KB
testcase_23 AC 216 ms
45,944 KB
testcase_24 AC 233 ms
46,120 KB
testcase_25 AC 187 ms
48,076 KB
testcase_26 AC 186 ms
48,016 KB
testcase_27 AC 182 ms
48,148 KB
testcase_28 AC 68 ms
34,420 KB
testcase_29 AC 217 ms
51,104 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

#define endl '\n'
#define ALL(a)  (a).begin(),(a).end()
#define ALLR(a)  (a).rbegin(),(a).rend()
#define spa << " " <<
#define test cout<<"test"<<endl;
#define fi first
#define se second
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define rep(i,n,m) for(ll i = n; i < (ll)(m); i++)
#define rrep(i,n,m) for(ll i = m - 1; i >= (ll)(n); i--)
using ll = long long;
using ld = long double;
const ll MOD = 1e9+7;
//const ll MOD = 998244353;
const ll INF = 1e18;
using P = pair<ll, ll>;
template<typename T>
void chmin(T &a,T b){if(a>b)a=b;}
template<typename T>
void chmax(T &a,T b){if(a<b)a=b;}
void pmod(ll &a,ll b){a=(a+b)%MOD;}
void pmod(ll &a,ll b,ll c){a=(b+c)%MOD;}
void qmod(ll &a,ll b){a=(a*b)%MOD;}
void qmod(ll &a,ll b,ll c){a=(b*c)%MOD;}
ll median(ll a,ll b, ll c){return a+b+c-max({a,b,c})-min({a,b,c});}
void ans1(bool x){if(x) cout<<"Yes"<<endl;else cout<<"No"<<endl;}
void ans2(bool x){if(x) cout<<"YES"<<endl;else cout<<"NO"<<endl;}
void ans3(bool x){if(x) cout<<"Yay!"<<endl;else cout<<":("<<endl;}
template<typename T1,typename T2>
void ans(bool x,T1 y,T2 z){if(x)cout<<y<<endl;else cout<<z<<endl;}  
template<typename T>
void debug(vector<vector<T>>&v,ll h,ll w){for(ll i=0;i<h;i++)
{cout<<v[i][0];for(ll j=1;j<w;j++)cout spa v[i][j];cout<<endl;}};
void debug(vector<string>&v,ll h,ll w){for(ll i=0;i<h;i++)
{for(ll j=0;j<w;j++)cout<<v[i][j];cout<<endl;}};
template<typename T>
void debug(vector<T>&v,ll n){if(n!=0)cout<<v[0];
for(ll i=1;i<n;i++)cout spa v[i];cout<<endl;};
template<typename T>
vector<vector<T>>vec(ll x, ll y, T w){
  vector<vector<T>>v(x,vector<T>(y,w));return v;}
ll gcd(ll x,ll y){ll r;while(y!=0&&(r=x%y)!=0){x=y;y=r;}return y==0?x:y;}
vector<ll>dx={1,0,-1,0,1,1,-1,-1};
vector<ll>dy={0,1,0,-1,1,-1,1,-1};
template<typename T>
vector<T> make_v(size_t a,T b){return vector<T>(a,b);}
template<typename... Ts>
auto make_v(size_t a,Ts... ts){
  return vector<decltype(make_v(ts...))>(a,make_v(ts...));
}

vector<ll>val(110000);
struct LCA{
  ll datasize = 21;
  ll n,root;//rootを根とする頂点数nの根付き木
  vector<ll>depth;//0から
  vector<vector<ll>>G,data;
  vector<ll>group;
  vector<P>dp;
  ll minbuf,minsum;
  LCA(vector<vector<ll>>g,ll r):G(g),n(g.size()),root(r){
    data.assign(n,vector<ll>(datasize,-1));
    depth.assign(n,-1LL);
    group.assign(n,-1LL);
    dp.assign(n,MP(0LL,0LL));
    minsum=0;
    build();
  }
  void build(){
    ll cnt=0;
    rep(i,0,n){
      if(depth[i]==-1){
        minbuf=INF;
        dfs(i,0,cnt);
        cnt++;
        dfsmin(i,0,MP(dp[i].fi,dp[i].fi+dp[i].se));
        minsum+=minbuf;
        //cout<<i spa minbuf<<endl;
      }
    }
    for(ll i=1;i<datasize;i++){
      for(ll j=0;j<n;j++){
        if(data[j][i-1]==-1)data[j][i]=-1;
        else data[j][i]=data[data[j][i-1]][i-1];
      }
    }
  }
  pair<ll,ll> dfs(ll k,ll d, ll x){
    depth[k]=d;
    group[k]=x;
    ll num=0;
    ll sumbuf=0;
    for(ll i=0;i<G[k].size();i++){
      if(depth[G[k][i]]==-1){
        data[G[k][i]][0]=k;
        auto p=dfs(G[k][i],d+1,x);
        num+=p.fi,sumbuf+=p.se;
      }
    }
    dp[k]=MP(num+val[k],sumbuf+num);
    return MP(num+val[k],sumbuf+num);
  }
  void dfsmin(ll k,ll d,P par){
    auto tmp=par;
    tmp.fi-=dp[k].fi;
    tmp.se-=dp[k].se+dp[k].fi;
    tmp.se+=tmp.fi;
    chmin(minbuf,tmp.se+dp[k].se);
    //cout<<k spa tmp.fi spa tmp.se spa par.fi spa par.se<<endl;
    //cout<<dp[k].fi spa dp[k].se spa minbuf<<endl;
    rep(i,0,G[k].size()){
      if(depth[G[k][i]]==d+1){
        dfsmin(G[k][i],d+1,MP(dp[k].fi+tmp.fi,dp[k].se+tmp.se));
      }
    }
  }
  ll deep(ll x){
    return x>=0?depth[x]:-1;
  }
  ll query(ll x,ll y){
    if(depth[x]<depth[y])swap(x,y);
    ll tmp = datasize - 1;
    while(depth[x]!=depth[y]){
      while(tmp>=1&&depth[y]>=deep(data[x][tmp]))tmp--;
      x = data[x][tmp];
    }
    tmp = datasize - 1;
    while(x!=y){
      while(tmp>=1&&data[x][tmp]==data[y][tmp])tmp--;
      x = data[x][tmp];
      y = data[y][tmp];
    }
    return x;
  }
};

struct UnionFind {
  vector<ll> data;
  UnionFind(ll size) : data(size, -1) { }
  bool unite(ll x, ll y) {
    x = root(x); y = root(y);
    if (x != y) {
      if (data[y] < data[x]) swap(x, y);
      data[x] += data[y]; data[y] = x;
    }
    return x != y;
  }
  bool find(ll x, ll y) {
    return root(x) == root(y);
  }
  ll root(ll x) {
    return data[x] < 0 ? x : data[x] = root(data[x]);
  }
  ll size(ll x) {
    return -data[root(x)];
  }
};
int main(){
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  //cout<<fixed<<setprecision(10);
  ll res=0,buf=0;
  bool judge = true;
  ll n,m,q;cin>>n>>m>>q;
  vector<vector<ll>>g(n);
  UnionFind uf(n);
  rep(i,0,m){
    ll u,v;cin>>u>>v;
    u--;v--;
    g[u].PB(v);
    g[v].PB(u);
    uf.unite(u,v);
  }
  vector<P>p;
  rep(i,0,q){
    ll a,b;cin>>a>>b;
    a--;b--;
    if(uf.find(a,b))p.EB(a,b);
    else val[a]++,val[b]++;
  }
  LCA lca(g,0);
  rep(i,0,p.size()){
    lca.minsum+=lca.deep(p[i].fi)+lca.deep(p[i].se)
      -2*lca.deep(lca.query(p[i].fi,p[i].se));
  }
  cout<<lca.minsum<<endl;
  return 0;
}
0