답안 #205100

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
205100 2020-02-28T00:07:06 Z achibasadzishvili Mergers (JOI19_mergers) C++17
100 / 100
1982 ms 232040 KB
#include<bits/stdc++.h>
#define ll int
#define f first
#define s second
#define pb push_back
using namespace std;
ll P[500002][20],lvl[500002],pp[500002],in[500002],out[500002],timer,ch[500002];
ll n,k,bst[500002],c[500002],par[500002],p[500002],sz[500002],raod[500002];
ll fix[500002];
set<ll>st[500002];
vector<ll>v[500002],del[500002],fer[500002];
void dfs(ll x,ll par) {
    P[x][0]=par;
    pp[x] = par;
    for (int i=1;i<=19;i++)
        P[x][i]=P[P[x][i-1]][i-1];
    timer++;
    in[x]=timer;
    ch[x] = 1;
    for (int i=0;i<v[x].size();i++)
        if (v[x][i] != par) {
            lvl[v[x][i]]=lvl[x]+1;
            dfs(v[x][i],x);
            ch[x] += ch[v[x][i]];
        }
    out[x]=timer;
}

ll lca(ll a,ll b) {
    if(lvl[a] > lvl[b])swap(a,b);
    if (in[a] <= in[b] && out[b] <= out[a]) return a;
    for (int i=19;i>=0;i--)
        if (P[a][i])
            if (!(in[P[a][i]] <= in[b] && out[b] <= out[P[a][i]]))
                a=P[a][i];
    return P[a][0];
}
ll find(ll x){
    if(p[x] == x)return x;
    return p[x] = find(p[x]);
}
void join(ll x,ll y){
    //cout << x << " " << y << endl;
    x = find(x);
    y = find(y);
    if(x == y)return;
    if(sz[y] > sz[x])swap(x , y);
    sz[x] += sz[y];
    p[y] = x;
}
void solve(ll x){
    if(v[x].size() == 0){
        bst[x] = x;
        if(par[c[x]] != x)st[x].insert(c[x]);
        return;
    }
    for(int i=0; i<v[x].size(); i++)
        solve(v[x][i]);
    bst[x] = bst[v[x][0]];
    if(st[bst[x]].size() > 0){
        join(c[x] , (*st[bst[x]].begin()));
    }
    st[bst[x]].insert(c[x]);
    for(int i=1; i<v[x].size(); i++){
        ll t = bst[v[x][i]];
        for(set<ll>::iterator it = st[t].begin(); it != st[t].end(); it++){
            join(c[x] , (*it));
            st[bst[x]].insert((*it));
        }
    }
    for(int i=0; i<del[x].size(); i++)
        st[bst[x]].erase(st[bst[x]].find(del[x][i]));
}
int main(){
    ios::sync_with_stdio(false);
    cin >> n >> k;
    
    for(int i=1; i<n; i++){
        ll x,y;
        cin >> x >> y;
        v[x].pb(y);
        v[y].pb(x);
    }
    
    for(int i=1; i<=n; i++){
        cin >> c[i];
        fer[c[i]].pb(i);
    }
    
    dfs(1 , 0);
    
    for(int i=1; i<=k; i++){
        par[i] = fer[i][0];
        for(int j=1; j<fer[i].size(); j++)
            par[i] = lca(par[i] , fer[i][j]);
        del[par[i]].pb(i);/*
        for(int j=0; j<fer[i].size(); j++){
            ll x = fer[i][j];
            while(1){
                join(i , c[x]);
                if(x == par[i])break;
                x = pp[x];
            }
        }*/
    }
    
    for(int i=1; i<=n; i++){
        sz[i] = 1;
        p[i] = i;
        v[i].clear();
    }
    
    for(int i=2; i<=n; i++)
        v[pp[i]].pb(i);
    
    for(int i=1; i<=n; i++)
        for(int j=1; j<v[i].size(); j++)
            if(ch[v[i][j]] > ch[v[i][0]])
                swap(v[i][j] , v[i][0]);
    
    
    solve(1);
    
    for(int i=1; i<=n; i++){
        for(int j=0; j<v[i].size(); j++){
            ll x = v[i][j];
            if(find(c[i]) != find(c[x])){
                raod[find(c[i])]++;
                raod[find(c[x])]++;
            }
        }
    }
    
    ll ans = 1;
    for(int i=1; i<=k; i++){
        if(!fix[find(i)]){
            fix[find(i)] = 1;
            if(raod[find(i)] == 1)ans++;
        }
    }
    
    cout << ans / 2 << '\n';
    
    
    return 0;
}

