#include using namespace std; #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #define INT(...) \ int __VA_ARGS__; \ IN(__VA_ARGS__) #define LL(...) \ ll __VA_ARGS__; \ IN(__VA_ARGS__) #define STR(...) \ string __VA_ARGS__; \ IN(__VA_ARGS__) #define CHR(...) \ char __VA_ARGS__; \ IN(__VA_ARGS__) #define DBL(...) \ double __VA_ARGS__; \ IN(__VA_ARGS__) #define ll long long #define yes cout<<"Yes"<<"\n" #define no cout<<"No"<<"\n" #define rep(i,a,b) for(int i=a;i=b;i--) #define fore(i,a) for(auto &i:a) #define all(x) (x).begin(),(x).end() #define allr(x) (x).rbegin(),(x).rend() #define SUM(v) accumulate(all(v), 0LL) #define MIN(v) *min_element(all(v)) #define MAX(v) *max_element(all(v)) #define lb(c, x) distance((c).begin(), lower_bound(all(c), (x))) #define ub(c, x) distance((c).begin(), upper_bound(all(c), (x))) #define pii pair #define pll pair #define pb push_back #define eb emplace_back #define ff first #define ss second #define vi vector #define vll vector #define vc vector #define vvi vector> #define vec(type, name, ...) vector name(__VA_ARGS__) #define VEC(type, name, size) \ vector name(size); \ IN(name) int scan() { return getchar(); } void scan(int &a) { cin >> a; } void scan(long long &a) { cin >> a; } void scan(char &a) { cin >> a; } void scan(double &a) { cin >> a; } void scan(string &a) { cin >> a; } template void scan(pair &p) { scan(p.first), scan(p.second); } template void scan(vector &); template void scan(vector &a) { for(auto &i : a) scan(i); } template void scan(T &a) { cin >> a; } void IN() {} template void IN(Head &head, Tail &...tail) { scan(head); IN(tail...); } template void print(const T &a) { cout << a; } void OUT() { cout << endl; } template void OUT(const Head &head, const Tail &...tail) { print(head); if(sizeof...(tail)) cout << ' '; OUT(tail...); } #define vv(type, name, h, ...) vector> name(h, vector(__VA_ARGS__)) #define VV(type, name, h, w) \ vector> name(h, vector(w)); \ IN(name) #define vvv(type, name, h, w, ...) vector>> name(h, vector>(w, vector(__VA_ARGS__))) #define vvvv(type, name, a, b, c, ...) \ vector>>> name(a, vector>>(b, vector>(c, vector(__VA_ARGS__)))) template using min_priority_queue = priority_queue, greater>; template pair operator-(const pair &x, const pair &y) { return pair(x.ff - y.ff, x.ss - y.ss); } template pair operator+(const pair &x, const pair &y) { return pair(x.ff + y.ff, x.ss + y.ss); } template pair operator&(const pair &l, const pair &r) { return pair(max(l.ff, r.ff), min(l.ss, r.ss)); } template vector &operator--(vector &v) { fore(e, v) e--; return v; } template vector operator--(vector &v, int) { auto res = v; fore(e, v) e--; return res; } template vector &operator++(vector &v) { fore(e, v) e++; return v; } template vector operator++(vector &v, int) { auto res = v; fore(e, v) e++; return res; } template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } #define UNIQUE(x) sort(all(x)), x.erase(unique(all(x)), x.end()) //座標圧縮 template void zip(vector &x) { vector y = x; UNIQUE(y); for(int i = 0; i < x.size(); ++i) { x[i] = lb(y, x[i]); } } template T ceil(T x, T y) { assert(y >= 1); return (x > 0 ? (x + y - 1) / y : x / y); } template T floor(T x, T y) { assert(y >= 1); return (x > 0 ? x / y : (x + y - 1) / y); } long long POW(long long x, int n) { long long res = 1LL; for(; n; n >>= 1, x *= x) if(n & 1) res *= x; return res; } //0^n=0 long long modpow(long long a, long long n, long long mod) { a%=mod; assert(a!=0||n!=0); if(a==0)return 0; long long res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } //return 0<=a&&a> x; return x; } ll lin() { unsigned long long x; cin >> x; return x; } long long sqrtll(long long x) { assert(x >= 0); long long rev = sqrt(x); while(rev * rev > x) --rev; while((rev+1) * (rev+1)<=x) ++rev; return rev; } int logN(long long n){ int ret=1; while((1LL<::max() / 2; const long long INFL = numeric_limits::max() / 2; #define inf INFINITY template void debug(vector a){ rep(i,0,(int)a.size()){ cout< class modint { using u64 = std::uint_fast64_t; public: u64 a; constexpr modint(const u64 x = 0) noexcept : a(x % Modulus) {} constexpr u64 &val() noexcept { return a; } constexpr const u64 &val() const noexcept { return a; } constexpr modint operator+(const modint rhs) const noexcept { return modint(*this) += rhs; } constexpr modint operator-(const modint rhs) const noexcept { return modint(*this) -= rhs; } constexpr modint operator*(const modint rhs) const noexcept { return modint(*this) *= rhs; } constexpr modint operator/(const modint rhs) const noexcept { return modint(*this) /= rhs; } constexpr modint &operator+=(const modint rhs) noexcept { a += rhs.a; if (a >= Modulus) { a -= Modulus; } return *this; } constexpr modint &operator-=(const modint rhs) noexcept { if (a < rhs.a) { a += Modulus; } a -= rhs.a; return *this; } constexpr modint &operator*=(const modint rhs) noexcept { a = a * rhs.a % Modulus; return *this; } constexpr modint &operator/=(modint rhs) noexcept { u64 exp = Modulus - 2; while (exp) { if (exp % 2) { *this *= rhs; } rhs *= rhs; exp /= 2; } return *this; } friend bool operator==(const modint& a,const modint& b) { return a.val()==b.val(); } friend bool operator!=(const modint& a,const modint& b) { return a.val()!=b.val(); } }; using mint9=modint<998244353>; using mint1=modint<1000000007>; //costを指定しないと重みなし辺になります struct Edge{ int from,to; ll cost; Edge()=default; Edge(int from,int to,ll cost=1):from(from),to(to),cost(cost){} operator int() const {return to;} }; constexpr pii dx4[4] = {pii{-1, 0},pii{0, -1}, pii{0, 1}, pii{1, 0} }; constexpr pii dx8[8] = {{1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}}; constexpr pii dx[100]={{1,0},{0,1},{1,1},{0,0}}; #define el "\n" #define endl "\n" #define fastio std::cin.sync_with_stdio(false);std::cin.tie(nullptr); template struct AHUAlgorithm{ map,int> name; map> rev; AHUAlgorithm(){} vector dfs(G& g,int root){ vector dist(g.size(),-1); stack st; dist[root]=0; for(int pos=root;; pos=st.top(),st.pop()){ for(auto& i:g[pos]){ if(dist[(int)i]!=-1)continue; dist[(int)i]=dist[pos]+1; st.push(i); } if(st.empty())break; } return dist; } bool dfs2(G& g,int pos,int pre,pair& c,int v2,int diam,int d){ if(pos==v2)return true; for(int &i:g[pos]){ if((int)i==pre)continue; if(dfs2(g,(int)i,pos,c,v2,diam,d+1)){ if(d==diam/2)c.first=pos; else if(d==(diam+1)/2)c.second=pos; return true; } } return false; } pair find_center(G& g){ vector d0=dfs(g,0); int v=distance(d0.begin(),max_element(d0.begin(),d0.end())); vector dv=dfs(g,v); int diam=*max_element(dv.begin(),dv.end()); pair res{-1,-1}; int v2=distance(dv.begin(),max_element(dv.begin(),dv.end())); dfs2(g,v,-1,res,v2,diam,0); return res; } int set_name(vector& v){ if(name.find(v)!=name.end())return name[v]; int n=name.size();name[v]=n;rev[n]=v; return n; } int get_name(G& g,int pos,vector& v,int pre=-1){ vector kt; for(auto& i:g[pos]){ if((int)i==pre)continue; kt.push_back(get_name(g,(int)i,v,pos)); } sort(kt.begin(),kt.end()); return v[pos]=set_name(kt); } bool Rooted_Tree_Isomorphism(G& g,int r1,G& h,int r2){ vector v1(g.size()),v2(h.size()); return get_name(g,r1,v1)==get_name(h,r2,v2); } //木g,hの同型判定を行う (同型ならtrue, そうでないならfalse) bool Tree_Isomorphism( G& g, G& h ){ pair c1=find_center(g),c2=find_center(h); if(c1.second!=-1 && c2.second!=-1){//お互い二つの中心を持つ return Rooted_Tree_Isomorphism(g,c1.first,h,c2.first) | Rooted_Tree_Isomorphism(g,c1.second,h,c2.first); }else if(c1.second==-1 && c2.second==-1){//お互い一つの中心を持つ return Rooted_Tree_Isomorphism(g,c1.first,h,c2.first); }else return false; } }; /* O(nlogn)で木の同型判定などを行える 参考:https://logic.pdmi.ras.ru/~smal/files/smal_jass08.pdf */ void solve(){ INT(n); vector> g(n); { rep(i,0,n-1){ INT(x,y,w);x--;y--; // x から y へ超頂点をw-1個追加する int pre=x; rep(i,0,w-1){ g[pre].pb(g.size()); g.pb({pre}); pre=(int)g.size()-1; } g[pre].pb(y); g[y].pb(pre); } } int sz=(int)g.size(); INT(h,w); VEC(string,s,h); vv(int,a,h,w,-1); { //頂点数が違うならNo int m=0; rep(i,0,h)rep(j,0,w){ if(s[i][j]=='.')continue; a[i][j]=m++; } if(m!=sz){ no; return; } } vector> t(sz); rep(i,0,h){ rep(j,0,w){ rep(k,0,4){ int x,y;tie(x,y)=pii(i,j)+dx4[k]; if(!ingrid(x,y,h,w))continue; if(s[x][y]=='.')continue; if(s[i][j]=='.')continue; t[a[i][j]].pb(a[x][y]); } } } AHUAlgorithm>> ahu; int r1,r2; { pii c1,c2; c1=ahu.find_center(g),c2=ahu.find_center(t); if(ahu.Rooted_Tree_Isomorphism(g,c1.first,t,c2.first)){ tie(r1,r2)=tie(c1.first,c2.first); } else if(c1.second!=-1 && c2.second!=-1 && ahu.Rooted_Tree_Isomorphism(g,c1.second,t,c2.first)){ tie(r1,r2)=tie(c1.ss,c2.ff); }else{ no; return ; } } vector v1(sz),v2(sz); ahu.get_name(g,r1,v1); ahu.get_name(t,r2,v2); vector mp(sz);//対応を構築 { auto dfs=[&](auto&& self,int pos1,int pre1,int pos2,int pre2)->void { vector kt1,kt2; fore(i,g[pos1]){ if(i==pre1)continue; kt1.pb({v1[i],i}); } fore(i,t[pos2]){ if(i==pre2)continue; kt2.pb({v2[i],i}); } sort(all(kt1));sort(all(kt2)); rep(i,0,(int)kt1.size()){ mp[kt2[i].ss]=kt1[i].ss; } fore(i,t[pos2]){ if(i==pre2)continue; self(self,mp[i],pos1,i,pos2); } }; mp[r2]=r1; dfs(dfs,r1,-1,r2,-1); } vector ans(n); rep(i,0,h){ rep(j,0,w){ if(s[i][j]=='.')continue; if(mp[a[i][j]]