Title Description
answer
Shouted questionsBut such a solution to a problem how fans
Consider a different approach and solution to a problem (understanding)
first zombie discrete, identical h Chin (ying) a point size
(this can be found in each case would only be counted just once)
calculation scheme is fully occupied, then 1 - program / probability
due to the size is determined, so eventually be divided into blocks that are not connected, and there is only a block zombie
define a block number is the number of occupied zombies
provided f [i] [x] ( x > 0) denotes the subtree rooted at i is the point where the block number i is x
then for f [j] [y] ( j∈son [i], j> 0) transferred following:
①X Y =
F [J ] [y] (x zombie through i - j is the number of programs) -> f [i] [ x]
then x and y in a zombie only one block in the same block, since the block it is necessary to communicate
② X <y
F [j] [y] (y zombies without i - j is the number of programs) -> F [I] [x]
x and y are not in the same block, so that x and y can not be connected, i.e., zombie larger (y) can not go to the other point (i)
and to ensure the presence of y j, x is not present , the reasons below
③ X-> Y
F [J] [Y] * (x zombies without i-- j is the number of programs) -> f [i] [ x]
Cause & scope supra
The initial value f [i] [x] = [x> = h [i]] (x> 0)
Restrictions ②③ of:
as to ensure that just the presence of the zombie to a point x i is the shallowest point of the block ,
ensures that when i and FA [i] is disconnected x subtree i of the leaves, where the block i son leaves the block to ensure the disconnect x is not outside the block , the block must be present so that x
Time complexity of O (n ^ 3), the prefix and suffix optimized to O (n ^ 2)
code
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <bitset>
#include <cstdio>
#define fo(a,b,c) for (a=b; a<=c; a++)
#define fd(a,b,c) for (a=b; a>=c; a--)
#define add(a,b) a=((a)+(b))%998244353
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
#define mod 998244353
#define Mod 998244351
using namespace std;
struct type{
int x,id;
} b[2001];
int a[4002][2];
int c[2001][2001];
int C[2001];
int ls[2001];
int L[2001];
int R[2001];
int h[2001];
int H[2001];
long long f[2001][2001];
long long s1[2002];
long long s2[2002];
bitset<2001> bz[2001];
int T,N,n,m,i,j,k,l,len;
long long ans,s;
bool cmp(type a,type b)
{
return a.x<b.x;
}
void New(int x,int y)
{
++len;
a[len][0]=y;
a[len][1]=ls[x];
ls[x]=len;
}
long long qpower(long long a,int b)
{
long long ans=1;
while (b)
{
if (b&1)
ans=ans*a%mod;
a=a*a%mod;
b>>=1;
}
return ans;
}
void Dfs(int Fa,int t)
{
int i;
if (h[t]) bz[t][h[t]]=1;
for (i=ls[t]; i; i=a[i][1])
if (a[i][0]!=Fa)
{
Dfs(t,a[i][0]);
bz[t]|=bz[a[i][0]];
}
}
void dfs(int Fa,int t)
{
int i,j,k,l,id;
long long x;
fo(i,max(1,h[t]),N) f[t][i]=1;
for (i=ls[t]; i; i=a[i][1])
if (a[i][0]!=Fa)
{
id=i/2;
dfs(t,a[i][0]);
fo(k,1,N)
{
s1[k]=s1[k-1];
if (bz[a[i][0]][k])
add(s1[k],f[a[i][0]][k]);
}
s2[N+1]=0;
fd(k,N,1)
{
s2[k]=s2[k+1];
if (bz[a[i][0]][k])
add(s2[k],f[a[i][0]][k]*max(R[id]-max(H[k],L[id])+1,0)%mod);
}
fo(j,1,N)
{
if (!bz[a[i][0]][j])
f[t][j]=f[t][j]*(s2[j+1]+s1[j-1]*max(R[id]-max(H[j],L[id])+1,0)%mod+f[a[i][0]][j]*max(min(H[j]-1,R[id])-L[id]+1,0)%mod)%mod;
else
f[t][j]=f[t][j]*(f[a[i][0]][j]*max(min(H[j]-1,R[id])-L[id]+1,0)%mod)%mod;
// O(n^3)
// fo(k,1,N)
// if (f[a[i][0]][k])
// {
// if (j<k)
// x=max(R[id]-max(H[k],L[id])+1,0);
// if (j==k)
// x=max(min(H[k]-1,R[id])-L[id]+1,0);
// if (j>k)
// x=max(R[id]-max(H[j],L[id])+1,0);
//
// if (j==k || bz[a[i][0]][k] && !bz[a[i][0]][j])
// add(F[j],f[t][j]*f[a[i][0]][k]%mod*x);
// }
}
}
}
int main()
{
freopen("zombie.in","r",stdin);
freopen("zombie.out","w",stdout);
scanf("%d",&T);
for (;T;--T)
{
memset(bz,0,sizeof(bz));
memset(ls,0,sizeof(ls));
memset(h,0,sizeof(h));
memset(H,0,sizeof(H));
memset(f,0,sizeof(f));
memset(C,0,sizeof(C));
len=1;
scanf("%d%d",&n,&m);
fo(i,1,n-1)
{
scanf("%d%d%d%d",&j,&k,&L[i],&R[i]);
New(j,k);
New(k,j);
}
fo(i,1,m)
{
scanf("%d%d",&j,&k);
h[j]=max(h[j],k);
}
N=0;
fo(i,1,n)
if (h[i])
b[++N]={h[i],i};
sort(b+1,b+N+1,cmp);
fo(i,1,N)
{
H[i]=b[i].x;
h[b[i].id]=i;
}
Dfs(0,1);
dfs(0,1);
ans=0;
fo(i,1,N)
add(ans,f[1][i]);
s=1;
fo(i,1,n-1)
s=s*(R[i]-L[i]+1)%mod;
ans=ans*qpower(s,Mod)%mod;
printf("%lld\n",((1-ans)%mod+mod)%mod);
}
fclose(stdin);
fclose(stdout);
return 0;
}