結果

問題 No.2981 Pack Tree into Grid
ユーザー 👑 NachiaNachia
提出日時 2024-12-05 00:24:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 31 ms / 2,000 ms
コード長 18,053 bytes
コンパイル時間 2,792 ms
コンパイル使用メモリ 128,740 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-12-05 00:24:33
合計ジャッジ時間 5,184 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 23 ms
5,248 KB
testcase_02 AC 23 ms
5,248 KB
testcase_03 AC 24 ms
5,248 KB
testcase_04 AC 24 ms
5,248 KB
testcase_05 AC 25 ms
5,248 KB
testcase_06 AC 29 ms
5,248 KB
testcase_07 AC 30 ms
5,248 KB
testcase_08 AC 31 ms
5,248 KB
testcase_09 AC 31 ms
5,248 KB
testcase_10 AC 22 ms
5,248 KB
testcase_11 AC 21 ms
5,248 KB
testcase_12 AC 22 ms
5,248 KB
testcase_13 AC 22 ms
5,248 KB
testcase_14 AC 24 ms
5,248 KB
testcase_15 AC 17 ms
5,248 KB
testcase_16 AC 17 ms
5,248 KB
testcase_17 AC 17 ms
5,248 KB
testcase_18 AC 17 ms
5,248 KB
testcase_19 AC 18 ms
5,248 KB
testcase_20 AC 18 ms
5,248 KB
testcase_21 AC 5 ms
5,248 KB
testcase_22 AC 5 ms
5,248 KB
testcase_23 AC 8 ms
5,248 KB
testcase_24 AC 17 ms
5,248 KB
testcase_25 AC 8 ms
5,248 KB
testcase_26 AC 9 ms
5,248 KB
testcase_27 AC 3 ms
5,248 KB
testcase_28 AC 6 ms
5,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifdef NACHIA
#define _GLIBCXX_DEBUG
#else
#define NDEBUG
#endif
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using i64 = long long;
using u64 = unsigned long long;
#define rep(i,n) for(int i=0; i<int(n); i++)
const i64 INF = 1001001001001001001;
template<typename A> void chmin(A& l, const A& r){ if(r < l) l = r; }
template<typename A> void chmax(A& l, const A& r){ if(l < r) l = r; }
using namespace std;
#include <atcoder/modint>
using Modint = atcoder::static_modint<998244353>;
#include <utility>
#include <cassert>

namespace nachia{

template<class Elem>
class CsrArray{
public:
    struct ListRange{
        using iterator = typename std::vector<Elem>::iterator;
        iterator begi, endi;
        iterator begin() const { return begi; }
        iterator end() const { return endi; }
        int size() const { return (int)std::distance(begi, endi); }
        Elem& operator[](int i) const { return begi[i]; }
    };
    struct ConstListRange{
        using iterator = typename std::vector<Elem>::const_iterator;
        iterator begi, endi;
        iterator begin() const { return begi; }
        iterator end() const { return endi; }
        int size() const { return (int)std::distance(begi, endi); }
        const Elem& operator[](int i) const { return begi[i]; }
    };
private:
    int m_n;
    std::vector<Elem> m_list;
    std::vector<int> m_pos;
public:
    CsrArray() : m_n(0), m_list(), m_pos() {}
    static CsrArray Construct(int n, std::vector<std::pair<int, Elem>> items){
        CsrArray res;
        res.m_n = n;
        std::vector<int> buf(n+1, 0);
        for(auto& [u,v] : items){ ++buf[u]; }
        for(int i=1; i<=n; i++) buf[i] += buf[i-1];
        res.m_list.resize(buf[n]);
        for(int i=(int)items.size()-1; i>=0; i--){
            res.m_list[--buf[items[i].first]] = std::move(items[i].second);
        }
        res.m_pos = std::move(buf);
        return res;
    }
    static CsrArray FromRaw(std::vector<Elem> list, std::vector<int> pos){
        CsrArray res;
        res.m_n = pos.size() - 1;
        res.m_list = std::move(list);
        res.m_pos = std::move(pos);
        return res;
    }
    ListRange operator[](int u) { return ListRange{ m_list.begin() + m_pos[u], m_list.begin() + m_pos[u+1] }; }
    ConstListRange operator[](int u) const { return ConstListRange{ m_list.begin() + m_pos[u], m_list.begin() + m_pos[u+1] }; }
    int size() const { return m_n; }
    int fullSize() const { return (int)m_list.size(); }
};

} // namespace nachia

