1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
| #include <bits/stdc++.h> #include <atcoder/modint> const int MOD=998244353; using mint = atcoder::static_modint<MOD>; using namespace std; typedef long long ll; #define rep(i,a,n) for (ll i=a;i<(ll)(n);i++) #define per(i,a,n) for (ll i=n;i-->(ll)(a);) ll read(){ll r;scanf("%lld",&r);return r;} struct state{ public: int v; int ip; int im; int op; int om; bool first; state(int v){ this->v=v; this->ip=0; this->im=0; this->op=0; this->om=0; this->first=false; } }; vector<state> g[10010]; const int PWR=15; const int N=10000; int d[N+10]; int fa[N+10][PWR];
int up(int u,int step){ per(pwr,0,PWR) if(step & (1<<pwr)) u = fa[u][pwr]; return u; }
int lca(int u,int v){ assert(u!=v); int du = d[u]; int dv = d[v]; if(du < dv) v=up(v,dv-du); else u=up(u,du-dv); if(u==v)return u; per(pwr,0,PWR) if(fa[u][pwr]!=fa[v][pwr]) { u=fa[u][pwr]; v=fa[v][pwr]; } return fa[u][0]; }
void add(int u,int v) { int uv = lca(u,v); if(uv == u) { g[u][(up(v,d[v]-d[u]-1) == g[u][0].v) ? 0 : 1].ip += 1; int fv=fa[v][0]; g[fv][(v == g[fv][0].v) ? 0 : 1].im += 1; } else if(uv == v) { int fu=fa[u][0]; g[fu][(u == g[fu][0].v) ? 0 : 1].op += 1; g[v][(up(u,d[u]-d[v]-1) == g[v][0].v) ? 0 : 1].om += 1; } else { g[uv][(up(u,d[u]-d[uv]-1) == g[uv][0].v)?0:1].first = true; add(u,uv); add(uv,v); } } int k; using ret_state = tuple<int,map<int,mint>>; ret_state solve(int u) { if(g[u].size() == 0) { map<int,mint> ret; ret[0]=1; return {0,ret}; } auto trans=[&](const state&s)->ret_state{ auto [v,ip,im,op,om,fir] = s; auto [o,res] = solve(v); map<int,mint> calc; for(auto [mx,cnt]:res) { int newmx = max({ip,mx+ip-im,ip-im+o+op}); if(newmx <= k) calc[newmx] += cnt; } return {ip-im+o+op-om,calc}; }; auto link=[&](const ret_state&s0,const ret_state&s1)->ret_state { auto [o_0,res_0] = s0; auto [o_1,res_1] = s1; mint tot0 = 0; for(auto [v,c]:res_0) tot0+=c; mint tot1 = 0; for(auto [v,c]:res_1) tot1+=c; map<int,mint>ret; auto itr0=res_0.end(); auto itr1=res_1.end(); while(itr0 != res_0.begin() and itr1 != res_1.begin()){ auto [mx0,c0] =*prev(itr0); auto [mx1,c1] =*prev(itr1); if(mx0 <= mx1+o_0){ if(mx1+o_0 <= k) ret[mx1+o_0] += tot0*c1; tot1 -= c1; itr1 =prev(itr1); }else{ ret[mx0] += c0*tot1; tot0 -= c0; itr0 = prev(itr0); } } return {o_0+o_1,ret}; }; auto [o_l,res_l] = trans(g[u][0]); if(g[u].size() == 1) { return {o_l,res_l}; }else { auto [o_r,res_r] = trans(g[u][1]); if(g[u][0].first and g[u][1].first) { return {0,{}}; }else if(g[u][0].first){ return link({o_l,res_l},{o_r,res_r}); }else if(g[u][1].first){ return link({o_r,res_r},{o_l,res_l}); }else{ auto [o_lr,res_lr] = link({o_l,res_l},{o_r,res_r}); auto [o_rl,res_rl] = link({o_r,res_r},{o_l,res_l}); map<int,mint> ret; for(auto [v,cnt]:res_lr) ret[v]+=cnt; for(auto [v,cnt]:res_rl) ret[v]+=cnt; return {o_lr,ret}; } } }
int main(){ int n=read(); int m=read(); k=read(); rep(i,2,n+1) { int x=read(); g[x].push_back(state(i)); fa[i][0] = x; } fa[1][0]=1; rep(u,2,n+1) d[u] = d[fa[u][0]]+1; rep(i,1,PWR) rep(u,1,n+1) fa[u][i] = fa[fa[u][i-1]][i-1]; rep(i,0,m) { int s=read(); int t=read(); add(s,t); } auto [o,cnt] = solve(1); assert(o==0); mint ans = 0; for(auto [mx,c]:cnt) ans+=c; printf("%d\n",ans.val()); return 0; }
|