結果

問題 No.3508 OR Mapping
コンテスト
ユーザー GOTKAKO
提出日時 2026-04-18 01:16: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
結果
WA  
実行時間 -
コード長 2,452 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,920 ms
コンパイル使用メモリ 233,828 KB
実行使用メモリ 292,740 KB
最終ジャッジ日時 2026-04-18 01:17:02
合計ジャッジ時間 22,325 ms
ジャッジサーバーID
(参考情報)
judge3_1 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 54 WA * 11
権限があれば一括ダウンロードができます

ソースコード

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<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);
                }
            }
        }
        swap(Graph,G);
    }
    if(N == 1){cout << "No\n"; return 0;}

    bool yes = true;
    for(int i=0; i<n-1; i++){
        bool ok = false;
        for(auto to : Graph.at(i)) if(to == i+1) ok = true;
        if(!ok && S.cycle.at(i).size() == 1 && S.cycle.at(i+1).size() == 1 && S.cycle.at(i).at(0)%N == S.cycle.at(i+1).at(0)%N) ok = true;
        if(!ok){yes = false; break;}
    }
    if(yes) cout << "Yes\n";
    else cout << "No\n";
}
    
0