結果

問題 No.3508 OR Mapping
コンテスト
ユーザー GOTKAKO
提出日時 2026-04-18 02:17:39
言語 C++17
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
RE  
実行時間 -
コード長 2,949 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,188 ms
コンパイル使用メモリ 236,188 KB
実行使用メモリ 296,664 KB
最終ジャッジ日時 2026-04-18 02:19:13
合計ジャッジ時間 27,468 ms
ジャッジサーバーID
(参考情報)
judge3_1 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1 RE * 1
other AC * 58 WA * 5 RE * 2
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

class SCC{
    public:
    int siz = 0,name = 0,nowc = 0;
    vector<bool> already;
    vector<int> ord,low,belong,topo;
    vector<vector<int>> Graph,cycle;
    stack<int> St;
 
    void dfs(int pos){
        low.at(pos) = (ord.at(pos) = name++); St.push(pos);
        for(auto to : Graph.at(pos)){
            if(ord.at(to) == -1){
                dfs(to);
                low.at(pos) = min(low.at(pos),low.at(to));
            }
            else if(!already.at(to)) low.at(pos) = min(low.at(pos),ord.at(to));
        }
        if(low.at(pos) == ord.at(pos)){
            cycle.push_back({});
            while(true){
                int s = St.top(); St.pop();
                cycle.back().push_back(s);
                belong.at(s) = nowc; already.at(s) = true;
                if(s == pos) break;
            }
            nowc++;
        }
    }
 
    void make(vector<vector<int>> &G){
        Graph = G; siz = G.size();
        ord.resize(siz,-1); low.resize(siz,1001001001);
        already.resize(siz); belong.resize(siz);
 
        for(int i=0; i<siz; i++) if(ord.at(i) == -1) dfs(i);
        for(int i=0; i<siz; i++) belong.at(i) = (nowc-1)-belong.at(i);
        reverse(cycle.begin(),cycle.end());
    }
};

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int N,M,K; cin >> N >> M >> K;
    vector<vector<int>> Graph(N+N);
    for(int i=0; i<M; i++){
        int u,v; cin >> u >> v;
        u--; v--;
        Graph.at(u).push_back(v+N);
        Graph.at(u+N).push_back(v);
    }
    SCC S; S.make(Graph);
    int n = S.nowc;
    vector<int> in(n);
    {
        vector<vector<int>> G(n);
        vector<int> T(n,-1);
        for(int i=0; i<n; i++){
            for(auto pos : S.cycle.at(i)){
                for(auto to : Graph.at(pos)){
                    int k = S.belong.at(to);
                    if(i == k || T.at(k) == i) continue;
                    T.at(k) = i,G.at(i).push_back(k),in.at(k)++;
                }
            }
        }
        swap(Graph,G);
    }

    bool yes = true,inf = false;
    queue<int> Q;
    for(int i=0; i<n; i++) if(in.at(i) == 0) Q.push(i);
    while(Q.size()){
        if(Q.size() > 2){yes = false; break;}
        int pos = Q.front(); Q.pop();
        if(Q.size() == 1){
            int pos2 = Q.front(); Q.pop();
            if(S.cycle.at(pos).size() == 1 && S.cycle.at(pos2).size() == 1){
                if(S.cycle.at(pos).at(0)%N == S.cycle.at(pos2+1).at(0)%N && inf) inf = false;
                else yes = false;
            }   
            else yes = false;
            for(auto to : Graph.at(pos)) if(--in.at(to) == 0) Q.push(to);
            for(auto to : Graph.at(pos2)) if(--in.at(to) == 0) Q.push(to);
        }
        else{
            inf = true;
            for(auto to : Graph.at(pos)) if(--in.at(to) == 0) Q.push(to);
        }
    }

    if(yes) cout << "Yes\n";
    else cout << "No\n"; 
}
    
0