QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#380202 | #8570. Idola-Tree | ucup-team1447# | AC ✓ | 8650ms | 140060kb | C++14 | 4.7kb | 2024-04-06 21:43:40 | 2024-04-06 21:43:41 |
Judging History
answer
// what is matter? never mind.
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,sse4,popcnt,abm,mmx,avx,avx2")
#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(int i=(a);i>=(b);--i)
#define ll long long
#define ull unsigned long long
typedef int iint;
#define int __int128
#define i128 __int128
using namespace std;
inline int read()
{
char c=getchar();int x=0;bool f=0;
for(;!isdigit(c);c=getchar())f^=!(c^45);
for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
if(f)x=-x;return x;
}
#define mod 998244353
struct modint{
int x;
modint(int o=0){x=o;}
modint &operator = (int o){return x=o,*this;}
modint &operator +=(modint o){return x=x+o.x>=mod?x+o.x-mod:x+o.x,*this;}
modint &operator -=(modint o){return x=x-o.x<0?x-o.x+mod:x-o.x,*this;}
modint &operator *=(modint o){return x=1ll*x*o.x%mod,*this;}
modint &operator ^=(int b){
modint a=*this,c=1;
for(;b;b>>=1,a*=a)if(b&1)c*=a;
return x=c.x,*this;
}
modint &operator /=(modint o){return *this *=o^=mod-2;}
friend modint operator +(modint a,modint b){return a+=b;}
friend modint operator -(modint a,modint b){return a-=b;}
friend modint operator *(modint a,modint b){return a*=b;}
friend modint operator /(modint a,modint b){return a/=b;}
friend modint operator ^(modint a,int b){return a^=b;}
friend bool operator ==(modint a,modint b){return a.x==b.x;}
friend bool operator !=(modint a,modint b){return a.x!=b.x;}
bool operator ! () {return !x;}
modint operator - () {return x?mod-x:0;}
bool operator <(const modint&b)const{return x<b.x;}
};
inline modint qpow(modint x,int y){return x^y;}
vector<modint> fac,ifac,iv;
inline void initC(int n)
{
if(iv.empty())fac=ifac=iv=vector<modint>(2,1);
int m=iv.size(); ++n;
if(m>=n)return;
iv.resize(n),fac.resize(n),ifac.resize(n);
For(i,m,n-1){
iv[i]=iv[mod%i]*(mod-mod/i);
fac[i]=fac[i-1]*i,ifac[i]=ifac[i-1]*iv[i];
}
}
inline modint C(int n,int m){
if(m<0||n<m)return 0;
return initC(n),fac[n]*ifac[m]*ifac[n-m];
}
inline modint sign(int n){return (n&1)?(mod-1):(1);}
#define fi first
#define se second
#define pb push_back
#define mkp make_pair
typedef pair<int,int>pii;
typedef vector<int>vi;
#define maxn 500005
#define inf 0x3f3f3f3f
int n,c;
vi e[maxn];
int rt;
modint out=0;
__int128 ans;
int t[maxn],m,pos[maxn];
__int128 val[maxn];
int fa[maxn];
int s[maxn],sz[maxn],ssz[maxn];
void dfs1(int u,int pa){
fa[u]=pa;
sz[u]=1;
ssz[u]=0;
for(int v:e[u])
if(v!=pa){
dfs1(v,u),sz[u]+=sz[v];
ans+=ssz[u]*ssz[v]*2;
ssz[u]+=ssz[v];
}
ssz[u]+=sz[u];
if(pa && e[u].size()==1){
t[++m]=u;
}
}
void dfs2(int u,int pa){
for(int v:e[u])if(v!=pa){
s[v]=s[u];
s[v]-=sz[v],s[v]+=n-sz[v];
dfs2(v,u);
}
}
int seq[10000007];
pii tr[maxn<<2];
int leaf[maxn];
void up(int p){
tr[p]=min(tr[p<<1],tr[p<<1|1]);
}
void build(int p,int l,int r){
if(l==r)return leaf[l]=p,tr[p]=mkp(val[l],l),void();
int mid=l+r>>1;
build(p<<1,l,mid),build(p<<1|1,mid+1,r),up(p);
}
void upd(int u){
int p=leaf[u];
tr[p]=mkp(val[u],u);
while(p>>=1) up(p);
}
modint S3(modint x){
return x*x*x;
}
int nxt[maxn],pre[maxn];
void work()
{
n=read(),c=read();
For(i,1,n)e[i].clear(),sz[i]=s[i]=fa[i]=0;
ans=0,out=0,m=0;
For(i,2,n){
int u=read(),v=read();
e[u].pb(v),e[v].pb(u);
}
rt=1;
For(i,1,n)if(e[i].size()>1){
rt=i;
break;
}
dfs1(rt,0);
For(i,1,n) if(i!=rt) s[rt]+=sz[i];
dfs2(rt,0);
For(i,1,m) {
// cout<<"t,st "<<t[i]<<" "<<s[t[i]]-(n-1)<<"\n";
pos[i]=1;
int u=i;
val[u]=(s[t[u]]-(n-1))*2+(n-1)*(pos[u]*2+1)-(pos[u]-1)*2;
}
priority_queue< pii,vector<pii>,greater<pii> > q;
For(i,1,m) q.push(mkp(val[i],i));
// int s2=0;
// For(i,1,n) s2+=sz[i];
For(i,1,n) if(i!=rt) ans+=(n-sz[i])*sz[i],ans+=(n-sz[i])*(ssz[i]-sz[i])*2;
// cout<<ans<<"\n";
// ans=15;
// cout<<ans<<"\n";
out+=S3(ans%mod);
int x=q.top().se; q.pop();
pre[x]=nxt[x]=x;
int u=x;
For(i,1,c-(n-1)){
u=nxt[u];
ans+=val[u];
ans+=(i-1)*2;
++pos[u];
// val[u]+=(n-1)*2;
val[u]=(s[t[u]]-(n-1))*2+(n-1)*(pos[u]*2+1)-(pos[u]-1)*2;
// val[u]=(s[t[u]]-(n-1))*2+(n-1)+2+(n-2)*(pos[u]*2);
while(q.size() && q.top().fi<=val[u]){
int u1=pre[u];
int x=q.top().se; q.pop();
nxt[u1]=x,pre[u]=x;
nxt[x]=u,pre[x]=u1;
}
// cout<<ans<<"\n";
out+=S3(ans%mod);
}
cout<<(iint)out.x<<"\n";
}
signed main()
{
// freopen("data.in","r",stdin);
// freopen("my.out","w",stdout);
int T=read();
while(T--)work();
return 0;
}
/*
1
5 10
1 4
1 3
1 2
5 4
1 2 3
*/
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 32732kb
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: 33068kb
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: 0
Accepted
time: 7082ms
memory: 140060kb
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
result:
ok 4 tokens
Test #4:
score: 0
Accepted
time: 8343ms
memory: 136864kb
input:
4 300000 49999999 260729 131757 51432 46938 257303 178692 218606 108144 299084 259822 82091 231405 101255 218808 287507 101249 29597 151244 43164 198032 122336 162072 177969 62812 277824 35758 182087 258797 235155 33806 188695 76915 289006 141702 39100 183183 224949 156588 9827 173233 27349 286822 1...
output:
283884918 837489229 12901428 323340145
result:
ok 4 tokens
Test #5:
score: 0
Accepted
time: 7930ms
memory: 131672kb
input:
4 300000 50000000 187086 121551 163664 292500 40569 125891 280767 56246 127162 299705 207527 280767 252551 217178 73197 278560 274282 31937 121290 280767 220367 278560 187814 40569 187372 278560 95157 131908 119390 161684 202404 283778 226289 130192 294021 278560 50286 102006 21592 222623 285215 237...
output:
4850582 364539310 683543335 559022036
result:
ok 4 tokens
Test #6:
score: 0
Accepted
time: 8050ms
memory: 122304kb
input:
4 300000 50000000 225743 104304 178831 182636 22896 17048 96503 294029 294029 28084 38933 104195 294029 1583 224079 175121 8797 197089 81985 139893 184175 103991 39351 217336 127576 82268 294029 292994 145502 294859 63119 104069 294029 41437 236951 199792 157992 294029 249477 284128 136077 294029 65...
output:
536508840 693675397 413103274 759128961
result:
ok 4 tokens
Test #7:
score: 0
Accepted
time: 8650ms
memory: 123140kb
input:
4 300000 50000000 246788 228391 158979 271301 96237 233512 276470 143087 132635 100991 189228 201152 1506 10986 75594 100408 279681 127708 140194 83425 50807 272520 277215 107214 249687 77890 261893 11907 109314 121422 76821 170561 11602 233066 80094 28275 27473 70572 130573 16191 13008 289605 25353...
output:
229607225 752154895 217060859 960988279
result:
ok 4 tokens
Test #8:
score: 0
Accepted
time: 8152ms
memory: 108468kb
input:
4 300000 50000000 282645 78888 157049 242271 143611 216821 287822 256908 2275 263546 49651 72865 31980 207555 107608 204451 138876 149271 175134 283496 8765 266276 288773 161294 250433 198847 292442 23211 93376 281917 221760 81331 133157 239663 276759 27628 115322 150583 89351 283086 291870 12125 68...
output:
62551856 430534707 675000909 391405102
result:
ok 4 tokens
Test #9:
score: 0
Accepted
time: 7163ms
memory: 131372kb
input:
4 300000 50000000 294569 56297 172540 287752 61940 152205 197674 254245 24085 113380 82637 123004 47497 92473 32184 178733 277456 157565 21776 156798 137130 134095 110025 202055 174662 297197 60043 118646 4537 248467 273141 53510 238177 252865 234966 233515 289687 229746 298433 191752 120484 36179 2...
output:
195781417 673490465 850215681 815198198
result:
ok 4 tokens
Test #10:
score: 0
Accepted
time: 4882ms
memory: 119136kb
input:
4 36778 50000000 17602 27103 25759 23876 24338 4623 29585 32937 25324 18644 2950 25005 25234 8990 10680 32086 9568 25870 23421 16592 1809 18727 29491 17171 22903 27836 4496 20939 25152 3804 12390 16492 3484 31766 6824 25795 1411 28354 3077 17532 6033 36029 11689 15579 20720 35844 5484 2622 15596 536...
output:
135800108 231398541 778024906 624480729
result:
ok 4 tokens
Test #11:
score: 0
Accepted
time: 5461ms
memory: 87296kb
input:
4 300000 29286167 155087 68009 83184 149687 206954 8699 86662 141944 237475 242716 124487 225583 168790 57088 207434 196893 573 4579 71930 257825 193317 77567 143825 182638 294310 270719 180399 209962 32628 203666 250650 234364 254067 255639 14682 227300 84292 38937 95079 54215 132911 56983 286874 1...
output:
741171004 852827875 22231673 170720066
result:
ok 4 tokens
Test #12:
score: 0
Accepted
time: 3870ms
memory: 81964kb
input:
4 300000 300000 175617 119821 181516 243657 160218 215766 162419 187680 293075 168627 290423 228281 274959 98317 107502 76378 79894 239145 45063 32200 69024 209908 1914 38016 94743 179968 32123 245964 295205 76517 97609 38830 189696 241669 137230 69860 66658 181410 70572 238631 156044 108622 108815 ...
output:
791655481 435035749 574582056 506992226
result:
ok 4 tokens
Test #13:
score: 0
Accepted
time: 4745ms
memory: 33120kb
input:
4 6 47918926 4 3 6 1 1 5 5 4 5 2 6 49261475 6 4 5 6 5 3 4 2 6 1 6 47347415 3 6 6 4 2 6 6 5 1 5 6 46149726 1 2 5 3 5 4 2 4 2 6
output:
593706496 305830436 556194176 708898110
result:
ok 4 tokens
Test #14:
score: 0
Accepted
time: 4693ms
memory: 33848kb
input:
4 6 49429002 2 1 2 4 2 6 6 5 3 6 7 45760900 5 7 7 2 6 2 7 1 3 2 4 7 6 47061835 6 5 6 3 6 1 2 6 6 4 4 47410821 1 4 1 3 3 2
output:
536172407 533054157 561317786 749859281
result:
ok 4 tokens
Test #15:
score: 0
Accepted
time: 4816ms
memory: 33700kb
input:
4 6 49726215 1 6 2 6 6 3 6 5 2 4 2 50000000 1 2 8 49331348 7 3 7 8 5 6 4 3 6 3 2 4 1 7 5 45313556 2 3 4 3 3 5 5 1
output:
429791300 656547393 307298104 165844147
result:
ok 4 tokens
Test #16:
score: 0
Accepted
time: 3547ms
memory: 31944kb
input:
4 2 1 2 1 6 49045599 4 2 6 1 6 2 2 3 6 5 7 47403164 5 4 4 7 6 4 6 1 3 1 2 1 5 45061409 2 4 2 1 5 4 3 4
output:
1 403846093 637856203 454170631
result:
ok 4 tokens
Test #17:
score: 0
Accepted
time: 4741ms
memory: 32656kb
input:
4 4 48454794 2 4 4 1 3 2 5 48326937 5 1 5 4 3 2 3 1 5 48676454 3 4 4 1 5 1 1 2 6 48862565 6 2 4 2 5 2 2 1 2 3
output:
346937787 946110101 893634681 642310081
result:
ok 4 tokens
Test #18:
score: 0
Accepted
time: 4631ms
memory: 32164kb
input:
4 5 46095989 3 1 4 1 1 2 1 5 5 46073375 4 2 5 4 3 4 1 4 5 48531505 1 5 4 5 2 5 5 3 6 47558014 2 3 6 4 3 5 4 1 5 6
output:
128951803 883389586 226899121 189625473
result:
ok 4 tokens
Test #19:
score: 0
Accepted
time: 4764ms
memory: 33728kb
input:
4 4 45855350 1 4 2 1 4 3 3 49740257 1 2 3 2 4 46480829 1 2 3 1 4 2 4 49937459 1 2 4 2 4 3
output:
605823624 478005949 142328825 551484866
result:
ok 4 tokens
Test #20:
score: 0
Accepted
time: 4816ms
memory: 32748kb
input:
4 4 48451769 4 3 1 3 2 4 6 48177189 6 2 4 3 1 2 3 5 6 3 5 48577030 4 5 2 4 1 5 3 1 5 49630910 3 4 1 5 4 2 4 1
output:
280695405 350730179 81027477 157985696
result:
ok 4 tokens
Test #21:
score: 0
Accepted
time: 4688ms
memory: 33268kb
input:
4 8 49687604 2 8 7 4 3 1 5 6 8 3 7 6 5 1 3 45328134 1 2 1 3 6 47773101 1 4 3 2 6 4 2 4 5 4 6 46139567 2 6 2 3 6 4 2 5 1 2
output:
851791018 997976249 56174545 995045254
result:
ok 4 tokens
Test #22:
score: 0
Accepted
time: 4863ms
memory: 32204kb
input:
4 5 49796273 2 5 1 3 5 1 3 4 5 46614868 3 4 5 4 2 4 1 4 4 49876006 3 4 2 1 3 2 5 49208056 2 1 4 2 2 5 1 3
output:
844326175 597753235 196308105 418510131
result:
ok 4 tokens
Test #23:
score: 0
Accepted
time: 4666ms
memory: 33860kb
input:
4 4 45326185 3 1 2 3 3 4 3 50000000 2 3 1 3 8 48919728 6 1 4 8 4 3 7 8 2 1 8 1 1 5 4 45882405 4 1 1 3 2 4
output:
830463674 893434183 29400274 505479262
result:
ok 4 tokens
Test #24:
score: 0
Accepted
time: 4750ms
memory: 33416kb
input:
4 4 45981477 4 1 1 3 1 2 4 50000000 2 3 3 4 1 4 6 49644522 5 4 1 2 6 3 1 6 3 5 4 46917732 2 4 3 2 2 1
output:
441930004 891846827 12660622 308147414
result:
ok 4 tokens
Test #25:
score: 0
Accepted
time: 4724ms
memory: 32440kb
input:
4 5 46558871 2 4 3 1 3 5 5 4 7 48040830 6 7 1 2 5 2 7 3 7 4 7 1 5 48622955 4 3 1 4 4 5 1 2 8 48009463 3 5 3 4 1 8 7 4 1 4 6 3 4 2
output:
819848891 31449847 684770567 371025692
result:
ok 4 tokens
Test #26:
score: 0
Accepted
time: 4681ms
memory: 32760kb
input:
4 6 48976172 6 4 6 1 2 5 1 5 2 3 5 47341555 1 5 4 1 2 3 3 4 5 47504872 5 1 5 3 2 4 2 5 5 45523046 4 1 4 5 3 4 5 2
output:
62444387 286055466 933929990 389184808
result:
ok 4 tokens
Test #27:
score: 0
Accepted
time: 3518ms
memory: 33356kb
input:
4 6 47135616 3 5 1 6 1 4 1 2 3 2 4 48171807 3 1 3 2 2 4 7 46881593 5 3 4 1 1 3 7 5 5 2 1 6 2 2 1 2
output:
647945243 159077979 837172093 65
result:
ok 4 tokens
Test #28:
score: 0
Accepted
time: 4679ms
memory: 33172kb
input:
4 6 45340510 6 1 2 4 5 1 3 4 4 5 5 49475059 5 2 4 1 1 3 4 2 4 45780627 4 3 3 1 1 2 4 49033871 4 1 4 3 2 4
output:
287357295 935929856 762506483 789460951
result:
ok 4 tokens
Test #29:
score: 0
Accepted
time: 4732ms
memory: 33264kb
input:
4 6 47361583 2 6 5 1 5 3 5 6 4 2 6 49346013 5 1 1 4 6 1 5 2 6 3 85 50000000 51 44 13 20 14 9 46 70 5 13 7 34 33 16 23 58 33 30 67 13 3 70 8 33 38 42 7 44 40 58 70 31 23 26 50 27 7 79 28 68 74 44 37 76 23 84 58 62 59 58 12 65 59 57 60 66 81 83 20 45 24 72 29 70 70 1 64 73 63 47 53 23 27 44 74 19 38 6...
output:
557083956 521912976 508811400 923917013
result:
ok 4 tokens
Test #30:
score: 0
Accepted
time: 4568ms
memory: 33416kb
input:
4 4 48048505 3 2 4 1 1 2 6 45752957 4 3 6 5 1 2 2 6 6 4 7 45027209 4 1 6 4 3 2 2 1 5 7 6 7 2 45788884 2 1
output:
797762478 105794984 368141529 925907990
result:
ok 4 tokens
Extra Test:
score: 0
Extra Test Passed