結果

問題 No.1023 Cyclic Tour
ユーザー tarattata1tarattata1
提出日時 2020-03-07 09:31:38
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 171 ms / 2,000 ms
コード長 3,153 bytes
コンパイル時間 1,088 ms
コンパイル使用メモリ 95,084 KB
実行使用メモリ 20,864 KB
最終ジャッジ日時 2024-04-23 19:04:54
合計ジャッジ時間 7,612 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 28 ms
8,320 KB
testcase_05 AC 28 ms
8,192 KB
testcase_06 AC 32 ms
8,576 KB
testcase_07 AC 29 ms
8,320 KB
testcase_08 AC 62 ms
13,056 KB
testcase_09 AC 62 ms
12,928 KB
testcase_10 AC 61 ms
12,928 KB
testcase_11 AC 69 ms
13,312 KB
testcase_12 AC 83 ms
14,976 KB
testcase_13 AC 74 ms
14,208 KB
testcase_14 AC 73 ms
14,208 KB
testcase_15 AC 79 ms
14,592 KB
testcase_16 AC 156 ms
20,556 KB
testcase_17 AC 162 ms
20,864 KB
testcase_18 AC 171 ms
20,736 KB
testcase_19 AC 153 ms
20,352 KB
testcase_20 AC 83 ms
12,672 KB
testcase_21 AC 91 ms
13,440 KB
testcase_22 AC 110 ms
16,128 KB
testcase_23 AC 123 ms
17,152 KB
testcase_24 AC 154 ms
19,712 KB
testcase_25 AC 91 ms
13,952 KB
testcase_26 AC 76 ms
11,008 KB
testcase_27 AC 60 ms
9,344 KB
testcase_28 AC 148 ms
19,200 KB
testcase_29 AC 152 ms
19,968 KB
testcase_30 AC 144 ms
19,168 KB
testcase_31 AC 127 ms
17,920 KB
testcase_32 AC 142 ms
20,096 KB
testcase_33 AC 141 ms
19,072 KB
testcase_34 AC 46 ms
8,960 KB
testcase_35 AC 50 ms
9,472 KB
testcase_36 AC 104 ms
15,360 KB
testcase_37 AC 127 ms
18,048 KB
testcase_38 AC 142 ms
19,584 KB
testcase_39 AC 123 ms
16,512 KB
testcase_40 AC 125 ms
17,152 KB
testcase_41 AC 119 ms
17,152 KB
testcase_42 AC 75 ms
10,496 KB
testcase_43 AC 140 ms
18,944 KB
testcase_44 AC 28 ms
8,320 KB
testcase_45 AC 86 ms
20,864 KB
testcase_46 AC 95 ms
20,864 KB
testcase_47 AC 29 ms
8,448 KB
testcase_48 AC 56 ms
9,728 KB
testcase_49 AC 57 ms
9,600 KB
testcase_50 AC 53 ms
9,600 KB
testcase_51 AC 53 ms
9,728 KB
testcase_52 AC 55 ms
9,472 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <iterator>
#include <assert.h>
#pragma warning(disable:4996) 

typedef long long ll;
#define MIN(a, b) ((a)>(b)? (b): (a))
#define MAX(a, b) ((a)<(b)? (b): (a))
#define LINF 9223300000000000000
#define INF 2140000000
const long long MOD = 1000000007;
//const long long MOD = 998244353;

using namespace std;

class UF
{
private:
    int         num;
    vector<int> par;
    vector<int> siz;
public:
    //vector<int> val;
public:
    UF(int n): num(n) {
        par.resize(n);
        siz.resize(n);
        //val.resize(n);
        int i;
        for(i=0; i<n; i++) {
            par[i]=i; siz[i]=1;
            //val[i]=0;
        }
    }
	
    int find(int x) {
        if(x==par[x]) return x;
        return par[x] = find(par[x]);
    }

    void unite(int x, int y) {
        x=find(x);
        y=find(y);
        if(x==y) return;
        if(siz[x]<siz[y]) {
            par[x]=y;
            siz[y]=siz[x]+siz[y];
            //val[y]=val[x]+val[y];
        }
        else {
            par[y]=x;
            siz[x]=siz[x]+siz[y];
            //val[x]=val[x]+val[y];
        }
    }

    bool same(int x, int y) {
        return find(x)==find(y);
    }

    int size(int x) {
        return siz[find(x)];
    }

    int ngroup() {
    //int ngroup( int& ans ) {
        int count=0;
        int i;
        for(i=0; i<num; i++) {
            if(par[i]==i) {
                count++;
                //ans += (val[i]? siz[i]: siz[i]-1);
            }
        }
        return count;
    }
};

vector<vector<int> > g;   // to
vector<int> vis;
vector<int> fin;

bool dfs(int curr)
{
    assert(vis[curr]==0);
    vis[curr]=1;
    int siz=(int)g[curr].size();
    int i;
    for(i=0; i<siz; i++) {
        int next=g[curr][i];
        if(vis[next] && fin[next]==0) {
            return true;
        }
        if(vis[next]) {
            continue;
        }
        if(dfs(next)) {
            return true;
        }
    }
    fin[curr]=1;
    return false;
}

void solve()
{
    int n,m;
    scanf("%d%d", &n, &m);
    g.resize(n);
    vis.resize(n); fin.resize(n);
    int i;
    vector<int> a(m),b(m),c(m);
    for(i=0; i<m; i++) {
        scanf("%d%d%d", &a[i], &b[i], &c[i]); a[i]--; b[i]--;
    }
    UF uf(n);
    for(i=0; i<m; i++) {
        if(c[i]==1) {
            if (uf.same(a[i], b[i])) {
                 printf("Yes\n"); return;
            }
            uf.unite(a[i],b[i]);
        }
    }
    set<pair<int,int> > s;
    for(i=0; i<m; i++) {        
        if(c[i]==2) {
            int tmp0=uf.find(a[i]), tmp1=uf.find(b[i]);
            if(s.insert(make_pair(tmp0,tmp1)).second) {
                g[tmp0].push_back(tmp1);
            }
        }
    }

    for(i=0; i<n; i++) {
        if(vis[i]) continue;
        bool ret=dfs(i);
        if(ret){
            printf("Yes\n");
            return;
        }
    }
    printf("No\n");
    return;
}


int main(int argc, char* argv[])
{
	solve();
	return 0;
}
0