답안 #376730

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
376730 2021-03-12T02:49:32 Z wiwiho Mergers (JOI19_mergers) C++14
48 / 100
3000 ms 23908 KB
#include<bits/stdc++.h>

#define mp make_pair
#define F first
#define S second
#define iter(a) a.begin(), a.end()
#define lsort(a) sort(iter(a))
#define printv(a, b) { \
    for(auto pv : a) b << pv << " "; \
    b << "\n"; \
}
#define eb emplace_back

using namespace std;

typedef long long ll;

using pll = pair<ll, ll>;

ostream& operator<<(ostream& o, pll p){
    return o << '(' << p.F << ',' << p.S << ')';
}

void waassert(bool t){
    if(!t){
        cout << "OAO\n";
        exit(0);
    }
}


struct DSU{
    vector<int> dsu;
    int cnt;
    void init(int n){
        dsu.resize(n + 1);
        cnt = n;
        for(int i = 1; i <= n; i++) dsu[i] = i;
    }
    int findDSU(int a){
        if(dsu[a] != a) dsu[a] = findDSU(dsu[a]);
        return dsu[a];
    }
    void unionDSU(int a, int b){
        a = findDSU(a);
        b = findDSU(b);
        if(a == b) return;
        cnt--;
        dsu[b] = a;
    }
};

int n;
vector<vector<int>> g;
vector<int> s;
vector<vector<int>> anc;
vector<int> in, out;
DSU dsu;
int ts = 0;

void dfs1(int now, int p){
    anc[0][now] = p;
    in[now] = ts++;
    for(int i : g[now]){
        if(i == p) continue;
        dfs1(i, now);
    }
    out[now] = ts++;
}

void buildLCA(){
    for(int i = 1; i < 20; i++){
        for(int j = 1; j <= n; j++){
            anc[i][j] = anc[i - 1][anc[i - 1][j]];
        }
    }
}

bool isAnc(int a, int b){
    return in[a] <= in[b] && out[a] >= out[b];
}

int getLCA(int a, int b){
    if(isAnc(a, b)) return a;
    for(int i = 19; i >= 0; i--){
        if(!isAnc(anc[i][a], b)) a = anc[i][a];
    }
    return anc[0][a];
}

int unionPath(int a, int b){
    int lca = getLCA(a, b);
    while(!isAnc(a, lca)){
        dsu.unionDSU(anc[0][a], a);
        a = dsu.findDSU(a);
    }
    while(!isAnc(b, lca)){
        dsu.unionDSU(anc[0][b], b);
        b = dsu.findDSU(b);
    }
    return dsu.findDSU(lca);
}

int fv = -1, fd = -1;

void dfs2(int now, int p, int dpt){
    if(dpt > fd) fv = now, fd = dpt;
    //cerr << "dfs2 " << now << " " << p << " " << dpt << "\n";
    for(int i : g[now]){
        if(i == p) continue;
        if(dsu.findDSU(now) != dsu.findDSU(i)) dfs2(i, now, dpt + 1);
        else dfs2(i, now, dpt);
    }
}

