QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#480888#7245. Frank SinatraHadtstiWA 2ms10544kbC++141.8kb2024-07-16 19:25:572024-07-16 19:25:57

Judging History

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

  • [2024-07-16 19:25:57]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:10544kb
  • [2024-07-16 19:25:57]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
int T,ID[100010],n,q,x,y,z,fa[100010],a[100010],num,st[200010],ed[200010],rk[200010],cnt[100010],s[310],ll[310],rr[310],ans[100010];
bool fg[100010];
vector<pair<int,int> >E[100010];
struct query
{
	int l,r,id;
	bool k;
	friend bool operator <(const query &a,const query &b)
	{
		return ID[a.l]^ID[a.l]?ID[a.l]<ID[b.l]:(ID[a.l]&1?a.r<b.r:a.r>b.r);
	}
}Q[100010];
void dfs(int x)
{
	st[x]=++num;
	rk[num]=x;
	for(pair<int,int>i:E[x])
	{
		int y=i.first,z=i.second;
		if(y!=fa[x])
		{
			fa[y]=x;
			a[y]=z+1;
			dfs(y);
		}
	}
	rk[++num]=x;
	ed[x]=num;
}
inline void update(int x)
{
	if(fg[x]^=1)
	{
		if(!(cnt[a[x]]++))
			s[ID[a[x]]]++;
	}
	else
			if(!(--cnt[a[x]]))
				s[ID[a[x]]]--;	
} 
inline int calc()
{
	for(int i=1;i<=ID[n+2];i++)
		if(s[i]<rr[i]-ll[i]+1)
		{
			for(int j=ll[i];j<=rr[i];j++)
				if(!cnt[j])
					return j-1;
		}
	return n;
}
int main()
{
	scanf("%d%d",&n,&q);
	T=sqrt(n);
	for(int i=1;i<n;i++)
	{
		scanf("%d%d%d",&x,&y,&z);
		if(z>n)
			z=n+1;
		E[x].push_back({y,z});
		E[y].push_back({x,z});
	}
	for(int i=1;i<=2*n;i++)
		ID[i]=(i-1)/T+1,rr[ID[i]]=i;
	for(int i=2*n;i>=1;i--)
		ll[ID[i]]=i;
	dfs(1);
	for(int i=1;i<=q;i++)
	{
		scanf("%d%d",&x,&y);
		if(st[x]>st[y])
			swap(x,y);
		Q[i]={ed[x],st[y],i,0};
		if(st[x]<=st[y]&&ed[x]>=ed[y])
			Q[i].k=1,Q[i].l=st[x];
	}
	sort(Q+1,Q+q+1);
	for(int i=1,L=1,R=0;i<=q;i++)
	{
		while(R<Q[i].r)
			update(rk[++R]);
		while(L>Q[i].l)
			update(rk[--L]);
		while(R>Q[i].r)
			update(rk[R--]);
		while(L<Q[i].l)
			update(rk[L++]);
		if(Q[i].k)
			update(Q[i].l);
		ans[Q[i].id]=calc();
		if(Q[i].k)
			update(Q[i].l);
	}
	for(int i=1;i<=q;i++)
		printf("%d\n",ans[i]);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 10012kb

input:

7 6
2 1 1
3 1 2
1 4 0
4 5 1
5 6 3
5 7 4
1 3
4 1
2 4
2 5
3 5
3 7

output:

0
1
2
2
3
3

result:

ok 6 numbers

Test #2:

score: 0
Accepted
time: 1ms
memory: 8012kb

input:

2 4
1 2 0
1 1
1 2
2 1
2 2

output:

0
1
1
0

result:

ok 4 number(s): "0 1 1 0"

Test #3:

score: -100
Wrong Answer
time: 1ms
memory: 10544kb

input:

10 100
3 7 1
3 1 0
6 1 1
1 9 0
4 1 1
5 8 1
10 6 0
1 2 1
5 3 1
6 1
4 10
6 5
3 3
5 8
9 2
1 3
8 4
8 5
10 10
5 2
7 10
2 10
8 9
5 3
9 4
6 2
1 8
4 7
3 9
2 5
3 7
10 7
2 2
6 6
6 7
1 9
7 8
6 8
7 3
5 10
5 1
1 2
10 8
8 7
4 2
2 3
7 6
2 9
5 4
10 3
2 4
10 6
3 6
7 4
5 6
10 4
8 2
1 4
9 10
7 9
3 5
9 8
7 2
1 1
7 1
7 ...

output:

0
2
2
2
0
2
1
2
0
1
2
2
2
2
2
2
0
2
2
1
2
2
2
0
2
2
1
0
2
2
2
2
0
2
0
0
2
2
2
2
2
0
0
2
2
2
2
2
0
2
2
2
2
2
0
2
2
2
0
2
2
2
2
2
1
2
2
0
2
0
0
2
2
1
0
2
2
2
2
0
2
0
2
2
2
0
0
0
0
1
2
0
2
2
1
2
2
2
2
2

result:

wrong answer 4th numbers differ - expected: '0', found: '2'