QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#155528#7118. Closing TimeMyrrhCompile Error//C++145.1kb2023-09-01 18:47:552024-04-28 06:46:04

Judging History

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

  • [2024-04-28 06:46:04]
  • 管理员手动重测本题所有提交记录
  • [2023-09-01 18:47:56]
  • 评测
  • [2023-09-01 18:47:55]
  • 提交

answer

#include <bits/stdc++.h>
/*
1
10 0 2 8670529
1 0 258689
2 0 352031
3 2 985731
4 2 701607
5 3 643693
6 5 572137
7 3 797441
8 0 496749
9 6 842815
*/
using namespace std;

const int MAXN=1e6+50;

int N,X,Y;
long long K;
struct Edge
{
	int x,y,Next;
	long long Len;
}e[MAXN<<1];
int elast[MAXN],tot;
void Add(int x,int y,long long Len)
{
	tot++;
	e[tot].x=x;
	e[tot].y=y;
	e[tot].Len=Len;
	e[tot].Next=elast[x];
	elast[x]=tot;
}

long long dis[2][MAXN];
int father[MAXN];
void dfs(int u,int fa,int op)
{
	father[u]=fa;
	for(int i=elast[u];i;i=e[i].Next)
	{
		int v=e[i].y;
		if(v==fa)
		continue;
		dis[op][v]=dis[op][u]+e[i].Len;
		dfs(v,u,op);
	}
}
struct Dot
{
	int Id;
	long long Val;
	Dot(){}
	Dot(int _Id,long long _Val)
	{
		Id=_Id;
		Val=_Val;
	}
};
bool operator >(Dot a,Dot b)
{
	if(a.Val==b.Val)
	return a.Id>b.Id;
	return a.Val>b.Val;
}
int Cnt[MAXN];
priority_queue<Dot,vector<Dot>,greater<Dot> >q1,q2;
long long Min[MAXN],Max[MAXN];
int ans;