namespace nachia{


struct Graph {
public:
    struct Edge{
        int from, to;
        void reverse(){ std::swap(from, to); }
        int xorval() const { return from ^ to; }
    };
    Graph(int n = 0, bool undirected = false, int m = 0) : m_n(n), m_e(m), m_isUndir(undirected) {}
    Graph(int n, const std::vector<std::pair<int, int>>& edges, int undirected = false) : m_n(n), m_isUndir(undirected){
        m_e.resize(edges.size());
        for(std::size_t i=0; i<edges.size(); i++) m_e[i] = { edges[i].first, edges[i].second };
    }
    template<class Cin>
    static Graph Input(Cin& cin, int n, bool undirected, int m, int offset = 0){
        Graph res(n, undirected, m);
        for(int i=0; i<m; i++){
            int u, v; cin >> u >> v;
            res[i].from = u - offset;
            res[i].to = v - offset;
        }
        return res;
    }
    int numVertices() const noexcept { return m_n; }
    int numEdges() const noexcept { return int(m_e.size()); }
    int addNode() noexcept { return m_n++; }
    int addEdge(int from, int to){ m_e.push_back({ from, to }); return numEdges() - 1; }
    Edge& operator[](int ei) noexcept { return m_e[ei]; }
    const Edge& operator[](int ei) const noexcept { return m_e[ei]; }
    Edge& at(int ei) { return m_e.at(ei); }
    const Edge& at(int ei) const { return m_e.at(ei); }
    auto begin(){ return m_e.begin(); }
    auto end(){ return m_e.end(); }
    auto begin() const { return m_e.begin(); }
    auto end() const { return m_e.end(); }
    bool isUndirected() const noexcept { return m_isUndir; }
    void reverseEdges() noexcept { for(auto& e : m_e) e.reverse(); }
    void contract(int newV, const std::vector<int>& mapping){
        assert(numVertices() == int(mapping.size()));
        for(int i=0; i<numVertices(); i++) assert(0 <= mapping[i] && mapping[i] < newV);
        for(auto& e : m_e){ e.from = mapping[e.from]; e.to = mapping[e.to]; }
        m_n = newV;
    }
    std::vector<Graph> induce(int num, const std::vector<int>& mapping) const {
        int n = numVertices();
        assert(n == int(mapping.size()));
        for(int i=0; i<n; i++) assert(-1 <= mapping[i] && mapping[i] < num);
        std::vector<int> indexV(n), newV(num);
        for(int i=0; i<n; i++) if(mapping[i] >= 0) indexV[i] = newV[mapping[i]]++;
        std::vector<Graph> res; res.reserve(num);
        for(int i=0; i<num; i++) res.emplace_back(newV[i], isUndirected());
        for(auto e : m_e) if(mapping[e.from] == mapping[e.to] && mapping[e.to] >= 0) res[mapping[e.to]].addEdge(indexV[e.from], indexV[e.to]);
        return res;
    }
    CsrArray<int> getEdgeIndexArray(bool undirected) const {
        std::vector<std::pair<int, int>> src;
        src.reserve(numEdges() * (undirected ? 2 : 1));
        for(int i=0; i<numEdges(); i++){
            auto e = operator[](i);
            src.emplace_back(e.from, i);
            if(undirected) src.emplace_back(e.to, i);
        }
        return CsrArray<int>::Construct(numVertices(), src);
    }
    CsrArray<int> getEdgeIndexArray() const { return getEdgeIndexArray(isUndirected()); }
    CsrArray<int> getAdjacencyArray(bool undirected) const {
        std::vector<std::pair<int, int>> src;
        src.reserve(numEdges() * (undirected ? 2 : 1));
        for(auto e : m_e){
            src.emplace_back(e.from, e.to);
            if(undirected) src.emplace_back(e.to, e.from);
        }
        return CsrArray<int>::Construct(numVertices(), src);
    }
    CsrArray<int> getAdjacencyArray() const { return getAdjacencyArray(isUndirected()); }
private:
    int m_n;
    std::vector<Edge> m_e;
    bool m_isUndir;
};

} // namespace nachia
#include <atcoder/string>

namespace nachia{
    
std::vector<int> BfsDistance(const nachia::CsrArray<int>& adj, const std::vector<int>& start){
    std::vector<int> dist(adj.size(), -1), bfs(adj.size());
    int p1 = 0;
    for(int s : start) if(dist[s] == -1){
        dist[s] = 0;
        bfs[p1++] = s;
    }
    for(int i=0; i<p1; i++){
        int p = bfs[i];
        for(int nx : adj[p]) if(dist[nx] == -1){
            dist[nx] = dist[p] + 1;
            bfs[p1++] = nx;
        }
    }
    return dist;
}

} // namespace nachia

