QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#584441#3736. Tree IntersectionmasterhuangAC ✓146ms38912kbC++23961b2024-09-23 14:15:272024-09-23 14:16:55

Judging History

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

  • [2024-09-23 14:16:55]
  • 评测
  • 测评结果:AC
  • 用时:146ms
  • 内存:38912kb
  • [2024-09-23 14:15:27]
  • 提交

answer

#include<bits/stdc++.h>
#define LL long long
#define P pair<int,int>
#define fi first
#define se second
#define fr(x) freopen(#x".in","r",stdin);freopen(#x".out","w",stdout);
using namespace std;
const int N=1e5+5;
int n,a[N],s[N],ans[N],c[N];
vector<P>g[N];
map<int,int>mp[N];
void dfs(int x,int fa,int w)
{
	mp[x][a[x]]=1;c[x]=(s[a[x]]>1);
	for(auto [y,t]:g[x]) if(y^fa)
	{
		dfs(y,x,t);
		if(mp[x].size()<mp[y].size()) swap(mp[x],mp[y]),swap(c[x],c[y]);
		for(auto [X,Y]:mp[y])
		{
			if(!mp[x].count(X)) c[x]++;
			mp[x][X]+=Y;
			if(mp[x][X]==s[X]) c[x]--;
		} 
	}ans[w]=c[x];
}
int main()
{
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	while(cin>>n)
	{
		for(int i=1;i<=n;i++) cin>>a[i],s[a[i]]++;
		for(int i=1,u,v;i<n;i++) cin>>u>>v,g[u].push_back({v,i}),g[v].push_back({u,i});
		dfs(1,0,0);
		for(int i=1;i<n;i++) cout<<ans[i]<<"\n";	
		for(int i=1;i<=n;i++) g[i].clear(),s[i]=0,mp[i].clear();
	}
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 11964kb

input:

1000
449 327 407 122 15 572 311 4 824 142 103 491 449 383 593 549 233 355 586 231 197 198 308 268 206 305 66 499 322 300 761 101 857 284 668 22 221 51 234 159 9 147 253 220 171 202 264 34 233 716 758 460 9 174 138 456 378 6 390 127 263 291 486 246 15 425 346 892 803 556 787 670 204 90 155 289 628 42...

output:

1
10
1
1
113
1
1
0
1
20
1
3
3
1
3
12
10
1
1
19
3
0
4
1
17
8
21
1
19
1
2
3
2
3
55
3
1
1
0
0
2
13
2
1
3
2
0
3
3
1
7
1
10
3
0
22
2
1
1
13
2
6
1
2
16
0
2
1
15
11
30
12
3
94
2
8
0
9
3
4
1
0
1
7
0
0
1
1
122
3
1
1
3
0
2
1
1
0
1
28
0
93
0
3
4
15
1
9
1
19
16
47
45
2
13
1
1
2
46
13
12
0
51
3
7
3
101
1
1
0
0
2...

result:

ok 999 numbers

Test #2:

score: 0
Accepted
time: 3ms
memory: 10528kb

input:

1000
229 45 2 237 88 50 16 99 403 2 106 5 8 35 95 195 10 130 157 26 156 7 29 48 133 55 210 40 68 48 99 277 113 24 83 153 78 77 13 53 5 42 141 53 74 89 58 46 11 165 8 60 3 27 78 34 99 57 58 84 18 182 5 9 50 5 206 130 86 78 46 194 100 237 16 40 123 76 112 58 23 208 29 27 56 174 3 97 69 62 96 92 32 21 ...

output:

1
1
3
1
1
21
0
1
1
7
1
2
2
31
5
0
1
75
5
3
1
1
45
1
1
3
4
118
2
9
15
2
10
45
15
0
3
5
1
2
2
14
3
1
20
2
5
6
1
10
7
1
2
1
3
2
1
2
5
11
19
4
31
6
16
1
1
53
13
2
1
6
2
1
1
2
19
47
4
12
1
6
2
44
2
1
8
1
1
7
1
3
1
58
56
5
1
1
2
6
5
1
2
66
2
1
2
2
44
3
5
1
5
7
1
25
39
3
15
12
2
1
2
4
1
13
3
5
15
3
2
11
4
...

result:

ok 999 numbers

Test #3:

score: 0
Accepted
time: 2ms
memory: 8604kb

input:

1000
5 27 3 16 19 33 8 5 12 6 1 5 2 12 1 20 13 6 1 4 10 1 16 5 5 13 13 3 8 4 9 5 6 15 2 13 4 19 3 7 4 3 24 2 4 16 34 8 21 2 5 23 1 46 2 2 1 6 1 3 1 3 1 2 10 1 10 3 12 17 2 3 1 4 10 3 22 2 8 3 4 2 7 4 5 2 2 9 14 8 8 8 8 8 45 20 15 3 5 5 4 10 21 19 1 6 20 15 13 5 1 8 3 7 11 9 9 7 15 1 33 13 9 12 5 26 ...

output:

1
5
6
8
1
10
2
1
1
1
5
5
1
19
3
2
1
2
3
11
1
1
7
36
36
1
7
2
28
1
4
1
10
1
3
2
2
1
4
3
8
12
13
1
2
1
2
1
3
4
1
1
23
6
35
1
9
1
10
21
3
18
1
4
5
1
3
11
18
1
16
2
24
7
2
5
22
1
2
8
1
3
1
7
4
1
3
16
1
11
5
10
2
1
26
2
2
2
36
11
3
5
2
5
1
3
1
3
16
14
4
1
2
1
1
35
1
1
4
8
3
3
21
1
1
1
3
1
2
3
6
8
2
8
1
1...

result:

ok 999 numbers

Test #4:

score: 0
Accepted
time: 0ms
memory: 8480kb

input:

1000
1 1 1 1 2 2 1 4 1 2 2 3 3 1 2 1 1 2 2 1 1 1 4 3 1 1 1 1 5 1 1 2 1 1 4 2 1 5 1 4 2 1 3 1 1 1 1 1 1 2 4 3 1 2 2 1 1 1 1 1 1 2 1 1 1 2 2 1 1 1 1 2 2 3 1 3 1 2 1 4 1 1 1 1 2 1 1 3 1 4 1 1 1 4 3 1 1 1 2 1 3 1 2 1 1 3 2 1 3 2 1 1 2 1 1 1 1 2 1 1 1 3 1 1 2 1 1 2 1 1 1 1 1 1 1 1 1 1 2 3 1 1 3 2 1 1 2 1...

output:

4
3
1
1
1
6
1
1
3
2
3
1
1
2
6
2
5
3
2
2
3
3
5
1
1
5
1
2
1
1
1
1
1
1
1
2
1
3
1
4
5
1
1
1
1
2
2
4
3
3
5
1
2
2
1
2
3
2
1
2
2
1
1
1
1
1
4
2
1
5
3
1
5
2
4
4
1
2
4
2
4
1
1
2
2
2
1
3
1
1
1
2
2
4
2
6
1
1
1
2
1
2
5
4
1
3
1
1
6
3
2
1
4
4
4
4
1
1
1
3
4
1
1
1
1
1
1
2
1
2
1
4
4
1
1
4
2
5
1
2
1
1
4
6
1
2
1
1
2
1
...

result:

ok 999 numbers

Test #5:

score: 0
Accepted
time: 2ms
memory: 8480kb

input:

1000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1...

output:

1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
...

result:

ok 999 numbers

Test #6:

score: 0
Accepted
time: 145ms
memory: 38912kb

input:

100000
40658 48129 18921 46405 6002 565 14350 33684 24252 81776 38020 9339 45717 22762 48488 24124 13924 35739 75128 22052 32854 11658 28348 6927 43826 34910 5796 30457 26931 19165 11161 20688 67082 77590 37363 12000 16763 6655 17557 8308 29722 4433 59088 22515 32922 69130 14043 17553 24670 62019 12...

output:

0
0
9
91
4
17
7
1
50
0
16
1
4
1
1
1
2
2
57
1
2
4
1
0
2
1
1
155
3
3
1
1
2
100
24
550
2
0
1
2
0
2
1
73
5
1
251
2
1
0
0
0
2
1
2
1
3
3
14
1
2
11
0
3
1
86
172
236
26
7
2
4
321
9
1
1
1
6
15
3
2
1
1
4
7
0
2
2
12
1
4
6285
48
1
2
26
10
19
1
3
1
29
6
1
4
2
33
6
2
2
5
4
3
0
1
2
9
1
31
0
1
23
1
18
1
1
4
1
1
10
...

result:

ok 99999 numbers

Test #7:

score: 0
Accepted
time: 146ms
memory: 36476kb

input:

100000
4805 6969 4355 4612 9193 21597 8220 17693 3367 19320 22040 1089 19816 6640 6002 5311 4779 3301 12923 5084 1157 227 1555 7101 663 7855 4774 1546 17703 5269 5641 15136 11644 6231 8457 440 11587 2696 10249 3378 13258 19627 12601 2801 3820 8034 1003 7108 2365 15446 1591 1218 3576 8015 16456 15124...

output:

1
2
248
1
4
13
2
8
5
9
2
5
9
1
0
20
1
3
11
184
1139
16
4
1
1
1
81
1
5
3
3
55
2
1
5
2
16
1
12
3
1
30
28
12
1
1
6
1
1
3
1
5
38
1
1
1
2
2
1
2
1
1
1
4
1
1
2
4
3846
6
1
1
4
13
3
8
1
6
3
5
3
1
2
4
2
1
19
0
1
5
1
20
2
1
2
2
20
13
24
34
1
1
1
49
285
0
0
11
257
8
37
2
1
6
1
1522
1
12
29
47
3
1
22
19
1
10
29
...

result:

ok 99999 numbers

Test #8:

score: 0
Accepted
time: 102ms
memory: 32684kb

input:

100000
845 1367 51 135 4683 226 2733 637 663 128 718 1008 127 4388 369 1455 473 462 187 1127 236 825 1718 196 1372 301 979 997 782 116 60 1834 2408 661 2697 999 1576 1422 38 3576 1972 1370 833 793 1123 2371 188 144 1001 282 380 122 2108 965 905 2489 1154 755 81 62 1453 130 1990 1241 1197 605 397 205...

output:

1
1
1
1
5
6
1
1
3
20
4
26
10
87
1
6
4
47
2
53
5
3
3
5
5
10
4
17
1
1
1
1
2
14
2
90
2
1
13
11
12
4
19
1
9
3
66
9
3101
6
4
1
1
8
3
3
2
5
4
1
5
78
1
1
620
5
3
85
3
39
12
19
58
6
1
1
5
4
4
7
1
24
14
11
6
13
14
45
232
1
13
1
1
2
3
17
3
3
1
3
18
250
215
1
9
5
12
1
66
3
5
3261
15
6
1
1
4
1
8
4
12
5
3
2
1
10...

result:

ok 99999 numbers

Test #9:

score: 0
Accepted
time: 76ms
memory: 28740kb

input:

100000
79 23 52 54 46 22 86 324 323 58 54 42 65 108 158 90 90 87 529 329 169 261 27 15 151 43 84 106 96 77 115 50 10 76 20 61 62 28 22 51 40 196 338 16 29 2 73 35 96 176 148 16 65 25 94 133 187 34 126 3 244 50 233 28 108 12 31 180 178 13 135 181 146 27 4 57 38 322 8 188 21 211 54 151 27 21 54 130 12...

output:

134
33
1
20
1
28
2
6
2
3
8
4
1
51
11
1
4
1
32
231
2
1
4
2
4
3
1
1
3
51
1
28
1
30
4
6
67
45
2
145
4
5
6
1
1
2
3
1
1
1
5
13
19
2
3
4
222
14
1
1
6
7
2
1
1
1
607
72
227
1
2
31
1
1
44
16
2
2
82
4
3
2
4
2
3
4
2
19
2
2
1
23
1
32
97
1
1
10
11
103
3
1
25
1
1
2
11
38
19
1
238
5
2
4
6
3
8
2
3
43
10
2
1
1
7
6
1...

result:

ok 99999 numbers

Test #10:

score: 0
Accepted
time: 53ms
memory: 25160kb

input:

100000
4 1 6 23 1 18 24 16 22 5 7 57 2 8 7 7 17 20 5 4 4 4 1 33 1 10 5 2 3 43 35 9 1 11 32 13 11 11 3 4 6 22 10 2 37 36 1 14 8 40 21 1 7 6 14 5 5 18 10 4 1 4 2 7 11 1 11 4 2 6 12 25 5 3 3 4 14 1 2 4 8 3 5 13 7 39 6 2 11 1 10 7 1 10 1 66 4 22 39 2 43 21 1 8 3 1 5 15 11 8 8 4 11 15 8 1 8 1 4 2 37 11 4...

output:

22
7
14
10
1
3
1
1
2
4
29
6
29
12
1
9
7
3
8
2
3
1
45
1
1
14
5
26
1
13
1
1
5
2
9
1
46
1
1
3
6
4
2
4
4
1
8
2
21
17
7
1
11
4
3
7
3
29
26
2
3
51
2
1
5
4
3
8
1
20
1
2
21
1
4
1
1
26
29
5
46
4
1
1
1
1
5
20
5
1
19
6
3
19
25
3
1
17
28
1
2
1
1
3
1
13
1
1
3
26
12
6
1
1
6
7
4
1
1
8
1
2
7
1
2
12
9
6
7
2
3
1
6
49...

result:

ok 99999 numbers