답안 #1117370

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1117370 2024-11-23T12:35:59 Z Zero_OP 수도 (JOI20_capital_city) C++14
100 / 100
187 ms 51228 KB
#include <bits/stdc++.h>

using namespace std;

#define rep(i, l, r) for(int i = (l); i < (r); ++i)
#define sz(v) (int)v.size()
#define dbg(x) "[" #x " = " << (x) << "]"
#define all(v) begin(v), end(v)
#define compact(v) v.erase(unique(all(v)), end(v))
#define file(name) if(fopen(name".inp", "r")){ freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }

template<typename T>
    bool minimize(T& a, const T& b){
        if(a > b){
            return a = b, true;
        }
        return false;
    }

template<typename T>
    bool maximize(T& a, const T& b){
        if(a < b){
            return a = b, true;
        }
        return false;
    }

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

using ll = long long;
using ld = long double;
using ull = unsigned long long;
using vi = vector<int>;
using vl = vector<ll>;

const int MAX = 2e5 + 5;

int N, K, timer_dfs, tin[MAX], tout[MAX], c[MAX], par[MAX], tour[MAX];
vi adj[MAX], group[MAX], adj_color[MAX];

int low[MAX], num[MAX], cnt[MAX], bad[MAX], id[MAX], num_scc;
stack<int> st;

void dfs(int u, int p){
    tour[tin[u] = ++timer_dfs] = u;
    for(int v : adj[u]) if(v != p){
        par[v] = u;
        dfs(v, u);
    }
    tout[u] = timer_dfs;
}

void dfs_tarjan(int u){
    low[u] = num[u] = ++timer_dfs;
    st.push(u);

    for(int v : adj_color[u]){
        if(num[v]) minimize(low[u], num[v]);
        else{
            dfs_tarjan(v);
            minimize(low[u], low[v]);
        }
    }

    if(low[u] == num[u]){
        int v;
        ++num_scc;
        do{
            v = st.top(); st.pop();
            low[v] = num[v] = MAX;
            id[v] = num_scc;
            ++cnt[num_scc];
        } while(v != u);
    }
}

bool in_subtree(int u, int v){
    return tin[u] <= tin[v] && tout[v] <= tout[u];
}

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

    file("task");

    cin >> N >> K;
    for(int i = 1; i < N; ++i){
        int u, v;
        cin >> u >> v;
        adj[u].emplace_back(v);
        adj[v].emplace_back(u);
    }

    for(int i = 1; i <= N; ++i){
        cin >> c[i];
    }

    dfs(1, -1);

    for(int i = 1; i <= N; ++i){
        group[c[i]].emplace_back(tin[i]);
    }

    for(int i = 2; i <= N; ++i){
        int j = par[i];
        if(c[i] == c[j]) continue;

        int p = lower_bound(all(group[c[j]]), tin[i]) - group[c[j]].begin();
        if(p < sz(group[c[j]]) && group[c[j]][p] <= tout[i]){
            adj_color[c[j]].emplace_back(c[i]); //c[j] need c[i]
        }

        if(!(in_subtree(i, tour[group[c[i]].front()]) && in_subtree(i, tour[group[c[i]].back()]))){
            adj_color[c[i]].emplace_back(c[j]); //c[i] need c[j]
        }
    }

    int ans = K;

    for(int i = 1; i <= K; ++i){
        if(!num[i]) dfs_tarjan(i);
    }

    for(int i = 1; i <= K; ++i){
        for(int j : adj_color[i]){
            if(id[i] != id[j]){
                bad[id[i]] = true;
            }
        }
    }

    for(int i = 1; i <= num_scc; ++i){
        if(!bad[i]) ans = min(ans, cnt[i] - 1);
    }

    cout << ans << '\n';

    return 0;
}

Compilation message

capital_city.cpp: In function 'int main()':
capital_city.cpp:10:55: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   10 | #define file(name) if(fopen(name".inp", "r")){ freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
capital_city.cpp:85:5: note: in expansion of macro 'file'
   85 |     file("task");
      |     ^~~~
capital_city.cpp:10:88: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   10 | #define file(name) if(fopen(name".inp", "r")){ freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                                                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
capital_city.cpp:85:5: note: in expansion of macro 'file'
   85 |     file("task");
      |     ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 20816 KB Output is correct
