Submission #637870

# Submission time Handle Problem Language Result Execution time Memory
637870 2022-09-03T12:40:34 Z Cross_Ratio Capital City (JOI20_capital_city) C++14
100 / 100
1109 ms 41388 KB
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> adj;
int C[200005];
bool vis[200005];
int sz[200005];
vector<vector<int>> B;
int get_sz(int c, int p) {
    sz[c] = 1;
    for(int n : adj[c]) {
        if(n==p || vis[n]) continue;
        sz[c] += get_sz(n, c);
    }
    return sz[c];
}
int get_cent(int c, int p, int s) {
    for(int n : adj[c]) {
        if(n==p || vis[n]) continue;
        if(sz[n] > s/2) return get_cent(n, c, s);
    }
    return c;
}
int N, K;
int par[200005];
int id[200005];
bool vis2[200005];
int cid[200005];
void dfs(int c, int p, int d) {
    int cnt = 0;
    par[c] = p;
    if(d != -1) id[c] = d;
    else id[c] = -1;
    vis2[c] = false;
    for(int n : adj[c]) {
        if(n==p || vis[n]) continue;
        if(d != -1) dfs(n, c, d);
        else dfs(n, c, cnt);
        cnt++;
    }
}
set<int> Col;
void dfs2(int c, int p, int d) {
    //cout << "dfs2 : "<< c << ' ' << p << ' ' <<d << '\n';
    if(cid[C[c]]==d) Col.insert(C[c]);
    for(int n : adj[c]) {
        if(n==p || vis[n]) continue;
        dfs2(n, c, d);//구현좃병싡장애니새기
    }
}
int DnC(int c) {
    //cout << c << ' ';
    if(Col.size()==0) return 1e9;
    assert(!vis[c]);
    get_sz(c, -1);
    int cen = get_cent(c, -1, sz[c]);
    dfs(cen, -1, -1);
    /*cout << "Dfs : "<< cen << '\n';
    cout << "Possible Color : ";
    for(int n : Col) cout << n <<' ';
    cout << '\n';*/
    set<int> S;
    int val = 1e9;
    vis[cen] = true;
    if(Col.find(C[cen])!=Col.end()) {
        set<int> S;
        queue<int> Q;
        bool isPos = true;
        S.insert(C[cen]);
        for(int n : B[C[cen]]) Q.push(n);
        while(!Q.empty()) {
            int c = Q.front();
            Q.pop();
            if(vis2[c]) continue;
            while(c != -1 && !vis2[c]) {
                if(Col.find(C[c])==Col.end()) {
                    isPos = false;
                    break;
                }
                if(S.find(C[c])==S.end()) {
                    S.insert(C[c]);
                    for(int n : B[C[c]]) Q.push(n);
                }
                vis2[c] = true;
                c = par[c];
            }
            if(!isPos) break;
        }
        if(isPos) val = min(val, (int)S.size()-1);
    }
    vector<set<int>> S2;
    int cnt = 0;
    for(int n : adj[cen]) {
        if(!vis[n]) cnt++;
    }
    for(int col : Col) {
        if(col == C[cen]) {
          cid[col] = -4;
          continue;
        }
        int d = -3;
        for(int n : B[col]) {
            if(id[n]<0) {
                d = -3;
                break;
            }
            if(d==-3) d = id[n];
            else if(d !=-3 &&d != id[n]) d = -2;
        }
        cid[col] = d;
    }
    /*for(int i=0;i<N;i++) cout << id[i] << ' ';
    cout << '\n';
    for(int i=0;i<K;i++) cout << cid[i] << ' ';
    cout << '\n';*/
    cnt = 0;
    for(int n : adj[cen]) {
        if(!vis[n]) {
            Col = set<int> {};
            dfs2(n, -1, cnt);
            val = min(val, DnC(n));
            cnt++;
        }
    }
    //cout << cen << " : " << val <<'\n';
    return val;
}
signed main() {
    cin.sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> N >> K;
    int i, j;
    adj.resize(N);
    for(i=0;i<N-1;i++) {
        int a, b;
        cin >> a >> b;
        adj[a-1].push_back(b-1);
        adj[b-1].push_back(a-1);
    }
    B.resize(K);
    for(i=0;i<N;i++) {
        cin >> C[i];
        C[i]--;
        B[C[i]].push_back(i);
    }
    for(i=0;i<K;i++) {
        if(B[i].size()==1) {
            cout << "0";
            return 0;
        }
    }
    for(i=0;i<K;i++) Col.insert(i);
    cout << DnC(0);
}

Compilation message

capital_city.cpp: In function 'int main()':
capital_city.cpp:132:12: warning: unused variable 'j' [-Wunused-variable]
  132 |     int i, j;
      |            ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
