답안 #535352

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
535352 2022-03-10T05:39:38 Z 79brue 수도 (JOI20_capital_city) C++14
100 / 100
1036 ms 194920 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int n, k;
int arr[200002], idx[200002];
vector<int> link[200002];
vector<int> vec[200002];

int in[200002], inCnt, sz[200002];
int top[200002], depth[200002], par[200002], LCADB[200002][20];

void dfs_sz(int x, int p=-1){
    sz[x] = 1;
    if(p!=-1) link[x].erase(find(link[x].begin(), link[x].end(), p));
    for(auto &y: link[x]){
        depth[y] = depth[x] + 1;
        par[y] = LCADB[y][0] = x;
        dfs_sz(y, x);
        sz[x] += sz[y];
        if(sz[y] > sz[link[x][0]]) swap(y, link[x][0]);
    }
}

void dfs_hld(int x, int p=-1){
    in[x] = ++inCnt;
    idx[inCnt] = x;
    for(auto y: link[x]){
        top[y] = (y==link[x][0]) ? top[x] : y;
        dfs_hld(y, x);
    }
}

int getLCA(int x, int y){
    if(depth[x] < depth[y]) swap(x, y);
    for(int d=0; d<20; d++) if((depth[x] - depth[y]) & (1<<d)) x = LCADB[x][d];
    if(x==y) return x;
    for(int d=19; d>=0; d--) if(LCADB[x][d] != LCADB[y][d]) x = LCADB[x][d], y = LCADB[y][d];
    return par[x];
}

vector<int> graphLink[1000002];
vector<int> revLink[1000002];
bool visited[1000002];
stack<int> stk;
int ans = 1e9;
int cnt;
int sccNum[1000002];
vector<int> sccLink[1000002];
int weight[1000002];
int deg[1000002];

void sccDfs1(int x){
    visited[x] = 1;
    for(auto y: graphLink[x]){
        if(visited[y]) continue;
        sccDfs1(y);
    }
    stk.push(x);
}

void sccDfs2(int x, int t){
    visited[x] = 1;
    sccNum[x] = t;
    for(auto y: revLink[x]){
        if(visited[y]) continue;
        sccDfs2(y, t);
    }
}

void init(int i, int l, int r){
    if(l==r){
        graphLink[i].push_back(4*n+arr[idx[l]]);
        return;
    }
    int m = (l+r)>>1;
    graphLink[i].push_back(i*2);
    graphLink[i].push_back(i*2+1);
    init(i*2, l, m);
    init(i*2+1, m+1, r);
}

void update(int i, int l, int r, int s, int e, int x){
    if(r<s || e<l) return;
    if(s<=l && r<=e){
        graphLink[4*n+x].push_back(i);
        return;
    }
    int m = (l+r)>>1;
    update(i*2, l, m, s, e, x);
    update(i*2+1, m+1, r, s, e, x);
}

int main(){
    scanf("%d %d", &n, &k);
    for(int i=1; i<n; i++){
        int x, y;
        scanf("%d %d", &x, &y);
        link[x].push_back(y);
        link[y].push_back(x);
    }
    for(int i=1; i<=n; i++){
        scanf("%d", &arr[i]);
        vec[arr[i]].push_back(i);
    }

    dfs_sz(1);
    top[1] = 1; dfs_hld(1);
    for(int d=1; d<20; d++) for(int i=1; i<=n; i++) LCADB[i][d] = LCADB[LCADB[i][d-1]][d-1];
    init(1, 1, n);

    for(int i=1; i<=k; i++){
        sort(vec[i].begin(), vec[i].end(), [&](auto x, auto y){
            return in[x] < in[y];
        });
        for(int j=0; j<(int)vec[i].size() - 1; j++){
            int x = vec[i][j];
            int y = vec[i][j+1];
            int z = getLCA(x, y);

            while(top[x] != top[y]){
                if(depth[top[x]] < depth[top[y]]) swap(x, y);
                update(1, 1, n, in[top[x]], in[x], i);
                x = par[top[x]];
            }
            if(depth[x] > depth[y]) swap(x, y);
            update(1, 1, n, in[x], in[y], i);
        }
    }

//    for(int i=1; i<=5*n; i++){
//        printf("%d: ", i);
//        for(auto x: graphLink[i]) printf("%d ", x);
//        puts("");
//    }

    for(int i=1; i<=5*n; i++){
        if(!visited[i]) sccDfs1(i);
    }
    memset(visited, 0, sizeof(visited));
    for(int i=1; i<=5*n; i++) for(auto x: graphLink[i]) revLink[x].push_back(i);
    while(!stk.empty()){
        if(!visited[stk.top()]) sccDfs2(stk.top(), stk.top());
        stk.pop();
    }

    for(int i=1; i<=5*n; i++){
        for(auto x: graphLink[i]){
            if(sccNum[x] == sccNum[i]) continue;
            sccLink[sccNum[x]].push_back(sccNum[i]);
            deg[sccNum[i]]++;
        }
        if(4*n < i && i <= 4*n+k) weight[sccNum[i]]++;
    }

    queue<int> que;
    for(int i=1; i<=5*n; i++) if(i==sccNum[i] && !deg[i]) que.push(i);

    while(!que.empty()){
        int x = que.front(); que.pop();
        if(weight[x]) ans = min(weight[x]-1, ans);

        for(auto y: sccLink[x]){
            deg[y]--;
            weight[y] += weight[x];
            weight[y] = min(weight[y], 1000000);
            if(!deg[y]) que.push(y);
        }
    }

    printf("%d", ans);
}

