답안 #429333

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
429333 2021-06-15T20:51:16 Z keko37 Inside information (BOI21_servers) C++14
100 / 100
2224 ms 58260 KB
#include<bits/stdc++.h>

using namespace std;

typedef long long llint;
typedef pair <int, int> pi;

const int MAXN = 250005;

int n, k, cnt;
char tip[MAXN];
int qa[MAXN], qb[MAXN], sol[MAXN];
int server_node[MAXN];
int lo[MAXN], hi[MAXN], hi2[MAXN];
vector <pi> v[MAXN];
vector <int> q[MAXN];

void add_edge (int a, int b) {
    v[a].push_back({b, 0});
    v[b].push_back({a, 1});
}

void preseli (int i, int ind, int a, int b) {
    if (hi[a] == n + k) hi[a] = i; else hi2[a] = i;
    server_node[ind] = b;
}

int cen[MAXN], siz[MAXN], par[MAXN], upp[MAXN], dwn[MAXN], col[MAXN];
int fen[MAXN];

void ubaci (int x, int k) {
    for (x++; x < MAXN; x += x&-x) fen[x] += k;
}

int upit (int x) {
    int res = 0;
    for (x++; x > 0; x -= x&-x) res += fen[x];
    return res;
}

void upd (int node, int flg) {
    ubaci(lo[node], flg * 2);
    ubaci(hi[node], -flg);
    ubaci(hi2[node], -flg);
}

void dfs (int x, int rod) {
    if (rod == 0) upp[x] = dwn[x] = 0;
    par[x] = rod;
    siz[x] = 1;
    for (auto pp : v[x]) {
        int sus = pp.first, w = pp.second;
        if (sus == rod || cen[sus]) continue;
        dwn[sus] = w + dwn[x];
        upp[sus] = (1 - w) + upp[x];
        dfs(sus, x);
        siz[x] += siz[sus];
    }
}

int nadi_centroid (int x, int root) {
    int mx = 0, ind = -1;
    for (auto pp : v[x]) {
        int sus = pp.first, w = pp.second;
        if (sus == par[x] || cen[sus]) continue;
        if (siz[sus] > mx) {
            mx = siz[sus];
            ind = sus;
        }
    }
    if (mx * 2 <= siz[root]) return x;
    return nadi_centroid(ind, root);
}

void oboji (int x, int pred, int flg) {
    if (flg != 0 && dwn[x] == 0 && x > n) upd(x, flg);
    col[x] = pred;
    for (auto pp : v[x]) {
        int sus = pp.first, w = pp.second;
        if (sus == par[x] || cen[sus]) continue;
        oboji(sus, pred, flg);
    }
}

void decompose (int x) {
    dfs(x, 0);
    int root = nadi_centroid(x, x);
    dfs(root, 0);
    cen[root] = 1;
    swap(q[root], q[x]);
    //cout << "root je " << root << endl;

    col[root] = root;
    if (root > n) upd(root, 1);
    for (auto pp : v[root]) {
        int sus = pp.first, w = pp.second;
        if (cen[sus]) continue;
        oboji(sus, sus, 1);
    }

    for (auto i : q[root]) {
        //cout << "upit " << i << endl;
        if (tip[i] == 'Q') {
            if (col[qa[i]] != col[qb[i]]) {
                sol[i] = upp[qb[i]] + dwn[qa[i]];
            } else {
                //cout << "premjesti " << col[qa[i]] << " " << i << endl;
                q[col[qa[i]]].push_back(i);
            }
        } else {
            //cout << "premjesti " << col[qa[i]] << " " << i << endl;
            if (qa[i] != root) q[col[qa[i]]].push_back(i);
        }
    }

    for (auto pp : v[root]) {
        int sus = pp.first, w = pp.second;
        if (cen[sus]) continue;
        oboji(sus, sus, -1);
        for (auto i : q[sus]) {
            if (tip[i] == 'C' && upp[qa[i]] == 0) {
                int val = upit(i);
                //cout << "add " << sus << " " << i << " " << val << endl;
                sol[i] += val;
            }
        }
        oboji(sus, sus, 1);
    }

    if (root > n) upd(root, -1);
    for (auto pp : v[root]) {
        int sus = pp.first, w = pp.second;
        if (cen[sus]) continue;
        oboji(sus, sus, -1);
    }

    //return;
    for (auto pp : v[root]) {
        int sus = pp.first, w = pp.second;
        if (cen[sus]) continue;
        decompose(sus);
    }
}

