using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; class TEST{ static void Main(){ Sol mySol =new Sol(); mySol.Solve(); } } class Sol{ public void Solve(){ root = 0; lca_init(); nNode = new int[N]; dfs_cnt(); EHeavy = new int[N]; for(int i=0;i>(); dfs_HLDec(); int[] e2n = new int[N-1]; for(int i=0;i[] ST = new SegTree[NHL]; for(int i=0;i(HLDec[i].Count, new Mat2x2(1, 0, 0, 1), (a, b) => a * b); ST[i].UniqInit(new Mat2x2(1, 0, 0, 1)); //ST[i].Dump(); } for(int q=0;q[] E; int[] A,B; List> HLDec; struct Triple{ public int node,compo,idx; public Triple(int n_,int c_,int i_){ node=n_;compo=c_;idx=i_; } } void dfs_HLDec(){ //Console.WriteLine("now={0},cmp={1},idx={2}",now,cmp,idx); Stack Stk=new Stack(); Stk.Push(new Triple(root,0,0)); Compo[0]=0; Idx[0]=0; HLDec.Add(new List()); HLDec[0].Add(0); //cmp=1; while(Stk.Count>0){ var p=Stk.Pop(); int now=p.node; int c=p.compo; int idx=p.idx; //Console.WriteLine(now+" "+c+" "+idx); int max=0;int trgt=-1; foreach(var nxt in E[now]){ if(nxt==parent[0][now])continue; if(max()); Compo[nxt]=HLDec.Count-1; Idx[nxt]=0; HLDec[Compo[nxt]].Add(nxt); Stk.Push(new Triple(nxt,Compo[nxt],0)); } if(nxt==EHeavy[now]){ Compo[nxt]=c; Idx[nxt]=idx+1; HLDec[Compo[nxt]].Add(nxt); Stk.Push(new Triple(nxt,c,idx+1)); } } } } class Pair{ public int node,depth; public Pair(int n_,int d_){ node=n_;depth=d_; } } void dfs_cnt(){ Pair[] P=new Pair[N]; for(int i=0;ix.depth>y.depth?-1:x.depthdepth[v])return lca(v,u); for(int i=0;i>i)&1) >0 ){ v=parent[i][v]; } } if(u==v)return u; for(int i=lmax-1;i>=0;i--){ if(parent[i][u]!=parent[i][v]){ u=parent[i][u]; v=parent[i][v]; } } return parent[0][u]; } void bfs(int strt,int dep_strt){ Queue Q=new Queue(); parent[0][strt]=-1;depth[strt]=0; Q.Enqueue(strt); while(Q.Count>0){ int now=Q.Dequeue(); int par=parent[0][now]; foreach(int v in E[now]){ if(v!=par){ parent[0][v]=now;depth[v]=depth[now]+1; Q.Enqueue(v); } } } } int Q; String[][] Qs; public Sol(){ N = ri(); E = new List[N]; for(int i=0;i(); A = new int[N-1]; B = new int[N-1]; for(int i=0;iint.Parse(e));} static long[] rla(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>long.Parse(e));} static double[] rda(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>double.Parse(e));} } struct Mat2x2{ long[] m; const long mod = (long) 1e9 + 7; public static Mat2x2 Unit = new Mat2x2(1,0,0,1); public long this[int r, int c]{ get{ return m[(r<<1) + c]; } set{ m[(r<<1) + c] = value; } } /* public Mat2x2(){ m = new long[4]; } */ public Mat2x2(long a = 0, long b = 0, long c = 0, long d = 0){ m = new long[]{a, b, c, d}; } public static Mat2x2 operator * (Mat2x2 ma, Mat2x2 mb){ var ret = new Mat2x2(0, 0, 0, 0); for(int i=0;i<2;i++){ for(int j=0;j<2;j++){ for(int k=0;k<2;k++){ ret[i, j] += ma[i, k] * mb[k, j]; ret[i, j] %= mod; } } } return ret; } public static long[] operator * (Mat2x2 ma, long[] v){ var ret = new long[2]; ret[0] = ma[0, 0] * v[0] + ma[0, 1] * v[1];// ret[0] %= mod; ret[1] = ma[1, 0] * v[0] + ma[1, 1] * v[1];// ret[1] %= mod; return ret; } public override String ToString(){ return String.Join(" ",m); } } class SegTree{ //segment Tree // 0-origin T[] Data; T Ini; Func Op; int N; int n; //コンストラクタ public SegTree(int n_,T ini_, Func op_){ N = n_; Ini = ini_; n = 1; while(n < n_) n *= 2; Data = new T[2*n - 1]; for(int i=0;i<2*n-1;i++) Data[i] = Ini; Op = op_; } //k-番目の値をaに変更 // 0 // 1 2 // 3 4 5 6 public void Update(int k,T a){ k += n-1; //上にはn-1個乗る Data[k] = a; while(k > 0){ k = (k-1) / 2; Data[k] = Op(Data[k*2+1],Data[k*2+2]); } } //Query; // [a,b) のOp(...)を返す // 後ろ3つの引数は k,l,r:ヒープのk-ノードが範囲 [l,r) に対応していることを示す // 外から呼ぶ時は Query(a,b,0,0,n) の形で呼ぶ public T Query(int a, int b){ return Query(a, b, 0, 0, n); } public T Query(int a,int b,int k,int l,int r){ // [a,b) ∩ [l,r) =φ ⇒Iniを返す if(r<=a || b<=l) return Ini; // [a,b) ⊃ [l,r) ⇒Data[k]を返す if(a<=l && r<=b)return Data[k]; // さもなくば、Op(l,r)を返す T vl = Query(a,b,k*2+1,l,(l+r)/2); T vr = Query(a,b,k*2+2,(l+r)/2,r); return Op(vl, vr); } public void UniqInit(T a){ for(int i=0;i=0;i--){ Data[i] = Op(Data[i*2+1], Data[i*2+2]); } } public void UniqUnit(T unit){ for(int i=0;i