| |
|
| |
|
| |
|
| |
|
| |
|
| | #include <algorithm>
|
| | #include <functional>
|
| | #include <numeric>
|
| | #include <iostream>
|
| | #include <iomanip>
|
| | #include <cstdio>
|
| | #include <cmath>
|
| | #include <complex>
|
| | #include <cstdlib>
|
| | #include <ctime>
|
| | #include <cstring>
|
| | #include <cassert>
|
| | #include <string>
|
| | #include <vector>
|
| | #include <list>
|
| | #include <map>
|
| | #include <set>
|
| | #include <deque>
|
| | #include <queue>
|
| | #include <stack>
|
| | #include <bitset>
|
| | #include <sstream>
|
| | using namespace std;
|
| |
|
| | #define LL long long
|
| | #define LD long double
|
| | #define PR pair<int,int>
|
| |
|
| | #define Fox(i,n) for (i=0; i<n; i++)
|
| | #define Fox1(i,n) for (i=1; i<=n; i++)
|
| | #define FoxI(i,a,b) for (i=a; i<=b; i++)
|
| | #define FoxR(i,n) for (i=(n)-1; i>=0; i--)
|
| | #define FoxR1(i,n) for (i=n; i>0; i--)
|
| | #define FoxRI(i,a,b) for (i=b; i>=a; i--)
|
| | #define Foxen(i,s) for (i=s.begin(); i!=s.end(); i++)
|
| | #define Min(a,b) a=min(a,b)
|
| | #define Max(a,b) a=max(a,b)
|
| | #define Sz(s) int((s).size())
|
| | #define All(s) (s).begin(),(s).end()
|
| | #define Fill(s,v) memset(s,v,sizeof(s))
|
| | #define pb push_back
|
| | #define mp make_pair
|
| | #define x first
|
| | #define y second
|
| |
|
| | template<typename T> T Abs(T x) { return(x<0 ? -x : x); }
|
| | template<typename T> T Sqr(T x) { return(x*x); }
|
| |
|
| | const int INF = (int)1e9;
|
| | const LD EPS = 1e-12;
|
| | const LD PI = acos(-1.0);
|
| |
|
| | bool Read(int &x)
|
| | {
|
| | char c,r=0,n=0;
|
| | x=0;
|
| | for(;;)
|
| | {
|
| | c=getchar();
|
| | if ((c<0) && (!r))
|
| | return(0);
|
| | if ((c=='-') && (!r))
|
| | n=1;
|
| | else
|
| | if ((c>='0') && (c<='9'))
|
| | x=x*10+c-'0',r=1;
|
| | else
|
| | if (r)
|
| | break;
|
| | }
|
| | if (n)
|
| | x=-x;
|
| | return(1);
|
| | }
|
| |
|
| | #define LIM 500000
|
| | #define LIM2 2100000
|
| | #define MOD 1000000007
|
| |
|
| | int ind;
|
| | int L[2][LIM],R[2][LIM],S[2][LIM];
|
| | vector<int> con[LIM];
|
| |
|
| | int sz;
|
| | vector<int> lst[LIM2];
|
| |
|
| | void rec(int z,int i,int p)
|
| | {
|
| | int j,k;
|
| | L[z][i]=ind++;
|
| | Fox(j,Sz(con[i]))
|
| | if ((k=con[i][j])!=p)
|
| | rec(z,k,i);
|
| | R[z][i]=ind-1;
|
| | S[z][i]=R[z][i]-L[z][i];
|
| | }
|
| |
|
| | int Query(int i,int r1,int r2,int a,int b,int v1,int v2)
|
| | {
|
| | if ((a<=r1) && (r2<=b))
|
| | {
|
| | return(
|
| | lower_bound(lst[i].begin(),lst[i].end(),v2+1) -
|
| | lower_bound(lst[i].begin(),lst[i].end(),v1)
|
| | );
|
| | }
|
| | i<<=1;
|
| | int m=(r1+r2)>>1,s=0;
|
| | if (a<=m)
|
| | s+=Query(i,r1,m,a,b,v1,v2);
|
| | if (b>m)
|
| | s+=Query(i+1,m+1,r2,a,b,v1,v2);
|
| | return(s);
|
| | }
|
| |
|
| | int main()
|
| | {
|
| |
|
| | int T,t;
|
| | int N,M;
|
| | int i,j,k,a,b,z,z1,z2;
|
| | int ans,tot;
|
| | char c1,c2;
|
| |
|
| | Read(T);
|
| | Fox1(t,T)
|
| | {
|
| |
|
| | Read(N),Read(M);
|
| | Fox(z,2)
|
| | {
|
| |
|
| | Fox(i,N)
|
| | con[i].clear();
|
| |
|
| | Fox(i,N-1)
|
| | {
|
| | Read(j),Read(k),j--,k--;
|
| | con[j].pb(k);
|
| | con[k].pb(j);
|
| | }
|
| |
|
| | ind=0;
|
| | rec(z,0,0);
|
| | }
|
| |
|
| | for(sz=1;sz<N;sz<<=1);
|
| | Fox(i,sz<<1)
|
| | lst[i].clear();
|
| | Fox(i,N)
|
| | lst[sz+L[0][i]].pb(L[1][i]);
|
| | FoxR1(i,sz-1)
|
| | {
|
| | j=i<<1,k=j+1;
|
| | a=b=0;
|
| | while ((a<Sz(lst[j])) && (b<Sz(lst[k])))
|
| | if (lst[j][a]<lst[k][b])
|
| | lst[i].pb(lst[j][a++]);
|
| | else
|
| | lst[i].pb(lst[k][b++]);
|
| | while (a<Sz(lst[j]))
|
| | lst[i].pb(lst[j][a++]);
|
| | while (b<Sz(lst[k]))
|
| | lst[i].pb(lst[k][b++]);
|
| | }
|
| |
|
| | tot=0;
|
| | while (M--)
|
| | {
|
| |
|
| | Read(i),Read(j),i--,j--;
|
| | scanf("%c %c",&c1,&c2);
|
| | z1=c1=='T' ? 0 : 1;
|
| | z2=c2=='T' ? 0 : 1;
|
| |
|
| | if (z1==z2)
|
| | {
|
| | z=z1;
|
| | if (L[z][i]>L[z][j])
|
| | swap(i,j);
|
| |
|
| | if (L[z][j]<=R[z][i])
|
| | {
|
| | ans=2*S[z][i] + S[z][j];
|
| | goto Done;
|
| | }
|
| |
|
| | ans=2*(S[z][i] + S[z][j]);
|
| | goto Done;
|
| | }
|
| |
|
| | if (z1)
|
| | swap(i,j);
|
| | ans=2*(S[0][i] + S[1][j]);
|
| | if ((S[0][i]) && (S[1][j]))
|
| | ans-=Query(1,0,sz-1,L[0][i]+1,R[0][i],L[1][j]+1,R[1][j]);
|
| |
|
| | Done:;
|
| | tot=((LL)tot*12345+ans)%MOD;
|
| | }
|
| |
|
| | printf("Case #%d: %d\n",t,tot);
|
| | }
|
| | return(0);
|
| | } |