結果

問題 No.2403 "Eight" Bridges of Königsberg
ユーザー umezoumezo
提出日時 2023-08-09 18:27:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 36 ms / 2,000 ms
コード長 1,347 bytes
コンパイル時間 2,208 ms
コンパイル使用メモリ 205,856 KB
実行使用メモリ 8,856 KB
最終ジャッジ日時 2023-08-17 18:49:17
合計ジャッジ時間 4,012 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 16 ms
4,376 KB
testcase_05 AC 30 ms
4,904 KB
testcase_06 AC 33 ms
5,256 KB
testcase_07 AC 19 ms
4,376 KB
testcase_08 AC 23 ms
4,624 KB
testcase_09 AC 36 ms
5,216 KB
testcase_10 AC 32 ms
5,112 KB
testcase_11 AC 26 ms
4,752 KB
testcase_12 AC 34 ms
5,088 KB
testcase_13 AC 24 ms
4,808 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 7 ms
8,856 KB
testcase_20 AC 3 ms
4,444 KB
testcase_21 AC 4 ms
5,900 KB
testcase_22 AC 6 ms
8,320 KB
testcase_23 AC 6 ms
8,228 KB
testcase_24 AC 5 ms
7,348 KB
testcase_25 AC 5 ms
7,172 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 7 ms
8,672 KB
testcase_28 AC 6 ms
7,488 KB
testcase_29 AC 3 ms
4,380 KB
testcase_30 AC 4 ms
5,584 KB
testcase_31 AC 7 ms
8,536 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 3 ms
4,892 KB
testcase_34 AC 3 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(v) v.begin(),v.end()
typedef long long ll;
 
#include<bits/stdc++.h>
using namespace std;

class Dis{
  public:
  vector<ll> rank,p,siz;
  
  Dis(int s){
    rank.resize(s,0);
    p.resize(s,0);
    siz.resize(s,1);
    rep(i,s) makeSet(i);
  }
  
  void makeSet(int x){
    p[x]=x;
    rank[x]=0;
  }
  
  bool same(int x,int y){
    return root(x)==root(y);
  }
  
  void unite(int x,int y){
    if(same(x,y)) return;
    link(root(x),root(y));
  }
  
  void link(int x,int y){
    if(rank[x]>rank[y]){
      p[y]=x;
      siz[x]+=siz[y];
    }
    else{
      p[x]=y;
      siz[y]+=siz[x];
      if(rank[x]==rank[y]) rank[y]++;
    }
  }
  
  int root(int x){
    if(x != p[x]) p[x]=root(p[x]);
    return p[x];
  }
  
  int size(int x){
    return siz[root(x)];
  }
};
 
int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  
  int n,m;
  cin>>n>>m;
  Dis ds=Dis(n);
  vector<int> U(m),V(m);
  vector<int> out(n),in(n);
  rep(i,m){
    cin>>U[i]>>V[i];
    U[i]--,V[i]--;
    out[U[i]]++;
    in[V[i]]++;
    ds.unite(U[i],V[i]);
  }
  vector<int> B(n);
  rep(i,n) B[ds.root(i)]+=max(0,out[i]-in[i]);
  int ans=0,cnt=0;
  rep(i,n) if(ds.root(i)==i){
    if(out[i]==0 && in[i]==0) continue;
    ans+=max(0,B[i]-1);
    cnt++;
  }
  cout<<ans+cnt-1<<endl;
  
  return 0;
}
0