Submission #202282

# Submission time Handle Problem Language Result Execution time Memory
202282 2020-02-15T05:21:02 Z Rakhmand Mergers (JOI19_mergers) C++14
100 / 100
1731 ms 130956 KB
//
//  ROIGold.cpp
//  Main calisma
//
//  Created by Rakhman on 05/02/2019.
//  Copyright © 2019 Rakhman. All rights reserved.
//
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <queue>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <iterator>

#define ios ios_base::sync_with_stdio(0), cout.tie(0), cin.tie(0);
#define S second
#define F first
#define pb push_back
#define nl '\n'
#define NL cout << '\n';
#define EX exit(0)
#define all(s) s.begin(), s.end()
#define FOR(i, start, finish, k) for(int i = start; i <= finish; i += k)

const int MXN = 5e5 + 10;
const long long MNN = 1e6 + 200;
const long long MOD = 1e9 + 7;
const long long INF = 1e16;
const int OO = 1e9 + 10;

typedef long long llong;
typedef unsigned long long ullong;

using namespace std;

void istxt(bool yes){
    if(yes == 1){
        freopen("balancing.in", "r", stdin);
        freopen("balancing.out", "w", stdout);
    }else{
        freopen("/Users/rakhmanabdirashov/Desktop/folder/Programming/Road2Master/Road2Master/input.txt", "r", stdin);
    }
}

int n, k, dp[MXN][20], lvl[MXN], cnt[MXN], p[MXN], lol[MXN];
vector<int> g[MXN];
vector<int> state[MXN];

void dfs(int x, int p){
    dp[x][0] = p;
    for(int i = 1; i < 20; i++) dp[x][i] = dp[dp[x][i - 1]][i - 1];
    lvl[x] = lvl[p] + 1;
    for(int i = 0; i < g[x].size(); i++) if(g[x][i] != p) dfs(g[x][i], x);
}

int lca(int u, int v){
    if(lvl[u] < lvl[v]) swap(u, v);
    int dist = lvl[u] - lvl[v];
    
    for(int i = 0; i < 20; i++)
        if((1 << i) & dist)
            u = dp[u][i];
    
    if(u == v) return u;
    
    for(int i = 19; i >= 0; i--){
        if(dp[u][i] != dp[v][i]){
            u = dp[u][i];
            v = dp[v][i];
        }
    }
    
    return dp[v][0];
}

int find(int x) { return p[x] = (x == p[x] ? x : find(p[x])); }
void attach(int u, int v) {u = find(u), v = find(v), p[u] = v; }

void dfs2(int v, int p){
    for(int i = 0; i < g[v].size(); i++){
        int to = g[v][i];
        if(to == p) continue;
        dfs2(to, v);
        cnt[v] += cnt[to];
    }
    if(v != 1 && cnt[v]) attach(p, v);
}

int main () {
    ios;
    //istxt(0);
    cin >> n >> k;
    for(int i = 1; i < n; i++){
        int u, v;
        cin >> u >> v;
        g[u].pb(v);
        g[v].pb(u);
    }
    dfs(1, 0);
    for(int i = 1; i <= n; i++){
        p[i] = i;
        int x;
        cin >> x;
        state[x].pb(i);
    }
    for(int st = 1; st <= k; st++){
        for(int j = 1; j < state[st].size(); j++){
            int u = state[st][j - 1], v = state[st][j];
            int batya = lca(u, v);
            cnt[u]++, cnt[v]++, cnt[batya]--, cnt[batya]--;
        }
    }
    dfs2(1, 1);
    
    for(int i = 2; i <= n; i++){
        int batya = dp[i][0];
        if(find(batya) == find(i)) continue;
        lol[find(batya)]++;
        lol[find(i)]++;
    }
    int ans = 0;
    for(int i = 1; i <= n; i++){
        if(lol[i] == 1) ans++;
    }
    if(ans == 1) ans = 0;
    ans++;
    cout << ans / 2;
}

Compilation message

mergers.cpp: In function 'void dfs(int, int)':
mergers.cpp:69:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < g[x].size(); i++) if(g[x][i] != p) dfs(g[x][i], x);
                    ~~^~~~~~~~~~~~~
mergers.cpp: In function 'void dfs2(int, int)':
mergers.cpp:96:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < g[v].size(); i++){
                    ~~^~~~~~~~~~~~~
mergers.cpp: In function 'int main()':
mergers.cpp:123:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int j = 1; j < state[st].size(); j++){
                        ~~^~~~~~~~~~~~~~~~~~
mergers.cpp: In function 'void istxt(bool)':
mergers.cpp:54:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
         freopen("balancing.in", "r", stdin);
         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
mergers.cpp:55:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
         freopen("balancing.out", "w", stdout);
         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mergers.cpp:57:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
         freopen("/Users/rakhmanabdirashov/Desktop/folder/Programming/Road2Master/Road2Master/input.txt", "r", stdin);
         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 19 ms 23800 KB Output is correct
2 Correct 19 ms 23800 KB Output is correct
3 Correct 19 ms 23800 KB Output is correct
4 Correct 19 ms 23800 KB Output is correct
5 Correct 19 ms 23796 KB Output is correct
6 Correct 20 ms 23800 KB Output is correct
7 Correct 18 ms 23800 KB Output is correct
8 Correct 18 ms 23800 KB Output is correct
9 Correct 19 ms 23800 KB Output is correct
10 Correct 19 ms 23928 KB Output is correct
11 Correct 19 ms 23800 KB Output is correct
12 Correct 19 ms 23800 KB Output is correct
13 Correct 21 ms 23852 KB Output is correct
14 Correct 21 ms 23916 KB Output is correct
15 Correct 20 ms 23800 KB Output is correct
16 Correct 19 ms 23928 KB Output is correct
17 Correct 19 ms 23928 KB Output is correct
18 Correct 21 ms 23928 KB Output is correct
19 Correct 22 ms 23800 KB Output is correct
20 Correct 19 ms 23800 KB Output is correct
21 Correct 19 ms 23800 KB Output is correct
22 Correct 19 ms 23928 KB Output is correct
23 Correct 21 ms 23800 KB Output is correct
24 Correct 19 ms 23928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 23800 KB Output is correct
2 Correct 19 ms 23800 KB Output is correct
3 Correct 19 ms 23800 KB Output is correct
4 Correct 19 ms 23800 KB Output is correct
5 Correct 19 ms 23796 KB Output is correct
6 Correct 20 ms 23800 KB Output is correct
7 Correct 18 ms 23800 KB Output is correct
8 Correct 18 ms 23800 KB Output is correct
9 Correct 19 ms 23800 KB Output is correct
10 Correct 19 ms 23928 KB Output is correct
11 Correct 19 ms 23800 KB Output is correct
12 Correct 19 ms 23800 KB Output is correct
13 Correct 21 ms 23852 KB Output is correct
14 Correct 21 ms 23916 KB Output is correct
15 Correct 20 ms 23800 KB Output is correct
16 Correct 19 ms 23928 KB Output is correct
17 Correct 19 ms 23928 KB Output is correct
18 Correct 21 ms 23928 KB Output is correct
19 Correct 22 ms 23800 KB Output is correct
20 Correct 19 ms 23800 KB Output is correct
21 Correct 19 ms 23800 KB Output is correct
22 Correct 19 ms 23928 KB Output is correct
23 Correct 21 ms 23800 KB Output is correct
24 Correct 19 ms 23928 KB Output is correct
25 Correct 18 ms 23800 KB Output is correct
26 Correct 23 ms 24312 KB Output is correct
27 Correct 22 ms 24312 KB Output is correct
28 Correct 22 ms 24440 KB Output is correct
29 Correct 21 ms 24312 KB Output is correct
30 Correct 23 ms 24312 KB Output is correct
31 Correct 19 ms 23800 KB Output is correct
32 Correct 22 ms 24568 KB Output is correct
33 Correct 18 ms 23800 KB Output is correct
34 Correct 21 ms 24312 KB Output is correct
35 Correct 22 ms 24312 KB Output is correct
36 Correct 22 ms 24312 KB Output is correct
37 Correct 23 ms 24312 KB Output is correct
38 Correct 19 ms 23928 KB Output is correct
39 Correct 22 ms 24312 KB Output is correct
40 Correct 21 ms 24312 KB Output is correct
41 Correct 21 ms 24268 KB Output is correct
42 Correct 21 ms 24312 KB Output is correct
43 Correct 21 ms 24440 KB Output is correct
44 Correct 19 ms 23872 KB Output is correct
45 Correct 21 ms 24440 KB Output is correct
46 Correct 22 ms 24440 KB Output is correct
47 Correct 19 ms 23928 KB Output is correct
48 Correct 22 ms 24312 KB Output is correct
49 Correct 21 ms 24312 KB Output is correct
50 Correct 22 ms 24440 KB Output is correct
51 Correct 21 ms 24312 KB Output is correct
52 Correct 21 ms 24312 KB Output is correct
53 Correct 22 ms 24312 KB Output is correct
54 Correct 21 ms 24300 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 23800 KB Output is correct
2 Correct 19 ms 23800 KB Output is correct
3 Correct 19 ms 23800 KB Output is correct
4 Correct 19 ms 23800 KB Output is correct
5 Correct 19 ms 23796 KB Output is correct
6 Correct 20 ms 23800 KB Output is correct
7 Correct 18 ms 23800 KB Output is correct
8 Correct 18 ms 23800 KB Output is correct
9 Correct 19 ms 23800 KB Output is correct
10 Correct 19 ms 23928 KB Output is correct
11 Correct 19 ms 23800 KB Output is correct
12 Correct 19 ms 23800 KB Output is correct
13 Correct 21 ms 23852 KB Output is correct
14 Correct 21 ms 23916 KB Output is correct
15 Correct 20 ms 23800 KB Output is correct
16 Correct 19 ms 23928 KB Output is correct
17 Correct 19 ms 23928 KB Output is correct
18 Correct 21 ms 23928 KB Output is correct
19 Correct 22 ms 23800 KB Output is correct
20 Correct 19 ms 23800 KB Output is correct
21 Correct 19 ms 23800 KB Output is correct
22 Correct 19 ms 23928 KB Output is correct
23 Correct 21 ms 23800 KB Output is correct
24 Correct 19 ms 23928 KB Output is correct
25 Correct 19 ms 23928 KB Output is correct
26 Correct 116 ms 38768 KB Output is correct
27 Correct 158 ms 38008 KB Output is correct
28 Correct 21 ms 24312 KB Output is correct
29 Correct 19 ms 23800 KB Output is correct
30 Correct 18 ms 23800 KB Output is correct
31 Correct 145 ms 38136 KB Output is correct
32 Correct 21 ms 24312 KB Output is correct
33 Correct 220 ms 43644 KB Output is correct
34 Correct 150 ms 38136 KB Output is correct
35 Correct 21 ms 24312 KB Output is correct
36 Correct 173 ms 38648 KB Output is correct
37 Correct 21 ms 24312 KB Output is correct
38 Correct 23 ms 24440 KB Output is correct
39 Correct 106 ms 38636 KB Output is correct
40 Correct 21 ms 24440 KB Output is correct
41 Correct 147 ms 38000 KB Output is correct
42 Correct 224 ms 39928 KB Output is correct
43 Correct 19 ms 23928 KB Output is correct
44 Correct 202 ms 43884 KB Output is correct
45 Correct 199 ms 41000 KB Output is correct
46 Correct 21 ms 24312 KB Output is correct
47 Correct 21 ms 24312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 149 ms 37360 KB Output is correct
2 Correct 107 ms 39536 KB Output is correct
3 Correct 21 ms 24312 KB Output is correct
4 Correct 21 ms 24312 KB Output is correct
5 Correct 19 ms 23800 KB Output is correct
6 Correct 18 ms 23800 KB Output is correct
7 Correct 20 ms 24312 KB Output is correct
8 Correct 163 ms 39800 KB Output is correct
9 Correct 24 ms 24312 KB Output is correct
10 Correct 146 ms 38624 KB Output is correct
11 Correct 20 ms 23928 KB Output is correct
12 Correct 164 ms 38648 KB Output is correct
13 Correct 156 ms 39804 KB Output is correct
14 Correct 141 ms 41336 KB Output is correct
15 Correct 110 ms 38640 KB Output is correct
16 Correct 21 ms 24312 KB Output is correct
17 Correct 19 ms 23928 KB Output is correct
18 Correct 117 ms 41456 KB Output is correct
19 Correct 171 ms 44408 KB Output is correct
20 Correct 22 ms 24312 KB Output is correct
21 Correct 19 ms 23924 KB Output is correct
22 Correct 120 ms 41204 KB Output is correct
23 Correct 21 ms 24312 KB Output is correct
24 Correct 164 ms 38904 KB Output is correct
25 Correct 169 ms 43384 KB Output is correct
26 Correct 21 ms 24312 KB Output is correct
27 Correct 20 ms 24440 KB Output is correct
28 Correct 22 ms 24312 KB Output is correct
29 Correct 20 ms 24312 KB Output is correct
30 Correct 21 ms 24312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 23800 KB Output is correct
2 Correct 19 ms 23800 KB Output is correct
3 Correct 19 ms 23800 KB Output is correct
4 Correct 19 ms 23800 KB Output is correct
5 Correct 19 ms 23796 KB Output is correct
6 Correct 20 ms 23800 KB Output is correct
7 Correct 18 ms 23800 KB Output is correct
8 Correct 18 ms 23800 KB Output is correct
9 Correct 19 ms 23800 KB Output is correct
10 Correct 19 ms 23928 KB Output is correct
11 Correct 19 ms 23800 KB Output is correct
12 Correct 19 ms 23800 KB Output is correct
13 Correct 21 ms 23852 KB Output is correct
14 Correct 21 ms 23916 KB Output is correct
15 Correct 20 ms 23800 KB Output is correct
16 Correct 19 ms 23928 KB Output is correct
17 Correct 19 ms 23928 KB Output is correct
18 Correct 21 ms 23928 KB Output is correct
19 Correct 22 ms 23800 KB Output is correct
20 Correct 19 ms 23800 KB Output is correct
21 Correct 19 ms 23800 KB Output is correct
22 Correct 19 ms 23928 KB Output is correct
23 Correct 21 ms 23800 KB Output is correct
24 Correct 19 ms 23928 KB Output is correct
25 Correct 18 ms 23800 KB Output is correct
26 Correct 23 ms 24312 KB Output is correct
27 Correct 22 ms 24312 KB Output is correct
28 Correct 22 ms 24440 KB Output is correct
29 Correct 21 ms 24312 KB Output is correct
30 Correct 23 ms 24312 KB Output is correct
31 Correct 19 ms 23800 KB Output is correct
32 Correct 22 ms 24568 KB Output is correct
33 Correct 18 ms 23800 KB Output is correct
34 Correct 21 ms 24312 KB Output is correct
35 Correct 22 ms 24312 KB Output is correct
36 Correct 22 ms 24312 KB Output is correct
37 Correct 23 ms 24312 KB Output is correct
38 Correct 19 ms 23928 KB Output is correct
39 Correct 22 ms 24312 KB Output is correct
40 Correct 21 ms 24312 KB Output is correct
41 Correct 21 ms 24268 KB Output is correct
42 Correct 21 ms 24312 KB Output is correct
43 Correct 21 ms 24440 KB Output is correct
44 Correct 19 ms 23872 KB Output is correct
45 Correct 21 ms 24440 KB Output is correct
46 Correct 22 ms 24440 KB Output is correct
47 Correct 19 ms 23928 KB Output is correct
48 Correct 22 ms 24312 KB Output is correct
49 Correct 21 ms 24312 KB Output is correct
50 Correct 22 ms 24440 KB Output is correct
51 Correct 21 ms 24312 KB Output is correct
52 Correct 21 ms 24312 KB Output is correct
53 Correct 22 ms 24312 KB Output is correct
54 Correct 21 ms 24300 KB Output is correct
55 Correct 19 ms 23928 KB Output is correct
56 Correct 116 ms 38768 KB Output is correct
57 Correct 158 ms 38008 KB Output is correct
58 Correct 21 ms 24312 KB Output is correct
59 Correct 19 ms 23800 KB Output is correct
60 Correct 18 ms 23800 KB Output is correct
61 Correct 145 ms 38136 KB Output is correct
62 Correct 21 ms 24312 KB Output is correct
63 Correct 220 ms 43644 KB Output is correct
64 Correct 150 ms 38136 KB Output is correct
65 Correct 21 ms 24312 KB Output is correct
66 Correct 173 ms 38648 KB Output is correct
67 Correct 21 ms 24312 KB Output is correct
68 Correct 23 ms 24440 KB Output is correct
69 Correct 106 ms 38636 KB Output is correct
70 Correct 21 ms 24440 KB Output is correct
71 Correct 147 ms 38000 KB Output is correct
72 Correct 224 ms 39928 KB Output is correct
73 Correct 19 ms 23928 KB Output is correct
74 Correct 202 ms 43884 KB Output is correct
75 Correct 199 ms 41000 KB Output is correct
76 Correct 21 ms 24312 KB Output is correct
77 Correct 21 ms 24312 KB Output is correct
78 Correct 149 ms 37360 KB Output is correct
79 Correct 107 ms 39536 KB Output is correct
80 Correct 21 ms 24312 KB Output is correct
81 Correct 21 ms 24312 KB Output is correct
82 Correct 19 ms 23800 KB Output is correct
83 Correct 18 ms 23800 KB Output is correct
84 Correct 20 ms 24312 KB Output is correct
85 Correct 163 ms 39800 KB Output is correct
86 Correct 24 ms 24312 KB Output is correct
87 Correct 146 ms 38624 KB Output is correct
88 Correct 20 ms 23928 KB Output is correct
89 Correct 164 ms 38648 KB Output is correct
90 Correct 156 ms 39804 KB Output is correct
91 Correct 141 ms 41336 KB Output is correct
92 Correct 110 ms 38640 KB Output is correct
93 Correct 21 ms 24312 KB Output is correct
94 Correct 19 ms 23928 KB Output is correct
95 Correct 117 ms 41456 KB Output is correct
96 Correct 171 ms 44408 KB Output is correct
97 Correct 22 ms 24312 KB Output is correct
98 Correct 19 ms 23924 KB Output is correct
99 Correct 120 ms 41204 KB Output is correct
100 Correct 21 ms 24312 KB Output is correct
101 Correct 164 ms 38904 KB Output is correct
102 Correct 169 ms 43384 KB Output is correct
103 Correct 21 ms 24312 KB Output is correct
104 Correct 20 ms 24440 KB Output is correct
105 Correct 22 ms 24312 KB Output is correct
106 Correct 20 ms 24312 KB Output is correct
107 Correct 21 ms 24312 KB Output is correct
108 Correct 797 ms 107796 KB Output is correct
109 Correct 1648 ms 109232 KB Output is correct
110 Correct 1731 ms 118036 KB Output is correct
111 Correct 1252 ms 130956 KB Output is correct
112 Correct 1222 ms 123128 KB Output is correct
113 Correct 683 ms 113244 KB Output is correct
114 Correct 945 ms 94740 KB Output is correct
115 Correct 975 ms 94696 KB Output is correct
116 Correct 1142 ms 101112 KB Output is correct
117 Correct 940 ms 112760 KB Output is correct
118 Correct 946 ms 96720 KB Output is correct
119 Correct 947 ms 112632 KB Output is correct
120 Correct 1187 ms 123768 KB Output is correct
121 Correct 932 ms 112560 KB Output is correct
122 Correct 1069 ms 106360 KB Output is correct
123 Correct 659 ms 112192 KB Output is correct
124 Correct 1166 ms 110584 KB Output is correct