結果

問題 No.2403 "Eight" Bridges of Königsberg
ユーザー as277575as277575
提出日時 2023-08-04 22:56:39
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 61 ms / 2,000 ms
コード長 2,853 bytes
コンパイル時間 1,277 ms
コンパイル使用メモリ 127,412 KB
実行使用メモリ 8,520 KB
最終ジャッジ日時 2023-08-17 18:40:30
合計ジャッジ時間 3,290 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 24 ms
5,656 KB
testcase_05 AC 48 ms
7,884 KB
testcase_06 AC 61 ms
8,488 KB
testcase_07 AC 30 ms
5,904 KB
testcase_08 AC 39 ms
6,404 KB
testcase_09 AC 61 ms
8,284 KB
testcase_10 AC 53 ms
8,148 KB
testcase_11 AC 41 ms
6,304 KB
testcase_12 AC 57 ms
8,336 KB
testcase_13 AC 41 ms
6,448 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 7 ms
8,520 KB
testcase_20 AC 3 ms
4,376 KB
testcase_21 AC 5 ms
5,740 KB
testcase_22 AC 6 ms
7,860 KB
testcase_23 AC 7 ms
7,472 KB
testcase_24 AC 6 ms
6,684 KB
testcase_25 AC 6 ms
6,536 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 8 ms
8,328 KB
testcase_28 AC 6 ms
6,944 KB
testcase_29 AC 3 ms
4,376 KB
testcase_30 AC 4 ms
5,164 KB
testcase_31 AC 8 ms
8,132 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 4 ms
4,576 KB
testcase_34 AC 3 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <bit>
#include <bitset>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <random>
#include <unordered_set>
#include <unordered_map>
#include <vector>
#include <set>
#include <string>

using namespace::std;

template<typename T, typename U>
ostream& operator<< (ostream& o, const pair<T, U>& p) {
  o << "<" << p.first << ", " << p.second << ">";
  return o;
}

template <typename T>
ostream& operator<< (ostream& o, const vector<T>& v) {
 for (const auto& x : v) o << x << " ";
 return o;
}

template <typename T>
ostream& operator<< (ostream& o, const set<T>& v) {
 for (const auto& x : v) o << x << " ";
 return o;
}

template <typename T>
ostream& operator<< (ostream& o, const deque<T>& v) {
 for (const auto& x : v) o << x << " ";
 return o;
}

template <typename K, typename V>
ostream& operator<<(ostream& o, const unordered_map<K, V>& m) {
  o << "{";
  for (const auto& [k, v] : m)
    o << " " << k << ": " << v << ",";
  o << "}";
  return o;
}

template <typename T>
ostream& operator<< (ostream& o, const vector<vector<T>>& v) {
 for (const auto& x : v) o << x << endl;
 return o;
}

ostream& operator<< (ostream& o, const vector<string>& v) {
 for (const auto& x : v) o << x << endl;
 return o;
}



template <typename T>
istream& operator>> (istream& i, vector<T>& v) {
  for (auto& x : v) i >> x;
  return i;
}

istream& operator>> (istream& i, pair<int, int>& p) {
  i >> p.first >> p.second;
  return i;
}



int main() {
  ios::sync_with_stdio(false);
   long long n, m;
  cin >> n >> m;
  vector< long long> c(n + 1);
  vector<vector<int>> e(n + 1);
  for ( long long i = 0; i < m; ++i) {
     long long u, v;
    cin >> u >> v;
    e[u].push_back(v);
    e[v].push_back(u);
    --c[u];
    ++c[v];
  }
  
  //cerr << "e " << e << endl;
  
  long long o = 0;
  vector<bool> v(n + 1);
  vector<int> q;
  for (int s = 1; s <= n; ++s) if (!v[s]) {
      // cerr << "s: " << s << endl;
      bool ok = true;
      int count = 0;
      q.push_back(s);
      while (q.size()) {
        int x = q.back();
        q.pop_back();
        // cerr << "checking x " << x << endl;
        if (v[x]) continue;
        // cerr << "visiting x " << x << endl;
        v[x] = true;
        ok &= c[x] == 0;
        count += e[x].size();
        for (int y : e[x]) {
          // cerr << "pushing y " << y << endl;
          q.push_back(y);
        }
      }
      // cerr << "s " << s << " ok " << ok << " count " << count << endl;
      o += ok && count > 1;
    }
  
  long long hi = 0;
  for ( long long i = 0; i <= n; ++i) {
    if (c[i] > 0) hi += c[i];
  }
  if (o == 0) { cout << max(0ll, hi- 1) << endl; }
  else if (hi == 0) { cout << max(0ll, o - 1) << endl; }
  else { cout << max(0ll, hi + o - 1) << endl; }

  return 0;
}


0