結果

問題 No.922 東北きりきざむたん
ユーザー kzyKTkzyKT
提出日時 2019-11-08 22:20:41
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 3,333 bytes
コンパイル時間 1,297 ms
コンパイル使用メモリ 150,396 KB
実行使用メモリ 38,236 KB
最終ジャッジ日時 2023-10-13 03:59:05
合計ジャッジ時間 4,929 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
25,408 KB
testcase_01 AC 9 ms
25,460 KB
testcase_02 AC 10 ms
25,300 KB
testcase_03 AC 9 ms
25,332 KB
testcase_04 AC 10 ms
25,424 KB
testcase_05 AC 10 ms
25,312 KB
testcase_06 AC 10 ms
25,264 KB
testcase_07 AC 10 ms
25,232 KB
testcase_08 AC 9 ms
25,256 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 22 ms
25,552 KB
testcase_13 AC 17 ms
25,384 KB
testcase_14 WA -
testcase_15 AC 18 ms
25,364 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 AC 174 ms
32,460 KB
testcase_25 AC 120 ms
32,316 KB
testcase_26 AC 118 ms
32,252 KB
testcase_27 AC 118 ms
32,388 KB
testcase_28 AC 39 ms
25,408 KB
testcase_29 AC 137 ms
38,236 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){
  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));
  dfs(0,-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];
}
int 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