結果

問題 No.3234 Infinite Propagation
ユーザー t98slider
提出日時 2025-08-15 22:02:33
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 64 ms / 2,000 ms
コード長 2,497 bytes
コンパイル時間 2,582 ms
コンパイル使用メモリ 224,448 KB
実行使用メモリ 16,960 KB
最終ジャッジ日時 2025-08-15 22:02:45
合計ジャッジ時間 3,608 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template <class T> struct csr {
    using itr = typename std::vector<T>::iterator;
    struct Node {
        itr st, en;
        itr begin() { return st; }
        itr end() { return en; }
        int size() { return en - st; }
        T operator[](int p){ return st[p]; }
    };
    const int N;
    std::vector<int> start;
    std::vector<T> E;
    std::vector<std::pair<int,T>> edge;
    csr(int n) : N(n), start(n + 1) {}
    void add_edge(int u, T v){
        assert(0 <= u && u < N);
        start[u + 1]++;
        edge.emplace_back(u, v);
    }
    void build(){
        E.resize(edge.size());
        for(int i = 0; i < N; i++) start[i + 1] += start[i];
        auto cnt = start;
        for(auto [u, v] : edge) E[cnt[u]++] = v;
    }
    Node operator[](int p) {
        return Node{E.begin() + start[p], E.begin() + start[p + 1]};
    }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;
    cin >> T;
    while(T--){
        int n;
        cin >> n;
        vector<pair<string,string>> b(n);
        vector<string> c;
        c.reserve(2 * n);
        for(auto &&[x, y] : b){
            cin >> x >> y;
            c.emplace_back(x);
            c.emplace_back(y);
        }
        sort(c.begin(), c.end());
        c.erase(unique(c.begin(), c.end()), c.end());
        if(c[0] != "a"){
            cout << "No\n";
            continue;
        }
        int m = c.size();
        csr<int> g(m);
        vector<int> S;
        int mx = 1 << 30;
        for(auto [x, y] : b){
            int u = lower_bound(c.begin(), c.end(), x) - c.begin();
            int v = lower_bound(c.begin(), c.end(), y) - c.begin();
            g.add_edge(u, v);
            if(x.find("a") == string::npos) mx = min(mx, (int)x.size());
        }
        g.build();
        sort(S.begin(), S.end());
        S.erase(unique(S.begin(), S.end()), S.end());
        auto dfs = [&](auto dfs, int v) -> bool {
            if(c[v].find("a") != string::npos) return true;
            if(c[v].size() > mx) return true;
            if(c[v].size() == mx){
                for(auto &&u : g[v]){
                    if(dfs(dfs, u)) return true;
                }
            }
            return false;
        };
        bool flg = false;
        for(auto u : g[0]){
            if(dfs(dfs, u)){
                flg = true;
                break;
            }
        }
        cout << (flg ? "Yes" : "No") << '\n';
    }
}
0