QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#379498 | #8570. Idola-Tree | ucup_team_qiuly# | ML | 2ms | 19652kb | C++17 | 1.3kb | 2024-04-06 17:41:44 | 2024-04-06 17:42:08 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mid ((l+r)/2)
const int N=3e5+5,M=5e7+5,MOD=998244353;
int T,n,m,rt,d,nw,hd,tl,ans,sz[N];ll t,a[N],s[N],s1[N],q[M];vector<int> e[N];
void dfs(int u,int f)
{sz[u]=1;s[u]=0;for(auto v:e[u]) if(v!=f) dfs(v,u),sz[u]+=sz[v],s[u]+=s[v]+sz[v];}
void dfs1(int u,int f)
{for(auto v:e[u]) if(v!=f) s1[v]=s1[u]+s[u]-s[v]-sz[v]+n-sz[v],dfs1(v,u);}
ll get()
{
while(nw<=a[0] && a[nw]<q[hd]+d) q[++tl]=a[nw++];
q[++tl]=q[hd]+d;return q[hd++];
}
void slv()
{
scanf("%d %d",&n,&m);d=n*2-4;t=ans=a[0]=0;for(int i=1;i<=n;++i) e[i].clear();
for(int i=1,u,v;i<n;++i) scanf("%d %d",&u,&v),e[u].pb(v),e[v].pb(u);
if(n==2)
{
for(int i=1;i<=m;++i) ans=(ans+1ll*i*i%MOD*i%MOD*i%MOD*i%MOD*i)%MOD;
printf("%d\n",ans);return;
}for(int i=1;i<=n;++i) if(e[i].size()>1) rt=i;s1[rt]=0;dfs(rt,0);dfs1(rt,0);
for(int i=1;i<=n;++i) if(i!=rt) t=(t+s[i]*(n-sz[i])+s1[i]*sz[i])%MOD;
for(int i=1;i<=n;++i) if(e[i].size()<2) a[++a[0]]=s1[i];
sort(a+1,a+a[0]+1);for(int i=1;i<=a[0];++i) a[i]=a[i]*2+n-2;
nw=2;hd=1;tl=0;q[++tl]=a[1];
for(int i=0,t1;i<=m-n+1;++i)
t1=(t+1ll*i*i)%MOD,ans=(ans+1ll*t1*t1%MOD*t1)%MOD,t=(t+get())%MOD;
printf("%d\n",ans);
}
int main()
{
scanf("%d",&T);
while(T--) slv();return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 19652kb
input:
2 4 3 1 4 1 3 2 1 4 4 1 4 1 3 2 1
output:
3375 25327
result:
ok 2 tokens
Test #2:
score: 0
Accepted
time: 0ms
memory: 18948kb
input:
4 4 3 1 4 1 3 2 1 4 4 1 4 1 3 2 1 5 4 1 4 1 3 1 2 5 4 5 5 1 4 1 3 1 2 5 4
output:
3375 25327 54872 249984
result:
ok 4 tokens
Test #3:
score: -100
Memory Limit Exceeded
input:
4 300000 50000000 216838 200677 44849 12926 125086 157230 26195 29767 241694 21336 21336 24457 105861 84565 184655 45583 175336 97945 286044 30927 295273 249694 109469 1566 193560 251229 176229 288707 206166 13532 166218 264413 299977 95587 159105 48116 57912 82606 97296 193689 115029 121192 68068 1...
output:
968050473 546482457 9595680 694101802