結果

問題 No.3508 OR Mapping
コンテスト
ユーザー ,
提出日時 2026-04-19 13:04:31
言語 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,510 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,765 ms
コンパイル使用メモリ 232,480 KB
実行使用メモリ 91,188 KB
最終ジャッジ日時 2026-04-19 13:04:59
合計ジャッジ時間 24,805 ms
ジャッジサーバーID
(参考情報)
judge3_1 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 55 WA * 10
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

const int MAXN = 500005;
int N, M, K;
vector<int> g[MAXN], rg[MAXN];
int comp[MAXN], num_scc;
vector<int> order;

void dfs1(int start) {
    comp[start] = -2;
    stack<pair<int,int>> st;
    st.push({start, 0});
    while (!st.empty()) {
        auto& [v, idx] = st.top();
        if (idx < (int)g[v].size()) {
            int u = g[v][idx++];
            if (comp[u] == -1) { comp[u] = -2; st.push({u, 0}); }
        } else { order.push_back(v); st.pop(); }
    }
}

void dfs2(int start, int c) {
    comp[start] = c;
    stack<int> st; st.push(start);
    while (!st.empty()) {
        int v = st.top(); st.pop();
        for (int u : rg[v]) if (comp[u] == -2) { comp[u] = c; st.push(u); }
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> N >> M >> K;
    for (int i = 0; i < M; i++) {
        int u, v; cin >> u >> v;
        g[u].push_back(v);
        rg[v].push_back(u);
    }

    fill(comp+1, comp+N+1, -1);
    for (int i = 1; i <= N; i++) if (comp[i] == -1) dfs1(i);
    num_scc = 0;
    for (int i = order.size()-1; i >= 0; i--)
        if (comp[order[i]] == -2) dfs2(order[i], num_scc++);

    vector<bool> reach(N+1, false);
    {
        queue<int> q; reach[1] = true; q.push(1);
        while (!q.empty()) {
            int v = q.front(); q.pop();
            for (int u : g[v]) if (!reach[u]) { reach[u] = true; q.push(u); }
        }
    }
    for (int i = 1; i <= N; i++) if (!reach[i]) { cout << "No\n"; return 0; }

    vector<vector<int>> sccs(num_scc);
    for (int i = 1; i <= N; i++) sccs[comp[i]].push_back(i);

    vector<int> scc_gcd(num_scc, 0);
    for (int c = 0; c < num_scc; c++) {
        unordered_map<int,int> dist;
        for (int v : sccs[c]) dist[v] = -1;
        int root = sccs[c][0];
        for (int u : g[root]) if (u == root) scc_gcd[c] = 1;
        if (sccs[c].size() == 1) continue;
        dist[root] = 0;
        queue<int> q; q.push(root);
        while (!q.empty()) {
            int v = q.front(); q.pop();
            for (int u : g[v]) {
                if (!dist.count(u)) continue;
                if (dist[u] == -1) { dist[u] = dist[v]+1; q.push(u); }
                else { int cy = dist[v]+1-dist[u]; if (cy > 0) scc_gcd[c] = __gcd(scc_gcd[c], cy); }
            }
        }
    }

    for (int c = 0; c < num_scc; c++) {
        if (scc_gcd[c] == 0 || scc_gcd[c] % 2 == 0) {
            cout << "No\n"; return 0;
        }
    }
    cout << "Yes\n";
    return 0;
}
0