QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#812978#72. Mergersguleng2007#0 48ms29852kbC++201.5kb2024-12-13 20:20:022024-12-13 20:20:03

Judging History

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

  • [2024-12-13 20:20:03]
  • 评测
  • 测评结果:0
  • 用时:48ms
  • 内存:29852kb
  • [2024-12-13 20:20:02]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

const int N=5e5+5;

int fa[N][20], dep[N], cnt[N], sum[N];
vector <int> g[N], vec[N];

void dfs1(int x)
{
	for(int i=1;i<20;i++)
		fa[x][i]=fa[fa[x][i-1]][i-1];

	for(auto to:g[x])
		if(to!=fa[x][0])
		{
			dep[to]=dep[x]+1;
			fa[to][0]=x;
			dfs1(to);
		}
}

int lca(int x,int y)
{
	if(dep[x]<dep[y])
		swap(x,y);

	for(int i=19;i>=0;i--)
		if(dep[fa[x][i]]>=dep[y])
			x=fa[x][i];

	if(x==y)
		return x;

	for(int i=19;i>=0;i--)
		if(fa[x][i]!=fa[y][i])
			x=fa[x][i], y=fa[y][i];

	return fa[x][0];
}

int dfsn[N], sz[N], indx;

void dfs2(int x)
{
	dfsn[x]=++indx, sz[x]=1;
	for(auto to:g[x])
		if(to!=fa[x][0])
			dfs2(to), cnt[x] += cnt[to], sz[x] += sz[to];
}

int main()
{
	int n,k;
	cin >> n >> k;
	for(int i=1;i<n;i++)
	{
		int x,y;
		scanf("%d %d",&x,&y);
		g[x].push_back(y);
		g[y].push_back(x);
	}

	dep[1]=1, dfs1(1);

	for(int i=1;i<=n;i++)
	{
		int typ;
		scanf("%d",&typ);
		vec[typ].push_back(i);
	}

	for(int i=1;i<=k;i++)
	{
		if(vec[i].size()==0)
			continue;

		for(int j=0;j<vec[i].size()-1;j++)
			cnt[vec[i][j]]++, cnt[vec[i][j+1]]++, cnt[lca(vec[i][j],vec[i][j+1])] -= 2;
	}

	dfs2(1);

	for(int i=2;i<=n;i++)
		if(cnt[i]==0)
			sum[dfsn[i]]++;

	for(int i=1;i<=n;i++)
		sum[i] += sum[i-1];

	int ans=0;
	for(int i=2;i<=n;i++)
		if(cnt[i]==0)
		{
			if(sum[dfsn[i]+sz[i]-1]-sum[dfsn[i]-1]==0 || sum[dfsn[i]+sz[i]-1]-sum[dfsn[i]-1]==sum[n])
				ans++;
		}

	printf("%d\n",(ans+1)/2);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 10
Accepted
time: 0ms
memory: 8060kb

input:

1 1
1

output:

0

result:

ok single line: '0'

Test #2:

score: 10
Accepted
time: 0ms
memory: 12076kb

input:

3 2
2 1
3 1
2
2
1

output:

1

result:

ok single line: '1'

Test #3:

score: 10
Accepted
time: 2ms
memory: 9920kb

input:

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

output:

0

result:

ok single line: '0'

Test #4:

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

input:

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

output:

1

result:

wrong answer 1st lines differ - expected: '2', found: '1'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Skipped

Dependency #2:

0%

Subtask #4:

score: 0
Wrong Answer

Test #66:

score: 0
Wrong Answer
time: 48ms
memory: 29852kb

input:

100000 100000
14957 4585
67467 70858
61843 47396
50630 17382
61027 39858
94990 21698
10240 22940
23505 67581
91432 14182
22040 40125
24556 60351
75822 41519
82801 23601
90653 29138
85096 34582
99587 59109
8932 45189
18235 36632
43160 14939
67600 76675
60175 65542
99294 53955
46429 66931
16822 48854
...

output:

0

result:

wrong answer 1st lines differ - expected: '25042', found: '0'

Subtask #5:

score: 0
Skipped

Dependency #1:

0%