12 Correct 2 ms 468 KB Output is correct
13 Correct 2 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 2 ms 596 KB Output is correct
16 Correct 2 ms 596 KB Output is correct
17 Correct 1 ms 596 KB Output is correct
18 Correct 1 ms 596 KB Output is correct
19 Correct 1 ms 640 KB Output is correct
20 Correct 1 ms 596 KB Output is correct
21 Correct 1 ms 596 KB Output is correct
22 Correct 1 ms 716 KB Output is correct
23 Correct 2 ms 596 KB Output is correct
24 Correct 3 ms 596 KB Output is correct
25 Correct 3 ms 596 KB Output is correct
26 Correct 3 ms 724 KB Output is correct
27 Correct 3 ms 596 KB Output is correct
28 Correct 3 ms 596 KB Output is correct
29 Correct 2 ms 596 KB Output is correct
30 Correct 3 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 182 ms 37236 KB Output is correct
2 Correct 112 ms 37684 KB Output is correct
3 Correct 168 ms 37004 KB Output is correct
4 Correct 104 ms 37672 KB Output is correct
5 Correct 240 ms 34496 KB Output is correct
6 Correct 111 ms 37656 KB Output is correct
7 Correct 236 ms 34648 KB Output is correct
8 Correct 124 ms 37044 KB Output is correct
9 Correct 786 ms 31236 KB Output is correct
10 Correct 801 ms 30660 KB Output is correct
11 Correct 768 ms 32328 KB Output is correct
12 Correct 859 ms 34444 KB Output is correct
13 Correct 850 ms 30596 KB Output is correct
14 Correct 905 ms 34688 KB Output is correct
15 Correct 823 ms 34468 KB Output is correct
16 Correct 774 ms 30708 KB Output is correct
17 Correct 785 ms 30940 KB Output is correct
18 Correct 773 ms 31144 KB Output is correct
19 Correct 766 ms 33484 KB Output is correct
20 Correct 760 ms 35440 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
12 Correct 2 ms 468 KB Output is correct
13 Correct 2 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 2 ms 596 KB Output is correct
16 Correct 2 ms 596 KB Output is correct
17 Correct 1 ms 596 KB Output is correct
18 Correct 1 ms 596 KB Output is correct
19 Correct 1 ms 640 KB Output is correct
20 Correct 1 ms 596 KB Output is correct
21 Correct 1 ms 596 KB Output is correct
22 Correct 1 ms 716 KB Output is correct
23 Correct 2 ms 596 KB Output is correct
24 Correct 3 ms 596 KB Output is correct
25 Correct 3 ms 596 KB Output is correct
26 Correct 3 ms 724 KB Output is correct
27 Correct 3 ms 596 KB Output is correct
28 Correct 3 ms 596 KB Output is correct
29 Correct 2 ms 596 KB Output is correct
30 Correct 3 ms 596 KB Output is correct
31 Correct 182 ms 37236 KB Output is correct
32 Correct 112 ms 37684 KB Output is correct
33 Correct 168 ms 37004 KB Output is correct
34 Correct 104 ms 37672 KB Output is correct
35 Correct 240 ms 34496 KB Output is correct
36 Correct 111 ms 37656 KB Output is correct
37 Correct 236 ms 34648 KB Output is correct
38 Correct 124 ms 37044 KB Output is correct
39 Correct 786 ms 31236 KB Output is correct
40 Correct 801 ms 30660 KB Output is correct
41 Correct 768 ms 32328 KB Output is correct
42 Correct 859 ms 34444 KB Output is correct
43 Correct 850 ms 30596 KB Output is correct
44 Correct 905 ms 34688 KB Output is correct
45 Correct 823 ms 34468 KB Output is correct
46 Correct 774 ms 30708 KB Output is correct
47 Correct 785 ms 30940 KB Output is correct
48 Correct 773 ms 31144 KB Output is correct
49 Correct 766 ms 33484 KB Output is correct
50 Correct 760 ms 35440 KB Output is correct
51 Correct 0 ms 340 KB Output is correct
52 Correct 226 ms 22476 KB Output is correct
53 Correct 266 ms 22496 KB Output is correct
54 Correct 109 ms 17740 KB Output is correct
55 Correct 236 ms 22488 KB Output is correct
56 Correct 102 ms 17612 KB Output is correct
57 Correct 105 ms 17620 KB Output is correct
58 Correct 641 ms 30196 KB Output is correct
59 Correct 554 ms 29416 KB Output is correct
60 Correct 468 ms 29588 KB Output is correct
61 Correct 544 ms 29860 KB Output is correct
62 Correct 107 ms 41292 KB Output is correct
63 Correct 109 ms 41388 KB Output is correct
64 Correct 130 ms 40832 KB Output is correct
65 Correct 117 ms 41292 KB Output is correct
66 Correct 193 ms 31056 KB Output is correct
67 Correct 189 ms 30924 KB Output is correct
68 Correct 195 ms 31104 KB Output is correct
69 Correct 194 ms 31044 KB Output is correct
70 Correct 193 ms 31056 KB Output is correct
71 Correct 193 ms 31036 KB Output is correct
72 Correct 195 ms 31148 KB Output is correct
73 Correct 194 ms 30756 KB Output is correct
74 Correct 222 ms 31068 KB Output is correct
75 Correct 196 ms 31088 KB Output is correct
76 Correct 1109 ms 33848 KB Output is correct
77 Correct 1044 ms 33640 KB Output is correct
78 Correct 762 ms 33888 KB Output is correct
79 Correct 763 ms 33696 KB Output is correct
80 Correct 757 ms 38036 KB Output is correct
81 Correct 767 ms 35288 KB Output is correct
82 Correct 751 ms 35212 KB Output is correct
83 Correct 750 ms 33760 KB Output is correct
84 Correct 763 ms 37312 KB Output is correct
85 Correct 763 ms 36060 KB Output is correct
86 Correct 768 ms 33692 KB Output is correct
87 Correct 821 ms 33724 KB Output is correct
88 Correct 656 ms 33996 KB Output is correct
89 Correct 630 ms 31040 KB Output is correct
90 Correct 606 ms 31216 KB Output is correct
91 Correct 634 ms 32460 KB Output is correct
92 Correct 621 ms 31440 KB Output is correct
93 Correct 616 ms 31100 KB Output is correct
94 Correct 617 ms 31180 KB Output is correct
95 Correct 610 ms 31808 KB Output is correct
96 Correct 615 ms 31108 KB Output is correct
97 Correct 623 ms 32332 KB Output is correct