2 Correct 4 ms 16720 KB Output is correct
3 Correct 3 ms 16720 KB Output is correct
4 Correct 4 ms 20816 KB Output is correct
5 Correct 4 ms 20816 KB Output is correct
6 Correct 4 ms 18768 KB Output is correct
7 Correct 3 ms 16720 KB Output is correct
8 Correct 5 ms 20816 KB Output is correct
9 Correct 4 ms 20816 KB Output is correct
10 Correct 3 ms 16720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 20816 KB Output is correct
2 Correct 4 ms 16720 KB Output is correct
3 Correct 3 ms 16720 KB Output is correct
4 Correct 4 ms 20816 KB Output is correct
5 Correct 4 ms 20816 KB Output is correct
6 Correct 4 ms 18768 KB Output is correct
7 Correct 3 ms 16720 KB Output is correct
8 Correct 5 ms 20816 KB Output is correct
9 Correct 4 ms 20816 KB Output is correct
10 Correct 3 ms 16720 KB Output is correct
11 Correct 5 ms 21072 KB Output is correct
12 Correct 6 ms 21072 KB Output is correct
13 Correct 5 ms 16976 KB Output is correct
14 Correct 6 ms 16976 KB Output is correct
15 Correct 7 ms 16976 KB Output is correct
16 Correct 6 ms 16976 KB Output is correct
17 Correct 10 ms 15296 KB Output is correct
18 Correct 8 ms 17144 KB Output is correct
19 Correct 10 ms 15316 KB Output is correct
20 Correct 6 ms 15440 KB Output is correct
21 Correct 7 ms 15440 KB Output is correct
22 Correct 6 ms 17232 KB Output is correct
23 Correct 5 ms 19024 KB Output is correct
24 Correct 4 ms 16976 KB Output is correct
25 Correct 4 ms 16976 KB Output is correct
26 Correct 4 ms 16976 KB Output is correct
27 Correct 6 ms 17032 KB Output is correct
28 Correct 5 ms 16976 KB Output is correct
29 Correct 6 ms 17088 KB Output is correct
30 Correct 4 ms 16976 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 115 ms 47440 KB Output is correct
2 Correct 97 ms 49224 KB Output is correct
3 Correct 125 ms 48712 KB Output is correct
4 Correct 81 ms 49040 KB Output is correct
5 Correct 117 ms 45896 KB Output is correct
6 Correct 78 ms 49148 KB Output is correct
7 Correct 135 ms 46160 KB Output is correct
8 Correct 75 ms 48068 KB Output is correct
9 Correct 117 ms 45828 KB Output is correct
10 Correct 137 ms 41800 KB Output is correct
11 Correct 124 ms 44104 KB Output is correct
12 Correct 115 ms 46152 KB Output is correct
13 Correct 112 ms 41332 KB Output is correct
14 Correct 117 ms 46408 KB Output is correct
15 Correct 127 ms 48200 KB Output is correct
16 Correct 131 ms 42056 KB Output is correct
17 Correct 118 ms 43336 KB Output is correct
18 Correct 101 ms 42856 KB Output is correct
19 Correct 109 ms 45384 KB Output is correct
20 Correct 115 ms 47176 KB Output is correct
21 Correct 4 ms 20984 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 20816 KB Output is correct
2 Correct 4 ms 16720 KB Output is correct
3 Correct 3 ms 16720 KB Output is correct
4 Correct 4 ms 20816 KB Output is correct
5 Correct 4 ms 20816 KB Output is correct
6 Correct 4 ms 18768 KB Output is correct
7 Correct 3 ms 16720 KB Output is correct
8 Correct 5 ms 20816 KB Output is correct
9 Correct 4 ms 20816 KB Output is correct
10 Correct 3 ms 16720 KB Output is correct
11 Correct 5 ms 21072 KB Output is correct
12 Correct 6 ms 21072 KB Output is correct
13 Correct 5 ms 16976 KB Output is correct
14 Correct 6 ms 16976 KB Output is correct
15 Correct 7 ms 16976 KB Output is correct
16 Correct 6 ms 16976 KB Output is correct
17 Correct 10 ms 15296 KB Output is correct
18 Correct 8 ms 17144 KB Output is correct
19 Correct 10 ms 15316 KB Output is correct
20 Correct 6 ms 15440 KB Output is correct
21 Correct 7 ms 15440 KB Output is correct
22 Correct 6 ms 17232 KB Output is correct
23 Correct 5 ms 19024 KB Output is correct
24 Correct 4 ms 16976 KB Output is correct
25 Correct 4 ms 16976 KB Output is correct
26 Correct 4 ms 16976 KB Output is correct
27 Correct 6 ms 17032 KB Output is correct
28 Correct 5 ms 16976 KB Output is correct
29 Correct 6 ms 17088 KB Output is correct
30 Correct 4 ms 16976 KB Output is correct
31 Correct 115 ms 47440 KB Output is correct
32 Correct 97 ms 49224 KB Output is correct
33 Correct 125 ms 48712 KB Output is correct
34 Correct 81 ms 49040 KB Output is correct
35 Correct 117 ms 45896 KB Output is correct
36 Correct 78 ms 49148 KB Output is correct
37 Correct 135 ms 46160 KB Output is correct
38 Correct 75 ms 48068 KB Output is correct
39 Correct 117 ms 45828 KB Output is correct
40 Correct 137 ms 41800 KB Output is correct
41 Correct 124 ms 44104 KB Output is correct
42 Correct 115 ms 46152 KB Output is correct
43 Correct 112 ms 41332 KB Output is correct
44 Correct 117 ms 46408 KB Output is correct
45 Correct 127 ms 48200 KB Output is correct
46 Correct 131 ms 42056 KB Output is correct
47 Correct 118 ms 43336 KB Output is correct
48 Correct 101 ms 42856 KB Output is correct
49 Correct 109 ms 45384 KB Output is correct
50 Correct 115 ms 47176 KB Output is correct
51 Correct 4 ms 20984 KB Output is correct
52 Correct 90 ms 36424 KB Output is correct
53 Correct 90 ms 36424 KB Output is correct
54 Correct 93 ms 34120 KB Output is correct
55 Correct 113 ms 34272 KB Output is correct
56 Correct 97 ms 34272 KB Output is correct
57 Correct 95 ms 34376 KB Output is correct
58 Correct 111 ms 38268 KB Output is correct
59 Correct 99 ms 37844 KB Output is correct
60 Correct 112 ms 37960 KB Output is correct
61 Correct 135 ms 37600 KB Output is correct
62 Correct 80 ms 49224 KB Output is correct
63 Correct 81 ms 51228 KB Output is correct
64 Correct 78 ms 50504 KB Output is correct
65 Correct 79 ms 51044 KB Output is correct
66 Correct 129 ms 42532 KB Output is correct
67 Correct 96 ms 40384 KB Output is correct
68 Correct 94 ms 42432 KB Output is correct
69 Correct 102 ms 40428 KB Output is correct
70 Correct 122 ms 42244 KB Output is correct
71 Correct 130 ms 42320 KB Output is correct
72 Correct 103 ms 40384 KB Output is correct
73 Correct 105 ms 41660 KB Output is correct
74 Correct 94 ms 42432 KB Output is correct
75 Correct 114 ms 40384 KB Output is correct
76 Correct 122 ms 43304 KB Output is correct
77 Correct 102 ms 41816 KB Output is correct
78 Correct 108 ms 44872 KB Output is correct
79 Correct 100 ms 43080 KB Output is correct
80 Correct 125 ms 48476 KB Output is correct
81 Correct 118 ms 43848 KB Output is correct
82 Correct 113 ms 45996 KB Output is correct
83 Correct 105 ms 43336 KB Output is correct
84 Correct 110 ms 48104 KB Output is correct
85 Correct 114 ms 44616 KB Output is correct
86 Correct 156 ms 43080 KB Output is correct
87 Correct 131 ms 42312 KB Output is correct
88 Correct 147 ms 43336 KB Output is correct
89 Correct 187 ms 38984 KB Output is correct
90 Correct 130 ms 38464 KB Output is correct
91 Correct 117 ms 40532 KB Output is correct
92 Correct 106 ms 39496 KB Output is correct
93 Correct 107 ms 39240 KB Output is correct
94 Correct 107 ms 38828 KB Output is correct
95 Correct 118 ms 40016 KB Output is correct
96 Correct 131 ms 40880 KB Output is correct
97 Correct 110 ms 43852 KB Output is correct