namespace nachia{

// list of nodes through the diameter path
std::vector<int> UnitTreeDiameter(const CsrArray<int>& T){
    int n = T.size();
    std::vector<int> I(n, 0);
    std::vector<int> P(n, -1);
    auto ii = I.begin();
    for(int i=0; i<(int)I.size(); i++){
        int p = I[i];
        for(int e : T[p]) if(P[p] != e){
            P[e] = p;
            *++ii = e;
        }
    }
    P[I[n-1]] = -1;
    for(int i=n-1; i>=0; i--){
        int p = I[i];
        for(int e : T[p]) if(P[p] != e){
            P[e] = p;
            *--ii = e;
        }
    }
    std::vector<int> res = { I[0] };
    int sz = 0, szp = res[0];
    while(P[szp] != -1){ sz++; szp = P[szp]; }
    res.reserve(sz);
    while(P[res.back()] != -1){ res.push_back(P[res.back()]);}
    return res;
}

std::vector<int> UnitTreeDiameter(const Graph& T){
    return UnitTreeDiameter(T.getAdjacencyArray(true));
}

std::vector<int> UnitTreeMaxDistance(const Graph& T){
    int n = T.numVertices();
    auto adj = T.getAdjacencyArray();
    auto diam = UnitTreeDiameter(adj);
    auto d0 = BfsDistance(adj, std::vector<int>(1,diam.front()));
    auto d1 = BfsDistance(adj, std::vector<int>(1,diam.back()));
    for(int i=0; i<n; i++) if(d0[i] < d1[i]) d0[i] = d1[i];
    return d0;
}

} // namespace nachia

namespace nachia{

// size 1 : center is a node
// size 2 : center is an edge between them
std::vector<int> UnitTreeCenter(const CsrArray<int>& T){
    auto diameter = UnitTreeDiameter(T);
    if(diameter.size() % 2 == 1){
        return { diameter[diameter.size() / 2] };
    }
    return { diameter[diameter.size() / 2 - 1], diameter[diameter.size() / 2] };
}

std::vector<int> UnitTreeCenter(const Graph& T){
    return UnitTreeCenter(T.getAdjacencyArray(true));
}

} // namespace nachia


namespace nachia{

namespace treetourlex_internal{
    
void sort_tg_by(CsrArray<int>& tg, std::vector<int>& by, int bound){
    std::vector<int> cnt(bound + 1);
    for(int i=0; i<(int)tg.size(); i++) for(int v : tg[i]) cnt[by[v]]++;
    for(int i=0; i<bound; i++) cnt[i+1] += cnt[i];
    std::vector<std::pair<int,int>> bucket(cnt.back());
    for(int i=0; i<(int)tg.size(); i++) for(int v : tg[i]) bucket[--cnt[by[v]]] = std::make_pair(i,v);
    std::vector<int> cnt2(tg.size());
    for(auto [i,v] : bucket) tg[i][cnt2[i]++] = v;
}


std::vector<int> coord_compress_from_arr_by(CsrArray<int>& tg, std::vector<int>& by, int bound){
    int n = tg.size();

    std::vector<int> sorted_tg_idx;
    auto predicate_by_that = [&](int l, int r) -> bool { return by[l] == by[r]; };

    std::vector<int> sa_src;
    std::vector<int> sa_recover;
    for(int i=0; i<n; i++){
        sa_recover.push_back(i);
        for(auto a : tg[i]){
            sa_src.push_back(by[a] + 1);
            sa_recover.push_back(-1);
        }
        sa_src.push_back(0);
    }

    auto sa = atcoder::suffix_array(sa_src, bound);

    for(int i=0; i<(int)sa.size(); i++) if(sa_recover[sa[i]] != -1) sorted_tg_idx.push_back(sa_recover[sa[i]]);
    
    std::vector<int> res(n);
    for(int i=1; i<n; i++){
        res[sorted_tg_idx[i]] = res[sorted_tg_idx[i-1]];
        bool same = std::equal(tg[sorted_tg_idx[i-1]].begin(), tg[sorted_tg_idx[i-1]].end(), tg[sorted_tg_idx[i]].begin(), tg[sorted_tg_idx[i]].end(), predicate_by_that);
        if(!same) res[sorted_tg_idx[i]]++;
    }
    return res;
}

} // namespace internal



struct AHUAlgorithmLinearTime{

    int N;
    std::vector<int> compressed;
    std::vector<int> depth;
    CsrArray<int> children_ordered;
    int root;

