답안 #1039546

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1039546 2024-07-31T04:15:17 Z vjudge1 Mergers (JOI19_mergers) C++17
100 / 100
850 ms 164864 KB
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fi first
#define se second
const int maxN=5e5+5,LG=__lg(maxN)+5;
const ll inf=2e18;
int n,k,a[maxN+1],num[maxN+1],timer=0,h[maxN+1],up[maxN+1][LG+1],f[maxN+1];
vector<int> adj[maxN+1],vec[maxN+1],nadj[maxN+1];
void dfs(int u)
{
    num[u]=++timer;
    for(auto v:adj[u])
    {
        if(v==up[u][0])
        {
            continue;
        }
        up[v][0]=u;
        h[v]=h[u]+1;
        for(int j=1;j<=LG;j++)
        {
            up[v][j]=up[up[v][j-1]][j-1];
        }
        dfs(v);
    }
}
int lca(int u,int v)
{
    if(h[u]<h[v])
    {
        swap(u,v);
    }
    for(int j=LG;j>=0;j--)
    {
        if(h[up[u][j]]>=h[v])
        {
            u=up[u][j];
        }
    }
    if(u==v)
    {
        return u;
    }
    for(int j=LG;j>=0;j--)
    {
        if(up[u][j]!=up[v][j])
        {
            u=up[u][j];
            v=up[v][j];
        }
    }
    return up[u][0];
}
void dfs1(int u)
{
    for(auto v:adj[u])
    {
        if(v!=up[u][0])
        {
            dfs1(v);
            f[u]+=f[v];
        }
    }
}
void dfs2(int u,int p)
{
    for(auto v:adj[u])
    {
        if(v!=up[u][0])
        {
            int nv=p;
            if(!f[v])
            {
                //cout<<u<<" "<<v<<'\n';
                nadj[v].push_back(p);
                nadj[p].push_back(v);
                nv=v;
            }
            dfs2(v,nv);
        }
    }
}
int main()
{
    //freopen("","r",stdin);
    //freopen("","w",stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    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);
    }
    h[1]=1;
    dfs(1);
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        vec[a[i]].push_back(i);
    }
    for(int i=1;i<=k;i++)
    {
        sort(vec[i].begin(),vec[i].end(),[&](int x,int y)
        {
            return num[x]<num[y];
        });
        int len=vec[i].size();
        if(len==1)
        {
            continue;
        }
        for(int j=0;j<len;j++)
        {
            int u=vec[i][j],v=vec[i][(j+1)%len];
            f[u]++;
            f[lca(u,v)]--;
        }
    }
    dfs1(1);
    dfs2(1,1);
    int cnt=0;
    for(int i=1;i<=n;i++)
    {
        cnt+=(nadj[i].size()==1);
    }
    cout<<(cnt+1)/2;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 35632 KB Output is correct
