답안 #1050248

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1050248 2024-08-09T08:14:04 Z hotboy2703 수도 (JOI20_capital_city) C++17
100 / 100
141 ms 53768 KB
#include<bits/stdc++.h>
using ll = long long;
using namespace std;
#define pll pair <ll,ll>
#define fi first
#define se second
#define MP make_pair
#define sz(a) (ll((a).size()))
#define BIT(mask,i) (((mask) >> (i))&1)
#define MASK(i) (1LL << (i))
const ll MAXN = 2e5+100;
vector <ll> g[MAXN];
ll in[MAXN],out[MAXN],timeDFS;
ll c[MAXN];
ll n,k;
vector <ll> color[MAXN];
namespace SCC{
    vector <ll> edge[MAXN];
    vector <ll> rev[MAXN];
    bool in[MAXN];
    void add(ll x,ll y){
        // cout<<x<<' '<<y<<'\n';
        edge[x].push_back(y);
        rev[y].push_back(x);
    }
    void dfs(ll u,vector <ll> gg[],vector <ll> &comp){
        in[u] = 1;
        for (auto v:gg[u]){
            if (!in[v]){
                dfs(v,gg,comp);
            }
        }
        comp.push_back(u);
    }
    ll solve(){
        vector <ll> order;
        for (ll i = 1;i <= k;i ++){
            if (!in[i]){
                dfs(i,edge,order);
            }
        }
        memset(in,0,sizeof in);
        reverse(order.begin(),order.end());
        ll res = k;
        for (auto x:order){
            if (!in[x]){
                vector <ll> comp;
                dfs(x,rev,comp);
                bool ok = 1;
                for (auto x:comp){
                    for (auto y:edge[x]){
                        if (!in[y]){ok=0;}
                    }
                }
                if (ok && sz(comp) < res){
                    res = sz(comp);
                }
            }
        }
        return res;
    }
}
void dfs(ll u,ll p){
    in[u] = ++timeDFS;
    color[c[u]].push_back(in[u]);
    for (auto v:g[u]){
        if (v==p)continue;
        dfs(v,u);
    }
    out[u] = timeDFS;
}
int main(){
    ios_base::sync_with_stdio(0);cin.tie(nullptr);
    cin>>n>>k;
    for (ll i = 1;i < n;i ++){
        ll u,v;
        cin>>u>>v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    for (ll i = 1;i <= n;i ++)cin>>c[i];
    dfs(1,1);
    for (ll i = 1;i <= n;i ++){
        for (auto v:g[i]){
            if (c[v] != c[i]){
                if (in[v] > in[i]){
                    auto tmp = lower_bound(color[c[i]].begin(),color[c[i]].end(),in[v]);
                    if (tmp!=color[c[i]].end() && (*tmp) <= out[v]){
                        SCC::add(c[i],c[v]);
                    }
                }
                else{
                    auto tmp = upper_bound(color[c[i]].begin(),color[c[i]].end(),out[i]) - lower_bound(color[c[i]].begin(),color[c[i]].end(),in[i]);
                    if (tmp != sz(color[c[i]]))SCC::add(c[i],c[v]);
                }
            }
        }
    }
    cout<<SCC::solve()-1;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 22108 KB Output is correct
2 Correct 3 ms 21852 KB Output is correct
3 Correct 3 ms 22108 KB Output is correct
4 Correct 3 ms 22104 KB Output is correct
5 Correct 3 ms 21852 KB Output is correct
6 Correct 3 ms 21852 KB Output is correct
7 Correct 3 ms 21916 KB Output is correct
8 Correct 3 ms 21852 KB Output is correct
9 Correct 3 ms 21848 KB Output is correct
10 Correct 3 ms 21848 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 22108 KB Output is correct
2 Correct 3 ms 21852 KB Output is correct
3 Correct 3 ms 22108 KB Output is correct
4 Correct 3 ms 22104 KB Output is correct
5 Correct 3 ms 21852 KB Output is correct
6 Correct 3 ms 21852 KB Output is correct
7 Correct 3 ms 21916 KB Output is correct
8 Correct 3 ms 21852 KB Output is correct
9 Correct 3 ms 21848 KB Output is correct
10 Correct 3 ms 21848 KB Output is correct
11 Correct 4 ms 22104 KB Output is correct
12 Correct 4 ms 22300 KB Output is correct
13 Correct 3 ms 22108 KB Output is correct
14 Correct 4 ms 22108 KB Output is correct
15 Correct 4 ms 22184 KB Output is correct
16 Correct 4 ms 22180 KB Output is correct
17 Correct 3 ms 22108 KB Output is correct
18 Correct 3 ms 22108 KB Output is correct
19 Correct 3 ms 22108 KB Output is correct
20 Correct 3 ms 22108 KB Output is correct
21 Correct 3 ms 22108 KB Output is correct
22 Correct 3 ms 22364 KB Output is correct
23 Correct 4 ms 22104 KB Output is correct
24 Correct 4 ms 22108 KB Output is correct
25 Correct 4 ms 22360 KB Output is correct
26 Correct 3 ms 22364 KB Output is correct
27 Correct 4 ms 22364 KB Output is correct
28 Correct 3 ms 22364 KB Output is correct
29 Correct 3 ms 22364 KB Output is correct
30 Correct 3 ms 22364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 105 ms 53564 KB Output is correct
2 Correct 73 ms 53740 KB Output is correct
3 Correct 115 ms 53372 KB Output is correct
4 Correct 66 ms 53768 KB Output is correct
5 Correct 103 ms 51148 KB Output is correct
6 Correct 61 ms 53676 KB Output is correct
7 Correct 111 ms 50848 KB Output is correct
8 Correct 59 ms 52296 KB Output is correct
9 Correct 112 ms 50624 KB Output is correct
10 Correct 111 ms 49200 KB Output is correct
11 Correct 107 ms 51076 KB Output is correct
12 Correct 112 ms 52628 KB Output is correct
13 Correct 115 ms 49160 KB Output is correct
14 Correct 107 ms 52912 KB Output is correct
15 Correct 115 ms 53236 KB Output is correct
16 Correct 108 ms 49716 KB Output is correct
17 Correct 107 ms 50364 KB Output is correct
18 Correct 131 ms 50216 KB Output is correct
19 Correct 106 ms 52640 KB Output is correct
20 Correct 105 ms 53416 KB Output is correct
21 Correct 3 ms 21852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 22108 KB Output is correct
2 Correct 3 ms 21852 KB Output is correct
3 Correct 3 ms 22108 KB Output is correct
4 Correct 3 ms 22104 KB Output is correct
5 Correct 3 ms 21852 KB Output is correct
6 Correct 3 ms 21852 KB Output is correct
7 Correct 3 ms 21916 KB Output is correct
8 Correct 3 ms 21852 KB Output is correct
9 Correct 3 ms 21848 KB Output is correct
10 Correct 3 ms 21848 KB Output is correct
11 Correct 4 ms 22104 KB Output is correct
12 Correct 4 ms 22300 KB Output is correct
13 Correct 3 ms 22108 KB Output is correct
14 Correct 4 ms 22108 KB Output is correct
15 Correct 4 ms 22184 KB Output is correct
16 Correct 4 ms 22180 KB Output is correct
17 Correct 3 ms 22108 KB Output is correct
18 Correct 3 ms 22108 KB Output is correct
19 Correct 3 ms 22108 KB Output is correct
20 Correct 3 ms 22108 KB Output is correct
21 Correct 3 ms 22108 KB Output is correct
22 Correct 3 ms 22364 KB Output is correct
23 Correct 4 ms 22104 KB Output is correct
24 Correct 4 ms 22108 KB Output is correct
25 Correct 4 ms 22360 KB Output is correct
26 Correct 3 ms 22364 KB Output is correct
27 Correct 4 ms 22364 KB Output is correct
28 Correct 3 ms 22364 KB Output is correct
29 Correct 3 ms 22364 KB Output is correct
30 Correct 3 ms 22364 KB Output is correct
31 Correct 105 ms 53564 KB Output is correct
32 Correct 73 ms 53740 KB Output is correct
33 Correct 115 ms 53372 KB Output is correct
34 Correct 66 ms 53768 KB Output is correct
35 Correct 103 ms 51148 KB Output is correct
36 Correct 61 ms 53676 KB Output is correct
37 Correct 111 ms 50848 KB Output is correct
38 Correct 59 ms 52296 KB Output is correct
39 Correct 112 ms 50624 KB Output is correct
40 Correct 111 ms 49200 KB Output is correct
41 Correct 107 ms 51076 KB Output is correct
42 Correct 112 ms 52628 KB Output is correct
43 Correct 115 ms 49160 KB Output is correct
44 Correct 107 ms 52912 KB Output is correct
45 Correct 115 ms 53236 KB Output is correct
46 Correct 108 ms 49716 KB Output is correct
47 Correct 107 ms 50364 KB Output is correct
48 Correct 131 ms 50216 KB Output is correct
49 Correct 106 ms 52640 KB Output is correct
50 Correct 105 ms 53416 KB Output is correct
51 Correct 3 ms 21852 KB Output is correct
52 Correct 94 ms 44472 KB Output is correct
53 Correct 114 ms 44456 KB Output is correct
54 Correct 89 ms 44492 KB Output is correct
55 Correct 95 ms 44464 KB Output is correct
56 Correct 112 ms 44492 KB Output is correct
57 Correct 94 ms 44496 KB Output is correct
58 Correct 100 ms 46716 KB Output is correct
59 Correct 94 ms 47052 KB Output is correct
60 Correct 118 ms 46284 KB Output is correct
61 Correct 101 ms 46028 KB Output is correct
62 Correct 59 ms 53704 KB Output is correct
63 Correct 78 ms 53712 KB Output is correct
64 Correct 59 ms 52812 KB Output is correct
65 Correct 60 ms 53704 KB Output is correct
66 Correct 71 ms 45128 KB Output is correct
67 Correct 85 ms 44964 KB Output is correct
68 Correct 82 ms 45160 KB Output is correct
69 Correct 79 ms 45160 KB Output is correct
70 Correct 72 ms 45124 KB Output is correct
71 Correct 71 ms 45168 KB Output is correct
72 Correct 98 ms 44960 KB Output is correct
73 Correct 70 ms 44624 KB Output is correct
74 Correct 69 ms 45124 KB Output is correct
75 Correct 81 ms 45332 KB Output is correct
76 Correct 103 ms 49440 KB Output is correct
77 Correct 111 ms 48336 KB Output is correct
78 Correct 105 ms 50700 KB Output is correct
79 Correct 102 ms 48936 KB Output is correct
80 Correct 108 ms 52920 KB Output is correct
81 Correct 118 ms 50788 KB Output is correct
82 Correct 104 ms 51404 KB Output is correct
83 Correct 117 ms 49084 KB Output is correct
84 Correct 141 ms 52996 KB Output is correct
85 Correct 126 ms 51640 KB Output is correct
86 Correct 107 ms 48836 KB Output is correct
87 Correct 110 ms 49780 KB Output is correct
88 Correct 119 ms 50828 KB Output is correct
89 Correct 104 ms 47780 KB Output is correct
90 Correct 105 ms 47640 KB Output is correct
91 Correct 104 ms 49576 KB Output is correct
92 Correct 105 ms 49524 KB Output is correct
93 Correct 103 ms 48192 KB Output is correct
94 Correct 101 ms 47764 KB Output is correct
95 Correct 107 ms 48476 KB Output is correct
96 Correct 113 ms 48944 KB Output is correct
97 Correct 106 ms 49276 KB Output is correct