結果

問題 No.922 東北きりきざむたん
ユーザー kzyKTkzyKT
提出日時 2019-11-08 23:00:22
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 166 ms / 2,000 ms
コード長 3,366 bytes
コンパイル時間 1,510 ms
コンパイル使用メモリ 150,588 KB
実行使用メモリ 38,232 KB
最終ジャッジ日時 2023-10-13 04:36:54
合計ジャッジ時間 4,949 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
25,304 KB
testcase_01 AC 10 ms
25,440 KB
testcase_02 AC 9 ms
25,436 KB
testcase_03 AC 10 ms
25,548 KB
testcase_04 AC 10 ms
25,392 KB
testcase_05 AC 10 ms
25,280 KB
testcase_06 AC 10 ms
25,480 KB
testcase_07 AC 9 ms
25,376 KB
testcase_08 AC 10 ms
25,468 KB
testcase_09 AC 44 ms
27,156 KB
testcase_10 AC 31 ms
26,240 KB
testcase_11 AC 42 ms
26,876 KB
testcase_12 AC 24 ms
25,700 KB
testcase_13 AC 18 ms
25,704 KB
testcase_14 AC 67 ms
27,668 KB
testcase_15 AC 20 ms
25,536 KB
testcase_16 AC 121 ms
30,160 KB
testcase_17 AC 121 ms
30,056 KB
testcase_18 AC 121 ms
30,068 KB
testcase_19 AC 120 ms
30,084 KB
testcase_20 AC 121 ms
30,084 KB
testcase_21 AC 147 ms
31,104 KB
testcase_22 AC 154 ms
31,096 KB
testcase_23 AC 158 ms
32,556 KB
testcase_24 AC 166 ms
32,508 KB
testcase_25 AC 118 ms
32,212 KB
testcase_26 AC 117 ms
32,472 KB
testcase_27 AC 115 ms
32,288 KB
testcase_28 AC 40 ms
25,460 KB
testcase_29 AC 143 ms
38,232 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define R cin>>
#define Z class
#define ll long long
#define ln cout<<'\n'
#define in(a) insert(a)
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\n",a)
#define mem(a) memset(a,0,sizeof(a))
#define all(c) (c).begin(),(c).end()
#define iter(c) __typeof((c).begin())
#define rrep(i,n) for(ll i=(ll)(n)-1;i>=0;i--)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define rep(i,n) REP(i,0,n)
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
template<Z A>void pr(A a){cout<<a;ln;}
template<Z A,Z B>void pr(A a,B b){cout<<a<<' ';pr(b);}
template<Z A,Z B,Z C>void pr(A a,B b,C c){cout<<a<<' ';pr(b,c);}
template<Z A,Z B,Z C,Z D>void pr(A a,B b,C c,D d){cout<<a<<' ';pr(b,c,d);}
template<Z A>void PR(A a,ll n){rep(i,n){if(i)cout<<' ';cout<<a[i];}ln;}
ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}
const ll MAX=1e9+7,MAXL=1LL<<61,dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
typedef pair<ll,ll> P;

ll e[200001],r[200001];
void init(){rep(i,200001)e[i]=i,r[i]=0;}
int find(int x){return (e[x]==x)?x:(e[x]=find(e[x]));}
void unite(int x,int y) {
  x=find(x),y=find(y);
  if(x==y)return;
  if(r[x]<r[y])e[x]=y;
  else{e[y]=x;if(r[x]==r[y])r[x]++;}
}
bool same(int x,int y){return find(x)==find(y);}

vector<P> v[100001];
ll c[100001],nn;

int solve(int i,int p) {
  if(v[i].size()==1&&v[i][0].F==p) {
    v[i][0].S=nn-c[i];
    return c[i];
  }
  int q=0,k=-1;
  for(int j=0; j<v[i].size(); j++) {
    if(v[i][j].F!=p) {
      if(v[i][j].S==-1) {
        v[i][j].S=solve(v[i][j].F,i);
        q+=v[i][j].S;
      } else q+=v[i][j].S;
    } else k=j;
  }
  if(k!=-1) v[i][k].S=nn-c[i]-q;
  return q+c[i];
}

bool u[100001];
P dfs(ll x) {
  u[x]=1;
  P p=P(0,0);
  rep(i,v[x].size()) {
    ll y=v[x][i].F;
    if(!u[y]) {
      P q=dfs(y);
      p.F+=q.F;
      p.S+=q.S;
    }
  }
  p.F+=c[x];
  p.S+=p.F;
  return p;
}

ll M;
void calc(ll x,ll p,ll sum) {
  M=min(M,sum);
  rep(i,v[x].size()) {
    ll y=v[x][i].F;
    if(y!=p) calc(y,x,sum-v[x][i].S*2+nn);
  }
}

ll n,m,T;
ll dep[100010];
ll par[100010][19];
void dfs(int x,int y,int d){
  u[x]=1;
  dep[x]=d;
  for(int i=0; i<v[x].size(); i++){
    int z=v[x][i].F;if(z==y) continue;
    par[z][0]=x;dfs(z,x,d+1);
  }
}
int lca(int a,int b){
  if(dep[a]<dep[b]) swap(a,b);
  for(int i=0; i<19; i++){
    if((dep[a]-dep[b])&(1<<i)) a=par[a][i];
  }
  if(a==b) return a;
  for(int i=18;i>=0;i--){
    if(par[a][i]!=par[b][i]) a=par[a][i],b=par[b][i];
  }
  return par[a][0];
}
void init2() {
  memset(par,0,sizeof(par));mem(u);
  rep(i,n)if(!u[i])dfs(i,-1,0);
  for(int i=0; i<18; i++)for(int j=0; j<n; j++) par[j][i+1]=par[par[j][i]][i];
}
ll D(int x,int y) {
  return dep[x]+dep[y]-dep[lca(x,y)]*2;
}

void Main() {
  init();
  cin >> n >> m >> T;
  vector<P> g;
  rep(i,m) {
    ll x,y;
    cin >> x >> y;
    x--,y--;
    v[x].pb(P(y,-1));
    v[y].pb(P(x,-1));
    unite(x,y);
  }
  rep(i,T) {
    ll x,y;
    cin >> x >> y;
    x--,y--;
    if(!same(x,y)) {
      c[x]++;
      c[y]++;
    } else g.pb(P(x,y));
  }
  ll ans=0;
  rep(i,n) {
    if(!u[i]) {
      P p=dfs(i);
      nn=p.F;
      p.S-=nn;
      solve(i,-1);
      M=MAXL;
      calc(i,-1,p.S);
      ans+=M;
    }
  }
  init2();
  rep(i,g.size()) ans+=D(g[i].F,g[i].S);
  pr(ans);
}

int main(){ios::sync_with_stdio(0);cin.tie(0);Main();return 0;}
0