    // O(N) time
    AHUAlgorithmLinearTime(const Graph& E, int new_root = 0){
        root = new_root;
        N = E.numVertices();
        auto adj = E.getAdjacencyArray();
        depth.assign(N, -1);
        std::vector<int> parent(N, -1);
        std::vector<int> bfs = {root};
        bfs.reserve(N);
        depth[root] = 0;
        for(int i=0; i<N; i++){
            int p = bfs[i];
            for(int e : adj[p]) if(depth[e] == -1){
                depth[e] = depth[p] + 1;
                parent[e] = p;
                bfs.push_back(e);
            }
        }

        int max_depth = *max_element(depth.begin(), depth.end());
        CsrArray<int> from_depth; {
            std::vector<std::pair<int,int>> elems;
            for(int i=0; i<N; i++) elems.push_back(std::make_pair(depth[i], i));
            from_depth = CsrArray<int>::Construct(max_depth+2, elems);
        }

        compressed.assign(N, 0);
        /* children_ordered */ {
            std::vector<std::pair<int,int>> edges;
            for(int p=0; p<N; p++) for(int c : adj[p]) if(depth[p] < depth[c]) edges.push_back(std::make_pair(p,c));
            children_ordered = CsrArray<int>::Construct(N, edges);
        }

        for(int d = max_depth; d >= 0; d--){
            auto vtxs = from_depth[d];
            CsrArray<int> children_ordered_part; {
                std::vector<std::pair<int,int>> elems;
                for(int i=0; i<(int)vtxs.size(); i++) for(auto p : children_ordered[vtxs[i]]) elems.push_back(std::make_pair(i,p));
                children_ordered_part = CsrArray<int>::Construct(vtxs.size(), elems);
            }
            treetourlex_internal::sort_tg_by(children_ordered_part, compressed, from_depth[d+1].size());
            auto compressed_part = treetourlex_internal::coord_compress_from_arr_by(children_ordered_part, compressed, from_depth[d+1].size());
            for(int i=0; i<(int)vtxs.size(); i++) for(int j=0; j<(int)children_ordered_part[i].size(); j++) children_ordered[vtxs[i]][j] = children_ordered_part[i][j];
            for(int i=0; i<(int)vtxs.size(); i++) compressed[vtxs[i]] = compressed_part[i];
        }
    }

    void secondary(){
        std::vector<int> bfs = {root};
        std::vector<int> parent(N, -1);
        std::vector<int> size(N, 1);
        bfs.reserve(N);
        for(int i=0; i<N; i++){
            int p = bfs[i];
            for(int e : children_ordered[p]){
                parent[e] = p;
                bfs.push_back(e);
            }
        }
        for(int i=N-1; i>=1; i--) size[parent[bfs[i]]] += size[bfs[i]];
        std::vector<int> pos(N, 0);
        std::vector<int> brack(N, 0);
        for(int i=0; i<N; i++){
            int p = bfs[i];
            int posv = pos[p] + 1;
            brack[pos[p] + size[p] - 1]++;
            for(int e : children_ordered[bfs[i]]){
                pos[e] = posv;
                posv += size[e];
            }
        }
        auto sa = atcoder::suffix_array(brack, N);
        auto lcp = atcoder::lcp_array(brack, sa);
        std::vector<int> invpos(N, 0);
        for(int i=0; i<N; i++) invpos[pos[i]] = i;
        compressed[invpos[sa[0]]] = 0;
        for(int i=1; i<N; i++){
            int prevtx = invpos[sa[i-1]];
            int vtx = invpos[sa[i]];
            compressed[vtx] = compressed[prevtx];
            if(lcp[i-1] < size[vtx]-1 || size[prevtx] != size[vtx]) compressed[vtx]++;
        }
    }

    static bool TreeIsomorphism(const Graph& a, const Graph& b){
        if(a.numVertices() != b.numVertices()) return false;
        int n = a.numVertices();
        if(n == 1) return true;
        int ca = UnitTreeCenter(a.getAdjacencyArray(true))[0];
        auto Cb = UnitTreeCenter(b.getAdjacencyArray(true));
        Graph g(n*2+1, true);
        for(auto e : a) g.addEdge(1 + e.from, 1 + e.to);
        for(auto e : b) g.addEdge(1 + n + e.from, 1 + n + e.to);
        for(auto cb : Cb){
            Graph g2 = g;
            g2.addEdge(0, 1 + ca);
            g2.addEdge(0, 1 + n + cb);
            auto ahu = AHUAlgorithmLinearTime(g2, 0).compressed;
            if(ahu[1 + ca] == ahu[1 + n + cb]) return true;
        }
        return false;
    }

