結果

問題 No.483 マッチ並べ
ユーザー sekiya9311sekiya9311
提出日時 2017-02-11 03:05:53
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 2,000 ms
コード長 3,947 bytes
コンパイル時間 1,135 ms
コンパイル使用メモリ 116,180 KB
実行使用メモリ 4,492 KB
最終ジャッジ日時 2023-08-28 12:45:23
合計ジャッジ時間 3,110 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <vector>
#include <complex>
#include <utility>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <bitset>
#include <ctime>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <cassert>
#include <cstddef>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <sstream>
#include <fstream>

using namespace std;
#define REP(i, n) for (int (i) = 0; (i) < (n); (i)++)
#define FOR(i, a, b) for (int (i) = (a); (i) < (b); (i)++)
#define RREP(i, a) for(int (i) = (a) - 1; (i) >= 0; (i)--)
#define FORR(i, a, b) for(int (i) = (a) - 1; (i) >= (b); (i)--)
#define DEBUG(C) cerr << #C << " = " << C << endl;
using LL = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<LL>;
using VVL = vector<VL>;
using VD = vector<double>;
using VVD = vector<VD>;
using PII = pair<int, int>;
using PDD = pair<double, double>;
using PLL = pair<LL, LL>;
using VPII = vector<PII>;
template<typename T> using VT = vector<T>;
#define ALL(a) begin((a)), end((a))
#define RALL(a) rbegin((a)), rend((a))
#define SORT(a) sort(ALL((a)))
#define RSORT(a) sort(RALL((a)))
#define REVERSE(a) reverse(ALL((a)))
#define MP make_pair
#define FORE(a, b) for (auto &&a : (b))
#define FIND(s, e) ((s).find(e) != (s).end())
#define EB emplace_back
template<typename T> inline bool chmax(T &a, T b){if (a < b){a = b;return true;}return false;}
template<typename T> inline bool chmin(T &a, T b){if (a > b){a = b;return true;}return false;}

const int INF = 1e9;
const int MOD = INF + 7;
const LL LLINF = 1e18;

//超点数や流量がめっちゃ大きいときは Dinic!!
//蟻本P190~
class FordFulkerson {
    struct edge {
        int to, rev;    //行き先,逆辺(のid)
        long long cap;  //容量
        edge(int _to, int _rev, long long _cap) {
            this->to = _to;
            this->rev = _rev;
            this->cap = _cap;
        }
    };
private:
    vector<vector<edge>> G;
    vector<bool> used;
    long long dfs(int v, int t, long long f) {
        if (v == t) return f;
        used[v] = true;
        for (auto &&el : G[v]) {
            if (used[el.to] || el.cap <= 0) {
                continue;
            }
            long long d = dfs(el.to, t, min(f, el.cap));
            if (d > 0) {
                el.cap -= d;
                G[el.to][el.rev].cap += d;
                return d;
            }
        }
        return 0;
    }
public:
    FordFulkerson() {}
    FordFulkerson(int n) {
        G.resize(n);
        used.resize(n, false);
    }
    void add_edge(int from, int to, long long cap) {
        G[from].emplace_back(edge(to, G[to].size(), cap));
        G[to].emplace_back(edge(from, G[from].size() - 1, 0));
    }
    long long max_flow(int s, int t) {
        if (s == t) return 0;
        long long flow = 0;
        while (1) {
            fill(begin(used), end(used), false);
            long long f = dfs(s, t, LLINF);
            if (f == 0) return flow;
            flow += f;
        }
    }
};

const int MAX = 150;
const int SIZE = 115;
FordFulkerson ff(MAX * MAX);
int N;

PII ip(int n) {
    return MP(n / SIZE, n % SIZE);
}
int pi(PII p) {
    return p.first * SIZE + p.second;
}
int pi(int r, int c) {
    return pi(MP(r, c));
}

int main(void) {
    const int s = MAX * MAX - 1, t = s - 1;
    scanf("%d", &N);
    //各格子点 -> t
    REP(i, SIZE)REP(j, SIZE) ff.add_edge(pi(i, j) + SIZE, t, 1);
    REP(i, N) {
        int a, b, c, d;
        scanf("%d%d%d%d", &a, &b, &c, &d);
        // s -> マッチ棒(棒はidxで表す)
        ff.add_edge(s, i + 1, 1);
        // マッチ棒 -> 棒の2つの頂点
        ff.add_edge(i + 1, pi(a, b) + SIZE, 1);
        ff.add_edge(i + 1, pi(c, d) + SIZE, 1);
    }
    cout << (ff.max_flow(s, t) == N ? "YES" : "NO") << endl;
}
0