萌新求助
查看原帖
萌新求助
174897
zjrdmd楼主2020/8/30 19:51

RT,感觉复杂度没假啊qwq,但是TLE了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <stdlib.h>
#include <stack>
#include <queue>
#define ri register int
#define ll long long
#define int long long

inline int read() {
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9') {if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9') {x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
	return x*f;
}

const int N=2e5+5;

struct node{
	int w,sum;
	bool operator<(const node x)const {
		return (x.w)/2*x.sum>w/2*sum;
	} 
};

std::priority_queue<node>q;
int n;
int siz[N];
int to[N<<1],next[N<<1],head[N],cnt=1;
int son[N<<1];
ll val[N<<1];             
ll S,ans=0;

void edge_add(int u,int v,ll w){
	to[cnt]=v;
	next[cnt]=head[u];
	val[cnt]=w;
	head[u]=cnt++;
}

void dfs(int now,int fa){
	bool check=false;
	for(ri i=head[now];i;i=next[i]){
		int v=to[i];
		if(v==fa)continue;
		check=true;
		son[i]=v;
		dfs(v,now);
		siz[now]+=siz[v]; 
	}
	if(check==false)siz[now]=1;
}

void work(){
	int tot=0;
	while(q.size()){
		if(ans<=S)break;
		node p=q.top();
		q.pop();
		q.push((node){p.w/2,p.sum});
		ans-=p.w*p.sum;
		ans+=p.w/2*p.sum;
		tot++; 
	} 
	printf("%lld\n",tot);
}

void clear(){
	ans=0;
	for(ri i=1;i<=n*2;i++)son[i]=0,siz[i]=0,head[i]=0,val[i]=0,to[i]=0,next[i]=0;
	cnt=1;
	while(q.size())q.pop();
}

signed main(void){
	int T=read();
	while(T--){
		n=read();scanf("%lld",&S);
		int u,v;ll w;
		for(ri i=1;i<n;i++){
			u=read(),v=read(),scanf("%lld",&w);
			edge_add(u,v,w);
			edge_add(v,u,w);
		} 
		dfs(1,0);
		for(ri i=1;i<=(n<<1);i++){
			if(son[i])q.push((node){val[i],siz[son[i]]}),ans+=val[i]*siz[son[i]];
		}
		work();
		clear();
	}
	return 0;
}

/*
1
5 50
1 3 100
1 5 10
2 3 123
5 4 55

8
*/

/*
3
3 20
2 1 8
3 1 7
5 50
1 3 100
1 5 10
2 3 123
5 4 55
2 100
1 2 409

0
8
3
*/

/*
1
4 250
1 2 100
2 3 100
2 4 100

1
*/


2020/8/30 19:51
加载中...