結果

問題 No.1023 Cyclic Tour
ユーザー Plan8Plan8
提出日時 2020-08-14 13:47:54
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 250 ms / 2,000 ms
コード長 3,403 bytes
コンパイル時間 2,614 ms
コンパイル使用メモリ 215,528 KB
実行使用メモリ 26,664 KB
最終ジャッジ日時 2024-04-18 18:33:24
合計ジャッジ時間 13,964 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 ms
5,376 KB
testcase_04 AC 111 ms
19,680 KB
testcase_05 AC 117 ms
19,876 KB
testcase_06 AC 142 ms
22,540 KB
testcase_07 AC 141 ms
20,844 KB
testcase_08 AC 108 ms
19,348 KB
testcase_09 AC 96 ms
19,020 KB
testcase_10 AC 104 ms
18,832 KB
testcase_11 AC 100 ms
19,632 KB
testcase_12 AC 82 ms
18,992 KB
testcase_13 AC 74 ms
18,132 KB
testcase_14 AC 82 ms
17,832 KB
testcase_15 AC 91 ms
18,252 KB
testcase_16 AC 148 ms
22,224 KB
testcase_17 AC 164 ms
22,436 KB
testcase_18 AC 154 ms
22,080 KB
testcase_19 AC 147 ms
21,312 KB
testcase_20 AC 225 ms
23,808 KB
testcase_21 AC 250 ms
24,064 KB
testcase_22 AC 195 ms
22,472 KB
testcase_23 AC 207 ms
23,116 KB
testcase_24 AC 161 ms
24,536 KB
testcase_25 AC 189 ms
23,680 KB
testcase_26 AC 186 ms
24,040 KB
testcase_27 AC 170 ms
23,444 KB
testcase_28 AC 199 ms
23,900 KB
testcase_29 AC 143 ms
22,932 KB
testcase_30 AC 184 ms
23,552 KB
testcase_31 AC 179 ms
23,080 KB
testcase_32 AC 158 ms
23,980 KB
testcase_33 AC 186 ms
23,944 KB
testcase_34 AC 164 ms
22,144 KB
testcase_35 AC 180 ms
24,064 KB
testcase_36 AC 194 ms
23,168 KB
testcase_37 AC 173 ms
23,352 KB
testcase_38 AC 158 ms
22,468 KB
testcase_39 AC 187 ms
22,456 KB
testcase_40 AC 168 ms
22,496 KB
testcase_41 AC 174 ms
22,476 KB
testcase_42 AC 212 ms
24,192 KB
testcase_43 AC 157 ms
22,936 KB
testcase_44 AC 133 ms
19,744 KB
testcase_45 AC 81 ms
26,664 KB
testcase_46 AC 70 ms
22,312 KB
testcase_47 AC 112 ms
25,312 KB
testcase_48 AC 125 ms
22,600 KB
testcase_49 AC 146 ms
22,176 KB
testcase_50 AC 130 ms
22,564 KB
testcase_51 AC 129 ms
22,472 KB
testcase_52 AC 136 ms
22,572 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef vector<vector<long long>> VVL;
typedef pair<int,int> P;
typedef tuple<int,int,int> tpl;

#define ALL(a)  (a).begin(),(a).end()
#define SORT(c) sort((c).begin(),(c).end())
#define REVERSE(c) reverse((c).begin(),(c).end())
#define EXIST(m,v) (m).find((v)) != (m).end()
#define LB(a,x) lower_bound((a).begin(), (a).end(), x) - (a).begin()
#define UB(a,x) upper_bound((a).begin(), (a).end(), x) - (a).begin()

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define RFOR(i,a,b) for(int i=(a)-1;i>=(b);--i)
#define RREP(i,n) RFOR(i,n,0)

#define en "\n"

constexpr double EPS = 1e-9;
constexpr double PI  = 3.1415926535897932;
constexpr int INF = 2147483647;
constexpr long long LINF = 1LL<<60;
constexpr long long MOD = 1000000007; // 998244353;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

struct Edge{
    int from, to, id=0;
    int cost=1;

    Edge(int from, int to, int cost=1, int id=0): from(from), to(to), cost(cost), id(id) {}
    Edge() {}

    friend istream &operator>>(istream  &input, Edge &e ){ 
        input >> e.from >> e.to;
        e.from--; e.to--;
        return input;            
    }

    Edge rev(void){
        return Edge(to, from, cost, id);
    }
};

vector<vector<Edge>> edge, re;
vector<bool> vis, done;
VI one_group, order, id;
VVI group;
int n,m;

void dfs1(int v){
    vis[v] = 1;
    for(auto& e : edge[v]){
        if(vis[e.to] || done[e.to]) continue;
        dfs1(e.to);
    }
    order.push_back(v);
    return;
}

void dfs2(int v){
    vis[v] = 0;
    for(auto& e : re[v]){
        if(vis[e.to]==0) continue;
        dfs2(e.to);
    }
    one_group.push_back(v);
    return;
}

void decomp(int v){
    dfs1(v);
    while(!order.empty()){
        if(vis[order.back()]==0){
            order.pop_back();
            continue;
        }
        dfs2(order.back());
        for(int u : one_group) done[u] = 1;
        group.emplace_back(one_group);
        one_group = {};
    }
    return;
}

bool dfs(int v){
    if(vis[v]==1){
        return true;
    }

    vis[v] = 1;

    for(auto& e : edge[v]){
        if(id[v] != id[e.to]) continue;
        if(done[e.id]) continue;
        done[e.id] = true;
        if(dfs(e.to)) return true;
    }

    return false;
}

void Main(){
    int N,M; cin >> N >> M; edge.resize(N);re.resize(N);
    REP(i,M){
        Edge e; int c; cin >> e >> c; e.id = i;
        edge[e.from].emplace_back(e); re[e.to].emplace_back(e.rev());
        if(c == 1){
            edge[e.to].emplace_back(e.rev());
            re[e.from].emplace_back(e);
        }
    }

    vis.resize(N,0);done.resize(N,0);
    REP(i,N) if(done[i]==0) decomp(i);

    id.resize(N);
    REP(i,group.size()){
        for(int v : group[i]) id[v] = i;
    }

    vis.assign(N,0);done.assign(M,0);
    REP(i,N){
        if(vis[i]) continue;
        if(dfs(i)){
            cout << "Yes" << en;
            return;
        }
    }
    cout << "No" << en;
    return;
}

int main(void){
    cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);cout<<fixed<<setprecision(15);
    int t=1; //cin>>t;
    REP(_,t) Main();
    return 0;
}
0