結果

問題 No.408 五輪ピック
ユーザー kimiyukikimiyuki
提出日時 2016-10-18 18:22:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 55 ms / 5,000 ms
コード長 1,274 bytes
コンパイル時間 714 ms
コンパイル使用メモリ 83,660 KB
実行使用メモリ 9,168 KB
最終ジャッジ日時 2023-08-14 14:39:04
合計ジャッジ時間 2,725 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 25 ms
4,376 KB
testcase_06 AC 12 ms
4,376 KB
testcase_07 AC 18 ms
4,376 KB
testcase_08 AC 16 ms
4,376 KB
testcase_09 AC 16 ms
4,376 KB
testcase_10 AC 12 ms
4,376 KB
testcase_11 AC 11 ms
4,380 KB
testcase_12 AC 21 ms
4,376 KB
testcase_13 AC 22 ms
4,376 KB
testcase_14 AC 5 ms
4,376 KB
testcase_15 AC 32 ms
5,432 KB
testcase_16 AC 26 ms
4,376 KB
testcase_17 AC 23 ms
4,376 KB
testcase_18 AC 23 ms
4,380 KB
testcase_19 AC 24 ms
4,376 KB
testcase_20 AC 7 ms
4,376 KB
testcase_21 AC 5 ms
4,380 KB
testcase_22 AC 11 ms
4,376 KB
testcase_23 AC 55 ms
9,168 KB
testcase_24 AC 39 ms
7,216 KB
testcase_25 AC 37 ms
5,804 KB
testcase_26 AC 26 ms
4,432 KB
testcase_27 AC 49 ms
5,748 KB
testcase_28 AC 27 ms
5,796 KB
testcase_29 AC 29 ms
5,644 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <set>
#define repeat(i,n) for (int i = 0; (i) < (n); ++(i))
using namespace std;
int main() {
    // input
    int n, m; cin >> n >> m;
    vector<int> a(m), b(m);
    repeat (i,m) {
        cin >> a[i] >> b[i];
        -- a[i]; -- b[i];
    }
    // select vertices and edges
    const int root = 0;
    set<int> v1;
    repeat (i,m) {
        if (b[i] == root) v1.insert(a[i]);
        if (a[i] == root) v1.insert(b[i]);
    }
    vector<set<int> > g1(n);
    repeat (i,m) {
        if (v1.count(b[i])) g1[a[i]].insert(b[i]);
        if (v1.count(a[i])) g1[b[i]].insert(a[i]);
    }
    // find a cycle
    bool ans = false;
    repeat (e,m) {
        int i = a[e];
        int j = b[e];
        if (i == root or j == root) continue;
        bool g1_i_count_j = g1[i].count(j); g1[i].erase(j);
        bool g1_j_count_i = g1[j].count(i); g1[j].erase(i);
        if (not g1[i].empty() and not g1[j].empty()) {
            if (g1[i].size() >= 2 or g1[j].size() >= 2 or g1[i] != g1[j]) {
                ans = true;
            }
        }
        if (g1_i_count_j) g1[i].insert(j);
        if (g1_j_count_i) g1[j].insert(i);
        if (ans) break;
    }
    // output
    cout << (ans ? "YES" : "NO") << endl;
    return 0;
}
0