Compilation message

mergers.cpp: In function 'void dfs(int, int)':
mergers.cpp:20:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i=0;i<v[x].size();i++)
                  ~^~~~~~~~~~~~
mergers.cpp: In function 'void solve(int)':
mergers.cpp:57:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0; i<v[x].size(); i++)
                  ~^~~~~~~~~~~~
mergers.cpp:64:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=1; i<v[x].size(); i++){
                  ~^~~~~~~~~~~~
mergers.cpp:71:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0; i<del[x].size(); i++)
                  ~^~~~~~~~~~~~~~
mergers.cpp: In function 'int main()':
mergers.cpp:94:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int j=1; j<fer[i].size(); j++)
                      ~^~~~~~~~~~~~~~
mergers.cpp:117:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int j=1; j<v[i].size(); j++)
                      ~^~~~~~~~~~~~
mergers.cpp:125:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int j=0; j<v[i].size(); j++){
                      ~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 59128 KB Output is correct
2 Correct 45 ms 59000 KB Output is correct
3 Correct 45 ms 59128 KB Output is correct
4 Correct 42 ms 59128 KB Output is correct
5 Correct 43 ms 59128 KB Output is correct
6 Correct 44 ms 59128 KB Output is correct
7 Correct 44 ms 59128 KB Output is correct
8 Correct 43 ms 59128 KB Output is correct
9 Correct 46 ms 59128 KB Output is correct
10 Correct 43 ms 59128 KB Output is correct
11 Correct 43 ms 59128 KB Output is correct
12 Correct 42 ms 59120 KB Output is correct
13 Correct 42 ms 59176 KB Output is correct
14 Correct 41 ms 59128 KB Output is correct
15 Correct 43 ms 59128 KB Output is correct
16 Correct 43 ms 59128 KB Output is correct
17 Correct 43 ms 59128 KB Output is correct
18 Correct 43 ms 59128 KB Output is correct
19 Correct 43 ms 59128 KB Output is correct
20 Correct 43 ms 59128 KB Output is correct
21 Correct 43 ms 59128 KB Output is correct
22 Correct 45 ms 59128 KB Output is correct
23 Correct 43 ms 59128 KB Output is correct
24 Correct 43 ms 59128 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 59128 KB Output is correct
2 Correct 45 ms 59000 KB Output is correct
3 Correct 45 ms 59128 KB Output is correct
4 Correct 42 ms 59128 KB Output is correct
5 Correct 43 ms 59128 KB Output is correct
6 Correct 44 ms 59128 KB Output is correct
7 Correct 44 ms 59128 KB Output is correct
8 Correct 43 ms 59128 KB Output is correct
9 Correct 46 ms 59128 KB Output is correct
10 Correct 43 ms 59128 KB Output is correct
11 Correct 43 ms 59128 KB Output is correct
12 Correct 42 ms 59120 KB Output is correct
13 Correct 42 ms 59176 KB Output is correct
14 Correct 41 ms 59128 KB Output is correct
15 Correct 43 ms 59128 KB Output is correct
16 Correct 43 ms 59128 KB Output is correct
17 Correct 43 ms 59128 KB Output is correct
18 Correct 43 ms 59128 KB Output is correct
19 Correct 43 ms 59128 KB Output is correct
20 Correct 43 ms 59128 KB Output is correct
21 Correct 43 ms 59128 KB Output is correct
22 Correct 45 ms 59128 KB Output is correct
23 Correct 43 ms 59128 KB Output is correct
24 Correct 43 ms 59128 KB Output is correct
25 Correct 43 ms 59128 KB Output is correct
26 Correct 47 ms 59784 KB Output is correct
27 Correct 44 ms 59640 KB Output is correct
28 Correct 48 ms 59896 KB Output is correct
29 Correct 45 ms 59896 KB Output is correct
30 Correct 46 ms 59896 KB Output is correct
31 Correct 44 ms 59128 KB Output is correct
32 Correct 47 ms 60024 KB Output is correct
33 Correct 43 ms 59128 KB Output is correct
34 Correct 46 ms 59772 KB Output is correct
35 Correct 47 ms 59960 KB Output is correct
36 Correct 46 ms 59640 KB Output is correct
37 Correct 47 ms 59896 KB Output is correct
38 Correct 42 ms 59128 KB Output is correct
39 Correct 47 ms 60152 KB Output is correct
40 Correct 46 ms 59640 KB Output is correct
41 Correct 47 ms 59640 KB Output is correct
42 Correct 46 ms 59896 KB Output is correct
43 Correct 46 ms 59768 KB Output is correct
44 Correct 42 ms 59128 KB Output is correct
45 Correct 47 ms 59896 KB Output is correct
46 Correct 47 ms 59768 KB Output is correct
47 Correct 43 ms 59128 KB Output is correct
48 Correct 46 ms 59768 KB Output is correct
49 Correct 47 ms 59896 KB Output is correct
50 Correct 46 ms 59896 KB Output is correct
51 Correct 46 ms 59640 KB Output is correct
52 Correct 45 ms 59768 KB Output is correct
53 Correct 45 ms 59896 KB Output is correct
54 Correct 46 ms 59896 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 59128 KB Output is correct
2 Correct 45 ms 59000 KB Output is correct
3 Correct 45 ms 59128 KB Output is correct
4 Correct 42 ms 59128 KB Output is correct
5 Correct 43 ms 59128 KB Output is correct
6 Correct 44 ms 59128 KB Output is correct
7 Correct 44 ms 59128 KB Output is correct
8 Correct 43 ms 59128 KB Output is correct
9 Correct 46 ms 59128 KB Output is correct
10 Correct 43 ms 59128 KB Output is correct
11 Correct 43 ms 59128 KB Output is correct
12 Correct 42 ms 59120 KB Output is correct
13 Correct 42 ms 59176 KB Output is correct
14 Correct 41 ms 59128 KB Output is correct
15 Correct 43 ms 59128 KB Output is correct
16 Correct 43 ms 59128 KB Output is correct
17 Correct 43 ms 59128 KB Output is correct
18 Correct 43 ms 59128 KB Output is correct
19 Correct 43 ms 59128 KB Output is correct
20 Correct 43 ms 59128 KB Output is correct
21 Correct 43 ms 59128 KB Output is correct
22 Correct 45 ms 59128 KB Output is correct
23 Correct 43 ms 59128 KB Output is correct
24 Correct 43 ms 59128 KB Output is correct
25 Correct 43 ms 59128 KB Output is correct
26 Correct 131 ms 80624 KB Output is correct
27 Correct 214 ms 83576 KB Output is correct
28 Correct 45 ms 59640 KB Output is correct
29 Correct 42 ms 59128 KB Output is correct
30 Correct 43 ms 59128 KB Output is correct
31 Correct 188 ms 77920 KB Output is correct
32 Correct 45 ms 59768 KB Output is correct
33 Correct 214 ms 81272 KB Output is correct
34 Correct 201 ms 82424 KB Output is correct
35 Correct 47 ms 59768 KB Output is correct
36 Correct 191 ms 76152 KB Output is correct
37 Correct 45 ms 59768 KB Output is correct
38 Correct 45 ms 59640 KB Output is correct
39 Correct 137 ms 80496 KB Output is correct
40 Correct 45 ms 59896 KB Output is correct
41 Correct 182 ms 77784 KB Output is correct
42 Correct 210 ms 77560 KB Output is correct
43 Correct 46 ms 59128 KB Output is correct
44 Correct 201 ms 81520 KB Output is correct
45 Correct 221 ms 80888 KB Output is correct
46 Correct 45 ms 59768 KB Output is correct
47 Correct 49 ms 59896 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 136 ms 79088 KB Output is correct
2 Correct 163 ms 81392 KB Output is correct
3 Correct 47 ms 59768 KB Output is correct
4 Correct 47 ms 59696 KB Output is correct
5 Correct 44 ms 59128 KB Output is correct
6 Correct 42 ms 59128 KB Output is correct
7 Correct 45 ms 59768 KB Output is correct
8 Correct 244 ms 83320 KB Output is correct
9 Correct 45 ms 59640 KB Output is correct
10 Correct 184 ms 78072 KB Output is correct
11 Correct 43 ms 59128 KB Output is correct
12 Correct 191 ms 75768 KB Output is correct
13 Correct 246 ms 80888 KB Output is correct
14 Correct 225 ms 82808 KB Output is correct
15 Correct 138 ms 80496 KB Output is correct
16 Correct 46 ms 59896 KB Output is correct
17 Correct 45 ms 59128 KB Output is correct
18 Correct 184 ms 83444 KB Output is correct
19 Correct 288 ms 86008 KB Output is correct
20 Correct 48 ms 59768 KB Output is correct
21 Correct 42 ms 59128 KB Output is correct
22 Correct 184 ms 83824 KB Output is correct
23 Correct 47 ms 59640 KB Output is correct
24 Correct 212 ms 77152 KB Output is correct
25 Correct 271 ms 84956 KB Output is correct
26 Correct 44 ms 59896 KB Output is correct
27 Correct 45 ms 59896 KB Output is correct
28 Correct 47 ms 59640 KB Output is correct
29 Correct 44 ms 59768 KB Output is correct
30 Correct 46 ms 59896 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 59128 KB Output is correct
2 Correct 45 ms 59000 KB Output is correct
3 Correct 45 ms 59128 KB Output is correct
4 Correct 42 ms 59128 KB Output is correct
5 Correct 43 ms 59128 KB Output is correct
6 Correct 44 ms 59128 KB Output is correct
7 Correct 44 ms 59128 KB Output is correct
8 Correct 43 ms 59128 KB Output is correct
9 Correct 46 ms 59128 KB Output is correct
10 Correct 43 ms 59128 KB Output is correct
11 Correct 43 ms 59128 KB Output is correct
12 Correct 42 ms 59120 KB Output is correct
13 Correct 42 ms 59176 KB Output is correct
14 Correct 41 ms 59128 KB Output is correct
15 Correct 43 ms 59128 KB Output is correct
16 Correct 43 ms 59128 KB Output is correct
17 Correct 43 ms 59128 KB Output is correct
18 Correct 43 ms 59128 KB Output is correct
19 Correct 43 ms 59128 KB Output is correct
20 Correct 43 ms 59128 KB Output is correct
21 Correct 43 ms 59128 KB Output is correct
22 Correct 45 ms 59128 KB Output is correct
23 Correct 43 ms 59128 KB Output is correct
24 Correct 43 ms 59128 KB Output is correct
25 Correct 43 ms 59128 KB Output is correct
26 Correct 47 ms 59784 KB Output is correct
27 Correct 44 ms 59640 KB Output is correct
28 Correct 48 ms 59896 KB Output is correct
29 Correct 45 ms 59896 KB Output is correct
30 Correct 46 ms 59896 KB Output is correct
31 Correct 44 ms 59128 KB Output is correct
32 Correct 47 ms 60024 KB Output is correct
33 Correct 43 ms 59128 KB Output is correct
34 Correct 46 ms 59772 KB Output is correct
35 Correct 47 ms 59960 KB Output is correct
36 Correct 46 ms 59640 KB Output is correct
37 Correct 47 ms 59896 KB Output is correct
38 Correct 42 ms 59128 KB Output is correct
39 Correct 47 ms 60152 KB Output is correct
40 Correct 46 ms 59640 KB Output is correct
41 Correct 47 ms 59640 KB Output is correct
42 Correct 46 ms 59896 KB Output is correct
43 Correct 46 ms 59768 KB Output is correct
44 Correct 42 ms 59128 KB Output is correct
45 Correct 47 ms 59896 KB Output is correct
46 Correct 47 ms 59768 KB Output is correct
47 Correct 43 ms 59128 KB Output is correct
48 Correct 46 ms 59768 KB Output is correct
49 Correct 47 ms 59896 KB Output is correct
50 Correct 46 ms 59896 KB Output is correct
51 Correct 46 ms 59640 KB Output is correct
52 Correct 45 ms 59768 KB Output is correct
53 Correct 45 ms 59896 KB Output is correct
54 Correct 46 ms 59896 KB Output is correct
55 Correct 43 ms 59128 KB Output is correct
56 Correct 131 ms 80624 KB Output is correct
57 Correct 214 ms 83576 KB Output is correct
58 Correct 45 ms 59640 KB Output is correct
59 Correct 42 ms 59128 KB Output is correct
60 Correct 43 ms 59128 KB Output is correct
61 Correct 188 ms 77920 KB Output is correct
62 Correct 45 ms 59768 KB Output is correct
63 Correct 214 ms 81272 KB Output is correct
64 Correct 201 ms 82424 KB Output is correct
65 Correct 47 ms 59768 KB Output is correct
66 Correct 191 ms 76152 KB Output is correct
67 Correct 45 ms 59768 KB Output is correct
68 Correct 45 ms 59640 KB Output is correct
69 Correct 137 ms 80496 KB Output is correct
70 Correct 45 ms 59896 KB Output is correct
71 Correct 182 ms 77784 KB Output is correct
72 Correct 210 ms 77560 KB Output is correct
73 Correct 46 ms 59128 KB Output is correct
74 Correct 201 ms 81520 KB Output is correct
75 Correct 221 ms 80888 KB Output is correct
76 Correct 45 ms 59768 KB Output is correct
77 Correct 49 ms 59896 KB Output is correct
78 Correct 136 ms 79088 KB Output is correct
79 Correct 163 ms 81392 KB Output is correct
80 Correct 47 ms 59768 KB Output is correct
81 Correct 47 ms 59696 KB Output is correct
82 Correct 44 ms 59128 KB Output is correct
83 Correct 42 ms 59128 KB Output is correct
84 Correct 45 ms 59768 KB Output is correct
85 Correct 244 ms 83320 KB Output is correct
86 Correct 45 ms 59640 KB Output is correct
87 Correct 184 ms 78072 KB Output is correct
88 Correct 43 ms 59128 KB Output is correct
89 Correct 191 ms 75768 KB Output is correct
90 Correct 246 ms 80888 KB Output is correct
91 Correct 225 ms 82808 KB Output is correct
92 Correct 138 ms 80496 KB Output is correct
93 Correct 46 ms 59896 KB Output is correct
94 Correct 45 ms 59128 KB Output is correct
95 Correct 184 ms 83444 KB Output is correct
96 Correct 288 ms 86008 KB Output is correct
97 Correct 48 ms 59768 KB Output is correct
98 Correct 42 ms 59128 KB Output is correct
99 Correct 184 ms 83824 KB Output is correct
100 Correct 47 ms 59640 KB Output is correct
101 Correct 212 ms 77152 KB Output is correct
102 Correct 271 ms 84956 KB Output is correct
103 Correct 44 ms 59896 KB Output is correct
104 Correct 45 ms 59896 KB Output is correct
105 Correct 47 ms 59640 KB Output is correct
106 Correct 44 ms 59768 KB Output is correct
107 Correct 46 ms 59896 KB Output is correct
108 Correct 1089 ms 183104 KB Output is correct
109 Correct 1255 ms 155952 KB Output is correct
110 Correct 1273 ms 165260 KB Output is correct
111 Correct 1982 ms 197496 KB Output is correct
112 Correct 1821 ms 189688 KB Output is correct
113 Correct 1064 ms 181904 KB Output is correct
114 Correct 1038 ms 153360 KB Output is correct
115 Correct 1027 ms 153316 KB Output is correct
116 Correct 1615 ms 208888 KB Output is correct
117 Correct 1527 ms 179192 KB Output is correct
118 Correct 1047 ms 153444 KB Output is correct
119 Correct 1536 ms 179192 KB Output is correct
120 Correct 1847 ms 190328 KB Output is correct
121 Correct 1524 ms 179192 KB Output is correct
122 Correct 1918 ms 232040 KB Output is correct
123 Correct 933 ms 180708 KB Output is correct
124 Correct 1819 ms 176764 KB Output is correct