Submission #602110

# Submission time Handle Problem Language Result Execution time Memory
602110 2022-07-22T15:02:25 Z AA_Surely Capital City (JOI20_capital_city) C++17
100 / 100
910 ms 41380 KB
#include <bits/stdc++.h>

#define FOR(i,x,n) 	for(int i=x; i<n; i++)
#define F0R(i,n) 	FOR(i,0,n)
#define ROF(i,x,n) 	for(int i=n-1; i>=x; i--)
#define R0F(i,n) 	ROF(i,0,n)

#define WTF 		cout << "WTF" << endl

#define IOS 		ios::sync_with_stdio(false); cin.tie(0)
#define F 			first
#define S	 		second
#define pb 			push_back

#define ALL(x) 		x.begin(), x.end()
#define RALL(x) 	x.rbegin(), x.rend()

using namespace std;
typedef long long 		LL;

typedef pair<int, int> 	PII;
typedef pair<LL, LL> 	PLL;

typedef vector<int> 	VI;
typedef vector<LL> 		VLL;
typedef vector<PII> 	VPII;
typedef vector<PLL> 	VPLL;

const int N = 2e5 + 7;
const int ALPHA = 27;
const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
const int LOG = 22;

int n, k, vcnt, ss;
int eset[N][2], ns[N];
int comp[N], par[N], sz[N];
bool valid[N], vis[N];
VI adj[N], rs, group[N];

void init() {

    /*ifstream cin;
    cin.open("input.txt");*/

    cin >> n >> k;
    F0R(i, n - 1) {
        cin >> eset[i][0] >> eset[i][1];
        eset[i][0]--; eset[i][1]--;
    }

    F0R(i, n) {
        cin >> ns[i];
        ns[i]--;
        group[ ns[i] ].pb(i);
    }

    fill(valid, valid + k, 1);
    vcnt = k;
    return;
}

void preD(int now, int c) {
    vis[now] = 1;
    comp[now] = c;
    ss++;

    for(int on : adj[now]) if (!vis[on]) preD(on, c);
    return;
}

bool buildGraph() {
    F0R(i, n) adj[i].clear();
    rs.clear();

    F0R(i, n - 1) {
        if (valid[ ns[ eset[i][0] ] ] && valid[ ns[ eset[i][1] ] ]) {
            adj[ eset[i][0] ].pb(eset[i][1]);
            adj[ eset[i][1] ].pb(eset[i][0]);
        }
    }
    
    memset(vis, 0, sizeof vis);

    int c = 0, mx = 0;
    F0R(i, n) if (!vis[i]) {
        rs.pb(i);
        ss = 0;
        preD(i, c);
        mx = max(mx, ss);
        c++;
    }
    //cout << "max size is " << mx << endl;
    
    F0R(i, k) {
        FOR(j, 1, group[i].size()) {
            if (valid[i] && comp[ group[i][j] ] != comp[ group[i][j - 1] ]) {
                vcnt--;
                valid[i] = 0;
            }
        }
    }

    return 1;
}

int getSz(int now, int p) {
    sz[now] = 1;
    for(int on : adj[now]) if (on != p) sz[now] += getSz(on, now);
    return sz[now];
}

int getCent(int now, int p, int s) {
    for(int on : adj[now]) if (on != p) 
        if (sz[on] > (s / 2)) return getCent(on, now, s);
    return now;
}

void getPar(int now, int p) {
    par[now] = p;
    for(int on : adj[now]) if (on != p) getPar(on, now);
    return;
}

int solve(int root) {
    getSz(root, -1);
    int c = getCent(root, -1, sz[root]);
    getPar(c, -1);

    int ret = 0;
    if (!valid[ ns[c] ]) return INF;

    queue<int> keep;
    keep.push(ns[c]);
    assert(!vis[ ns[c] ]);
    vis[ ns[c] ] = 1;

    while(!keep.empty()) {
        int now = keep.front();
        keep.pop();

        for(int on : group[now]) {
            int p = par[on];
            if (p == -1) continue;
            if (!valid[ ns[p] ]) {
                if (valid[ ns[c] ]) vcnt--;
                valid[ ns[c] ] = 0;
                return INF;
            }
            if (!vis[ ns[p] ]) {
                keep.push(ns[p]);
                vis[ ns[p] ] = 1;
                ret++;
            }
        }
    }
    
    if (valid[ ns[c] ]) vcnt--;
    valid[ ns[c] ] = 0;

    return ret;
}

