#ifdef NACHIA #define _GLIBCXX_DEBUG #else #define NDEBUG #endif #include #include #include #include using i64 = long long; using u64 = unsigned long long; #define rep(i,n) for(int i=0; i void chmin(A& l, const A& r){ if(r < l) l = r; } template void chmax(A& l, const A& r){ if(l < r) l = r; } using namespace std; #include using Modint = atcoder::static_modint<998244353>; #include #include namespace nachia{ template class CsrArray{ public: struct ListRange{ using iterator = typename std::vector::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::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 m_list; std::vector m_pos; public: CsrArray() : m_n(0), m_list(), m_pos() {} static CsrArray Construct(int n, std::vector> items){ CsrArray res; res.m_n = n; std::vector 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 list, std::vector 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>& edges, int undirected = false) : m_n(n), m_isUndir(undirected){ m_e.resize(edges.size()); for(std::size_t i=0; i static Graph Input(Cin& cin, int n, bool undirected, int m, int offset = 0){ Graph res(n, undirected, m); for(int i=0; i> 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& mapping){ assert(numVertices() == int(mapping.size())); for(int i=0; i induce(int num, const std::vector& mapping) const { int n = numVertices(); assert(n == int(mapping.size())); for(int i=0; i indexV(n), newV(num); for(int i=0; i= 0) indexV[i] = newV[mapping[i]]++; std::vector res; res.reserve(num); for(int i=0; i= 0) res[mapping[e.to]].addEdge(indexV[e.from], indexV[e.to]); return res; } CsrArray getEdgeIndexArray(bool undirected) const { std::vector> src; src.reserve(numEdges() * (undirected ? 2 : 1)); for(int i=0; i::Construct(numVertices(), src); } CsrArray getEdgeIndexArray() const { return getEdgeIndexArray(isUndirected()); } CsrArray getAdjacencyArray(bool undirected) const { std::vector> 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::Construct(numVertices(), src); } CsrArray getAdjacencyArray() const { return getAdjacencyArray(isUndirected()); } private: int m_n; std::vector m_e; bool m_isUndir; }; } // namespace nachia #include namespace nachia{ std::vector BfsDistance(const nachia::CsrArray& adj, const std::vector& start){ std::vector 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 UnitTreeDiameter(const CsrArray& T){ int n = T.size(); std::vector I(n, 0); std::vector 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 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 UnitTreeDiameter(const Graph& T){ return UnitTreeDiameter(T.getAdjacencyArray(true)); } std::vector UnitTreeMaxDistance(const Graph& T){ int n = T.numVertices(); auto adj = T.getAdjacencyArray(); auto diam = UnitTreeDiameter(adj); auto d0 = BfsDistance(adj, std::vector(1,diam.front())); auto d1 = BfsDistance(adj, std::vector(1,diam.back())); for(int i=0; i UnitTreeCenter(const CsrArray& 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 UnitTreeCenter(const Graph& T){ return UnitTreeCenter(T.getAdjacencyArray(true)); } } // namespace nachia namespace nachia{ namespace treetourlex_internal{ void sort_tg_by(CsrArray& tg, std::vector& by, int bound){ std::vector 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> 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 cnt2(tg.size()); for(auto [i,v] : bucket) tg[i][cnt2[i]++] = v; } std::vector coord_compress_from_arr_by(CsrArray& tg, std::vector& by, int bound){ int n = tg.size(); std::vector sorted_tg_idx; auto predicate_by_that = [&](int l, int r) -> bool { return by[l] == by[r]; }; std::vector sa_src; std::vector sa_recover; for(int i=0; i res(n); for(int i=1; i compressed; std::vector depth; CsrArray 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 parent(N, -1); std::vector bfs = {root}; bfs.reserve(N); depth[root] = 0; for(int i=0; i from_depth; { std::vector> elems; for(int i=0; i::Construct(max_depth+2, elems); } compressed.assign(N, 0); /* children_ordered */ { std::vector> edges; for(int p=0; p::Construct(N, edges); } for(int d = max_depth; d >= 0; d--){ auto vtxs = from_depth[d]; CsrArray children_ordered_part; { std::vector> 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::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 bfs = {root}; std::vector parent(N, -1); std::vector size(N, 1); bfs.reserve(N); for(int i=0; i=1; i--) size[parent[bfs[i]]] += size[bfs[i]]; std::vector pos(N, 0); std::vector brack(N, 0); for(int i=0; i invpos(N, 0); for(int i=0; i> 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 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 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> Map0; { vector> Id(H, vector(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 bfs0; bfs0.push_back(r0); vector 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 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; }