답안 #376786

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
376786 2021-03-12T03:57:11 Z wiwiho Mergers (JOI19_mergers) C++14
70 / 100
3000 ms 196396 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>;
using pii = pair<int, int>;

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

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

struct Info{
    pii mn, mx, al, lb, alb;
};

Info pull(Info a, Info b){
    Info c;
    c.mn = min(a.mn, b.mn);
    c.mx = max(a.mx, b.mx);
    c.al = max({a.al, b.al, mp(a.mx.F - 2 * b.mn.F, b.mn.S)});
    c.lb = max({a.lb, b.lb, mp(- 2 * a.mn.F + b.mx.F, a.mn.S)});
    c.alb = max({a.alb, b.alb, mp(a.al.F + b.mx.F, a.al.S), mp(a.mx.F + b.lb.F, b.lb.S)});
    return c;
}

ostream& operator<<(ostream& o, Info info){
    return o << '(' << info.mn << ',' << info.mx << ',' << info.al << ',' << info.lb << ',' << info.alb << ')';
}

struct Node{
    int l = -1, r = -1;
    int tag = 0;
    Info info;
    Info rv(){
        Info t = info;
        t.mn.F += tag;
        t.mx.F += tag;
        t.al.F -= tag;
        t.lb.F -= tag;
        return t;
    }
};

struct SegmentTree{
    vector<Node> st;
    int ts = 0;
    int build(int l, int r){
        int id = ts++;
        st[id].info.mn.S = st[id].info.mx.S = l;
        st[id].info.al.S = l;
        st[id].info.lb.S = l;
        st[id].info.alb.S = l;
        if(l == r) return id;
        int m = (l + r) / 2;
        st[id].l = build(l, m);
        st[id].r = build(m + 1, r);
        return id;
    }
    void push(int id){
        st[st[id].l].tag += st[id].tag;
        st[st[id].r].tag += st[id].tag;
        st[id].info = st[id].rv();
        st[id].tag = 0;
    }
    void modify(int l, int r, int v, int L, int R, int id){
        if(l == L && r == R){
            st[id].tag += v;
            return;
        }
        int M = (L + R) / 2;
        if(r <= M) modify(l, r, v, L, M, st[id].l);
        else if(l > M) modify(l, r, v, M + 1, R, st[id].r);
        else{
            modify(l, M, v, L, M, st[id].l);
            modify(M + 1, r, v, M + 1, R, st[id].r);
        }
        st[id].info = pull(st[st[id].l].rv(), st[st[id].r].rv());
    }
    pii query(int l, int r, int L, int R, int id){
        if(l == L && r == R) return st[id].rv().mx;
        push(id);
        int M = (L + R) / 2;
        if(r <= M) return query(l, r, L, M, st[id].l);
        else if(l > M) return query(l, r, M + 1, R, st[id].r);
        else return max(query(l, M, L, M, st[id].l), query(M + 1, r, M + 1, R, st[id].r));
    }
    void print(int l, int r, int id){
        cerr << "print " << l << " " << r << " " << id << " " << st[id].l << " " << st[id].r << " " << st[id].info << "\n";
        if(l == r) return;
        push(id);
        int m = (l + r) / 2;
        print(l, m, st[id].l);
        print(m + 1, r, st[id].r);
    }
};

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;
vector<int> et(1);
DSU dsu;
int ts = 0;
SegmentTree st;