void Solve1(int ans1,long long Now,bool op)
{
	while(true)
	{
	//	puts("ee");
		while(!q2.empty()&&Cnt[q2.top().Id]==2)
		q2.pop();
		while(!q1.empty()&&Cnt[q1.top().Id]==3)
		q1.pop();
		Dot a1=q1.top();
		q1.pop();
		while(!q1.empty()&&Cnt[q1.top().Id]==3)
		q1.pop();
		Dot a2=q1.top();
		q1.pop();
		Dot b1=q2.top();
		q2.pop();//cout<<"Now:"<<a1.Id<<" "<<a1.Val<<" "<<a2.Id<<" "<<a2.Val<<" "<<b1.Val<<endl;
		if(Now+min(a1.Val+a2.Val,b1.Val)>K)
		{
			ans1-=op;
			ans=max(ans,ans1);
	//		cout<<ans1<<endl;
			return;
		}
		
		if(a1.Val+a2.Val<b1.Val)
		{
	//		cout<<"选:"<<a1.Id<<" "<<a2.Id<<endl;
			Now+=a1.Val+a2.Val;
			ans1++;
			if(Cnt[a1.Id]==1)
			{
				Cnt[a1.Id]++;
				q1.push(Dot(a1.Id,Max[a1.Id]-Min[a1.Id]));
			}
			ans1++;
			if(Cnt[a2.Id]==1)
			{
				Cnt[a2.Id]++;
				q1.push(Dot(a2.Id,Max[a2.Id]-Min[a2.Id]));
			}
			q2.push(b1);
		}
		else
		{
			ans1+=2;
	//		cout<<"选2:"<<b1.Id<<endl;
			Now+=b1.Val;
			Cnt[b1.Id]=3;
			q1.push(a1);
			q1.push(a2);
		}
	}
//	cout<<ans1<<endl;
}
void Solve2()
{
	long long Now=0;
	int ans1=0;
	while(q1.size()>=1)
	{
		Dot a1=q1.top();
		q1.pop();
		if(Now+a1.Val<=K)
		{
	//		cout<<"选1:"<<a1.Id<<endl;
			Now+=a1.Val;
			ans1++;
		}
	}
	ans=max(ans1,ans);
}
bool OnTree[MAXN];
/*
1
10 5 0 55
1 0 8
2 0 4
3 1 3
4 1 6
5 2 7
6 1 7
7 2 7
8 5 7
9 8 10
*/
void Solve()
{
	ans=0;
	scanf("%d%d%d%lld",&N,&X,&Y,&K);
	X++;
	Y++;
	for(int i=1;i<N;i++)
	{
		int x,y;
		long long Len;
		scanf("%d%d%lld",&x,&y,&Len);
		x++;
		y++;
		Add(x,y,Len);
		Add(y,x,Len);
	}
	for(int i=0;i<=2*N;i++)
	{
		OnTree[i]=false;
		father[i]=0;
		dis[0][i]=dis[1][i]=0;
	}
	dis[0][X]=0;
	dis[1][Y]=0;
	dfs(X,0,0);
	int u=Y;
	while(u)
	{
		OnTree[u]=true;
	//	cout<<"ON:"<<u<<endl;
		if(u==X)
		break;
		u=father[u];
	}
	
	
	dfs(Y,0,1);	
	/*
	for(int i=1;i<=N;i++)
	{
		cout<<i<<" "<<dis[0][i]<<" "<<dis[1][i]<<endl;
	}
	*/
	Min[N+1]=Max[N+1]=K+1;
	Min[N+2]=Max[N+2]=K+1;
	while(!q1.empty())
	q1.pop();
	while(!q2.empty())
	q2.pop();
	for(int i=1;i<=N;i++)
	{
		Cnt[i]=2;
		q1.push(Dot(i,dis[0][i]));
		Min[i]=min(dis[0][i],dis[1][i]);
		Max[i]=max(dis[0][i],dis[1][i]);
	//	cout<<"MINMAX:"<<i<<" "<<Min[i]<<" "<<Max[i]<<endl;
	}

	for(int i=N+1;i<=2*N;i++)
	{
		Cnt[i]=2;
		q1.push(Dot(i,dis[1][i-N]));
	}
	//puts("ans1:");
	Solve2();
	/*
	if(dis[0][Y]<2*K)
	{
		assert(0);
		return;
	}
	printf("%d\n",ans);
	tot=0;
	for(int i=0;i<=N;i++)
	{
		elast[i]=Cnt[i]=0;
	}
	return;
	*/
	int ans1=0;
	while(!q1.empty())
	q1.pop();
	while(!q2.empty())
	q2.pop();
	long long Now=0;
	for(int i=1;i<=N+2;i++)
	{
		if(OnTree[i]==false)
		{
			Cnt[i]=1;
			q1.push(Dot(i,Min[i]));
			q2.push(Dot(i,Max[i]));		
		}
		else
		{
			Now+=Min[i];
			ans1++;
			Cnt[i]=2;
			q1.push(Dot(i,Max[i]-Min[i]));
		}
	}
	
//	puts("ans2:");
	if(Now<=K)
	Solve1(ans1,Now,0);
/*	
	for(int i=0;i<=N+2;i++)
	{
		cout<<"Cnt:"<<i<<" "<<Cnt[i]<<endl;
	}
*/	
	while(!q1.empty())
	q1.pop();
	while(!q2.empty())
	q2.pop();
	ans1=0;
	Now=0;
	Cnt[0]=2;
	q1.push(Dot(0,0));
	for(int i=1;i<=N+2;i++)
	{
		if(OnTree[i]==false)
		{
			Cnt[i]=1;
			q1.push(Dot(i,Min[i]));
			q2.push(Dot(i,Max[i]));
		}
		else
		{
			ans1++;
			Now+=Min[i];
			Cnt[i]=2;
			q1.push(Dot(i,Max[i]-Min[i]));
		}
	}
	//puts("ans3:");
	if(Now<=K)
	Solve1(ans1,Now,1);
	/*
	for(int i=0;i<=N+2;i++)
	cout<<"Cnt:"<<i<<" "<<Cnt[i]<<endl;*/
	printf("%d\n",ans);
	
	tot=0;
	for(int i=0;i<=2*N;i++)
	{
		elast[i]=Cnt[i]=0;
	}
}


int main()
{
//	freopen("Data.txt","r",stdin);
	int T;
	scanf("%d",&T);
	while(T--)
	{
		Solve();
	}
}
/*
stuff you should look for
	* int overflow, array bounds
	* special cases (n=1?)
	* i and j are misplaced
	* variable misuse
	* do smth instead of nothing and stay organized
	* vector cause MLE or TLE
	* wrong file name
	* DON'T SLEEP IN THE EXAM
	* WRITE STUFF DOWN
	* DON'T GET STUCK ON ONE APPROACH
	* DON'T GET A SWELLED HEAD
	* YOU MUSTN'T HAVE EMOTION
->be based on Benq's code
*/

詳細信息

answer.code: In function ‘void Solve()’:
answer.code:162:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  162 |         scanf("%d%d%d%lld",&N,&X,&Y,&K);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
answer.code:169:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  169 |                 scanf("%d%d%lld",&x,&y,&Len);
      |                 ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
answer.code: In function ‘int main()’:
answer.code:314:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  314 |         scanf("%d",&T);
      |         ~~~~~^~~~~~~~~
/usr/bin/ld: /tmp/cclOphb9.o: in function `main':
answer.code:(.text.startup+0x0): multiple definition of `main'; /tmp/cctyFrK6.o:implementer.cpp:(.text.startup+0x0): first defined here
/usr/bin/ld: /tmp/cctyFrK6.o: in function `main':
implementer.cpp:(.text.startup+0x744): undefined reference to `max_score(int, int, int, long long, std::vector<int, std::allocator<int> >, std::vector<int, std::allocator<int> >, std::vector<int, std::allocator<int> >)'
collect2: error: ld returned 1 exit status