int main() {
    IOS;
    
    init();

    int ans = INF;
    int cc = 0;
    while(buildGraph() && vcnt) {
        cc++;
        memset(vis, 0, sizeof vis);
        for(const int &on : rs) {
            int f = solve(on);
            ans = min(ans, f);
        }

        //cout << vcnt << endl;
    }
    
    //cout << cc << endl;
    cout << ans;
}

Compilation message

capital_city.cpp: In function 'bool buildGraph()':
capital_city.cpp:3:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    3 | #define FOR(i,x,n)  for(int i=x; i<n; i++)
......
   96 |         FOR(j, 1, group[i].size()) {
      |             ~~~~~~~~~~~~~~~~~~~~~  
capital_city.cpp:96:9: note: in expansion of macro 'FOR'
   96 |         FOR(j, 1, group[i].size()) {
      |         ^~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9940 KB Output is correct
2 Correct 5 ms 9940 KB Output is correct
3 Correct 5 ms 9940 KB Output is correct
4 Correct 5 ms 9940 KB Output is correct
5 Correct 5 ms 9940 KB Output is correct
6 Correct 6 ms 9940 KB Output is correct
7 Correct 6 ms 9940 KB Output is correct
8 Correct 5 ms 9940 KB Output is correct
9 Correct 6 ms 9872 KB Output is correct
10 Correct 6 ms 9940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9940 KB Output is correct
2 Correct 5 ms 9940 KB Output is correct
3 Correct 5 ms 9940 KB Output is correct
4 Correct 5 ms 9940 KB Output is correct
5 Correct 5 ms 9940 KB Output is correct
6 Correct 6 ms 9940 KB Output is correct
7 Correct 6 ms 9940 KB Output is correct
8 Correct 5 ms 9940 KB Output is correct
9 Correct 6 ms 9872 KB Output is correct
10 Correct 6 ms 9940 KB Output is correct
11 Correct 6 ms 10068 KB Output is correct
12 Correct 6 ms 10068 KB Output is correct
13 Correct 6 ms 10068 KB Output is correct
14 Correct 6 ms 10068 KB Output is correct
15 Correct 7 ms 10068 KB Output is correct
16 Correct 7 ms 10068 KB Output is correct
17 Correct 9 ms 10220 KB Output is correct
18 Correct 7 ms 10128 KB Output is correct
19 Correct 6 ms 10068 KB Output is correct
20 Correct 9 ms 10068 KB Output is correct
21 Correct 9 ms 10068 KB Output is correct
22 Correct 7 ms 10124 KB Output is correct
23 Correct 7 ms 10068 KB Output is correct
24 Correct 8 ms 10068 KB Output is correct
25 Correct 7 ms 10120 KB Output is correct
26 Correct 7 ms 10196 KB Output is correct
27 Correct 7 ms 10120 KB Output is correct
28 Correct 6 ms 10068 KB Output is correct
29 Correct 6 ms 10120 KB Output is correct
30 Correct 7 ms 10124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 195 ms 36320 KB Output is correct
2 Correct 94 ms 36708 KB Output is correct
3 Correct 200 ms 36080 KB Output is correct
4 Correct 110 ms 36640 KB Output is correct
5 Correct 216 ms 34792 KB Output is correct
6 Correct 109 ms 37852 KB Output is correct
7 Correct 236 ms 35044 KB Output is correct
8 Correct 112 ms 40652 KB Output is correct
9 Correct 290 ms 36660 KB Output is correct
10 Correct 317 ms 34668 KB Output is correct
11 Correct 268 ms 37060 KB Output is correct
12 Correct 344 ms 39076 KB Output is correct
13 Correct 301 ms 34380 KB Output is correct
14 Correct 336 ms 39360 KB Output is correct
15 Correct 293 ms 39112 KB Output is correct
16 Correct 324 ms 35116 KB Output is correct
17 Correct 296 ms 35520 KB Output is correct
18 Correct 307 ms 32472 KB Output is correct
19 Correct 288 ms 35048 KB Output is correct
20 Correct 332 ms 36736 KB Output is correct
21 Correct 5 ms 9936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9940 KB Output is correct
2 Correct 5 ms 9940 KB Output is correct
3 Correct 5 ms 9940 KB Output is correct
4 Correct 5 ms 9940 KB Output is correct
5 Correct 5 ms 9940 KB Output is correct
6 Correct 6 ms 9940 KB Output is correct
7 Correct 6 ms 9940 KB Output is correct
8 Correct 5 ms 9940 KB Output is correct
9 Correct 6 ms 9872 KB Output is correct
10 Correct 6 ms 9940 KB Output is correct
11 Correct 6 ms 10068 KB Output is correct
12 Correct 6 ms 10068 KB Output is correct
13 Correct 6 ms 10068 KB Output is correct
14 Correct 6 ms 10068 KB Output is correct
15 Correct 7 ms 10068 KB Output is correct
16 Correct 7 ms 10068 KB Output is correct
17 Correct 9 ms 10220 KB Output is correct
18 Correct 7 ms 10128 KB Output is correct
19 Correct 6 ms 10068 KB Output is correct
20 Correct 9 ms 10068 KB Output is correct
21 Correct 9 ms 10068 KB Output is correct
22 Correct 7 ms 10124 KB Output is correct
23 Correct 7 ms 10068 KB Output is correct
24 Correct 8 ms 10068 KB Output is correct
25 Correct 7 ms 10120 KB Output is correct
26 Correct 7 ms 10196 KB Output is correct
27 Correct 7 ms 10120 KB Output is correct
28 Correct 6 ms 10068 KB Output is correct
29 Correct 6 ms 10120 KB Output is correct
30 Correct 7 ms 10124 KB Output is correct
31 Correct 195 ms 36320 KB Output is correct
32 Correct 94 ms 36708 KB Output is correct
33 Correct 200 ms 36080 KB Output is correct
34 Correct 110 ms 36640 KB Output is correct
35 Correct 216 ms 34792 KB Output is correct
36 Correct 109 ms 37852 KB Output is correct
37 Correct 236 ms 35044 KB Output is correct
38 Correct 112 ms 40652 KB Output is correct
39 Correct 290 ms 36660 KB Output is correct
40 Correct 317 ms 34668 KB Output is correct
41 Correct 268 ms 37060 KB Output is correct
42 Correct 344 ms 39076 KB Output is correct
43 Correct 301 ms 34380 KB Output is correct
44 Correct 336 ms 39360 KB Output is correct
45 Correct 293 ms 39112 KB Output is correct
46 Correct 324 ms 35116 KB Output is correct
47 Correct 296 ms 35520 KB Output is correct
48 Correct 307 ms 32472 KB Output is correct
49 Correct 288 ms 35048 KB Output is correct
50 Correct 332 ms 36736 KB Output is correct
51 Correct 5 ms 9936 KB Output is correct
52 Correct 256 ms 24040 KB Output is correct
53 Correct 262 ms 23976 KB Output is correct
54 Correct 270 ms 23960 KB Output is correct
55 Correct 293 ms 23988 KB Output is correct
56 Correct 264 ms 23980 KB Output is correct
57 Correct 276 ms 24052 KB Output is correct
58 Correct 614 ms 26908 KB Output is correct
59 Correct 612 ms 30712 KB Output is correct
60 Correct 552 ms 30144 KB Output is correct
61 Correct 549 ms 30068 KB Output is correct
62 Correct 112 ms 40256 KB Output is correct
63 Correct 124 ms 40172 KB Output is correct
64 Correct 113 ms 41212 KB Output is correct
65 Correct 112 ms 41380 KB Output is correct
66 Correct 140 ms 34408 KB Output is correct
67 Correct 170 ms 34184 KB Output is correct
68 Correct 182 ms 34380 KB Output is correct
69 Correct 176 ms 34380 KB Output is correct
70 Correct 172 ms 34316 KB Output is correct
71 Correct 158 ms 34336 KB Output is correct
72 Correct 197 ms 34320 KB Output is correct
73 Correct 165 ms 33624 KB Output is correct
74 Correct 152 ms 34308 KB Output is correct
75 Correct 196 ms 34328 KB Output is correct
76 Correct 910 ms 33448 KB Output is correct
77 Correct 843 ms 31960 KB Output is correct
78 Correct 363 ms 35756 KB Output is correct
79 Correct 297 ms 34220 KB Output is correct
80 Correct 333 ms 39852 KB Output is correct
81 Correct 328 ms 36968 KB Output is correct
82 Correct 297 ms 37208 KB Output is correct
83 Correct 332 ms 34532 KB Output is correct
84 Correct 288 ms 39252 KB Output is correct
85 Correct 332 ms 37944 KB Output is correct
86 Correct 309 ms 34116 KB Output is correct
87 Correct 270 ms 35600 KB Output is correct
88 Correct 263 ms 36612 KB Output is correct
89 Correct 233 ms 33300 KB Output is correct
90 Correct 199 ms 33072 KB Output is correct
91 Correct 270 ms 34964 KB Output is correct
92 Correct 223 ms 33924 KB Output is correct
93 Correct 224 ms 33708 KB Output is correct
94 Correct 246 ms 33224 KB Output is correct
95 Correct 225 ms 34432 KB Output is correct
96 Correct 230 ms 33400 KB Output is correct
97 Correct 225 ms 34968 KB Output is correct