QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#812961#72. Mergersguleng2007#0 66ms30100kbC++201.4kb2024-12-13 20:05:382024-12-13 20:05:44

Judging History

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

  • [2024-12-13 20:05:44]
  • 评测
  • 测评结果:0
  • 用时:66ms
  • 内存:30100kb
  • [2024-12-13 20:05:38]
  • 提交

answer

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

const int N=5e5+5;

int fa[N][20], dep[N], cnt[N], ans[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];
}

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

void dfs3(int x)
{
	for(auto to:g[x])
		if(to!=fa[x][0])
		{
			dfs3(to);
			ans[x] += ans[to];
		}

	if(!cnt[x] && x!=1 && ans[x]==0)
		ans[x]++;
}

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);
	dfs3(1);
	printf("%d\n",(ans[1]+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: 2ms
memory: 5812kb

input:

1 1
1

output:

0

result:

ok single line: '0'

Test #2:

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

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

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: 2ms
memory: 6004kb

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: 22
Accepted
time: 50ms
memory: 27576kb

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:

25042

result:

ok single line: '25042'

Test #67:

score: 22
Accepted
time: 66ms
memory: 25820kb

input:

100000 50000
3065 28396
57666 99424
91431 92279
85107 9397
40557 2609
33754 76986
41982 13262
29268 57814
57080 70949
46008 55628
16881 69517
7925 97656
11194 27261
1023 44957
63053 94265
10347 36227
57858 50853
6707 37237
14023 64077
97278 89836
45448 37054
47530 12666
1789 54939
97196 38225
46942 ...

output:

4686

result:

ok single line: '4686'

Test #68:

score: 22
Accepted
time: 63ms
memory: 22724kb

input:

100000 1000
15892 97873
74044 8515
2591 62904
30957 88449
38848 47109
51265 48273
55368 54375
67431 61662
74306 32695
87126 54432
72682 57299
66802 28200
3201 72387
26153 31276
44001 47337
27981 6477
15995 26102
90218 8030
97876 99025
38725 79748
61045 12996
27173 63491
31237 7298
64134 63770
59955 ...

output:

252

result:

ok single line: '252'

Test #69:

score: 22
Accepted
time: 51ms
memory: 25684kb

input:

100000 50000
33160 23295
28957 34869
60520 32863
7862 68797
48379 81124
95012 36902
86480 11178
89023 90581
98753 45510
95 163
50395 60900
89762 36279
56185 22354
15359 97979
61728 37941
68183 27192
52885 40031
54463 33490
22324 25911
44115 5401
62127 25301
91088 99953
1770 73224
78624 45860
87886 2...

output:

4466

result:

ok single line: '4466'

Test #70:

score: 22
Accepted
time: 64ms
memory: 30100kb

input:

100000 100000
55749 64307
73914 18165
55104 67680
34976 24633
3159 10270
97438 41767
63073 69455
57888 71630
93066 38126
39516 27458
45215 74459
19402 17568
91414 23901
84838 44332
86229 23656
41101 30330
35829 54890
34576 52494
61355 23809
68784 29220
60951 22840
14091 80771
98672 67030
55873 80581...

output:

1

result:

ok single line: '1'

Test #71:

score: 22
Accepted
time: 62ms
memory: 25296kb

input:

100000 10000
30268 39315
46960 33279
28973 55390
22427 61537
60481 79337
30617 20845
59764 27962
17169 39537
65704 18355
51311 86246
34879 31926
90444 83548
94519 80339
98171 79780
79228 94164
91379 44847
97383 90313
45917 64737
22007 66486
49018 7172
90191 31105
80577 48727
26092 3821
9302 17496
60...

output:

2509

result:

ok single line: '2509'

Test #72:

score: 22
Accepted
time: 57ms
memory: 22732kb

input:

100000 1000
11983 6879
71457 57412
57580 56139
50216 34933
12061 84340
23054 62561
94774 46880
55255 30380
38271 57910
59277 70810
80275 32460
39997 11027
70416 43264
1617 86814
15261 89577
12622 88741
16344 80224
10682 4096
73248 12167
99850 50822
16526 23109
40141 86110
91267 35155
16588 24615
385...

output:

250

result:

ok single line: '250'

Test #73:

score: 22
Accepted
time: 45ms
memory: 26932kb

input:

100000 50000
1644 78388
28413 1644
1644 85336
34228 1644
1644 17492
44196 1644
1644 81781
1644 48872
1644 13725
1644 11683
1644 81789
1644 331
88941 1644
24119 1644
16646 1644
28572 1644
1644 86432
1644 65072
51344 1644
82782 1644
1644 29259
1644 55827
82183 1644
1644 51814
1644 61332
23306 1644
164...

output:

9226

result:

ok single line: '9226'

Test #74:

score: 0
Wrong Answer
time: 31ms
memory: 25920kb

input:

100000 100000
75311 62863
12992 62863
62863 84650
62863 51354
62863 26895
62863 45506
73949 62863
52325 62863
82731 62863
62863 69152
62863 90856
32788 62863
55788 62863
62863 27200
62863 57738
62863 31
62863 23706
62863 66045
56818 62863
62863 67848
62863 29291
62863 76276
62863 64266
73 62863
2299...

output:

49999

result:

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

Subtask #5:

score: 0
Skipped

Dependency #1:

0%