int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> k;
    cnt = n;
    for (int i = 1; i <= n; i++) {
        server_node[i] = i;
        lo[i] = 0;
        hi[i] = n + k;
        hi2[i] = n + k;
    }
    for (int i = 1; i <= n + k - 1; i++) {
        cin >> tip[i];
        if (tip[i] == 'S') {
            cin >> qa[i] >> qb[i];
            cnt++;
            lo[cnt] = i;
            hi[cnt] = n + k;
            hi2[cnt] = n + k;
            add_edge(server_node[qa[i]], cnt);
            add_edge(server_node[qb[i]], cnt);
            preseli(i, qa[i], server_node[qa[i]], cnt);
            preseli(i, qb[i], server_node[qb[i]], cnt);
        } else if (tip[i] == 'Q') {
            cin >> qa[i] >> qb[i];
            qa[i] = server_node[qa[i]];
            q[1].push_back(i);
        } else {
            cin >> qa[i];
            if (hi[qa[i]] == n + k) sol[i] = 1;
            q[1].push_back(i);
        }
    }
    decompose(1);
    for (int i = 1; i <= n + k - 1; i++) {
        if (tip[i] == 'Q') {
            if (sol[i] == 0) cout << "yes\n"; else cout << "no\n";
        } else if (tip[i] == 'C') {
            cout << sol[i] << '\n';
        }
    }
    return 0;
}

Compilation message

servers.cpp: In function 'int nadi_centroid(int, int)':
servers.cpp:64:29: warning: unused variable 'w' [-Wunused-variable]
   64 |         int sus = pp.first, w = pp.second;
      |                             ^
servers.cpp: In function 'void oboji(int, int, int)':
servers.cpp:79:29: warning: unused variable 'w' [-Wunused-variable]
   79 |         int sus = pp.first, w = pp.second;
      |                             ^
servers.cpp: In function 'void decompose(int)':
servers.cpp:96:29: warning: unused variable 'w' [-Wunused-variable]
   96 |         int sus = pp.first, w = pp.second;
      |                             ^
servers.cpp:117:29: warning: unused variable 'w' [-Wunused-variable]
  117 |         int sus = pp.first, w = pp.second;
      |                             ^
servers.cpp:132:29: warning: unused variable 'w' [-Wunused-variable]
  132 |         int sus = pp.first, w = pp.second;
      |                             ^
