結果

問題 No.922 東北きりきざむたん
ユーザー snow39snow39
提出日時 2019-11-08 23:17:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,883 bytes
コンパイル時間 1,118 ms
コンパイル使用メモリ 97,780 KB
実行使用メモリ 25,672 KB
最終ジャッジ日時 2023-10-13 04:55:36
合計ジャッジ時間 5,634 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
13,792 KB
testcase_01 AC 4 ms
13,900 KB
testcase_02 AC 4 ms
14,124 KB
testcase_03 AC 4 ms
13,848 KB
testcase_04 AC 4 ms
13,968 KB
testcase_05 AC 4 ms
13,864 KB
testcase_06 AC 4 ms
13,848 KB
testcase_07 AC 5 ms
13,920 KB
testcase_08 AC 5 ms
13,932 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 18 ms
15,756 KB
testcase_13 AC 19 ms
14,680 KB
testcase_14 WA -
testcase_15 AC 9 ms
15,680 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 61 ms
16,424 KB
testcase_29 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;

int N,M,Q;
vector<int> U,V;
vector<int> A,B;

vector<int> g[100010];
ll num[100010];
ll child[100010];
int used[100010];

#define MAX_LOG_V 20
#define MAX_V 100010

int parent[MAX_LOG_V][MAX_V];  //親を2^k回辿って到達する頂点(根を通り過ぎる場合は-1)
int depth[MAX_V];  //根からの深さ

int root;

void dfs(int v,int p,int d){
  parent[0][v]=p;
  depth[v]=d;
  used[v]=root;
  for(int i=0;i<g[v].size();i++){
    if(g[v][i]!=p) dfs(g[v][i],v,d+1);
  }
}

//初期化
void init(int V){
  //parent[0]とdepthを初期化する
  //dfs(root,-1,0);
  //parentを初期化する
  for(int k=0;k+1<MAX_LOG_V;k++){
    for(int v=0;v<V;v++){
      if(parent[k][v]<0) parent[k+1][v]=-1;
      else parent[k+1][v]=parent[k][parent[k][v]];
    }
  }
}

//uとvのLCAを求める
int lca(int u,int v){
  //uとvの深さが同じになるまで親を辿る
  if(depth[u]>depth[v]) swap(u,v);
  for(int k=0;k<MAX_LOG_V;k++){
    if((depth[v]-depth[u])>>k&1){
      v=parent[k][v];
    }
  }
  if(u==v) return u;
  //二分探索でLCAを求める
  for(int k=MAX_LOG_V-1;k>=0;k--){
    if(parent[k][u]!=parent[k][v]){
      u=parent[k][u];
      v=parent[k][v];
    }
  }
  return parent[0][u];
}

void setChild(int now,int par){
  child[now]=0;
  for(auto nex:g[now]){
    if(par==nex) continue;
    setChild(nex,now);
    child[now]+=child[nex];
  }
  child[now]+=num[now];
}

ll eval(int now,int par){
  ll res=0;
  for(auto nex:g[now]){
    if(nex==par) continue;
    ll p=eval(nex,now);
    res+=p+child[nex];
  }
  return res;
}

ll global;
int tp;

void rfs(int now,int par,ll val){
  global=min(global,val);
  for(auto nex:g[now]){
    if(nex==par) continue;
    ll nexVal=val-child[nex]+(child[tp]-child[nex]);
    rfs(nex,now,nexVal);
  }
}

const ll INF=1e18;

int main(){
  cin>>N>>M>>Q;
  U.resize(M);
  V.resize(M);
  rep(i,M) cin>>U[i]>>V[i];
  A.resize(Q);
  B.resize(Q);
  rep(i,Q) cin>>A[i]>>B[i];

  rep(i,M){
    U[i]--;V[i]--;
    g[U[i]].push_back(V[i]);
    g[V[i]].push_back(U[i]);
  }
  rep(i,N){
    child[i]=-1;
    used[i]=-1;
  }
  rep(i,N){
    if(used[i]!=-1) continue;
    root=i;
    dfs(i,-1,0);
  }
  init(N);

  ll ans=0;
  rep(i,Q){
    A[i]--;
    B[i]--;
    if(used[A[i]]==used[B[i]]){
      ans+=depth[A[i]]+depth[B[i]]-depth[lca(A[i],B[i])];
      continue;
    }
    num[A[i]]++;
    num[B[i]]++;
  }

  rep(i,N){
    if(used[i]!=i) continue;
    setChild(i,-1);
    ll res=eval(i,-1);

    tp=i;
    global=res;
    rfs(i,-1,res);
    ans+=global;
  }

  cout<<ans<<endl;
  

  return 0;
}
0