結果

問題 No.483 マッチ並べ
ユーザー yuppe19 😺yuppe19 😺
提出日時 2017-02-16 17:14:57
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 2,258 bytes
コンパイル時間 1,517 ms
コンパイル使用メモリ 74,520 KB
実行使用メモリ 4,400 KB
最終ジャッジ日時 2023-08-28 17:39:27
合計ジャッジ時間 2,939 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,380 KB
testcase_01 AC 3 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 3 ms
4,376 KB
testcase_13 AC 3 ms
4,380 KB
testcase_14 AC 3 ms
4,380 KB
testcase_15 AC 3 ms
4,376 KB
testcase_16 AC 3 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 3 ms
4,380 KB
testcase_20 AC 3 ms
4,376 KB
testcase_21 AC 3 ms
4,376 KB
testcase_22 AC 3 ms
4,376 KB
testcase_23 AC 3 ms
4,376 KB
testcase_24 AC 3 ms
4,376 KB
testcase_25 AC 3 ms
4,376 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 3 ms
4,380 KB
testcase_28 AC 3 ms
4,376 KB
testcase_29 AC 3 ms
4,376 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 3 ms
4,376 KB
testcase_32 AC 3 ms
4,376 KB
testcase_33 AC 3 ms
4,376 KB
testcase_34 AC 3 ms
4,376 KB
testcase_35 AC 3 ms
4,380 KB
testcase_36 AC 2 ms
4,384 KB
testcase_37 AC 3 ms
4,400 KB
testcase_38 AC 3 ms
4,380 KB
testcase_39 AC 2 ms
4,380 KB
testcase_40 AC 2 ms
4,376 KB
testcase_41 AC 2 ms
4,380 KB
testcase_42 AC 3 ms
4,384 KB
testcase_43 AC 3 ms
4,380 KB
testcase_44 AC 2 ms
4,380 KB
testcase_45 AC 2 ms
4,376 KB
testcase_46 AC 3 ms
4,376 KB
testcase_47 AC 3 ms
4,376 KB
testcase_48 AC 3 ms
4,380 KB
testcase_49 AC 3 ms
4,376 KB
testcase_50 AC 3 ms
4,380 KB
testcase_51 AC 3 ms
4,376 KB
testcase_52 AC 3 ms
4,380 KB
testcase_53 AC 2 ms
4,376 KB
testcase_54 AC 2 ms
4,380 KB
testcase_55 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;

constexpr int inf = 987654321;

struct Edge {
  int dst, rev;
  int capa;
};

class Graph {
protected:
  int size;
  vector<vector<Edge>> g;
public:
  Graph(int size) : size(size) { g.resize(size); }
  void add_edge(int src, int dst, int capa);
  int max_flow(int s, int t) { throw; }
};

class Dinic : public Graph {
  vector<int> level, iter;
  void bfs(int s);
  int dfs(int v, int t, int flow);
public:
  Dinic(int size) : Graph(size) {}
  int max_flow(int s, int t);
};

void Graph::add_edge(int src, int dst, int capa) {
  g[src].push_back(Edge({dst, int(g[dst].size()),   capa}));
  g[dst].push_back(Edge({src, int(g[src].size())-1,    0}));
}

void Dinic::bfs(int s) {
  level.assign(size, -1);
  queue<int> que;
  level[s] = 0;
  que.push(s);
  while(!que.empty()) {
    int v = que.front(); que.pop();
    for(Edge& e : g[v]) {
      if(e.capa > 0 && level[e.dst] < 0) {
        level[e.dst] = level[v] + 1;
        que.push(e.dst);
      }
    }
  }
}

int Dinic::dfs(int v, int t, int flow) {
  if(v == t) { return flow; }
  for(int& i=iter[v], n=g[v].size(); i<n; ++i) {
    Edge& e = g[v][i];
    if(e.capa <= 0 || level[v] >= level[e.dst]) { continue; }
    int d = dfs(e.dst, t, min(flow, e.capa));
    if(d > 0) {
      e.capa -= d;
      g[e.dst][e.rev].capa += d;
      return d;
    }
  }
  return 0;
}

int Dinic::max_flow(int s, int t) {
  int res = 0;
  while(true) {
    bfs(s);
    if(level[t] < 0) { return res; }
    iter.assign(size, 0);
    int flow;
    while((flow = dfs(s, t, inf)) > 0) {
      res += flow;
      if(res >= inf) { return inf; }
    }
  }
}

int main(void) {
  constexpr int N = 111;
  int n; scanf("%d", &n);
  Dinic graph(n + N*N + 2);
  int s = n + N*N,
      t = s + 1;
  for(int i=0; i<n; ++i) {
    int r0, c0, r1, c1; scanf("%d%d%d%d", &r0, &c0, &r1, &c1);
    int x = n + r0 * N + c0,
        y = n + r1 * N + c1;
    graph.add_edge(s, i, 1);
    graph.add_edge(i, x, 1);
    graph.add_edge(i, y, 1);
  }
  for(int r=0; r<N; ++r) {
    for(int c=0; c<N; ++c) {
      int x = n + r * N + c;
      graph.add_edge(x, t, 1);
    }
  }
  int max_flow = graph.max_flow(s, t);
  puts(max_flow == n ? "YES" : "NO");
  return 0;
}
0