結果

問題 No.1640 簡単な色塗り
ユーザー 👑 jupirojupiro
提出日時 2021-08-06 21:30:46
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 3,606 bytes
コンパイル時間 2,678 ms
コンパイル使用メモリ 213,204 KB
実行使用メモリ 39,824 KB
最終ジャッジ日時 2023-09-12 01:37:05
合計ジャッジ時間 19,711 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
11,484 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 87 ms
32,696 KB
testcase_05 AC 87 ms
32,496 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 301 ms
20,740 KB
testcase_11 AC 216 ms
17,784 KB
testcase_12 AC 168 ms
15,884 KB
testcase_13 AC 542 ms
30,116 KB
testcase_14 AC 536 ms
30,440 KB
testcase_15 AC 104 ms
12,404 KB
testcase_16 AC 143 ms
14,528 KB
testcase_17 AC 436 ms
25,804 KB
testcase_18 AC 14 ms
5,352 KB
testcase_19 AC 175 ms
15,932 KB
testcase_20 AC 265 ms
20,244 KB
testcase_21 AC 183 ms
17,376 KB
testcase_22 AC 10 ms
4,600 KB
testcase_23 AC 291 ms
21,640 KB
testcase_24 AC 43 ms
8,368 KB
testcase_25 AC 175 ms
15,944 KB
testcase_26 AC 343 ms
23,892 KB
testcase_27 AC 96 ms
11,868 KB
testcase_28 AC 551 ms
28,572 KB
testcase_29 AC 382 ms
23,240 KB
testcase_30 AC 17 ms
7,692 KB
testcase_31 AC 247 ms
39,824 KB
testcase_32 AC 181 ms
31,920 KB
testcase_33 AC 101 ms
21,684 KB
testcase_34 AC 191 ms
29,620 KB
testcase_35 AC 83 ms
21,924 KB
testcase_36 AC 17 ms
7,724 KB
testcase_37 AC 23 ms
9,472 KB
testcase_38 AC 173 ms
33,340 KB
testcase_39 AC 57 ms
17,096 KB
testcase_40 AC 50 ms
15,512 KB
testcase_41 AC 107 ms
25,896 KB
testcase_42 AC 61 ms
18,008 KB
testcase_43 AC 78 ms
19,984 KB
testcase_44 AC 61 ms
17,536 KB
testcase_45 AC 43 ms
14,108 KB
testcase_46 AC 17 ms
8,228 KB
testcase_47 AC 12 ms
6,668 KB
testcase_48 AC 190 ms
33,004 KB
testcase_49 AC 6 ms
4,680 KB
testcase_50 AC 2 ms
4,380 KB
testcase_51 AC 1 ms
4,376 KB
testcase_52 AC 334 ms
38,712 KB
testcase_53 TLE -
07_evil_01.txt -- -
07_evil_02.txt -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using ll = long long;
using std::cin;
using std::cout;
using std::endl;
std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count());
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const int inf = (int)1e9 + 7;
const long long INF = 1LL << 60;

template<typename T>
struct Dinic {
    struct edge {
        int to;
        T cap;
        int rev;
        bool isrev;
        int idx;
        edge(int _to, T _cap, int _rev, bool _isrev, int _idx) :to(_to), cap(_cap), rev(_rev), isrev(_isrev), idx(_idx) {}
    };
    std::vector<std::vector<edge>> g;
    std::vector<int> min_cost, iter;
    T INF;
    Dinic(int n, T INF) :g(n), INF(INF) {}

    void add_edge(int from, int to, T cap, int idx = -1) {
        g[from].emplace_back(to, cap, (int)g[to].size(), false, idx);
        g[to].emplace_back(from, 0, (int)g[from].size() - 1, true, idx);
    }

    bool bfs(int s, int t) {
        min_cost.assign(g.size(), -1);
        std::queue<int> q;
        q.emplace(s);
        min_cost[s] = 0;
        while (!q.empty() && min_cost[t] == -1) {
            int cur = q.front();
            q.pop();
            for (auto& e : g[cur]) {
                if (e.cap > 0 && min_cost[e.to] == -1) {
                    min_cost[e.to] = min_cost[cur] + 1;
                    q.push(e.to);
                }
            }
        }
        return min_cost[t] != -1;
    }

    T dfs(int idx, const int t, T flow) {
        if (idx == t) return flow;
        for (int& i = iter[idx]; i < (int)g[idx].size(); i++) {
            edge& e = g[idx][i];
            if (e.cap > 0 && min_cost[idx] < min_cost[e.to]) {
                T d = dfs(e.to, t, std::min(flow, e.cap));
                if (d > 0) {
                    e.cap -= d;
                    g[e.to][e.rev].cap += d;
                    return d;
                }
            }
        }
        return 0;
    }

    T max_flow(int s, int t) {
        T flow = 0;
        while (bfs(s, t)) {
            iter.assign(g.size(), 0);
            T f = 0;
            while ((f = dfs(s, t, INF)) > 0) flow += f;
        }
        return flow;
    }

    void output() {
        for(int i = 0; i < (int)g.size(); i++) {
          for(auto &e : g[i]) {
            if(e.isrev) continue;
            auto &rev_e = g[e.to][e.rev];
            cout << i << "->" << e.to << " (flow: " << rev_e.cap << "/" << e.cap + rev_e.cap << ")" << endl;
          }
        }
    }
};
void solve()
{
  int n; cin >> n;
  Dinic<int> g(n * 2 + 2, inf);
  const int s = n * 2;
  const int t = s + 1;
  for (int i = 0; i < n; ++i)
  {
    int a, b; cin >> a >> b;
    a -= 1, b -= 1;
    g.add_edge(i, n + a, 1);
    g.add_edge(i, n + b, 1);
  }
  for (int i = 0; i < n; ++i)
  {
    g.add_edge(s, i, 1);
    g.add_edge(i + n, t, 1);
  }
  const int max_flow = g.max_flow(s, t);
  if(max_flow == n)
  {
    cout << "Yes" << "\n";
    for(int i = 0; i < n; i++) {
      for(auto &e : g.g[i]) {
        if(e.isrev) continue;
        auto &rev_e = g.g[e.to][e.rev];
        if(rev_e.cap == 1)
        {
          const int to = e.to - n;
          cout << to + 1 << "\n";
        }
        // cout << i << "->" << e.to << " (flow: " << rev_e.cap << "/" << e.cap + rev_e.cap << ")" << endl;
      }
    }
  }
  else
  {
    cout << "No" << "\n";
  }
}
int main()
{
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);

  int kkt = 1; 
  // cin >> kkt;
  while(kkt--)
    solve();
  return 0;
  
}
0