servers.cpp:139:29: warning: unused variable 'w' [-Wunused-variable]
  139 |         int sus = pp.first, w = pp.second;
      |                             ^
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 15692 KB Output is correct
2 Correct 67 ms 16432 KB Output is correct
3 Correct 63 ms 18376 KB Output is correct
4 Correct 58 ms 16572 KB Output is correct
5 Correct 69 ms 16616 KB Output is correct
6 Correct 61 ms 16976 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 15692 KB Output is correct
2 Correct 67 ms 16432 KB Output is correct
3 Correct 63 ms 18376 KB Output is correct
4 Correct 58 ms 16572 KB Output is correct
5 Correct 69 ms 16616 KB Output is correct
6 Correct 61 ms 16976 KB Output is correct
7 Correct 36 ms 15656 KB Output is correct
8 Correct 78 ms 18984 KB Output is correct
9 Correct 97 ms 20956 KB Output is correct
10 Correct 77 ms 20148 KB Output is correct
11 Correct 95 ms 20084 KB Output is correct
12 Correct 86 ms 20228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 15692 KB Output is correct
2 Correct 1002 ms 46364 KB Output is correct
3 Correct 1026 ms 46360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 15692 KB Output is correct
2 Correct 1002 ms 46364 KB Output is correct
3 Correct 1026 ms 46360 KB Output is correct
4 Correct 36 ms 15812 KB Output is correct
5 Correct 1112 ms 50544 KB Output is correct
6 Correct 1286 ms 54304 KB Output is correct
7 Correct 1190 ms 54848 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 15620 KB Output is correct
2 Correct 2158 ms 53172 KB Output is correct
3 Correct 1996 ms 53092 KB Output is correct
4 Correct 589 ms 46532 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 15620 KB Output is correct
2 Correct 2158 ms 53172 KB Output is correct
3 Correct 1996 ms 53092 KB Output is correct
4 Correct 589 ms 46532 KB Output is correct
5 Correct 47 ms 15788 KB Output is correct
6 Correct 1936 ms 57604 KB Output is correct
7 Correct 719 ms 55868 KB Output is correct
8 Correct 1992 ms 58228 KB Output is correct
9 Correct 2092 ms 58224 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 15556 KB Output is correct
2 Correct 681 ms 38400 KB Output is correct
3 Correct 1509 ms 43276 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 15556 KB Output is correct
2 Correct 681 ms 38400 KB Output is correct
3 Correct 1509 ms 43276 KB Output is correct
4 Correct 36 ms 15616 KB Output is correct
5 Correct 736 ms 44068 KB Output is correct
6 Correct 1424 ms 47720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 15716 KB Output is correct
2 Correct 1850 ms 53160 KB Output is correct
3 Correct 1978 ms 53248 KB Output is correct
4 Correct 618 ms 46496 KB Output is correct
5 Correct 41 ms 15572 KB Output is correct
6 Correct 663 ms 38228 KB Output is correct
7 Correct 1418 ms 43016 KB Output is correct
8 Correct 1699 ms 36248 KB Output is correct
9 Correct 1833 ms 42116 KB Output is correct
10 Correct 1539 ms 47780 KB Output is correct
11 Correct 1565 ms 42528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 15716 KB Output is correct
2 Correct 1850 ms 53160 KB Output is correct
3 Correct 1978 ms 53248 KB Output is correct
4 Correct 618 ms 46496 KB Output is correct
5 Correct 41 ms 15572 KB Output is correct
6 Correct 663 ms 38228 KB Output is correct
7 Correct 1418 ms 43016 KB Output is correct
8 Correct 1699 ms 36248 KB Output is correct
9 Correct 1833 ms 42116 KB Output is correct
10 Correct 1539 ms 47780 KB Output is correct
11 Correct 1565 ms 42528 KB Output is correct
12 Correct 44 ms 15684 KB Output is correct
13 Correct 2100 ms 57612 KB Output is correct
14 Correct 685 ms 55892 KB Output is correct
15 Correct 1988 ms 58156 KB Output is correct
16 Correct 2176 ms 58192 KB Output is correct
17 Correct 41 ms 16132 KB Output is correct
18 Correct 813 ms 46560 KB Output is correct
19 Correct 1339 ms 47804 KB Output is correct
20 Correct 1945 ms 44524 KB Output is correct
21 Correct 1954 ms 47416 KB Output is correct
22 Correct 1623 ms 52148 KB Output is correct
23 Correct 2012 ms 52244 KB Output is correct
24 Correct 1868 ms 50944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 15332 KB Output is correct
2 Correct 54 ms 16276 KB Output is correct
3 Correct 83 ms 18212 KB Output is correct
4 Correct 61 ms 16480 KB Output is correct
5 Correct 59 ms 16484 KB Output is correct
6 Correct 60 ms 16760 KB Output is correct
7 Correct 42 ms 15632 KB Output is correct
8 Correct 1136 ms 46176 KB Output is correct
9 Correct 1027 ms 46108 KB Output is correct
10 Correct 37 ms 15556 KB Output is correct
11 Correct 1966 ms 52964 KB Output is correct
12 Correct 1978 ms 52968 KB Output is correct
13 Correct 606 ms 46312 KB Output is correct
14 Correct 36 ms 15428 KB Output is correct
15 Correct 662 ms 38296 KB Output is correct
16 Correct 1467 ms 43040 KB Output is correct
17 Correct 1795 ms 36152 KB Output is correct
18 Correct 1829 ms 41932 KB Output is correct
19 Correct 1516 ms 47804 KB Output is correct
20 Correct 1434 ms 42548 KB Output is correct
21 Correct 1040 ms 39692 KB Output is correct
22 Correct 1338 ms 40552 KB Output is correct
23 Correct 2054 ms 39776 KB Output is correct
24 Correct 1935 ms 39812 KB Output is correct
25 Correct 1850 ms 47424 KB Output is correct
26 Correct 1600 ms 41804 KB Output is correct
27 Correct 1873 ms 41604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 15332 KB Output is correct
2 Correct 54 ms 16276 KB Output is correct
3 Correct 83 ms 18212 KB Output is correct
4 Correct 61 ms 16480 KB Output is correct
5 Correct 59 ms 16484 KB Output is correct
6 Correct 60 ms 16760 KB Output is correct
7 Correct 42 ms 15632 KB Output is correct
8 Correct 1136 ms 46176 KB Output is correct
9 Correct 1027 ms 46108 KB Output is correct
10 Correct 37 ms 15556 KB Output is correct
11 Correct 1966 ms 52964 KB Output is correct
12 Correct 1978 ms 52968 KB Output is correct
13 Correct 606 ms 46312 KB Output is correct
14 Correct 36 ms 15428 KB Output is correct
15 Correct 662 ms 38296 KB Output is correct
16 Correct 1467 ms 43040 KB Output is correct
17 Correct 1795 ms 36152 KB Output is correct
18 Correct 1829 ms 41932 KB Output is correct
19 Correct 1516 ms 47804 KB Output is correct
20 Correct 1434 ms 42548 KB Output is correct
21 Correct 1040 ms 39692 KB Output is correct
22 Correct 1338 ms 40552 KB Output is correct
23 Correct 2054 ms 39776 KB Output is correct
24 Correct 1935 ms 39812 KB Output is correct
25 Correct 1850 ms 47424 KB Output is correct
26 Correct 1600 ms 41804 KB Output is correct
27 Correct 1873 ms 41604 KB Output is correct
28 Correct 39 ms 15632 KB Output is correct
29 Correct 81 ms 18916 KB Output is correct
30 Correct 99 ms 21040 KB Output is correct
31 Correct 80 ms 20172 KB Output is correct
32 Correct 85 ms 20040 KB Output is correct
33 Correct 87 ms 20288 KB Output is correct
34 Correct 41 ms 16304 KB Output is correct
35 Correct 1397 ms 50460 KB Output is correct
36 Correct 1210 ms 54364 KB Output is correct
37 Correct 1058 ms 55076 KB Output is correct
38 Correct 38 ms 16324 KB Output is correct
39 Correct 2132 ms 57468 KB Output is correct
40 Correct 674 ms 55916 KB Output is correct
41 Correct 2194 ms 58208 KB Output is correct
42 Correct 2151 ms 58260 KB Output is correct
43 Correct 37 ms 16068 KB Output is correct
44 Correct 749 ms 46600 KB Output is correct
45 Correct 1501 ms 47768 KB Output is correct
46 Correct 2009 ms 44568 KB Output is correct
47 Correct 2043 ms 47316 KB Output is correct
48 Correct 1679 ms 52140 KB Output is correct
49 Correct 2069 ms 52292 KB Output is correct
50 Correct 1864 ms 51180 KB Output is correct
51 Correct 1372 ms 52556 KB Output is correct
52 Correct 1157 ms 51648 KB Output is correct
53 Correct 1228 ms 51832 KB Output is correct
54 Correct 1376 ms 51580 KB Output is correct
55 Correct 1451 ms 52820 KB Output is correct
56 Correct 2224 ms 43632 KB Output is correct
57 Correct 2193 ms 52216 KB Output is correct
58 Correct 1817 ms 47664 KB Output is correct
59 Correct 1690 ms 45804 KB Output is correct