結果

問題 No.3508 OR Mapping
コンテスト
ユーザー Ian
提出日時 2026-04-19 19:18:59
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 350 ms / 2,000 ms
コード長 2,757 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,279 ms
コンパイル使用メモリ 345,444 KB
実行使用メモリ 70,720 KB
最終ジャッジ日時 2026-04-19 19:19:15
合計ジャッジ時間 15,748 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 65
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

int N, M, K;
vector<int> adj[500005], radj[500005];
bool visited[500005];
int comp_[500005], sz[500005], color_[500005];

int main(){
    scanf("%d %d %d", &N, &M, &K);
    for(int i = 0; i < M; i++){
        int u, v; scanf("%d %d", &u, &v);
        adj[u].push_back(v);
        radj[v].push_back(u);
    }
    
    vector<bool> R(N+1, false);
    R[1] = true;
    int cnt = 1;
    vector<int> stk = {1};
    while(!stk.empty()){
        int u = stk.back(); stk.pop_back();
        for(int v : adj[u]) if(!R[v]){ R[v] = true; cnt++; stk.push_back(v); }
    }
    if(cnt != N){ printf("No\n"); return 0; }
    
    vector<int> order_;
    visited[1] = true;
    vector<pair<int,int>> ds;
    ds.push_back({1, 0});
    while(!ds.empty()){
        auto& [u, i] = ds.back();
        if(i < (int)adj[u].size()){
            int v = adj[u][i++];
            if(!visited[v]){ visited[v] = true; ds.push_back({v, 0}); }
        } else {
            order_.push_back(u);
            ds.pop_back();
        }
    }
    
    int c = 0;
    for(int idx = (int)order_.size() - 1; idx >= 0; idx--){
        int u = order_[idx];
        if(comp_[u]) continue;
        c++;
        comp_[u] = c;
        int s = 1;
        vector<int> st = {u};
        while(!st.empty()){
            int x = st.back(); st.pop_back();
            for(int y : radj[x]) if(!comp_[y]){ comp_[y] = c; s++; st.push_back(y); }
        }
        sz[c] = s;
    }
    int num_scc = c;
    
    vector<bool> has_next(num_scc + 2, false);
    for(int u = 1; u <= N; u++){
        int cu = comp_[u];
        if(has_next[cu]) continue;
        for(int v : adj[u]) if(comp_[v] == cu + 1){ has_next[cu] = true; break; }
    }
    for(int i = 1; i < num_scc; i++) if(!has_next[i]){ printf("No\n"); return 0; }
    
    if(sz[1] == 1){ printf("No\n"); return 0; }
    
    for(int i = 2; i <= num_scc; i++){
        if(sz[i] == 1 && sz[i-1] == 1){ printf("No\n"); return 0; }
    }
    
    memset(color_, -1, sizeof(color_));
    for(int start = 1; start <= N; start++){
        if(color_[start] != -1) continue;
        int scc_id = comp_[start];
        if(sz[scc_id] == 1){ color_[start] = 0; continue; }
        color_[start] = 0;
        queue<int> q; q.push(start);
        bool has_odd = false;
        while(!q.empty()){
            int u = q.front(); q.pop();
            int cc = color_[u];
            int nc = 1 - cc;
            for(int v : adj[u]){
                if(comp_[v] != scc_id) continue;
                if(color_[v] == -1){ color_[v] = nc; q.push(v); }
                else if(color_[v] == cc) has_odd = true;
            }
        }
        if(!has_odd){ printf("No\n"); return 0; }
    }
    
    printf("Yes\n");
    return 0;
}
0