結果

問題 No.483 マッチ並べ
ユーザー 🍡yurahuna🍡yurahuna
提出日時 2016-04-10 03:21:24
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 6 ms / 2,000 ms
コード長 2,215 bytes
コンパイル時間 1,630 ms
コンパイル使用メモリ 173,056 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-28 10:28:23
合計ジャッジ時間 3,543 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<P> vp;
typedef vector<ll> vll;

// 入力に出現する頂点が非連結でもOK
// !!!!!!!打倒 test15.txt!!!!!!!!

const int H = 100;
const int W = 100;
const int SIZE = H * W;

signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);

    int N;
    cin >> N;

    // 各頂点の次数を記録
    vi deg(SIZE, 0);
    // 各頂点の連結成分番号を記録 (頂点自身の番号で初期化)
    vi comp(SIZE);
    rep(i, SIZE) comp[i] = i;

    rep(i, N) {
        int r1, c1, r2, c2;
        cin >> r1 >> c1 >> r2 >> c2;
        r1--; c1--; r2--; c2--;

        int u = r1 * W + c1;
        int v = r2 * W + c2;

        // 次数は始点側にのみ記録しておく (こうすると総和を取ったときあとで2で割らなくて済むので)
        deg[u]++;

        // もし辺の両端の番号が異なっていれば連結させる
        if (comp[u] != comp[v]) {
            int min_c = min(comp[u], comp[v]);
            int max_c = max(comp[u], comp[v]);

            // 大きい方の番号を小さい方に置き換える
            rep(i, SIZE) {
                if (comp[i] == max_c) {
                    comp[i] = min_c;
                }
            }
        }
    }

    // 連結成分番号、(頂点数, 辺の本数)
    map<int, P> mp;
    rep(i, SIZE) {
        mp[comp[i]].first++;
        mp[comp[i]].second += deg[i];
    }

    // 各連結成分について、サイクルが 1個以下であるか確認
    for (auto p : mp) {
        int num_of_vertice = p.second.first;
        int num_of_edges = p.second.second;

        // 辺の本数 > 頂点数ならば、この連結成分は サイクルを 2個 以上含むので不適
        if (num_of_edges > num_of_vertice) {
            cout << "NO" << endl;
            return 0;
        }
    }

    cout << "YES" << endl;

}
0