QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#354305 | #7997. 树 V 图 | Naganohara_Yoimiya | AC ✓ | 1111ms | 51256kb | C++14 | 2.7kb | 2024-03-15 08:04:59 | 2024-03-15 08:05:00 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define mk make_pair
#define fi first
#define se second
using namespace std;
inline int read(){
int x=0,f=1;char c=getchar();
for(;(c<'0'||c>'9');c=getchar()){if(c=='-')f=-1;}
for(;(c>='0'&&c<='9');c=getchar())x=x*10+(c&15);
return x*f;
}
const int mod=998244353;
int ksm(int x,ll y,int p=mod){
int ans=1;y%=(p-1);
for(int i=y;i;i>>=1,x=1ll*x*x%p)if(i&1)ans=1ll*ans*x%p;
return ans%p;
}
int inv(int x,int p=mod){return ksm(x,p-2,p)%p;}
mt19937 rnd(time(0));
int randint(int l,int r){return rnd()%(r-l+1)+l;}
void add(int &x,int v){x+=v;if(x>=mod)x-=mod;}
void Mod(int &x){if(x>=mod)x-=mod;}
int cmod(int x){if(x>=mod)x-=mod;return x;}
template<typename T>void cmax(T &x,T v){x=max(x,v);}
template<typename T>void cmin(T &x,T v){x=min(x,v);}
void solve(){
int n=read(),k=read();
vector<vector<int> >adj(n+1),G(n+1);
vector<int>a(n+1);
vector<pair<int,int> >edges(n-1);
for(int i=1;i<=n-1;i++){
int u=read(),v=read();edges[i-1]=mk(u,v);
adj[u].emplace_back(v),adj[v].emplace_back(u);
}
vector<vector<int> >vec(n+1);
for(int i=1;i<=n;i++)vec[a[i]=read()].emplace_back(i);
vector<int>In(n+1),dep(n+1),root(n+1),fa(n+1);int tot=0;
function<void(int)>dfs1=[&](int u){
if(a[u]!=a[fa[u]])In[u]=++tot,root[a[u]]=u;
else In[u]=In[fa[u]];
dep[u]=dep[fa[u]]+1;
for(int v:adj[u])if(v!=fa[u])fa[v]=u,dfs1(v);
};
dfs1(1);
for(int i=1;i<=k;i++){
if(vec[i].size()==0)return puts("0"),void();
for(int j=1;j<vec[i].size();j++)if(In[vec[i][j]]!=In[vec[i][j-1]])return puts("0"),void();
}
for(auto [u,v]:edges){
if(a[u]!=a[v])G[a[u]].emplace_back(a[v]),G[a[v]].emplace_back(a[u]);
}
vector<vector<int> >dis(n+1,vector<int>(n+1));
for(int i=1;i<=n;i++){
fill(dis[i].begin(),dis[i].end(),-1);
queue<int>q;q.push(i),dis[i][i]=0;
while(q.size()){
int x=q.front();q.pop();
for(int y:adj[x])if(dis[i][y]==-1)dis[i][y]=dis[i][x]+1,q.push(y);
}
}
vector<vector<int> >f(k+1,vector<int>(n+1));
function<void(int,int)>DP=[&](int u,int fr){
for(int v:G[u])if(v!=fr)DP(v,u);
for(int j:vec[u]){
f[u][j]=1;
for(int v:G[u])if(v!=fr){
int sum=0;
for(int k:vec[v]){
int dv=dis[k][root[v]],du=dis[fa[root[v]]][j];
if(dv==du)add(sum,f[v][k]);
else if(dv==du+1&&u>v)add(sum,f[v][k]);
else if(du==dv+1&&u<v)add(sum,f[v][k]);
}
f[u][j]=1ll*f[u][j]*sum%mod;
}
}
};
DP(a[1],0);
int ans=0;for(int i=1;i<=n;i++)add(ans,f[a[1]][i]);
cout<<ans<<endl;
}
signed main(void){
#ifndef ONLINE_JUDGE
freopen("in.in","r",stdin);
#endif
int tt=read();while(tt--)solve();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3672kb
input:
10 15 2 10 5 3 5 12 5 10 9 11 7 3 8 2 4 7 1 15 14 8 13 15 6 2 1 4 8 11 15 1 1 1 1 2 1 1 1 2 2 1 2 1 1 1 15 3 8 11 12 8 1 3 13 15 5 9 10 13 6 12 14 4 4 9 15 5 11 10 2 14 7 2 6 3 3 2 3 2 2 3 2 1 2 1 1 3 1 2 1 15 5 1 7 5 2 11 9 6 8 13 3 14 12 3 1 8 9 5 10 10 11 5 1 12 13 10 15 11 4 3 3 3 2 3 2 1 2 2 2 ...
output:
11 15 8 9 5 2 1 0 15 18
result:
ok 10 numbers
Test #2:
score: 0
Accepted
time: 1ms
memory: 3604kb
input:
10 15 2 7 10 10 3 11 4 9 7 4 15 14 8 7 1 8 15 13 4 2 1 15 9 11 6 2 12 5 6 2 2 2 1 1 1 2 1 2 2 1 2 1 1 1 15 3 4 10 12 3 7 3 9 1 6 7 9 11 11 7 10 8 13 15 14 2 14 7 5 15 12 13 11 10 1 2 3 3 3 3 3 3 1 3 3 3 3 2 3 15 5 12 9 6 8 13 2 6 10 2 3 14 15 10 5 4 15 14 13 5 7 11 10 14 12 1 5 13 8 2 4 1 3 2 2 5 3 ...
output:
21 4 5 2 13 17 6 0 0 0
result:
ok 10 numbers
Test #3:
score: 0
Accepted
time: 1106ms
memory: 51220kb
input:
10 3000 2 2052 2631 688 2422 2401 352 654 1669 1566 2157 1187 334 179 178 948 1821 1084 99 878 793 410 336 2218 865 2558 236 2808 430 799 1238 1468 226 312 268 1860 991 2946 96 2540 241 2242 1103 299 1527 788 2026 1885 2104 229 1627 2461 2649 498 2642 1354 98 113 980 947 2790 1281 2232 2682 713 1841...
output:
2420 21248 731916691 864589674 3936 9543168 561554568 0 0 0
result:
ok 10 numbers
Test #4:
score: 0
Accepted
time: 1111ms
memory: 51256kb
input:
10 3000 2 1962 622 2353 451 2284 659 848 1392 2032 111 2647 1540 706 1072 2158 461 887 1075 178 481 2350 2658 2309 96 753 1572 1703 2171 365 1286 2540 2487 2687 194 1585 1241 2147 1247 1720 701 507 2127 2207 812 1568 2621 725 1342 1385 245 2819 2731 2357 450 467 672 2549 1243 1517 702 1808 2415 76 8...
output:
2252 21504 217815511 420767421 3008 36864 187077271 0 0 0
result:
ok 10 numbers
Test #5:
score: 0
Accepted
time: 904ms
memory: 39264kb
input:
10 3000 2 774 1607 49 867 1384 2838 247 454 1369 2077 2145 1433 1057 1845 1940 2195 1869 967 2830 2829 739 2783 1474 1047 1688 2054 2958 1044 1041 135 768 1621 667 257 1309 1336 2456 2605 2402 1425 1572 1275 2327 1438 1111 1265 607 2583 2557 2788 2694 1378 2332 1848 107 1749 1804 236 2585 309 2761 4...
output:
383799 552301 9347 280893 298918 39575 180102 18385 0 78
result:
ok 10 numbers
Test #6:
score: 0
Accepted
time: 928ms
memory: 39120kb
input:
10 3000 2 630 377 2445 1749 374 616 1421 937 429 724 2962 2139 1501 2291 2916 2433 2404 2502 755 1952 2756 692 547 420 507 2438 1082 2413 2929 1624 2556 2310 2895 266 2697 2184 1597 502 524 2083 898 1989 190 1269 2189 2104 1751 1387 2986 2041 646 493 627 1899 2516 2434 456 2016 223 1222 2329 2784 94...
output:
7096 400826 74841 21895 331464 110972 215781 220870 0 57
result:
ok 10 numbers
Test #7:
score: 0
Accepted
time: 848ms
memory: 51000kb
input:
10 3000 2 1490 1252 1922 2810 462 631 2806 1842 1552 1043 493 2043 339 2377 1271 354 1043 1795 192 1242 1758 2799 433 452 2473 2444 292 1404 1171 770 1194 2617 2457 2502 124 2059 818 2883 81 835 300 1498 1265 601 2038 1830 1163 1973 1110 1369 1428 392 2061 1037 559 1133 1296 140 2216 565 1001 2432 7...
output:
3074 962904591 573541113 831594950 4847397 673682640 122362172 0 0 0
result:
ok 10 numbers
Test #8:
score: 0
Accepted
time: 957ms
memory: 50936kb
input:
10 3000 2 459 801 2023 2946 2356 57 2998 264 1498 2840 1755 1603 692 1624 1929 1474 1376 1542 2953 2021 934 2740 1747 1918 270 2928 925 2366 886 2966 1692 2224 1528 1835 1748 2417 76 2539 956 1834 2570 2423 418 146 559 1376 1278 2981 564 555 2200 1472 1006 749 1262 920 2410 429 2149 1023 1365 2864 2...
output:
101017 297761161 615903767 469063443 5029034 178745072 0 0 0 0
result:
ok 10 numbers
Test #9:
score: 0
Accepted
time: 883ms
memory: 50900kb
input:
10 3000 2 1130 2866 2675 1317 1817 1398 1916 550 777 1510 955 1371 2842 2940 1069 86 2086 1600 2635 2465 940 2644 954 2270 2517 895 2981 110 2477 993 2854 2889 247 1409 1008 2137 2436 2896 2588 1174 2774 1946 265 622 1903 100 1052 2594 1864 2791 453 1319 11 2630 2468 1464 952 325 632 2214 1218 2459 ...
output:
32539 153019389 251875443 633091978 10314829 918558837 687532775 0 0 0
result:
ok 10 numbers
Test #10:
score: 0
Accepted
time: 843ms
memory: 50884kb
input:
10 3000 2 758 2015 2186 297 1518 1181 2657 1064 687 2929 1271 265 646 408 2332 217 2486 1313 362 1555 2701 1097 2014 2885 1691 43 2804 1610 1866 119 516 740 343 2335 1608 1278 330 1421 1967 2298 94 2157 1392 458 64 216 1090 541 1670 459 1870 750 1780 1973 2276 1910 1744 1699 2602 2901 2484 1380 1422...
output:
159905 198653564 859658661 608555088 9674254 170770457 138273469 0 0 0
result:
ok 10 numbers