Run ID 作者 问题 语言 测评结果 分数 时间 内存 代码长度 提交时间
33719 LYLAKIOI 【S】T2 C++ 通过 100 480 MS 684 KB 1715 2024-10-20 15:18:16

Tests(10/10):


#include<bits/stdc++.h> #define up(i,l,r) for(int i=(l);i<=(r);++i) #define down(i,l,r) for(int i=(l);i>=(r);--i) #define pi pair<int,int> #define p1 first #define p2 second #define p_b push_back #define m_p make_pair using namespace std; typedef long long ll; const int maxn=1e6+10,mod=998244353; inline ll read(){ ll x=0;short t=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')t=-1;ch=getchar();} while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar(); return x*t; }int n,deg[maxn],tg[maxn],dp[maxn][2],pw[maxn],vis[maxn]; vector<int>v[5005]; void dfs(int u,int fa){ dp[u][0]=1,dp[u][1]=0,vis[u]=1; for(int x:v[u])if(x!=fa){ if(!tg[x])continue; dfs(x,u); int f0=dp[u][0],f1=dp[u][1]; int g0=dp[x][0],g1=dp[x][1]; dp[u][0]=f0*1ll*(g0+g1*2%mod)%mod; dp[u][1]=(f0*1ll*g1%mod+f1*1ll*(g0+g1*2%mod)%mod)%mod; } int au=0; if(deg[u])au=deg[u]*1ll*pw[deg[u]-1]%mod; int bu=pw[deg[u]]; int f0=dp[u][0],f1=dp[u][1]; dp[u][0]=f0*1ll*bu%mod; dp[u][1]=(f0*1ll*au%mod+f1*1ll*bu%mod)%mod; } void slv(){ n=read(); pw[0]=1;up(i,1,n)pw[i]=pw[i-1]*2%mod; up(i,1,n-1){ int x=read(),y=read(); v[x].p_b(y),v[y].p_b(x); } up(i,1,n)for(int x:v[i])++deg[x]; up(i,1,n-1){ int u=read();tg[u]=1; for(int x:v[u])--deg[x]; up(j,1,n)vis[j]=0; int res=1; up(j,1,n)if((tg[j])&&(!vis[j]))dfs(j,0),res=res*1ll*dp[j][1]%mod; printf("%d\n",res); } }int main(){ //freopen("tree.in","r",stdin); //freopen("tree.out","w",stdout); slv(); fclose(stdin); fclose(stdout); return 0; }


测评信息: