QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#185510#2550. Lion and ZebrakkioWA 0ms17780kbC++204.3kb2023-09-22 10:35:072023-09-22 10:35:07

Judging History

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

  • [2023-09-22 10:35:07]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:17780kb
  • [2023-09-22 10:35:07]
  • 提交

answer

#include <bits/stdc++.h>
//#include "PrettyDebug.hpp"
using namespace std;
namespace FastIO {
	struct IO {
	    char ibuf[(1 << 20) + 1], *iS, *iT, obuf[(1 << 20) + 1], *oS;
	    IO() : iS(ibuf), iT(ibuf), oS(obuf) {} ~IO() { fwrite(obuf, 1, oS - obuf, stdout); }
		#if ONLINE_JUDGE
		#define gh() (iS == iT ? iT = (iS = ibuf) + fread(ibuf, 1, (1 << 20) + 1, stdin), (iS == iT ? EOF : *iS++) : *iS++)
		#else
		#define gh() getchar()
		#endif
		inline bool eof (const char &ch) { return ch == ' ' || ch == '\n' || ch == '\r' || ch == 't' || ch == EOF; }
	    inline long long read() {
	        char ch = gh();
	        long long x = 0;
	        bool t = 0;
	        while (ch < '0' || ch > '9') t |= ch == '-', ch = gh();
	        while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = gh();
	        return t ? ~(x - 1) : x;
	    }
	    inline void read (char *s) {
	    	char ch = gh(); int l = 0;
	    	while (eof(ch)) ch = gh();
	    	while (!eof(ch)) s[l++] = ch, ch = gh();
	    }
	    inline void read (double &x) {
	    	char ch = gh(); bool t = 0;
	    	while (ch < '0' || ch > '9') t |= ch == '-', ch = gh();
	    	while (ch >= '0' && ch <= '9') x = x * 10 + (ch ^ 48), ch = gh();
	    	if (ch != '.') return t && (x = -x), void(); ch = gh();
	    	for (double cf = 0.1; '0' <= ch && ch <= '9'; ch = gh(), cf *= 0.1) x += cf * (ch ^ 48);
	    	t && (x = -x);
	    }
	    inline void pc (char ch) {
	    	#ifdef ONLINE_JUDGE
	    	if (oS == obuf + (1 << 20) + 1) fwrite(obuf, 1, oS - obuf, stdout), oS = obuf; 
	    	*oS++ = ch;
	    	#else
	    	putchar(ch);
	    	#endif
		}
		template<typename _Tp>
	    inline void write (_Tp x) {
	    	static char stk[64], *tp = stk;
	    	if (x < 0) x = ~(x - 1), pc('-');
			do *tp++ = x % 10, x /= 10;
			while (x);
			while (tp != stk) pc((*--tp) | 48);
	    }
	    inline void write (char *s) {
	    	int n = strlen(s);
	    	for (int i = 0; i < n; i++) pc(s[i]);
	    }
	} io;
	inline long long read () { return io.read(); }
	template<typename Tp>
	inline void read (Tp &x) { io.read(x); }
	template<typename _Tp>
	inline void write (_Tp x) { io.write(x); }
}
using namespace FastIO;
constexpr int maxn=1e5+10;
int n,q;vector<int> G[maxn];
int anc[maxn][19],dep[maxn];
inline void ancdfs(int u,int fa)
{
	anc[u][0]=fa;dep[u]=dep[fa]+1;
	for(int i=1;i<=18;i++)anc[u][i]=anc[anc[u][i-1]][i-1];
	for(int v:G[u])
		if(v!=fa)
			ancdfs(v,u);
}
inline int getlca(int x,int y)
{
	if(dep[x]<dep[y])swap(x,y);
	for(int i=18;i>=0;i--)if(dep[anc[x][i]]>=dep[y])x=anc[x][i];
	if(x==y)return x;
	for(int i=18;i>=0;i--)if(anc[x][i]^anc[y][i])x=anc[x][i],y=anc[y][i];
	return anc[x][0];
}
inline int k_anc(int x,int k)
{
	for(int i=18;i>=0;i--)if(k>>i&1)x=anc[x][i];
	return x;
}
inline int pathto(int x,int y,int d)
{
	int z=getlca(x,y);
	int disxz=dep[x]-dep[z],diszy=dep[y]-dep[z];
	if(disxz>=d)return k_anc(x,d);
	d=disxz+diszy-d;return k_anc(y,d);
}
int upd[maxn],dwd[maxn],ftd[maxn],udd[maxn];
set<int> sdep[maxn];
void downdfs(int u,int fa)
{
	ftd[u]=u,dwd[u]=0;
	udd[u]=u,upd[u]=0;
	for(int v:G[u])
		if(v!=fa)
		{
			downdfs(v,u);
			dwd[u]=max(dwd[u],dwd[v]+1);
			if(dwd[u]==dwd[v]+1)ftd[u]=ftd[v]; 
			sdep[u].insert(dwd[v]);
		}
}
void updfs(int u,int fa)
{
	int mxd1=-1e9,pd=0,mxd2=-1e9,gd=0;
	for(int v:G[u])
		if(v!=fa)
		{
			if(dwd[v]+1>mxd1)mxd2=mxd1,mxd1=dwd[v]+1,gd=pd,pd=v;
			else if(dwd[v]+1>mxd2)mxd2=dwd[v]+1,gd=v;
		}
	for(int v:G[u])
		if(v!=fa)
		{
			if(v!=pd)upd[v]=upd[u]>mxd1?(udd[v]=udd[u],upd[u]+1):(udd[v]=ftd[pd],mxd1+1);
			else upd[v]=upd[u]>mxd2?(udd[v]=udd[u],upd[u]+1):(udd[v]=ftd[gd],mxd2+1);
			sdep[v].insert(upd[v]-1);
			updfs(v,u);
		}
}
int main()
{
	n=read(),q=read();
	for(int i=1,u,v;i<n;i++)
	{
		u=read(),v=read();
		G[u].push_back(v);G[v].push_back(u);
	}
	ancdfs(1,1);
	downdfs(1,1);
	updfs(1,1);
	for(int t=1;t<=q;t++)
	{
		int v=read(),d=read();
		int s=dwd[v]>upd[v]?ftd[v]:udd[v];
		//debug(s,dwd[v],upd[v]);
		int ed=(d-1)/2,c=pathto(v,s,ed),step=0;
		//debug(ed,c);
		if(d>2)
		{
			int p=pathto(v,s,ed-1);
			//debug(p,dwd[p],upd[p]);
			if(dep[c]<dep[p])step=max(step,dwd[p]+1);
			else step=max(step,upd[p]+1); 
		}
		if(d%2==0&&sdep[c].count(d/2))step=max(step,d/2);
		//debug(step);
		int ans=step+d-ed;
		write(ans),io.pc('\n'); 
	}
}


詳細信息

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 17780kb

input:

5 2
1 2
2 3
3 4
4 5
1 4
3 1

output:

5
1

result:

wrong answer 1st numbers differ - expected: '4', found: '5'