QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#480821#7245. Frank SinatraHadtstiWA 1ms10080kbC++141.6kb2024-07-16 19:03:042024-07-16 19:03:04

Judging History

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

  • [2024-07-16 19:03:04]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:10080kb
  • [2024-07-16 19:03:04]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
int T,ID[100010],n,q,x,y,z,fa[100010],a[100010],num,dfn[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;
	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)
{
	dfn[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;
}
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<=n+2;i++)
		ID[i]=(i-1)/T+1,rr[ID[i]]=i;
	for(int i=n+2;i>=1;i--)
		ll[ID[i]]=i;
	dfs(1);
	for(int i=1;i<=q;i++)
	{
		scanf("%d%d",&x,&y);
		if(dfn[x]>dfn[y])
			swap(x,y);
		Q[i]={x,y,i};
	}
	sort(Q+1,Q+q+1);
	for(int i=1,L=1,R=0;i<=q;i++)
	{
		while(R<dfn[Q[i].r])
			update(rk[++R]);
		while(L>dfn[Q[i].l])
			update(rk[--L]);
		while(R>dfn[Q[i].r])
			update(rk[R--]);
		while(L<dfn[Q[i].l])
			update(rk[L++]);
		update(Q[i].l);
		ans[Q[i].id]=calc();
		update(Q[i].l);
	}
	for(int i=1;i<=q;i++)
		printf("%d\n",ans[i]);
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 7952kb

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: 10080kb

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: 0
Accepted
time: 1ms
memory: 8456kb

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
0
0
2
1
2
0
0
2
2
2
2
0
2
0
2
2
1
2
0
2
0
0
2
1
0
2
0
2
2
0
2
0
0
2
2
2
2
2
0
1
2
2
2
2
2
0
2
2
0
2
2
0
2
0
2
0
2
2
2
2
2
1
2
2
1
2
0
0
2
2
0
0
2
2
2
2
0
2
0
2
0
2
0
0
0
0
1
2
0
2
0
1
2
2
2
2
2

result:

ok 100 numbers

Test #4:

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

input:

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

output:

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

result:

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