QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#381572#8570. Idola-Treeucup-team1004WA 3ms14212kbC++172.2kb2024-04-07 19:03:582024-04-07 19:04:02

Judging History

你现在查看的是最新测评结果

  • [2024-04-07 19:04:02]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:14212kb
  • [2024-04-07 19:03:58]
  • 提交

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),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: 3ms
memory: 11336kb

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: -100
Wrong Answer
time: 3ms
memory: 14212kb

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
314432
1255624

result:

wrong answer 3rd words differ - expected: '54872', found: '314432'