QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#381631 | #8570. Idola-Tree | 275307894a | WA | 1291ms | 37972kb | C++14 | 2.1kb | 2024-04-07 19:40:22 | 2024-04-07 19:40:22 |
Judging History
answer
#include<bits/stdc++.h>
#define Gc() getchar()
#define Me(x,y) memset(x,y,sizeof(x))
#define Mc(x,y) memcpy(x,y,sizeof(x))
#define d(x,y) ((m)*(x-1)+(y))
#define R(n) (rnd()%(n)+1)
#define Pc(x) putchar(x)
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define eb emplace_back
#define all(x) x.begin(),x.end()
using namespace std;using ll=long long;using db=double;using lb=long db;using ui=unsigned;using ull=unsigned long long;using pii=pair<int,int>;
const int N=3e5+5,M=2000+5,K=1000+5,mod=998244353,Mod=mod-1;const db eps=1e-8;const int INF=1e9+7;mt19937 rnd(263082);
#define Tp template<typename T>
#define Ts template<typename T,typename... Ar>
namespace Debug{
Tp void _debug(char* f,T t){cerr<<f<<'='<<t<<endl;}
Ts void _debug(char* f,T x,Ar... y){while(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);}
#ifdef LOCAL
#define gdb(...) _debug((char*)#__VA_ARGS__,__VA_ARGS__)
#else
#define gdb(...) void()
#endif
}using namespace Debug;
int n,C;vector<int> S[N];
int siz[N];ll f1[N],f2[N];
void DP1(int x,int La){
siz[x]=1;f1[x]=0;
for(int i:S[x]) if(i^La) DP1(i,x),f1[x]+=f1[i]+siz[i],siz[x]+=siz[i];
}
void DP2(int x,int La){
for(int i:S[x]) if(i^La) f2[i]=f2[x]+n-siz[i]+f1[x]-f1[i]-siz[i],DP2(i,x);
}
ll w[N];int wh;
void Solve(){
int i,j;scanf("%d%d",&n,&C);
for(i=1;i<=n;i++) S[i].clear();
for(i=1;i<n;i++){
int x,y;scanf("%d%d",&x,&y);
S[x].emplace_back(y);S[y].emplace_back(x);
}
int rt=1;for(i=2;i<=n;i++) if(S[i].size()>=2) rt=i;
DP1(rt,0);f2[rt]=0;DP2(rt,0);
ll tot=0,ans=0;for(i=1;i<=n;i++) if(i^rt) tot+=(f1[i]%mod*(n-siz[i])+f2[i]%mod*siz[i])%mod;
wh=0;for(i=1;i<=n;i++) if(i^rt&&S[i].size()==1) w[++wh]=f2[i];
sort(w+1,w+wh+1);
queue<ll> q;q.emplace(w[1]);int R=2;
int add=0;
for(i=n-1;i<=C;i++){
ans+=1ll*tot*tot%mod*tot%mod;
// gdb(tot);
ll x=q.front();q.pop();
tot+=2*(x+add)+n-1;tot%=mod;
add++;
while(R<=wh&&w[R]<x+n-2) q.emplace(w[R]),R++;
q.emplace(x+n-2);
}
printf("%lld\n",ans%mod);
}
int main(){
int t=1;
scanf("%d",&t);
while(t--) Solve();
cerr<<clock()*1.0/CLOCKS_PER_SEC<<'\n';
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 16716kb
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: 16516kb
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
Wrong Answer
time: 1291ms
memory: 37972kb
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:
809508167 667745402 446346265 904902108
result:
wrong answer 1st words differ - expected: '968050473', found: '809508167'