結果

問題 No.1023 Cyclic Tour
ユーザー fuppy_kyoprofuppy_kyopro
提出日時 2020-04-10 22:43:05
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 419 ms / 2,000 ms
コード長 3,959 bytes
コンパイル時間 1,909 ms
コンパイル使用メモリ 180,884 KB
実行使用メモリ 43,644 KB
最終ジャッジ日時 2023-10-14 03:16:21
合計ジャッジ時間 20,219 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 111 ms
19,864 KB
testcase_05 AC 113 ms
20,064 KB
testcase_06 AC 126 ms
21,248 KB
testcase_07 AC 118 ms
20,408 KB
testcase_08 AC 142 ms
20,140 KB
testcase_09 AC 138 ms
19,796 KB
testcase_10 AC 120 ms
19,664 KB
testcase_11 AC 231 ms
20,936 KB
testcase_12 AC 209 ms
20,648 KB
testcase_13 AC 199 ms
19,624 KB
testcase_14 AC 125 ms
19,400 KB
testcase_15 AC 209 ms
19,828 KB
testcase_16 AC 233 ms
25,420 KB
testcase_17 AC 238 ms
25,572 KB
testcase_18 AC 245 ms
25,012 KB
testcase_19 AC 229 ms
24,728 KB
testcase_20 AC 407 ms
25,216 KB
testcase_21 AC 419 ms
25,416 KB
testcase_22 AC 396 ms
24,904 KB
testcase_23 AC 397 ms
25,116 KB
testcase_24 AC 382 ms
25,628 KB
testcase_25 AC 343 ms
25,096 KB
testcase_26 AC 273 ms
25,956 KB
testcase_27 AC 243 ms
25,008 KB
testcase_28 AC 287 ms
25,356 KB
testcase_29 AC 246 ms
24,696 KB
testcase_30 AC 258 ms
25,800 KB
testcase_31 AC 239 ms
26,556 KB
testcase_32 AC 258 ms
32,292 KB
testcase_33 AC 262 ms
28,808 KB
testcase_34 AC 280 ms
24,056 KB
testcase_35 AC 282 ms
25,536 KB
testcase_36 AC 402 ms
25,164 KB
testcase_37 AC 249 ms
29,860 KB
testcase_38 AC 235 ms
26,980 KB
testcase_39 AC 379 ms
24,684 KB
testcase_40 AC 247 ms
24,900 KB
testcase_41 AC 273 ms
25,080 KB
testcase_42 AC 316 ms
25,688 KB
testcase_43 AC 274 ms
24,216 KB
testcase_44 AC 140 ms
21,140 KB
testcase_45 AC 256 ms
43,644 KB
testcase_46 AC 181 ms
43,592 KB
testcase_47 AC 180 ms
41,984 KB
testcase_48 AC 295 ms
24,756 KB
testcase_49 AC 295 ms
24,780 KB
testcase_50 AC 265 ms
24,972 KB
testcase_51 AC 292 ms
24,800 KB
testcase_52 AC 233 ms
24,972 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
 
using namespace std;
 
#define DEBUG(x) cerr<<#x<<": "<<x<<endl;
#define DEBUG_VEC(v) cerr<<#v<<":";for(int i=0;i<v.size();i++) cerr<<" "<<v[i]; cerr<<endl;
#define DEBUG_MAT(v) cerr<<#v<<endl;for(int i=0;i<v.size();i++){for(int j=0;j<v[i].size();j++) {cerr<<v[i][j]<<" ";}cerr<<endl;}
typedef long long ll;
#define int ll
 
#define vi vector<int>
#define vl vector<ll>
#define vii vector< vector<int> >
#define vll vector< vector<ll> >
#define vs vector<string>
#define pii pair<int,int>
#define pis pair<int,string>
#define psi pair<string,int>
#define pll pair<ll,ll>
template<class S, class T> pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first + t.first, s.second + t.second); }
template<class S, class T> pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first - t.first, s.second - t.second); }
template<class S, class T> ostream& operator<<(ostream& os, pair<S, T> p) { os << "(" << p.first << ", " << p.second << ")"; return os; }
#define X first
#define Y second
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define rrep1(i,n) for(int i=(int)(n);i>0;i--)
#define REP(i,a,b) for(int i=a;i<b;i++)
#define in(x, a, b) (a <= x && x < b)
#define all(c) c.begin(),c.end()
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a = b; return 1; } return 0; }
#define UNIQUE(v) v.erase(std::unique(v.begin(), v.end()), v.end());
const ll inf = 1000000001;
const ll INF = (ll)1e18 + 1;
const long double pi = 3.1415926535897932384626433832795028841971L;
#define Sp(p) cout<<setprecision(25)<< fixed<<p<<endl;
//int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
//int dx2[8] = { 1,1,0,-1,-1,-1,0,1 }, dy2[8] = { 0,1,1,1,0,-1,-1,-1 };
vi dx = {1, 0, -1, 0}, dy = {0, 1, 0, -1};
vi dx2 = { 1,1,0,-1,-1,-1,0,1 }, dy2 = { 0,1,1,1,0,-1,-1,-1 };
#define fio() cin.tie(0); ios::sync_with_stdio(false);
const ll MOD = 1000000007;
//const ll MOD = 998244353;
// #define mp make_pair
//#define endl '\n'

vector<vector<vector<pii>>> G(3);
vii visited;

bool dfs(int now, int from, int state, int turn) {
    visited[now][state] = turn;
    vi idx;
    if (state == 0) idx = {0, 1, 2};
    else if (state == 1) idx = {0, 1};
    else if (state == 2) idx = {0, 2};

    for (int i: idx) {
        int nstate;
        if (i == 0) nstate = state;
        else nstate = i;
        for (pii v: G[i][now]) {
            if (v.second == from) continue;
            if (visited[v.first][nstate] == turn) return true;
            if (visited[v.first][nstate] == 0) {
                if (dfs(v.first, v.second, nstate, turn)) return true;
            }
        }
    }
    visited[now][state] = -1;
    return false;
}

signed main() {
    int n, m;
    cin >> n >> m;
    rep (i, 3) {
        G[i].resize(n);
    }
    visited.resize(n);
    rep (i, n) {
        visited[i].resize(3);
        rep (j, 3) visited[i][j] = 0;
    }

    rep (i, m) {
        int u, v, t;
        cin >> u >> v >> t;
        u--; v--;
        if (t == 1) {
            G[0][u].push_back(pii(v, i));
            G[0][v].push_back(pii(u, i));
        }
        else {
            G[1][u].push_back(pii(v, i));
            G[2][v].push_back(pii(u, i + m));
        }
    }

    // rep (i, 3) {
    //     DEBUG(i);
    //     rep (u, n) {
    //         cout << u << ": ";
    //         for (pii v: G[i][u]) {
    //             cout << v << " ";
    //         }
    //         cout << endl;
    //     }
    // }

    int turn = 1;
    rep (i, n) {
        if (visited[i][0] != 0) continue;
        if (dfs(i, -1, 0, turn)) {
            //DEBUG_MAT(visited);
            cout << "Yes" << endl;
            return 0;
        }
        //DEBUG_MAT(visited);
        turn++;
    }
    cout << "No" << endl;
}
0