結果

問題 No.2403 "Eight" Bridges of Königsberg
ユーザー Focus_SashFocus_Sash
提出日時 2023-08-04 21:45:36
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 5,307 bytes
コンパイル時間 3,299 ms
コンパイル使用メモリ 215,476 KB
実行使用メモリ 9,600 KB
最終ジャッジ日時 2024-04-22 20:06:08
合計ジャッジ時間 4,263 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 19 ms
5,376 KB
testcase_05 AC 38 ms
5,376 KB
testcase_06 AC 43 ms
5,504 KB
testcase_07 AC 24 ms
5,376 KB
testcase_08 AC 29 ms
5,376 KB
testcase_09 AC 47 ms
5,516 KB
testcase_10 AC 40 ms
5,376 KB
testcase_11 AC 34 ms
5,376 KB
testcase_12 AC 43 ms
5,376 KB
testcase_13 AC 32 ms
5,376 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 6 ms
9,600 KB
testcase_20 AC 3 ms
5,376 KB
testcase_21 AC 5 ms
6,656 KB
testcase_22 AC 6 ms
9,124 KB
testcase_23 AC 6 ms
8,692 KB
testcase_24 AC 5 ms
7,808 KB
testcase_25 AC 4 ms
7,424 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 6 ms
8,140 KB
testcase_29 WA -
testcase_30 AC 4 ms
6,016 KB
testcase_31 AC 7 ms
9,472 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
namespace util {
using ll = long long;
using vl = std::vector<long long>;
using pl = std::pair<long long, long long>;

constexpr long long kInf = std::numeric_limits<long long>::max() / 8;
constexpr long long kMax = std::numeric_limits<long long>::max();

template <typename T, typename U>
inline bool UpdateMax(T &x, const U &y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}

template <typename T, typename U>
inline bool UpdateMin(T &x, const U &y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}

// verified
inline long long Pow(long long x, long long n) {
  assert(n >= 0);
  if (x == 0) return 0;
  long long res = 1LL;
  while (n > 0) {
    if (n & 1) {
      assert(x != 0 && std::abs(res) <= kMax / std::abs(x));
      res = res * x;
    }
    if (n >>= 1) {
      assert(x != 0 && std::abs(x) <= kMax / std::abs(x));
      x = x * x;
    }
  }
  return res;
}

// verified
inline long long Mod(long long n, const long long m) {
  // returns the "arithmetic modulo"
  // for a pair of integers (n, m) with m != 0, there exists a unique pair of
  // integer (q, r) s.t. n = qm + r and 0 <= r < |m| returns this r
  assert(m != 0);
  if (m < 0) return Mod(n, -m);
  if (n >= 0)
    return n % m;
  else
    return (m + n % m) % m;
}

inline long long Quotient(long long n, long long m) {
  // returns the "arithmetic quotient"
  assert((n - Mod(n, m)) % m == 0);
  return (n - Mod(n, m)) / m;
}

inline long long DivFloor(long long n, long long m) {
  // returns floor(n / m)
  assert(m != 0);
  if (m < 0) {
    n = -n;
    m = -m;
  }
  if (n >= 0)
    return n / m;
  else if (n % m == 0)
    return -(abs(n) / m);
  else
    return -(abs(n) / m) - 1;
}

inline long long DivCeil(long long n, long long m) {
  // returns ceil(n / m)
  assert(m != 0);
  if (n % m == 0)
    return DivFloor(n, m);
  else
    return DivFloor(n, m) + 1;
}

template <typename T>
inline T Sum(const std::vector<T> &vec) {
  return std::accumulate(vec.begin(), vec.end(), T(0));
}
}  // namespace util
using namespace util;

template <typename T>
std::vector<T> Deduplicate(std::vector<T> v) {
  std::sort(v.begin(), v.end());
  v.erase(std::unique(v.begin(), v.end()), v.end());
  return v;
}

using Graph = std::vector<std::vector<int>>;

Graph TransposeGraph(const Graph &g) {
  int n = (int)size(g);
  Graph res(n);
  for (int i = 0; i < n; ++i) {
    for (int to : g[i]) {
      res[to].emplace_back(i);
    }
  }
  return res;
}

// 返り値:強連結成分を表すリストのリスト(トポロジカルソート済み)・各頂点がリストの何番に入っているかを表すリスト・強連結成分を頂点とするグラフ
std::tuple<std::vector<std::vector<int>>, std::vector<int>, Graph> Scc(
    const Graph &g) {
  int n = (int)size(g);
  std::vector<int> time(n);
  std::vector<int> seen(n);

  {
    int counter = 0;
    auto CalculateReachingTime = [&](auto &&self, const int s) -> void {
      seen[s] = true;
      for (int nv : g[s]) {
        if (seen[nv]) continue;
        self(self, nv);
      }
      time[s] = counter;
      counter++;
    };
    for (int i = 0; i < n; ++i) {
      if (!seen[i]) CalculateReachingTime(CalculateReachingTime, i);
    }
  }

  Graph g_transpose = TransposeGraph(g);
  std::vector<int> vertex_of_time(n);

  for (int i = 0; i < n; ++i) {
    vertex_of_time[time[i]] = i;
  }

  std::vector<std::vector<int>> connected_components;

  {
    std::vector<int> transpose_seen(n, false);
    auto SearchInComponent = [&g_transpose, &transpose_seen](
                                 auto &&self, const int s,
                                 std::vector<int> &component) -> void {
      component.emplace_back(s);
      transpose_seen[s] = true;
      for (int nv : g_transpose[s]) {
        if (transpose_seen[nv]) continue;
        self(self, nv, component);
      }
    };
    for (int t = n - 1; t >= 0; t--) {
      int v = vertex_of_time[t];
      if (!transpose_seen[v]) {
        std::vector<int> component;
        SearchInComponent(SearchInComponent, v, component);
        connected_components.emplace_back(component);
      }
    }
  }

  int c = (int)connected_components.size();

  std::vector<int> owner(n);
  for (int i = 0; i < c; ++i) {
    for (int v : connected_components[i]) {
      owner[v] = i;
    }
  }

  Graph contracted_graph(c);

  for (int i = 0; i < n; i++) {
    int from = owner[i];
    for (auto v : g[i]) {
      int to = owner[v];
      if (from != to) {
        contracted_graph[from].emplace_back(to);
      }
    }
  }

  for (int v = 0; v < c; ++v) {
    Deduplicate(contracted_graph[v]);
  }
  return {connected_components, owner, contracted_graph};
}

void solve() {
  ll n, m;
  cin >> n >> m;
  Graph g(n);
  vl od(n, 0), id(n, 0);
  for (ll i = 0; i < m; i++) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    od[a]++;
    id[b]++;
    g[a].emplace_back(b);
  }
  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    if (od[i] > id[i]) {
      ans += od[i] - id[i];
    }
  }
  if (ans <= 1) {
    cout << 0 << '\n';
  } else {
    cout << ans - 1 << '\n';
  }
}

int main() {
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);

  solve();

  return 0;
}
0