QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#381574 | #8570. Idola-Tree | ucup-team1004 | TL | 0ms | 16212kb | C++17 | 2.2kb | 2024-04-07 19:05:05 | 2024-04-07 19:05:06 |
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,w[N];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]+1ll*siz[i]*w[i],siz[x]+=siz[i];
}
void DP2(int x,int La){
for(int i:S[x]) if(i^La) f2[i]=f2[x]+1ll*(n-siz[i])*w[i];
ll tot=0;for(int i:S[x]) if(i^La) f2[i]+=tot,tot+=f1[i]+1ll*siz[i]*w[i];
reverse(all(S[x]));
tot=0;for(int i:S[x]) if(i^La) f2[i]+=tot,tot+=f1[i]+1ll*siz[i]*w[i];
for(int i:S[x]) if(i^La) DP2(i,x);
}
void Solve(){
int i,j;scanf("%d%d",&n,&C);
for(i=1;i<=n;i++) S[i].clear(),w[i]=1;
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(j=1;j<=n;j++) if(j^rt) tot+=(f1[j]%mod*(n-siz[j])+f2[j]%mod*siz[j])%mod*w[j]%mod;
priority_queue<ll> q;
for(j=1;j<=n;j++) if(j^rt&&S[j].size()==1) q.emplace(-f2[j]);
int add=0;
for(i=n-1;i<=C;i++){
ans+=1ll*tot*tot%mod*tot%mod;
ll x=-q.top();q.pop();
tot+=2*(x+add)+n-1;tot%=mod;
q.emplace(-(x+n-2));add++;
}
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: 0ms
memory: 14780kb
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: 16212kb
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
Time 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...