void owo(){
    //cerr << "owo\n";
    fv = -1; fd = -1;
    dfs2(1, 1, 0);
    int t1 = fv;
    fv = -1; fd = -1;
    dfs2(t1, t1, 0);
    int t2 = fv;
    //cerr << t1 << " " << t2 << "\n";
    unionPath(t1, t2);
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int k;
    cin >> n >> k;
    g.resize(n + 1);
    s.resize(n + 1);
    anc.resize(20, vector<int>(n + 1));
    in.resize(n + 1);
    out.resize(n + 1);
    dsu.init(n);
    vector<vector<int>> city(k + 1);

    for(int i = 0; i < n - 1; i++){
        int u, v;
        cin >> u >> v;
        g[u].eb(v);
        g[v].eb(u);
    }
    for(int i = 1; i <= n; i++){
        cin >> s[i];
        city[s[i]].eb(i);
    }

    dfs1(1, 1);
    buildLCA();


    for(int i = 1; i <= k; i++){
        int v = city[i].front();
        for(int j = 1; j < city[i].size(); j++){
            v = unionPath(v, city[i][j]);
        }
    }

    int ans = 0;
    while(dsu.cnt > 1){
        owo();
        ans++;
    }
    cout << ans << "\n";


    return 0;
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:159:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  159 |         for(int j = 1; j < city[i].size(); j++){
      |                        ~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 492 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 54 ms 876 KB Output is correct
27 Correct 5 ms 876 KB Output is correct
28 Correct 123 ms 1336 KB Output is correct
29 Correct 118 ms 1132 KB Output is correct
30 Correct 3 ms 876 KB Output is correct
31 Correct 1 ms 364 KB Output is correct
32 Correct 3 ms 1516 KB Output is correct
33 Correct 1 ms 364 KB Output is correct
34 Correct 2 ms 876 KB Output is correct
35 Correct 119 ms 1132 KB Output is correct
36 Correct 5 ms 876 KB Output is correct
37 Correct 31 ms 976 KB Output is correct
38 Correct 1 ms 364 KB Output is correct
39 Correct 23 ms 876 KB Output is correct
40 Correct 2 ms 876 KB Output is correct
41 Correct 3 ms 916 KB Output is correct
42 Correct 32 ms 876 KB Output is correct
43 Correct 2 ms 1004 KB Output is correct
44 Correct 1 ms 364 KB Output is correct
45 Correct 6 ms 1132 KB Output is correct
46 Correct 25 ms 896 KB Output is correct
47 Correct 1 ms 364 KB Output is correct
48 Correct 16 ms 876 KB Output is correct
49 Correct 114 ms 1004 KB Output is correct
50 Correct 119 ms 1260 KB Output is correct
51 Correct 16 ms 896 KB Output is correct
52 Correct 2 ms 876 KB Output is correct
53 Correct 27 ms 876 KB Output is correct
54 Correct 2 ms 876 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 492 KB Output is correct
25 Correct 1 ms 492 KB Output is correct
26 Correct 56 ms 17696 KB Output is correct
27 Correct 107 ms 17528 KB Output is correct
28 Correct 7 ms 876 KB Output is correct
29 Correct 1 ms 392 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 310 ms 17252 KB Output is correct
32 Correct 2 ms 876 KB Output is correct
33 Correct 126 ms 21732 KB Output is correct
34 Correct 99 ms 17380 KB Output is correct
35 Correct 5 ms 876 KB Output is correct
36 Correct 559 ms 19300 KB Output is correct
37 Correct 2 ms 876 KB Output is correct
38 Correct 3 ms 876 KB Output is correct
39 Correct 58 ms 17624 KB Output is correct
40 Correct 2 ms 1004 KB Output is correct
41 Correct 79 ms 17636 KB Output is correct
42 Correct 128 ms 23908 KB Output is correct
43 Correct 1 ms 364 KB Output is correct
44 Correct 99 ms 22116 KB Output is correct
45 Correct 106 ms 19684 KB Output is correct
46 Correct 2 ms 876 KB Output is correct
47 Correct 2 ms 876 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 60 ms 16228 KB Output is correct
2 Execution timed out 3087 ms 21092 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 492 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 54 ms 876 KB Output is correct
27 Correct 5 ms 876 KB Output is correct
28 Correct 123 ms 1336 KB Output is correct
29 Correct 118 ms 1132 KB Output is correct
30 Correct 3 ms 876 KB Output is correct
31 Correct 1 ms 364 KB Output is correct
32 Correct 3 ms 1516 KB Output is correct
33 Correct 1 ms 364 KB Output is correct
34 Correct 2 ms 876 KB Output is correct
35 Correct 119 ms 1132 KB Output is correct
36 Correct 5 ms 876 KB Output is correct
37 Correct 31 ms 976 KB Output is correct
38 Correct 1 ms 364 KB Output is correct
39 Correct 23 ms 876 KB Output is correct
40 Correct 2 ms 876 KB Output is correct
41 Correct 3 ms 916 KB Output is correct
42 Correct 32 ms 876 KB Output is correct
43 Correct 2 ms 1004 KB Output is correct
44 Correct 1 ms 364 KB Output is correct
45 Correct 6 ms 1132 KB Output is correct
46 Correct 25 ms 896 KB Output is correct
47 Correct 1 ms 364 KB Output is correct
48 Correct 16 ms 876 KB Output is correct
49 Correct 114 ms 1004 KB Output is correct
50 Correct 119 ms 1260 KB Output is correct
51 Correct 16 ms 896 KB Output is correct
52 Correct 2 ms 876 KB Output is correct
53 Correct 27 ms 876 KB Output is correct
54 Correct 2 ms 876 KB Output is correct
55 Correct 1 ms 492 KB Output is correct
56 Correct 56 ms 17696 KB Output is correct
57 Correct 107 ms 17528 KB Output is correct
58 Correct 7 ms 876 KB Output is correct
59 Correct 1 ms 392 KB Output is correct
60 Correct 1 ms 364 KB Output is correct
61 Correct 310 ms 17252 KB Output is correct
62 Correct 2 ms 876 KB Output is correct
63 Correct 126 ms 21732 KB Output is correct
64 Correct 99 ms 17380 KB Output is correct
65 Correct 5 ms 876 KB Output is correct
66 Correct 559 ms 19300 KB Output is correct
67 Correct 2 ms 876 KB Output is correct
68 Correct 3 ms 876 KB Output is correct
69 Correct 58 ms 17624 KB Output is correct
70 Correct 2 ms 1004 KB Output is correct
71 Correct 79 ms 17636 KB Output is correct
72 Correct 128 ms 23908 KB Output is correct
73 Correct 1 ms 364 KB Output is correct
74 Correct 99 ms 22116 KB Output is correct
75 Correct 106 ms 19684 KB Output is correct
76 Correct 2 ms 876 KB Output is correct
77 Correct 2 ms 876 KB Output is correct
78 Correct 60 ms 16228 KB Output is correct
79 Execution timed out 3087 ms 21092 KB Time limit exceeded
80 Halted 0 ms 0 KB -