답안 #596765

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
596765 2022-07-15T03:29:46 Z Hanksburger Mergers (JOI19_mergers) C++17
100 / 100
625 ms 78952 KB
#include <bits/stdc++.h>
using namespace std;
int c[500005], l[500005], r[500005], t, t2, ans;
vector<int> adj[500005];
void f(int u, int p)
{
    t++;
    if (!l[c[u]])
        l[c[u]]=t;
    for (int v:adj[u])
        if (v!=p)
            f(v, u);
    r[c[u]]=t;
}
pair<pair<int, int>, int> f2(int u, int p)
{
    t2++;
    int t3=t2, x=l[c[u]], y=r[c[u]], z=0;
    for (int v:adj[u])
    {
        if (v!=p)
        {
            pair<pair<int, int>, int> r=f2(v, u);
            x=min(x, r.first.first);
            y=max(y, r.first.second);
            z+=r.second;
        }
    }
    if (!p)
        ans+=(z==1);
    else if (x==t3 && y==t2)
    {
        ans+=!z;
        z=1;
    }
    return {{x, y}, z};
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n, k;
    cin >> n >> k;
    for (int i=1; i<n; i++)
    {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    for (int i=1; i<=n; i++)
        cin >> c[i];
    f(1, 0);
    f2(1, 0);
    cout << (ans+1)/2;
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 12076 KB Output is correct
2 Correct 6 ms 12072 KB Output is correct
3 Correct 9 ms 11988 KB Output is correct
4 Correct 6 ms 12076 KB Output is correct
5 Correct 7 ms 12072 KB Output is correct
6 Correct 6 ms 12084 KB Output is correct
7 Correct 6 ms 11988 KB Output is correct
8 Correct 6 ms 12076 KB Output is correct
9 Correct 8 ms 12116 KB Output is correct
10 Correct 7 ms 12076 KB Output is correct
11 Correct 7 ms 12072 KB Output is correct
12 Correct 8 ms 12004 KB Output is correct
13 Correct 7 ms 12036 KB Output is correct
14 Correct 7 ms 11988 KB Output is correct
15 Correct 6 ms 12000 KB Output is correct
16 Correct 6 ms 12072 KB Output is correct
17 Correct 7 ms 12072 KB Output is correct
18 Correct 6 ms 12076 KB Output is correct
19 Correct 7 ms 11988 KB Output is correct
20 Correct 7 ms 12068 KB Output is correct
21 Correct 6 ms 11988 KB Output is correct
22 Correct 7 ms 11988 KB Output is correct
23 Correct 6 ms 12072 KB Output is correct
24 Correct 7 ms 11988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 12076 KB Output is correct
2 Correct 6 ms 12072 KB Output is correct
3 Correct 9 ms 11988 KB Output is correct
4 Correct 6 ms 12076 KB Output is correct
5 Correct 7 ms 12072 KB Output is correct
6 Correct 6 ms 12084 KB Output is correct
7 Correct 6 ms 11988 KB Output is correct
8 Correct 6 ms 12076 KB Output is correct
9 Correct 8 ms 12116 KB Output is correct
10 Correct 7 ms 12076 KB Output is correct
11 Correct 7 ms 12072 KB Output is correct
12 Correct 8 ms 12004 KB Output is correct
13 Correct 7 ms 12036 KB Output is correct
14 Correct 7 ms 11988 KB Output is correct
15 Correct 6 ms 12000 KB Output is correct
16 Correct 6 ms 12072 KB Output is correct
17 Correct 7 ms 12072 KB Output is correct
18 Correct 6 ms 12076 KB Output is correct
19 Correct 7 ms 11988 KB Output is correct
20 Correct 7 ms 12068 KB Output is correct
21 Correct 6 ms 11988 KB Output is correct
22 Correct 7 ms 11988 KB Output is correct
23 Correct 6 ms 12072 KB Output is correct
24 Correct 7 ms 11988 KB Output is correct
25 Correct 8 ms 12084 KB Output is correct
26 Correct 8 ms 12244 KB Output is correct
27 Correct 8 ms 12176 KB Output is correct
28 Correct 8 ms 12372 KB Output is correct
29 Correct 8 ms 12244 KB Output is correct
30 Correct 7 ms 12216 KB Output is correct
31 Correct 7 ms 12080 KB Output is correct
32 Correct 8 ms 12472 KB Output is correct
33 Correct 7 ms 11988 KB Output is correct
34 Correct 8 ms 12148 KB Output is correct
35 Correct 7 ms 12244 KB Output is correct
36 Correct 8 ms 12244 KB Output is correct
37 Correct 8 ms 12244 KB Output is correct
38 Correct 7 ms 11988 KB Output is correct
39 Correct 8 ms 12220 KB Output is correct
40 Correct 9 ms 12116 KB Output is correct
41 Correct 8 ms 12216 KB Output is correct
42 Correct 8 ms 12260 KB Output is correct
43 Correct 8 ms 12500 KB Output is correct
44 Correct 7 ms 11988 KB Output is correct
45 Correct 7 ms 12244 KB Output is correct
46 Correct 7 ms 12228 KB Output is correct
47 Correct 7 ms 11992 KB Output is correct
48 Correct 9 ms 12116 KB Output is correct
49 Correct 8 ms 12244 KB Output is correct
50 Correct 8 ms 12244 KB Output is correct
51 Correct 8 ms 12116 KB Output is correct
52 Correct 7 ms 12244 KB Output is correct
53 Correct 7 ms 12224 KB Output is correct
54 Correct 7 ms 12116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 12076 KB Output is correct
2 Correct 6 ms 12072 KB Output is correct
3 Correct 9 ms 11988 KB Output is correct
4 Correct 6 ms 12076 KB Output is correct
5 Correct 7 ms 12072 KB Output is correct
6 Correct 6 ms 12084 KB Output is correct
7 Correct 6 ms 11988 KB Output is correct
8 Correct 6 ms 12076 KB Output is correct
9 Correct 8 ms 12116 KB Output is correct
10 Correct 7 ms 12076 KB Output is correct
11 Correct 7 ms 12072 KB Output is correct
12 Correct 8 ms 12004 KB Output is correct
13 Correct 7 ms 12036 KB Output is correct
14 Correct 7 ms 11988 KB Output is correct
15 Correct 6 ms 12000 KB Output is correct
16 Correct 6 ms 12072 KB Output is correct
17 Correct 7 ms 12072 KB Output is correct
18 Correct 6 ms 12076 KB Output is correct
19 Correct 7 ms 11988 KB Output is correct
20 Correct 7 ms 12068 KB Output is correct
21 Correct 6 ms 11988 KB Output is correct
22 Correct 7 ms 11988 KB Output is correct
23 Correct 6 ms 12072 KB Output is correct
24 Correct 7 ms 11988 KB Output is correct
25 Correct 6 ms 12076 KB Output is correct
26 Correct 40 ms 17348 KB Output is correct
27 Correct 63 ms 17028 KB Output is correct
28 Correct 7 ms 12148 KB Output is correct
29 Correct 7 ms 12016 KB Output is correct
30 Correct 6 ms 12076 KB Output is correct
31 Correct 54 ms 17064 KB Output is correct
32 Correct 8 ms 12212 KB Output is correct
33 Correct 70 ms 27116 KB Output is correct
34 Correct 54 ms 17024 KB Output is correct
35 Correct 7 ms 12116 KB Output is correct
36 Correct 73 ms 18176 KB Output is correct
37 Correct 8 ms 12116 KB Output is correct
38 Correct 8 ms 12116 KB Output is correct
39 Correct 53 ms 17388 KB Output is correct
40 Correct 9 ms 12512 KB Output is correct
41 Correct 52 ms 17056 KB Output is correct
42 Correct 68 ms 20420 KB Output is correct
43 Correct 9 ms 12080 KB Output is correct
44 Correct 97 ms 27504 KB Output is correct
45 Correct 77 ms 22264 KB Output is correct
46 Correct 8 ms 12244 KB Output is correct
47 Correct 8 ms 12116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 43 ms 17436 KB Output is correct
2 Correct 43 ms 18460 KB Output is correct
3 Correct 8 ms 12256 KB Output is correct
4 Correct 7 ms 12220 KB Output is correct
5 Correct 7 ms 11980 KB Output is correct
6 Correct 6 ms 12012 KB Output is correct
7 Correct 10 ms 12152 KB Output is correct
8 Correct 61 ms 17812 KB Output is correct
9 Correct 8 ms 12152 KB Output is correct
10 Correct 66 ms 17136 KB Output is correct
11 Correct 7 ms 11988 KB Output is correct
12 Correct 55 ms 17172 KB Output is correct
13 Correct 56 ms 17724 KB Output is correct
14 Correct 59 ms 18168 KB Output is correct
15 Correct 43 ms 17356 KB Output is correct
16 Correct 7 ms 12216 KB Output is correct
17 Correct 8 ms 12076 KB Output is correct
18 Correct 44 ms 18372 KB Output is correct
19 Correct 91 ms 23720 KB Output is correct
20 Correct 8 ms 12220 KB Output is correct
21 Correct 7 ms 11988 KB Output is correct
22 Correct 51 ms 17988 KB Output is correct
23 Correct 9 ms 12220 KB Output is correct
24 Correct 57 ms 17264 KB Output is correct
25 Correct 64 ms 21868 KB Output is correct
26 Correct 7 ms 12244 KB Output is correct
27 Correct 8 ms 12244 KB Output is correct
28 Correct 8 ms 12220 KB Output is correct
29 Correct 8 ms 12200 KB Output is correct
30 Correct 8 ms 12216 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 12076 KB Output is correct
2 Correct 6 ms 12072 KB Output is correct
3 Correct 9 ms 11988 KB Output is correct
4 Correct 6 ms 12076 KB Output is correct
5 Correct 7 ms 12072 KB Output is correct
6 Correct 6 ms 12084 KB Output is correct
7 Correct 6 ms 11988 KB Output is correct
8 Correct 6 ms 12076 KB Output is correct
9 Correct 8 ms 12116 KB Output is correct
10 Correct 7 ms 12076 KB Output is correct
11 Correct 7 ms 12072 KB Output is correct
12 Correct 8 ms 12004 KB Output is correct
13 Correct 7 ms 12036 KB Output is correct
14 Correct 7 ms 11988 KB Output is correct
15 Correct 6 ms 12000 KB Output is correct
16 Correct 6 ms 12072 KB Output is correct
17 Correct 7 ms 12072 KB Output is correct
18 Correct 6 ms 12076 KB Output is correct
19 Correct 7 ms 11988 KB Output is correct
20 Correct 7 ms 12068 KB Output is correct
21 Correct 6 ms 11988 KB Output is correct
22 Correct 7 ms 11988 KB Output is correct
23 Correct 6 ms 12072 KB Output is correct
24 Correct 7 ms 11988 KB Output is correct
25 Correct 8 ms 12084 KB Output is correct
26 Correct 8 ms 12244 KB Output is correct
27 Correct 8 ms 12176 KB Output is correct
28 Correct 8 ms 12372 KB Output is correct
29 Correct 8 ms 12244 KB Output is correct
30 Correct 7 ms 12216 KB Output is correct
31 Correct 7 ms 12080 KB Output is correct
32 Correct 8 ms 12472 KB Output is correct
33 Correct 7 ms 11988 KB Output is correct
34 Correct 8 ms 12148 KB Output is correct
35 Correct 7 ms 12244 KB Output is correct
36 Correct 8 ms 12244 KB Output is correct
37 Correct 8 ms 12244 KB Output is correct
38 Correct 7 ms 11988 KB Output is correct
39 Correct 8 ms 12220 KB Output is correct
40 Correct 9 ms 12116 KB Output is correct
41 Correct 8 ms 12216 KB Output is correct
42 Correct 8 ms 12260 KB Output is correct
43 Correct 8 ms 12500 KB Output is correct
44 Correct 7 ms 11988 KB Output is correct
45 Correct 7 ms 12244 KB Output is correct
46 Correct 7 ms 12228 KB Output is correct
47 Correct 7 ms 11992 KB Output is correct
48 Correct 9 ms 12116 KB Output is correct
49 Correct 8 ms 12244 KB Output is correct
50 Correct 8 ms 12244 KB Output is correct
51 Correct 8 ms 12116 KB Output is correct
52 Correct 7 ms 12244 KB Output is correct
53 Correct 7 ms 12224 KB Output is correct
54 Correct 7 ms 12116 KB Output is correct
55 Correct 6 ms 12076 KB Output is correct
56 Correct 40 ms 17348 KB Output is correct
57 Correct 63 ms 17028 KB Output is correct
58 Correct 7 ms 12148 KB Output is correct
59 Correct 7 ms 12016 KB Output is correct
60 Correct 6 ms 12076 KB Output is correct
61 Correct 54 ms 17064 KB Output is correct
62 Correct 8 ms 12212 KB Output is correct
63 Correct 70 ms 27116 KB Output is correct
64 Correct 54 ms 17024 KB Output is correct
65 Correct 7 ms 12116 KB Output is correct
66 Correct 73 ms 18176 KB Output is correct
67 Correct 8 ms 12116 KB Output is correct
68 Correct 8 ms 12116 KB Output is correct
69 Correct 53 ms 17388 KB Output is correct
70 Correct 9 ms 12512 KB Output is correct
71 Correct 52 ms 17056 KB Output is correct
72 Correct 68 ms 20420 KB Output is correct
73 Correct 9 ms 12080 KB Output is correct
74 Correct 97 ms 27504 KB Output is correct
75 Correct 77 ms 22264 KB Output is correct
76 Correct 8 ms 12244 KB Output is correct
77 Correct 8 ms 12116 KB Output is correct
78 Correct 43 ms 17436 KB Output is correct
79 Correct 43 ms 18460 KB Output is correct
80 Correct 8 ms 12256 KB Output is correct
81 Correct 7 ms 12220 KB Output is correct
82 Correct 7 ms 11980 KB Output is correct
83 Correct 6 ms 12012 KB Output is correct
84 Correct 10 ms 12152 KB Output is correct
85 Correct 61 ms 17812 KB Output is correct
86 Correct 8 ms 12152 KB Output is correct
87 Correct 66 ms 17136 KB Output is correct
88 Correct 7 ms 11988 KB Output is correct
89 Correct 55 ms 17172 KB Output is correct
90 Correct 56 ms 17724 KB Output is correct
91 Correct 59 ms 18168 KB Output is correct
92 Correct 43 ms 17356 KB Output is correct
93 Correct 7 ms 12216 KB Output is correct
94 Correct 8 ms 12076 KB Output is correct
95 Correct 44 ms 18372 KB Output is correct
96 Correct 91 ms 23720 KB Output is correct
97 Correct 8 ms 12220 KB Output is correct
98 Correct 7 ms 11988 KB Output is correct
99 Correct 51 ms 17988 KB Output is correct
100 Correct 9 ms 12220 KB Output is correct
101 Correct 57 ms 17264 KB Output is correct
102 Correct 64 ms 21868 KB Output is correct
103 Correct 7 ms 12244 KB Output is correct
104 Correct 8 ms 12244 KB Output is correct
105 Correct 8 ms 12220 KB Output is correct
106 Correct 8 ms 12200 KB Output is correct
107 Correct 8 ms 12216 KB Output is correct
108 Correct 293 ms 43000 KB Output is correct
109 Correct 570 ms 63220 KB Output is correct
110 Correct 580 ms 78952 KB Output is correct
111 Correct 625 ms 76544 KB Output is correct
112 Correct 525 ms 62960 KB Output is correct
113 Correct 329 ms 45188 KB Output is correct
114 Correct 478 ms 37808 KB Output is correct
115 Correct 480 ms 37792 KB Output is correct
116 Correct 466 ms 40540 KB Output is correct
117 Correct 494 ms 44000 KB Output is correct
118 Correct 429 ms 38208 KB Output is correct
119 Correct 483 ms 44080 KB Output is correct
120 Correct 545 ms 63804 KB Output is correct
121 Correct 506 ms 44116 KB Output is correct
122 Correct 460 ms 42420 KB Output is correct
123 Correct 320 ms 45460 KB Output is correct
124 Correct 528 ms 43724 KB Output is correct