結果

問題 No.2403 "Eight" Bridges of Königsberg
ユーザー 蜜蜂蜜蜂
提出日時 2023-08-05 12:23:59
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,547 bytes
コンパイル時間 3,785 ms
コンパイル使用メモリ 230,764 KB
実行使用メモリ 24,948 KB
最終ジャッジ日時 2024-04-23 09:34:33
合計ジャッジ時間 5,563 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,948 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 16 ms
6,940 KB
testcase_05 AC 30 ms
6,944 KB
testcase_06 AC 34 ms
6,940 KB
testcase_07 AC 19 ms
6,944 KB
testcase_08 AC 22 ms
6,940 KB
testcase_09 AC 36 ms
6,940 KB
testcase_10 AC 31 ms
6,940 KB
testcase_11 AC 26 ms
6,940 KB
testcase_12 AC 34 ms
6,944 KB
testcase_13 AC 25 ms
6,944 KB
testcase_14 AC 1 ms
6,940 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 2 ms
6,944 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 39 ms
24,948 KB
testcase_20 AC 9 ms
7,192 KB
testcase_21 AC 20 ms
14,048 KB
testcase_22 AC 33 ms
24,056 KB
testcase_23 AC 33 ms
23,596 KB
testcase_24 AC 26 ms
17,564 KB
testcase_25 AC 27 ms
16,796 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 29 ms
18,164 KB
testcase_29 WA -
testcase_30 AC 16 ms
11,148 KB
testcase_31 AC 37 ms
24,424 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 6 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// g++ 1.cpp -std=c++17 -O2 -I .
#include <bits/stdc++.h>
using namespace std;

#include <atcoder/all>
using namespace atcoder;

using ll = long long;
using ld = long double;
 
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vld = vector<ld>;
using vvld = vector<vld>;
using vst = vector<string>;
using vvst = vector<vst>;
 
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define pq_big(T) priority_queue<T,vector<T>,less<T>>
#define pq_small(T) priority_queue<T,vector<T>,greater<T>>
#define all(a) a.begin(),a.end()
#define rep(i,start,end) for(ll i=start;i<(ll)(end);i++)
#define per(i,start,end) for(ll i=start;i>=(ll)(end);i--)
#define uniq(a) sort(all(a));a.erase(unique(all(a)),a.end())

#include <vector>
#include <cassert>

struct union_find_group{
  private:
  int n;
  std::vector<int> parent_size;
  // 正 親
  // 負 -1*サイズ
  std::vector<std::vector<int>> group_info;
  // group の集合情報を常に管理

  public:
  union_find_group(int sz){
    n=sz;
    parent_size.resize(n,-1);
    group_info.resize(n);
    for(int i=0;i<n;i++){
      group_info[i].emplace_back(i);
    }
  }

  int root(int i){
    assert(0<=i&&i<n);
    if(parent_size[i]<0)return i;
    return parent_size[i]=root(parent_size[i]);
  }

  int size(int i){
    return -1*parent_size[root(i)];
  }

  bool same(int i,int j){
    assert(0<=i&&i<n&&0<=j&&j<n);
    return root(i)==root(j);
  }

  int merge(int i,int j){
    int ri=root(i),rj=root(j);
    if(ri==rj)return ri;

    // |riの部分集合|>=|rj|の部分集合としたい
    if(parent_size[ri]>parent_size[rj]){
      std::swap(ri,rj);
    }

    parent_size[ri]+=parent_size[rj];
    parent_size[rj]=ri;
    for(int vartex_rj:group_info[rj]){
      group_info[ri].emplace_back(vartex_rj);
    }
    return ri;
  }

  std::vector<int> group(int i){
    int ri=root(i);
    return group_info[ri];
  }
};

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n,m;cin>>n>>m;
  union_find_group uf(n);
  vi in(n,0);
  vi out(n,0);

  rep(i,0,m){
    int u,v;cin>>u>>v;
    u--;v--;
    in[v]++;
    out[u]++;
    uf.merge(u,v);
  }

  vvi v;
  rep(i,0,n){
    if(uf.root(i)==i){
      v.emplace_back(uf.group(i));
    }
  }

  ll ans=v.size()-1;
  for(vi g:v){
    vi d;
    for(int id:g){
      d.emplace_back(in[id]-out[id]);
    }
    int d2=0;
    for(int dd:d){
      d2+=abs(dd);
    }
    
    if(d2>2)ans+=d2/2-1;
    if(g.size()==1&&d2==0)ans--;
  }

  cout<<ans<<endl;
}
0