/ Randle /

记录详情

Wrong Answer

/in/foo.cc: In function 'const int solve(int)':
/in/foo.cc:71:30: warning: unused variable 'j' [-Wunused-variable]
   int k=deep[s]-deep[lca[i]],j=0;
                              ^
/in/foo.cc:78:30: warning: unused variable 'j' [-Wunused-variable]
   int k=deep[e]-deep[lca[i]],j=0;
                              ^
# 状态 耗时 内存占用
#1 Wrong Answer 7ms 24.684 MiB
#2 Wrong Answer 7ms 24.812 MiB
#3 Wrong Answer 7ms 24.805 MiB
#4 Wrong Answer 30ms 23.184 MiB
#5 Wrong Answer 32ms 24.809 MiB
#6 Accepted 32ms 23.312 MiB
#7 Wrong Answer 100ms 23.551 MiB
#8 Wrong Answer 105ms 25.559 MiB
#9 Wrong Answer 110ms 25.188 MiB
#10 Wrong Answer 127ms 26.551 MiB

代码

#include<bits/stdc++.h>
const int maxn=4e5;
inline const void read(int &a)
{
	a=0;
	char c=getchar();
	while(c<'0'||c>'9')c=getchar();
	while(c>='0'&&c<='9')
	{
		a=(a<<1)+(a<<3)+c-'0';
		c=getchar();
	}
}
inline const void write(int a)
{
	if(a>9)write(a/10);
	putchar(a%10+'0');
}
int n,m,d=0,l=0;
int deep[maxn],point[maxn],next[maxn],to[maxn],len[maxn],belong[maxn],f[maxn],father[maxn],fatherside[maxn];
int lca[maxn],exist[maxn],askto[maxn],askpoint[maxn],asknext[maxn],askbelong[maxn],start[maxn],end[maxn];
bool been[maxn];
inline const void add(int a,int b,int c)
{
	next[++d]=point[a];
	point[a]=d;
	to[d]=b;
	len[d]=c;
}
inline const void askadd(int a,int b,int c)
{
	asknext[++l]=askpoint[a];
	askpoint[a]=l;
	askto[l]=b;
	askbelong[l]=c;
}
inline const int check(int p)
{
	if(p==belong[p])return p;
	else return belong[p]=check(belong[p]);
}
const void tarjan(int p)
{
	been[p]=true;
	int side=point[p];
	while(side)
	{
		if(!been[to[side]])
		{
			father[to[side]]=p;
			fatherside[to[side]]=side;
			deep[to[side]]=deep[p]+1;
			tarjan(to[side]);
			f[p]+=f[to[side]]+len[side];
			belong[to[side]]=p;
		}
		side=next[side];
	}
	side=askpoint[p];
	while(side)
	{
		if(been[askto[side]])lca[askbelong[side]]=check(askto[side]);
		side=asknext[side];
	}
}
inline const int solve(int i)
{
	int s=start[i],e=end[i];
	if(lca[i]==end[i])
	{
		int k=deep[s]-deep[lca[i]],j=0;
		k=k-k/2;
		while(k){k--;s=father[s];}
		return f[s];
	}
	if(lca[i]==start[i])
	{
		int k=deep[e]-deep[lca[i]],j=0;
		k=k/2;
		e=end[i];
		while(k){k--;e=father[e];}
		return f[1]-f[e];
	}
	else
	{
		int k=deep[s]-deep[lca[i]],p=deep[e]-deep[lca[i]],r=k+p;
		k=r-r/2;p=r/2;
		while(k&&s!=lca[i]){k--;s=father[s];}
		if(k)
		{
			std::stack<int>path;
			while(e!=lca[i]){path.push(e);e=father[e];}
			while(k)
			{
				s=path.top();
				path.pop();
				k--;
			}
			return f[1]-f[s];
		}
		else
		{
			if(s==lca[i])
			{
				while(father[e]!=lca[i])e=father[e];
				return f[1]-f[e]-len[fatherside[e]];
			}
			else return f[s];
		}
	}
}
int main()
{
	//freopen("b.in","r",stdin);
	//freopen("b.out","w",stdout);
	memset(been,false,sizeof(been));
	memset(point,0,sizeof(point));
	memset(askpoint,0,sizeof(askpoint));
	read(n);read(m);
	for(int i=1;i<=n;i++)belong[i]=i;
	int x,y,z;
	for(int i=1;i<=n-1;i++)
	{
		read(x);read(y);read(z);
		add(x,y,z);add(y,x,z);
	}
	for(int i=1;i<=m;i++)
	{
		read(start[i]);read(end[i]);
		askadd(start[i],end[i],i);
		askadd(end[i],start[i],i);
	}
	deep[1]=0;
	tarjan(1);
	for(int i=1;i<=m;i++){write(solve(i));printf("\n");}
	return 0;
}

信息

递交者
类型
递交
题目
树上角斗 T2
题目数据
下载
语言
C++
递交时间
2017-10-02 13:17:56
评测时间
2017-10-02 13:17:56
评测机
分数
10
总耗时
561ms
峰值内存
26.551 MiB