骞朵笉瀵瑰姴鐨刲oj3124:uoj477:p5405:[CTS2019]姘噾鎵嬫父
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了骞朵笉瀵瑰姴鐨刲oj3124:uoj477:p5405:[CTS2019]姘噾鎵嬫父相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/ade' title='ade'>ade
def char git line tchar cst map ==棰樼洰澶ф剰
鏈変竴涓湁鍚戝浘锛岃繖涓湁鍚戝浘鐨勭敓鎴愭柟寮忔槸鎶婁竴妫?span class="math inline">(n)
涓偣鐨勬爲鐨勬瘡鏉℃棤鍚戣竟閲嶆柊瀹氬悜銆?br> 姣忎釜鐐?span class="math inline">(i)鐨勭偣鏉冩湁(p_{i,1})鐨勬鐜囦负1锛屾湁(p_{i,2})鐨勬鐜囦负2锛屾湁(p_{i,3})鐨勬鐜囦负3銆?br> 鍙栨棤闄愭鐐癸紝姣忔鍙栨煇涓偣鐨勬鐜?(璇ョ偣鐐规潈)/(鎬荤偣鏉冨拰)銆?br> 闂細瀵逛簬姣忔潯杈癸紝閮芥弧瓒宠捣鐐圭涓€娆¤鍙栧嚭鏉ョ殑鏃堕棿姣旂粓鐐圭涓€娆¤鍙栧嚭鏉ョ殑鏃堕棿鏃╃殑姒傜巼妯?98244353銆?br> (nleq 1000)棰樿В
褰撴墍鏈夎竟鍦ㄦ爲涓兘鏄埗浜叉寚鍚戜笖鎵€鏈夌偣鐨勬潈鍊奸兘纭畾鏃讹紝璁?span class="math inline">(f(i))
琛ㄧず鐐?span class="math inline">(i)鍜屽畠鐨勫瓙鏍戠鍚堟潯浠剁殑姒傜巼銆?br> 鑰冭檻瀛愭爲鍚戠埗浜茬殑杞Щ锛氬綋涓€涓偣鐨勫瓙鏍戦兘绗﹀悎鏉′欢鏃讹紝璇ョ偣鍙婂瓙鏍戦兘绗﹀悎鏉′欢褰撲笖浠呭綋璇ョ偣绗竴娆″彇鍦ㄥ瓙鏍戜腑鎵€鏈夌偣绗竴娆″彇涔嬪墠锛岃繖浠朵簨鍙戠敓鐨勬鐜囨槸(frac{璇ョ偣鐐规潈}{璇ョ偣鍙婂瓙鏍戜腑鎵€鏈夌偣鐨勭偣鏉冨拰})銆?br> 鐐规潈涓嶇‘瀹氭椂锛岃(f(i,j))琛ㄧず鐐?span class="math inline">(i)鐨勫瓙鏍戝拰鐐?span class="math inline">(i)鐨勭偣鏉冨拰涓?span class="math inline">(j)鏃讹紝鐐?span class="math inline">(i)鍜屽畠鐨勫瓙鏍戠鍚堟潯浠剁殑姒傜巼銆傛爲涓婅儗鍖呰浆绉汇€?br> 鏈夊弽鍚戣竟鏃讹細鎵€鏈夊弽鍚戣竟閮界鍚堢殑姒傜巼=鎵€鏈夊弽鍚戣竟绗﹀悎鎴栦笉绗﹀悎閮借鐨勬鐜?瀛樺湪鍙嶅悜杈逛笉绗﹀悎鐨勬鐜囥€?br> 鍙嶅悜杈逛笉绗﹀悎鐩稿綋浜庢妸鍙嶅悜杈瑰綋鎴愭鍚戣竟绠椼€?br> 绠楄繖涓渶瑕佸鏂ワ紝鎶婂弽鍚戣竟褰撴鍚戣竟绠楁椂锛岃浆绉绘椂涔樺鏂ョ郴鏁般€?/p>浠g爜
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define rep(i,x,y) for(register int i=(x);i<=(y);++i)
#define dwn(i,x,y) for(register int i=(x);i>=(y);--i)
#define view(u,k) for(int k=fir[u];~k;k=nxt[k])
#define maxn 1007
#define maxsiz 3007
#define LL long long
using namespace std;
int read()
{
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)&&ch!=鈥?鈥?ch=getchar();
if(ch==鈥?鈥?f=-1,ch=getchar();
while(isdigit(ch))x=(x<<1)+(x<<3)+ch-鈥?鈥?ch=getchar();
return x*f;
}
void write(int x)
{
if(x==0){putchar(鈥?鈥?,putchar(鈥榎n鈥?;return;}
int f=0;char ch[20];
if(x<0)putchar(鈥?鈥?,x=-x;
while(x)ch[++f]=x%10+鈥?鈥?x/=10;
while(f)putchar(ch[f--]);
putchar(鈥榎n鈥?;
return;
}
const int mod=998244353;
int n,dp[maxn][maxsiz],siz[maxn],fir[maxn],v[maxn<<1],nxt[maxn<<1],cnte,p[maxn][4],ans,tmp[maxsiz],inv[maxsiz];
int getd(int k){return k<(k^1)?1:0;}
void ade(int u1,int v1){v[cnte]=v1,nxt[cnte]=fir[u1],fir[u1]=cnte++;}
int mo(int x){if(x<0)return x+mod;return x>=mod?x-mod:x;}
int mul(int x,int y){int res=1;while(y){if(y&1)res=(LL)res*x%mod;x=(LL)x*x%mod,y>>=1;}return res;}
void getdp(int u,int fa)
{
rep(i,1,3)dp[u][i]=(LL)i*p[u][i]%mod;siz[u]=1;
view(u,k)if(v[k]!=fa)
{
getdp(v[k],u);
if(getd(k))
{
int li1=siz[u]*3,li2=siz[v[k]]*3;
rep(i,1,li1+li2)tmp[i]=0;
rep(i,1,li1)rep(j,1,li2)
{
tmp[i+j]=mo(tmp[i+j]+(LL)dp[u][i]*dp[v[k]][j]%mod);
}
}
else
{
int li1=siz[u]*3,li2=siz[v[k]]*3;
rep(i,1,li1+li2)tmp[i]=0;
rep(i,1,li1)rep(j,1,li2)tmp[i+j]=mo(tmp[i+j]+(LL)dp[u][i]*mo(-dp[v[k]][j])%mod);
rep(i,1,li1)rep(j,1,li2)tmp[i]=mo(tmp[i]+(LL)dp[u][i]*dp[v[k]][j]%mod);
}
siz[u]+=siz[v[k]];int li=siz[u]*3;
rep(i,1,li)dp[u][i]=tmp[i];
}
int li=siz[u]*3;
rep(i,1,li)dp[u][i]=(LL)dp[u][i]*inv[i]%mod;
}
int main()
{
n=read();int li=n+n+n;
rep(i,1,li)inv[i]=mul(i,mod-2);
rep(i,1,n)
{
p[i][1]=read(),p[i][2]=read(),p[i][3]=read(),fir[i]=-1;
int invp=mo(p[i][1]+mo(p[i][2]+p[i][3]));invp=mul(invp,mod-2);
p[i][1]=(LL)p[i][1]*invp%mod,p[i][2]=(LL)p[i][2]*invp%mod,p[i][3]=(LL)p[i][3]*invp%mod;
}
rep(i,2,n){int x=read(),y=read();ade(x,y),ade(y,x);}
getdp(1,0);li=siz[1]*3;rep(i,0,li)ans=mo(ans+dp[1][i]);
write(ans);
return 0;
}
以上是关于骞朵笉瀵瑰姴鐨刲oj3124:uoj477:p5405:[CTS2019]姘噾鎵嬫父的主要内容,如果未能解决你的问题,请参考以下文章