Run ID | 作者 | 问题 | 语言 | 测评结果 | 分数 | 时间 | 内存 | 代码长度 | 提交时间 |
---|---|---|---|---|---|---|---|---|---|
33737 | masppy | 【S】T2 | C++ | 通过 | 100 | 368 MS | 940 KB | 1680 | 2024-10-20 16:43:37 |
#include<bits/stdc++.h> #define ll long long using namespace std; const int maxn=1e4+10; const ll mod=998244353; const ll inf=1e10; ll n,m,N; int a[maxn]; char c[maxn]; ll mark[maxn],dp[maxn][3],f[maxn],vis[maxn]; map<int,int> mp; vector<int> q[maxn]; ll q_pow(ll a,ll b){ ll tmp=1; while(b){ if(b&1){ tmp=(tmp*a)%mod; } b>>=1; a=(a*a)%mod; } return tmp; } void dfs(int pos,int from){ int siz=q[pos].size(); vis[pos]=1; dp[pos][0]=1,dp[pos][1]=0; ll cnt=0; for(int i=0;i<siz;i++){ int v=q[pos][i]; if(v==from) continue; if(mark[v]){ dfs(v,pos); ll tmp0=dp[pos][0],tmp1=dp[pos][1]; dp[pos][0]=(tmp0*(dp[v][0]+dp[v][1]*2ll)%mod)%mod; dp[pos][1]=(tmp0*dp[v][1]+tmp1*(dp[v][0]+dp[v][1]*2ll)%mod)%mod; } else cnt++; } if(cnt){ dp[pos][1]=(dp[pos][0]*q_pow(2,cnt-1)%mod*cnt+dp[pos][1]*q_pow(2,cnt))%mod; dp[pos][0]=(dp[pos][0]*q_pow(2,cnt))%mod; } } int main(){ //freopen("tree.in","r",stdin); //freopen("tree.out","w",stdout); scanf("%lld",&n); for(int i=1;i<n;i++){ int u,v; scanf("%d%d",&u,&v); q[u].push_back(v); q[v].push_back(u); } //for(int i=1;i<=n;i++) cout<<degn[i][i]<<endl; int N=n-1; while(N--){ int x; scanf("%d",&x); mark[x]=1; ll ans=1ll; memset(vis,0,sizeof(vis)); for(int i=1;i<=n;i++){ if(!vis[i]&&mark[i]){ dfs(i,0); ans=(ans*dp[i][1])%mod; } } printf("%lld\n",ans); } fclose(stdin); fclose(stdout); return 0; }