Compilation message

capital_city.cpp: In function 'int main()':
capital_city.cpp:121:17: warning: unused variable 'z' [-Wunused-variable]
  121 |             int z = getLCA(x, y);
      |                 ^
capital_city.cpp:97:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   97 |     scanf("%d %d", &n, &k);
      |     ~~~~~^~~~~~~~~~~~~~~~~
capital_city.cpp:100:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  100 |         scanf("%d %d", &x, &y);
      |         ~~~~~^~~~~~~~~~~~~~~~~
capital_city.cpp:105:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  105 |         scanf("%d", &arr[i]);
      |         ~~~~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 81112 KB Output is correct
2 Correct 36 ms 81100 KB Output is correct
3 Correct 36 ms 81104 KB Output is correct
4 Correct 38 ms 81192 KB Output is correct
5 Correct 36 ms 81100 KB Output is correct
6 Correct 39 ms 81096 KB Output is correct
7 Correct 41 ms 81140 KB Output is correct
8 Correct 45 ms 81156 KB Output is correct
9 Correct 36 ms 81112 KB Output is correct
10 Correct 36 ms 81084 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 81112 KB Output is correct
2 Correct 36 ms 81100 KB Output is correct
3 Correct 36 ms 81104 KB Output is correct
4 Correct 38 ms 81192 KB Output is correct
5 Correct 36 ms 81100 KB Output is correct
6 Correct 39 ms 81096 KB Output is correct
7 Correct 41 ms 81140 KB Output is correct
8 Correct 45 ms 81156 KB Output is correct
9 Correct 36 ms 81112 KB Output is correct
10 Correct 36 ms 81084 KB Output is correct
11 Correct 41 ms 81996 KB Output is correct
12 Correct 40 ms 81988 KB Output is correct
13 Correct 42 ms 82028 KB Output is correct
14 Correct 43 ms 81980 KB Output is correct
15 Correct 45 ms 81940 KB Output is correct
16 Correct 39 ms 82076 KB Output is correct
17 Correct 39 ms 82016 KB Output is correct
18 Correct 41 ms 82064 KB Output is correct
19 Correct 39 ms 81976 KB Output is correct
20 Correct 40 ms 81992 KB Output is correct
21 Correct 44 ms 81988 KB Output is correct
22 Correct 39 ms 82192 KB Output is correct
23 Correct 39 ms 82140 KB Output is correct
24 Correct 41 ms 81912 KB Output is correct
25 Correct 44 ms 81996 KB Output is correct
26 Correct 47 ms 81976 KB Output is correct
27 Correct 40 ms 82024 KB Output is correct
28 Correct 39 ms 82028 KB Output is correct
29 Correct 40 ms 82032 KB Output is correct
30 Correct 44 ms 81976 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 809 ms 191460 KB Output is correct
2 Correct 442 ms 194904 KB Output is correct
3 Correct 767 ms 194684 KB Output is correct
4 Correct 449 ms 194920 KB Output is correct
5 Correct 799 ms 190388 KB Output is correct
6 Correct 431 ms 194596 KB Output is correct
7 Correct 717 ms 188192 KB Output is correct
8 Correct 440 ms 189468 KB Output is correct
9 Correct 497 ms 164368 KB Output is correct
10 Correct 530 ms 161672 KB Output is correct
11 Correct 502 ms 164972 KB Output is correct
12 Correct 582 ms 167672 KB Output is correct
13 Correct 563 ms 161952 KB Output is correct
14 Correct 536 ms 167640 KB Output is correct
15 Correct 588 ms 168588 KB Output is correct
16 Correct 508 ms 162580 KB Output is correct
17 Correct 551 ms 163640 KB Output is correct
18 Correct 526 ms 163276 KB Output is correct
19 Correct 590 ms 167756 KB Output is correct
20 Correct 541 ms 168212 KB Output is correct
21 Correct 39 ms 81100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 81112 KB Output is correct
2 Correct 36 ms 81100 KB Output is correct
3 Correct 36 ms 81104 KB Output is correct
4 Correct 38 ms 81192 KB Output is correct
5 Correct 36 ms 81100 KB Output is correct
6 Correct 39 ms 81096 KB Output is correct
7 Correct 41 ms 81140 KB Output is correct
8 Correct 45 ms 81156 KB Output is correct
9 Correct 36 ms 81112 KB Output is correct
10 Correct 36 ms 81084 KB Output is correct
11 Correct 41 ms 81996 KB Output is correct
12 Correct 40 ms 81988 KB Output is correct
13 Correct 42 ms 82028 KB Output is correct
14 Correct 43 ms 81980 KB Output is correct
15 Correct 45 ms 81940 KB Output is correct
16 Correct 39 ms 82076 KB Output is correct
17 Correct 39 ms 82016 KB Output is correct
18 Correct 41 ms 82064 KB Output is correct
19 Correct 39 ms 81976 KB Output is correct
20 Correct 40 ms 81992 KB Output is correct
21 Correct 44 ms 81988 KB Output is correct
22 Correct 39 ms 82192 KB Output is correct
23 Correct 39 ms 82140 KB Output is correct
24 Correct 41 ms 81912 KB Output is correct
25 Correct 44 ms 81996 KB Output is correct
26 Correct 47 ms 81976 KB Output is correct
27 Correct 40 ms 82024 KB Output is correct
28 Correct 39 ms 82028 KB Output is correct
29 Correct 40 ms 82032 KB Output is correct
30 Correct 44 ms 81976 KB Output is correct
31 Correct 809 ms 191460 KB Output is correct
32 Correct 442 ms 194904 KB Output is correct
33 Correct 767 ms 194684 KB Output is correct
34 Correct 449 ms 194920 KB Output is correct
35 Correct 799 ms 190388 KB Output is correct
36 Correct 431 ms 194596 KB Output is correct
37 Correct 717 ms 188192 KB Output is correct
38 Correct 440 ms 189468 KB Output is correct
39 Correct 497 ms 164368 KB Output is correct
40 Correct 530 ms 161672 KB Output is correct
41 Correct 502 ms 164972 KB Output is correct
42 Correct 582 ms 167672 KB Output is correct
43 Correct 563 ms 161952 KB Output is correct
44 Correct 536 ms 167640 KB Output is correct
45 Correct 588 ms 168588 KB Output is correct
46 Correct 508 ms 162580 KB Output is correct
47 Correct 551 ms 163640 KB Output is correct
48 Correct 526 ms 163276 KB Output is correct
49 Correct 590 ms 167756 KB Output is correct
50 Correct 541 ms 168212 KB Output is correct
51 Correct 39 ms 81100 KB Output is correct
52 Correct 927 ms 180992 KB Output is correct
53 Correct 1036 ms 181376 KB Output is correct
54 Correct 958 ms 181496 KB Output is correct
55 Correct 953 ms 181348 KB Output is correct
56 Correct 973 ms 181700 KB Output is correct
57 Correct 963 ms 181576 KB Output is correct
58 Correct 690 ms 172572 KB Output is correct
59 Correct 677 ms 173712 KB Output is correct
60 Correct 826 ms 185440 KB Output is correct
61 Correct 875 ms 186712 KB Output is correct
62 Correct 488 ms 194880 KB Output is correct
63 Correct 441 ms 194856 KB Output is correct
64 Correct 457 ms 190796 KB Output is correct
65 Correct 438 ms 194448 KB Output is correct
66 Correct 559 ms 174432 KB Output is correct
67 Correct 545 ms 171732 KB Output is correct
68 Correct 573 ms 174764 KB Output is correct
69 Correct 584 ms 174392 KB Output is correct
70 Correct 508 ms 174120 KB Output is correct
71 Correct 552 ms 174392 KB Output is correct
72 Correct 548 ms 174532 KB Output is correct
73 Correct 495 ms 172200 KB Output is correct
74 Correct 551 ms 174512 KB Output is correct
75 Correct 538 ms 174260 KB Output is correct
76 Correct 536 ms 169680 KB Output is correct
77 Correct 612 ms 167916 KB Output is correct
78 Correct 589 ms 164068 KB Output is correct
79 Correct 572 ms 161268 KB Output is correct
80 Correct 522 ms 167420 KB Output is correct
81 Correct 556 ms 164408 KB Output is correct
82 Correct 598 ms 165460 KB Output is correct
83 Correct 567 ms 161540 KB Output is correct
84 Correct 594 ms 168596 KB Output is correct
85 Correct 596 ms 165660 KB Output is correct
86 Correct 637 ms 161304 KB Output is correct
87 Correct 565 ms 162804 KB Output is correct
88 Correct 666 ms 169168 KB Output is correct
89 Correct 606 ms 164972 KB Output is correct
90 Correct 609 ms 165152 KB Output is correct
91 Correct 542 ms 166828 KB Output is correct
92 Correct 634 ms 166100 KB Output is correct
93 Correct 619 ms 165400 KB Output is correct
94 Correct 544 ms 164892 KB Output is correct
95 Correct 654 ms 166248 KB Output is correct
96 Correct 603 ms 165492 KB Output is correct
97 Correct 569 ms 167724 KB Output is correct