void dfs1(int now, int p){
    anc[0][now] = p;
    in[now] = ++ts;
    et.eb(now);
    for(int i : g[now]){
        if(i == p) continue;
        dfs1(i, now);
        ts++;
        et.eb(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);
    a = dsu.findDSU(a);
    b = dsu.findDSU(b);
    while(!isAnc(a, lca)){
        st.modify(in[a], out[a], -1, 1, 2 * n - 1, 0);
        dsu.unionDSU(anc[0][a], a);
        a = dsu.findDSU(a);
    }
    while(!isAnc(b, lca)){
        st.modify(in[b], out[b], -1, 1, 2 * n - 1, 0);
        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";
    int mid = st.st[0].info.alb.S;
    int t1 = et[st.query(1, mid, 1, 2 * n - 1, 0).S];
    int t2 = et[st.query(mid, 2 * n - 1, 1, 2 * n - 1, 0).S];
    //cerr << mid << " " << 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);
    st.st.resize(4 * n);

    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);
    //printv(et, cerr);
    buildLCA();
    st.build(1, 2 * n - 1);
    for(int i = 1; i <= n; i++){
        st.modify(in[i], out[i], 1, 1, 2 * n - 1, 0);
    }

    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]);
        }
    }
    /*for(int i = 1; i < 2 * n; i++){
        cerr << i << " " << st.query(i, i, 1, 2 * n - 1, 0) << "\n";
    }
    st.print(1, 2 * n - 1, 0);*/

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


    return 0;
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:255:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  255 |         for(int j = 1; j < city[i].size(); j++){
      |                        ~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 512 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 384 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 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 512 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 384 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 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 11 ms 1644 KB Output is correct
27 Correct 11 ms 1516 KB Output is correct
28 Correct 11 ms 1772 KB Output is correct
29 Correct 12 ms 1644 KB Output is correct
30 Correct 10 ms 1536 KB Output is correct
31 Correct 1 ms 376 KB Output is correct
32 Correct 12 ms 1772 KB Output is correct
33 Correct 1 ms 364 KB Output is correct
34 Correct 8 ms 1644 KB Output is correct
35 Correct 10 ms 1644 KB Output is correct
36 Correct 10 ms 1516 KB Output is correct
37 Correct 13 ms 1516 KB Output is correct
38 Correct 1 ms 364 KB Output is correct
39 Correct 10 ms 1516 KB Output is correct
40 Correct 10 ms 1516 KB Output is correct
41 Correct 9 ms 1516 KB Output is correct
42 Correct 9 ms 1516 KB Output is correct
43 Correct 12 ms 1644 KB Output is correct
44 Correct 1 ms 384 KB Output is correct
45 Correct 12 ms 1644 KB Output is correct
46 Correct 11 ms 1516 KB Output is correct
47 Correct 1 ms 364 KB Output is correct
48 Correct 10 ms 1516 KB Output is correct
49 Correct 9 ms 1644 KB Output is correct
50 Correct 11 ms 1644 KB Output is correct
51 Correct 11 ms 1516 KB Output is correct
52 Correct 8 ms 1516 KB Output is correct
53 Correct 8 ms 1516 KB Output is correct
54 Correct 10 ms 1516 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 512 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 384 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 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 447 ms 38244 KB Output is correct
27 Correct 538 ms 38076 KB Output is correct
28 Correct 13 ms 1516 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 467 ms 37988 KB Output is correct
32 Correct 8 ms 1516 KB Output is correct
33 Correct 660 ms 43700 KB Output is correct
34 Correct 531 ms 38116 KB Output is correct
35 Correct 10 ms 1516 KB Output is correct
36 Correct 598 ms 38628 KB Output is correct
37 Correct 9 ms 1516 KB Output is correct
38 Correct 9 ms 1516 KB Output is correct
39 Correct 442 ms 38372 KB Output is correct
40 Correct 12 ms 1644 KB Output is correct
41 Correct 502 ms 38108 KB Output is correct
42 Correct 654 ms 39908 KB Output is correct
43 Correct 1 ms 364 KB Output is correct
44 Correct 672 ms 43876 KB Output is correct
45 Correct 584 ms 41060 KB Output is correct
46 Correct 8 ms 1516 KB Output is correct
47 Correct 12 ms 1516 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 428 ms 38244 KB Output is correct
2 Correct 508 ms 43236 KB Output is correct
3 Correct 11 ms 1644 KB Output is correct
4 Correct 11 ms 1516 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 8 ms 1516 KB Output is correct
8 Correct 623 ms 40292 KB Output is correct
9 Correct 10 ms 1516 KB Output is correct
10 Correct 487 ms 38116 KB Output is correct
11 Correct 1 ms 492 KB Output is correct
12 Correct 563 ms 38104 KB Output is correct
13 Correct 636 ms 40164 KB Output is correct
14 Correct 596 ms 42980 KB Output is correct
15 Correct 416 ms 38264 KB Output is correct
16 Correct 9 ms 1644 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 511 ms 42756 KB Output is correct
19 Correct 660 ms 45924 KB Output is correct
20 Correct 10 ms 1516 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 515 ms 40548 KB Output is correct
23 Correct 10 ms 1516 KB Output is correct
24 Correct 567 ms 38628 KB Output is correct
25 Correct 608 ms 45028 KB Output is correct
26 Correct 9 ms 1644 KB Output is correct
27 Correct 11 ms 1772 KB Output is correct
28 Correct 11 ms 1516 KB Output is correct
29 Correct 8 ms 1516 KB Output is correct
30 Correct 8 ms 1644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 512 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 384 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 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 11 ms 1644 KB Output is correct
27 Correct 11 ms 1516 KB Output is correct
28 Correct 11 ms 1772 KB Output is correct
29 Correct 12 ms 1644 KB Output is correct
30 Correct 10 ms 1536 KB Output is correct
31 Correct 1 ms 376 KB Output is correct
32 Correct 12 ms 1772 KB Output is correct
33 Correct 1 ms 364 KB Output is correct
34 Correct 8 ms 1644 KB Output is correct
35 Correct 10 ms 1644 KB Output is correct
36 Correct 10 ms 1516 KB Output is correct
37 Correct 13 ms 1516 KB Output is correct
38 Correct 1 ms 364 KB Output is correct
39 Correct 10 ms 1516 KB Output is correct
40 Correct 10 ms 1516 KB Output is correct
41 Correct 9 ms 1516 KB Output is correct
42 Correct 9 ms 1516 KB Output is correct
43 Correct 12 ms 1644 KB Output is correct
44 Correct 1 ms 384 KB Output is correct
45 Correct 12 ms 1644 KB Output is correct
46 Correct 11 ms 1516 KB Output is correct
47 Correct 1 ms 364 KB Output is correct
48 Correct 10 ms 1516 KB Output is correct
49 Correct 9 ms 1644 KB Output is correct
50 Correct 11 ms 1644 KB Output is correct
51 Correct 11 ms 1516 KB Output is correct
52 Correct 8 ms 1516 KB Output is correct
53 Correct 8 ms 1516 KB Output is correct
54 Correct 10 ms 1516 KB Output is correct
55 Correct 1 ms 364 KB Output is correct
56 Correct 447 ms 38244 KB Output is correct
57 Correct 538 ms 38076 KB Output is correct
58 Correct 13 ms 1516 KB Output is correct
59 Correct 1 ms 364 KB Output is correct
60 Correct 1 ms 364 KB Output is correct
61 Correct 467 ms 37988 KB Output is correct
62 Correct 8 ms 1516 KB Output is correct
63 Correct 660 ms 43700 KB Output is correct
64 Correct 531 ms 38116 KB Output is correct
65 Correct 10 ms 1516 KB Output is correct
66 Correct 598 ms 38628 KB Output is correct
67 Correct 9 ms 1516 KB Output is correct
68 Correct 9 ms 1516 KB Output is correct
69 Correct 442 ms 38372 KB Output is correct
70 Correct 12 ms 1644 KB Output is correct
71 Correct 502 ms 38108 KB Output is correct
72 Correct 654 ms 39908 KB Output is correct
73 Correct 1 ms 364 KB Output is correct
74 Correct 672 ms 43876 KB Output is correct
75 Correct 584 ms 41060 KB Output is correct
76 Correct 8 ms 1516 KB Output is correct
77 Correct 12 ms 1516 KB Output is correct
78 Correct 428 ms 38244 KB Output is correct
79 Correct 508 ms 43236 KB Output is correct
80 Correct 11 ms 1644 KB Output is correct
81 Correct 11 ms 1516 KB Output is correct
82 Correct 1 ms 364 KB Output is correct
83 Correct 1 ms 364 KB Output is correct
84 Correct 8 ms 1516 KB Output is correct
85 Correct 623 ms 40292 KB Output is correct
86 Correct 10 ms 1516 KB Output is correct
87 Correct 487 ms 38116 KB Output is correct
88 Correct 1 ms 492 KB Output is correct
89 Correct 563 ms 38104 KB Output is correct
90 Correct 636 ms 40164 KB Output is correct
91 Correct 596 ms 42980 KB Output is correct
92 Correct 416 ms 38264 KB Output is correct
93 Correct 9 ms 1644 KB Output is correct
94 Correct 1 ms 364 KB Output is correct
95 Correct 511 ms 42756 KB Output is correct
96 Correct 660 ms 45924 KB Output is correct
97 Correct 10 ms 1516 KB Output is correct
98 Correct 1 ms 364 KB Output is correct
99 Correct 515 ms 40548 KB Output is correct
100 Correct 10 ms 1516 KB Output is correct
101 Correct 567 ms 38628 KB Output is correct
102 Correct 608 ms 45028 KB Output is correct
103 Correct 9 ms 1644 KB Output is correct
104 Correct 11 ms 1772 KB Output is correct
105 Correct 11 ms 1516 KB Output is correct
106 Correct 8 ms 1516 KB Output is correct
107 Correct 8 ms 1644 KB Output is correct
108 Execution timed out 3089 ms 196396 KB Time limit exceeded
109 Halted 0 ms 0 KB -