    static vector<pair<int,int>> TreeIsomorphismGetRoot(const Graph& a, const Graph& b){
        if(a.numVertices() != b.numVertices()) return {};
        int n = a.numVertices();
        if(n == 1) return {{0,0}};
        int ca = UnitTreeCenter(a.getAdjacencyArray(true))[0];
        auto Cb = UnitTreeCenter(b.getAdjacencyArray(true));
        Graph g(n*2+1, true);
        for(auto e : a) g.addEdge(1 + e.from, 1 + e.to);
        for(auto e : b) g.addEdge(1 + n + e.from, 1 + n + e.to);
        for(auto cb : Cb){
            Graph g2 = g;
            g2.addEdge(0, 1 + ca);
            g2.addEdge(0, 1 + n + cb);
            auto ahu = AHUAlgorithmLinearTime(g2, 0).compressed;
            if(ahu[1 + ca] == ahu[1 + n + cb]) return {{ca,cb}};
        }
        return {};
    }
};



} // namespace nachia

void testcase(){
    i64 N; cin >> N;
    vector<i64> U(N-1), V(N-1), D(N-1);
    rep(i,N-1){ cin >> U[i] >> V[i] >> D[i]; U[i]--; V[i]--; }
    i64 H, W; cin >> H >> W;
    vector<string> S(H); rep(y,H) cin >> S[y];
    int N2 = 0; rep(y,H) rep(x,W) if(S[y][x] == '#') N2++;

    nachia::Graph T0(N2, true);
    vector<pair<int,int>> Map0;
    {
        vector<vector<int>> Id(H, vector<int>(W));
        int idi = 0;
        rep(y,H) rep(x,W) if(S[y][x] == '#') Id[y][x] = idi++;
        rep(y,H) rep(x,W-1) if(S[y][x] == '#' && S[y][x+1] == '#'){
            T0.addEdge(Id[y][x], Id[y][x+1]);
        }
        rep(y,H-1) rep(x,W) if(S[y][x] == '#' && S[y+1][x] == '#'){
            T0.addEdge(Id[y][x], Id[y+1][x]);
        }
        rep(y,H) rep(x,W) if(S[y][x] == '#') Map0.push_back({y,x});
    }
    
    nachia::Graph T1(N2, true);
    {
        int idi = N;
        rep(e,N-1){
            int u = U[e];
            rep(d,D[e]-1){
                int v = idi++;
                if(v >= N2){ cout << "No\n"; return; }
                T1.addEdge(u, v);
                u = v;
            }
            T1.addEdge(u, V[e]);
        }
        if(idi != N2){ cout << "No\n"; return; }
    }

    auto root = nachia::AHUAlgorithmLinearTime::TreeIsomorphismGetRoot(T0, T1);
    if(root.empty()){ cout << "No\n"; return; }
    auto [r0, r1] = root[0];

    auto T0x = nachia::AHUAlgorithmLinearTime(T0, r0);
    auto T1x = nachia::AHUAlgorithmLinearTime(T1, r1);

    //cout << "N2 = " << N2 << endl;
//
    //for(auto [u,v] : T0) cout << u << " " << v << endl;
//
    //cout << "##" << endl;
//
    //for(auto [u,v] : T1) cout << u << " " << v << endl;
//
    //cout << T0x.children_ordered.fullSize() << endl;
//
    //cout << T1x.children_ordered.fullSize() << endl;
//
    //cout << "##" << endl;
//
    //cout << "r0 = " << r0 << " , r1 = " << r1 << endl;

    //rep(u,N2){
    //    cout << u << " : ";
    //    for(auto v : T0x.children_ordered[u]) cout << v << " ";
    //    cout << endl;
    //}

    vector<int> bfs0; bfs0.push_back(r0);
    vector<int> bfs1; bfs1.push_back(r1);
    rep(i,bfs0.size()){
        //cout << "i = " << i << endl;
        int u0 = bfs0[i];
        int u1 = bfs1[i];
        for(auto v : T0x.children_ordered[u0]){
            bfs0.push_back(v);
        }
        for(auto v : T1x.children_ordered[u1]){
            bfs1.push_back(v);
        }
    }

    //cout << "##" << endl;


    vector<int> mapping(N2);
    rep(i,N2) mapping[bfs1[i]] = bfs0[i];

    //cout << "##" << endl;


    cout << "Yes\n";
    rep(i,N){
        auto [y,x] = Map0[mapping[i]];
        cout << (y+1) << ' ' << (x+1) << '\n';
    }
}

int main(){
    ios::sync_with_stdio(false); cin.tie(nullptr);
    i64 Q = 0; cin >> Q;
    rep(qi,Q) testcase();
    return 0;
}
0