2 Correct 15 ms 35676 KB Output is correct
3 Correct 15 ms 35676 KB Output is correct
4 Correct 17 ms 35676 KB Output is correct
5 Correct 15 ms 35676 KB Output is correct
6 Correct 15 ms 35548 KB Output is correct
7 Correct 15 ms 35672 KB Output is correct
8 Correct 15 ms 35676 KB Output is correct
9 Correct 16 ms 35724 KB Output is correct
10 Correct 15 ms 35676 KB Output is correct
11 Correct 15 ms 35492 KB Output is correct
12 Correct 16 ms 35676 KB Output is correct
13 Correct 16 ms 35676 KB Output is correct
14 Correct 16 ms 35672 KB Output is correct
15 Correct 16 ms 35672 KB Output is correct
16 Correct 16 ms 35688 KB Output is correct
17 Correct 16 ms 35676 KB Output is correct
18 Correct 19 ms 35928 KB Output is correct
19 Correct 15 ms 35716 KB Output is correct
20 Correct 15 ms 35600 KB Output is correct
21 Correct 16 ms 35672 KB Output is correct
22 Correct 16 ms 35676 KB Output is correct
23 Correct 23 ms 35532 KB Output is correct
24 Correct 16 ms 35676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 35632 KB Output is correct
2 Correct 15 ms 35676 KB Output is correct
3 Correct 15 ms 35676 KB Output is correct
4 Correct 17 ms 35676 KB Output is correct
5 Correct 15 ms 35676 KB Output is correct
6 Correct 15 ms 35548 KB Output is correct
7 Correct 15 ms 35672 KB Output is correct
8 Correct 15 ms 35676 KB Output is correct
9 Correct 16 ms 35724 KB Output is correct
10 Correct 15 ms 35676 KB Output is correct
11 Correct 15 ms 35492 KB Output is correct
12 Correct 16 ms 35676 KB Output is correct
13 Correct 16 ms 35676 KB Output is correct
14 Correct 16 ms 35672 KB Output is correct
15 Correct 16 ms 35672 KB Output is correct
16 Correct 16 ms 35688 KB Output is correct
17 Correct 16 ms 35676 KB Output is correct
18 Correct 19 ms 35928 KB Output is correct
19 Correct 15 ms 35716 KB Output is correct
20 Correct 15 ms 35600 KB Output is correct
21 Correct 16 ms 35672 KB Output is correct
22 Correct 16 ms 35676 KB Output is correct
23 Correct 23 ms 35532 KB Output is correct
24 Correct 16 ms 35676 KB Output is correct
25 Correct 15 ms 35676 KB Output is correct
26 Correct 17 ms 36188 KB Output is correct
27 Correct 18 ms 36028 KB Output is correct
28 Correct 22 ms 36444 KB Output is correct
29 Correct 17 ms 36184 KB Output is correct
30 Correct 17 ms 36188 KB Output is correct
31 Correct 16 ms 35672 KB Output is correct
32 Correct 17 ms 36444 KB Output is correct
33 Correct 16 ms 35520 KB Output is correct
34 Correct 16 ms 36100 KB Output is correct
35 Correct 16 ms 36184 KB Output is correct
36 Correct 17 ms 36104 KB Output is correct
37 Correct 16 ms 36188 KB Output is correct
38 Correct 16 ms 35676 KB Output is correct
39 Correct 17 ms 36188 KB Output is correct
40 Correct 20 ms 35968 KB Output is correct
41 Correct 18 ms 36188 KB Output is correct
42 Correct 17 ms 36176 KB Output is correct
43 Correct 17 ms 36188 KB Output is correct
44 Correct 15 ms 35676 KB Output is correct
45 Correct 17 ms 36188 KB Output is correct
46 Correct 17 ms 36188 KB Output is correct
47 Correct 16 ms 35676 KB Output is correct
48 Correct 16 ms 36024 KB Output is correct
49 Correct 17 ms 36408 KB Output is correct
50 Correct 18 ms 36408 KB Output is correct
51 Correct 16 ms 36188 KB Output is correct
52 Correct 16 ms 36116 KB Output is correct
53 Correct 17 ms 36188 KB Output is correct
54 Correct 19 ms 36012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 35632 KB Output is correct
2 Correct 15 ms 35676 KB Output is correct
3 Correct 15 ms 35676 KB Output is correct
4 Correct 17 ms 35676 KB Output is correct
5 Correct 15 ms 35676 KB Output is correct
6 Correct 15 ms 35548 KB Output is correct
7 Correct 15 ms 35672 KB Output is correct
8 Correct 15 ms 35676 KB Output is correct
9 Correct 16 ms 35724 KB Output is correct
10 Correct 15 ms 35676 KB Output is correct
11 Correct 15 ms 35492 KB Output is correct
12 Correct 16 ms 35676 KB Output is correct
13 Correct 16 ms 35676 KB Output is correct
14 Correct 16 ms 35672 KB Output is correct
15 Correct 16 ms 35672 KB Output is correct
16 Correct 16 ms 35688 KB Output is correct
17 Correct 16 ms 35676 KB Output is correct
18 Correct 19 ms 35928 KB Output is correct
19 Correct 15 ms 35716 KB Output is correct
20 Correct 15 ms 35600 KB Output is correct
21 Correct 16 ms 35672 KB Output is correct
22 Correct 16 ms 35676 KB Output is correct
23 Correct 23 ms 35532 KB Output is correct
24 Correct 16 ms 35676 KB Output is correct
25 Correct 16 ms 35672 KB Output is correct
26 Correct 74 ms 51792 KB Output is correct
27 Correct 94 ms 51288 KB Output is correct
28 Correct 17 ms 36184 KB Output is correct
29 Correct 15 ms 35580 KB Output is correct
30 Correct 16 ms 35676 KB Output is correct
31 Correct 87 ms 51296 KB Output is correct
32 Correct 17 ms 36188 KB Output is correct
33 Correct 104 ms 58568 KB Output is correct
34 Correct 107 ms 51280 KB Output is correct
35 Correct 23 ms 36188 KB Output is correct
36 Correct 100 ms 52048 KB Output is correct
37 Correct 17 ms 36184 KB Output is correct
38 Correct 17 ms 36184 KB Output is correct
39 Correct 85 ms 51728 KB Output is correct
40 Correct 18 ms 36188 KB Output is correct
41 Correct 80 ms 51404 KB Output is correct
42 Correct 106 ms 53588 KB Output is correct
43 Correct 16 ms 35676 KB Output is correct
44 Correct 150 ms 58860 KB Output is correct
45 Correct 107 ms 55120 KB Output is correct
46 Correct 18 ms 36212 KB Output is correct
47 Correct 17 ms 36188 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 73 ms 51564 KB Output is correct
2 Correct 80 ms 57292 KB Output is correct
3 Correct 19 ms 36188 KB Output is correct
4 Correct 17 ms 36188 KB Output is correct
5 Correct 15 ms 35672 KB Output is correct
6 Correct 16 ms 35676 KB Output is correct
7 Correct 18 ms 36184 KB Output is correct
8 Correct 98 ms 52828 KB Output is correct
9 Correct 20 ms 36184 KB Output is correct
10 Correct 79 ms 51400 KB Output is correct
11 Correct 15 ms 35672 KB Output is correct
12 Correct 101 ms 51408 KB Output is correct
13 Correct 91 ms 53076 KB Output is correct
14 Correct 88 ms 57064 KB Output is correct
15 Correct 76 ms 51556 KB Output is correct
16 Correct 16 ms 36184 KB Output is correct
17 Correct 15 ms 35676 KB Output is correct
18 Correct 79 ms 56632 KB Output is correct
19 Correct 121 ms 61200 KB Output is correct
20 Correct 17 ms 36184 KB Output is correct
21 Correct 20 ms 35676 KB Output is correct
22 Correct 97 ms 53280 KB Output is correct
23 Correct 17 ms 36188 KB Output is correct
24 Correct 90 ms 51888 KB Output is correct
25 Correct 96 ms 59732 KB Output is correct
26 Correct 17 ms 36184 KB Output is correct
27 Correct 18 ms 36188 KB Output is correct
28 Correct 18 ms 36188 KB Output is correct
29 Correct 17 ms 36228 KB Output is correct
30 Correct 17 ms 36240 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 35632 KB Output is correct
2 Correct 15 ms 35676 KB Output is correct
3 Correct 15 ms 35676 KB Output is correct
4 Correct 17 ms 35676 KB Output is correct
5 Correct 15 ms 35676 KB Output is correct
6 Correct 15 ms 35548 KB Output is correct
7 Correct 15 ms 35672 KB Output is correct
8 Correct 15 ms 35676 KB Output is correct
9 Correct 16 ms 35724 KB Output is correct
10 Correct 15 ms 35676 KB Output is correct
11 Correct 15 ms 35492 KB Output is correct
12 Correct 16 ms 35676 KB Output is correct
13 Correct 16 ms 35676 KB Output is correct
14 Correct 16 ms 35672 KB Output is correct
15 Correct 16 ms 35672 KB Output is correct
16 Correct 16 ms 35688 KB Output is correct
17 Correct 16 ms 35676 KB Output is correct
18 Correct 19 ms 35928 KB Output is correct
19 Correct 15 ms 35716 KB Output is correct
20 Correct 15 ms 35600 KB Output is correct
21 Correct 16 ms 35672 KB Output is correct
22 Correct 16 ms 35676 KB Output is correct
23 Correct 23 ms 35532 KB Output is correct
24 Correct 16 ms 35676 KB Output is correct
25 Correct 15 ms 35676 KB Output is correct
26 Correct 17 ms 36188 KB Output is correct
27 Correct 18 ms 36028 KB Output is correct
28 Correct 22 ms 36444 KB Output is correct
29 Correct 17 ms 36184 KB Output is correct
30 Correct 17 ms 36188 KB Output is correct
31 Correct 16 ms 35672 KB Output is correct
32 Correct 17 ms 36444 KB Output is correct
33 Correct 16 ms 35520 KB Output is correct
34 Correct 16 ms 36100 KB Output is correct
35 Correct 16 ms 36184 KB Output is correct
36 Correct 17 ms 36104 KB Output is correct
37 Correct 16 ms 36188 KB Output is correct
38 Correct 16 ms 35676 KB Output is correct
39 Correct 17 ms 36188 KB Output is correct
40 Correct 20 ms 35968 KB Output is correct
41 Correct 18 ms 36188 KB Output is correct
42 Correct 17 ms 36176 KB Output is correct
43 Correct 17 ms 36188 KB Output is correct
44 Correct 15 ms 35676 KB Output is correct
45 Correct 17 ms 36188 KB Output is correct
46 Correct 17 ms 36188 KB Output is correct
47 Correct 16 ms 35676 KB Output is correct
48 Correct 16 ms 36024 KB Output is correct
49 Correct 17 ms 36408 KB Output is correct
50 Correct 18 ms 36408 KB Output is correct
51 Correct 16 ms 36188 KB Output is correct
52 Correct 16 ms 36116 KB Output is correct
53 Correct 17 ms 36188 KB Output is correct
54 Correct 19 ms 36012 KB Output is correct
55 Correct 16 ms 35672 KB Output is correct
56 Correct 74 ms 51792 KB Output is correct
57 Correct 94 ms 51288 KB Output is correct
58 Correct 17 ms 36184 KB Output is correct
59 Correct 15 ms 35580 KB Output is correct
60 Correct 16 ms 35676 KB Output is correct
61 Correct 87 ms 51296 KB Output is correct
62 Correct 17 ms 36188 KB Output is correct
63 Correct 104 ms 58568 KB Output is correct
64 Correct 107 ms 51280 KB Output is correct
65 Correct 23 ms 36188 KB Output is correct
66 Correct 100 ms 52048 KB Output is correct
67 Correct 17 ms 36184 KB Output is correct
68 Correct 17 ms 36184 KB Output is correct
69 Correct 85 ms 51728 KB Output is correct
70 Correct 18 ms 36188 KB Output is correct
71 Correct 80 ms 51404 KB Output is correct
72 Correct 106 ms 53588 KB Output is correct
73 Correct 16 ms 35676 KB Output is correct
74 Correct 150 ms 58860 KB Output is correct
75 Correct 107 ms 55120 KB Output is correct
76 Correct 18 ms 36212 KB Output is correct
77 Correct 17 ms 36188 KB Output is correct
78 Correct 73 ms 51564 KB Output is correct
79 Correct 80 ms 57292 KB Output is correct
80 Correct 19 ms 36188 KB Output is correct
81 Correct 17 ms 36188 KB Output is correct
82 Correct 15 ms 35672 KB Output is correct
83 Correct 16 ms 35676 KB Output is correct
84 Correct 18 ms 36184 KB Output is correct
85 Correct 98 ms 52828 KB Output is correct
86 Correct 20 ms 36184 KB Output is correct
87 Correct 79 ms 51400 KB Output is correct
88 Correct 15 ms 35672 KB Output is correct
89 Correct 101 ms 51408 KB Output is correct
90 Correct 91 ms 53076 KB Output is correct
91 Correct 88 ms 57064 KB Output is correct
92 Correct 76 ms 51556 KB Output is correct
93 Correct 16 ms 36184 KB Output is correct
94 Correct 15 ms 35676 KB Output is correct
95 Correct 79 ms 56632 KB Output is correct
96 Correct 121 ms 61200 KB Output is correct
97 Correct 17 ms 36184 KB Output is correct
98 Correct 20 ms 35676 KB Output is correct
99 Correct 97 ms 53280 KB Output is correct
100 Correct 17 ms 36188 KB Output is correct
101 Correct 90 ms 51888 KB Output is correct
102 Correct 96 ms 59732 KB Output is correct
103 Correct 17 ms 36184 KB Output is correct
104 Correct 18 ms 36188 KB Output is correct
105 Correct 18 ms 36188 KB Output is correct
106 Correct 17 ms 36228 KB Output is correct
107 Correct 17 ms 36240 KB Output is correct
108 Correct 472 ms 121020 KB Output is correct
109 Correct 850 ms 130796 KB Output is correct
110 Correct 842 ms 142020 KB Output is correct
111 Correct 793 ms 164864 KB Output is correct
112 Correct 678 ms 149804 KB Output is correct
113 Correct 486 ms 141624 KB Output is correct
114 Correct 622 ms 112764 KB Output is correct
115 Correct 643 ms 112844 KB Output is correct
116 Correct 767 ms 116564 KB Output is correct
117 Correct 730 ms 143448 KB Output is correct
118 Correct 637 ms 112976 KB Output is correct
119 Correct 685 ms 143548 KB Output is correct
120 Correct 724 ms 157008 KB Output is correct
121 Correct 677 ms 143372 KB Output is correct
122 Correct 651 ms 124104 KB Output is correct
123 Correct 472 ms 144676 KB Output is correct
124 Correct 786 ms 135452 KB Output is correct