結果

問題 No.1023 Cyclic Tour
ユーザー tarattata1tarattata1
提出日時 2020-03-07 08:51:36
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,170 bytes
コンパイル時間 1,354 ms
コンパイル使用メモリ 95,140 KB
実行使用メモリ 20,608 KB
最終ジャッジ日時 2024-04-23 19:04:22
合計ジャッジ時間 7,767 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 27 ms
7,680 KB
testcase_05 AC 27 ms
7,936 KB
testcase_06 AC 32 ms
8,192 KB
testcase_07 AC 30 ms
7,808 KB
testcase_08 AC 61 ms
12,800 KB
testcase_09 AC 61 ms
12,544 KB
testcase_10 AC 60 ms
12,416 KB
testcase_11 WA -
testcase_12 AC 77 ms
14,592 KB
testcase_13 AC 70 ms
13,824 KB
testcase_14 AC 71 ms
13,824 KB
testcase_15 AC 78 ms
14,208 KB
testcase_16 AC 148 ms
20,224 KB
testcase_17 AC 159 ms
20,480 KB
testcase_18 AC 151 ms
20,352 KB
testcase_19 AC 148 ms
19,840 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 87 ms
13,440 KB
testcase_26 AC 77 ms
10,624 KB
testcase_27 AC 60 ms
9,088 KB
testcase_28 AC 136 ms
18,816 KB
testcase_29 AC 143 ms
19,584 KB
testcase_30 AC 138 ms
18,944 KB
testcase_31 AC 120 ms
17,536 KB
testcase_32 AC 132 ms
18,432 KB
testcase_33 AC 129 ms
18,304 KB
testcase_34 AC 44 ms
8,704 KB
testcase_35 AC 47 ms
8,960 KB
testcase_36 WA -
testcase_37 AC 116 ms
17,152 KB
testcase_38 AC 144 ms
19,200 KB
testcase_39 WA -
testcase_40 AC 128 ms
16,640 KB
testcase_41 AC 127 ms
16,768 KB
testcase_42 AC 76 ms
10,112 KB
testcase_43 AC 138 ms
18,432 KB
testcase_44 AC 27 ms
7,936 KB
testcase_45 AC 80 ms
20,352 KB
testcase_46 AC 92 ms
20,608 KB
testcase_47 AC 28 ms
8,064 KB
testcase_48 WA -
testcase_49 WA -
testcase_50 AC 53 ms
9,216 KB
testcase_51 AC 53 ms
9,344 KB
testcase_52 AC 53 ms
9,216 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;

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

void solve()
{
    int n,m;
    scanf("%d%d", &n, &m);
    g.resize(n);
    vis.resize(n);
    int i;
    for(i=0; i<n; i++) {
        vis[i]=-1;
    }
    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]>=0) continue;
        bool ret=dfs(i,i);
        if(ret){
            printf("Yes\n");
            return;
        }
    }
    printf("No\n");
    return;
}


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