Submission #241299

# Submission time Handle Problem Language Result Execution time Memory
241299 2020-06-23T18:05:44 Z Mercenary Capital City (JOI20_capital_city) C++14
100 / 100
890 ms 46888 KB
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>

#define pb push_back
#define mp make_pair
#define taskname "A"

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef tree <int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;

const int maxn = 2e5 + 10;
int n , k;
int c[maxn] , cnt_col[maxn];
vector<int> col_node[maxn];
vector<int> adj[maxn];
int sub[maxn],big[maxn];
int vis[maxn];
int vis1[maxn];

int FindRoot(int u){
    function<void(int,int)> dfs = [&](int u , int v){
        sub[u] = 1;big[u] = -1;
        for(auto c : adj[u]){
            if(c != v && vis[c] == 0){
                dfs(c , u);
                sub[u] += sub[c];
                if(big[u] == -1 || sub[big[u]] < sub[c])big[u] = c;
            }
        }
    };
    dfs(u , 0);
    int tot = sub[u];
    while(big[u] != -1 && sub[big[u]] * 2 >= tot)u = big[u];
    return u;
}

int pa[maxn];
int res = 1e9;
bool vis_col[maxn];

void centroid(int u){
    u = FindRoot(u);
    int root = u;
    pa[u] = u;
    function<void(int,int)> dfs = [&](int u , int v){
        vis1[u] = root;
        for(auto c : adj[u]){
            if(c != v && vis[c] == 0){
                pa[c] = u;
                dfs(c , u);
            }
        }
    };
    dfs(u , 0);
    vector<int> ans;
    queue<int> q;q.push(u);
    int cnt = -1;
    bool ok = 1;
    while(q.size() && ok == 1){
        int u = q.front();q.pop();
        ++cnt;
        while(ok == 1){
            if(vis1[u] == 0)break;
            if(vis_col[c[u]] == 0){
                cnt -= cnt_col[c[u]];
                vis_col[c[u]] = 1;
                ans.pb(c[u]);
                for(auto &v : col_node[c[u]]){
                    q.push(v);
                    if(vis1[v] != root){
                        ok = 0;break;
                    }
                }
            }
            vis1[u] = 0;
            u = pa[u];
            if(u == root)break;
        }
    }
    for(auto c : ans){
        vis_col[c] = 0;
    }
    if(cnt == 0){
        res = min(res , (int)ans.size() - 1);
    }
    vis[u] = 1;
    for(auto c : adj[u]){
        if(vis[c] == 0)centroid(c);
    }
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    if(fopen(taskname".INP","r")){
		freopen(taskname".INP", "r",stdin);
		freopen(taskname".OUT", "w",stdout);
    }
    cin >> n >> k;
    for(int i = 1 ; i < n ; ++i){
        int u , v;cin >> u >> v;
        adj[u].pb(v);adj[v].pb(u);
    }
    for(int i = 1 ; i <= n ; ++i){
        cin >> c[i];
        cnt_col[c[i]]++;
        col_node[c[i]].pb(i);
    }
    centroid(1);
    cout << res;
}

Compilation message

capital_city.cpp: In function 'int main()':
capital_city.cpp:103:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   freopen(taskname".INP", "r",stdin);
   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
capital_city.cpp:104:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   freopen(taskname".OUT", "w",stdout);
   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 12 ms 9856 KB Output is correct
2 Correct 11 ms 9856 KB Output is correct
3 Correct 10 ms 9728 KB Output is correct
4 Correct 11 ms 9728 KB Output is correct
5 Correct 13 ms 9728 KB Output is correct
6 Correct 11 ms 9856 KB Output is correct
7 Correct 11 ms 9728 KB Output is correct
8 Correct 10 ms 9856 KB Output is correct
9 Correct 10 ms 9728 KB Output is correct
10 Correct 11 ms 9856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 9856 KB Output is correct
2 Correct 11 ms 9856 KB Output is correct
3 Correct 10 ms 9728 KB Output is correct
4 Correct 11 ms 9728 KB Output is correct
5 Correct 13 ms 9728 KB Output is correct
6 Correct 11 ms 9856 KB Output is correct
7 Correct 11 ms 9728 KB Output is correct
8 Correct 10 ms 9856 KB Output is correct
9 Correct 10 ms 9728 KB Output is correct
10 Correct 11 ms 9856 KB Output is correct
11 Correct 16 ms 9984 KB Output is correct
12 Correct 12 ms 9984 KB Output is correct
13 Correct 14 ms 9984 KB Output is correct
14 Correct 13 ms 9984 KB Output is correct
15 Correct 12 ms 9984 KB Output is correct
16 Correct 15 ms 9984 KB Output is correct
17 Correct 11 ms 9984 KB Output is correct
18 Correct 11 ms 9984 KB Output is correct
19 Correct 13 ms 9984 KB Output is correct
20 Correct 13 ms 9984 KB Output is correct
21 Correct 16 ms 9984 KB Output is correct
22 Correct 15 ms 10152 KB Output is correct
23 Correct 13 ms 9984 KB Output is correct
24 Correct 12 ms 9984 KB Output is correct
25 Correct 14 ms 10112 KB Output is correct
26 Correct 12 ms 10112 KB Output is correct
27 Correct 14 ms 10112 KB Output is correct
28 Correct 15 ms 9984 KB Output is correct
29 Correct 12 ms 9984 KB Output is correct
30 Correct 12 ms 10112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 835 ms 46304 KB Output is correct
2 Correct 274 ms 46816 KB Output is correct
3 Correct 722 ms 45848 KB Output is correct
4 Correct 275 ms 46888 KB Output is correct
5 Correct 784 ms 42464 KB Output is correct
6 Correct 282 ms 46816 KB Output is correct
7 Correct 890 ms 43000 KB Output is correct
8 Correct 264 ms 46708 KB Output is correct
9 Correct 696 ms 41096 KB Output is correct
10 Correct 728 ms 38076 KB Output is correct
11 Correct 816 ms 41576 KB Output is correct
12 Correct 756 ms 44664 KB Output is correct
13 Correct 763 ms 37544 KB Output is correct
14 Correct 790 ms 45088 KB Output is correct
15 Correct 792 ms 44848 KB Output is correct
16 Correct 771 ms 38740 KB Output is correct
17 Correct 841 ms 39512 KB Output is correct
18 Correct 729 ms 39628 KB Output is correct
19 Correct 739 ms 43616 KB Output is correct
20 Correct 760 ms 46088 KB Output is correct
21 Correct 10 ms 9728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 9856 KB Output is correct
2 Correct 11 ms 9856 KB Output is correct
3 Correct 10 ms 9728 KB Output is correct
4 Correct 11 ms 9728 KB Output is correct
5 Correct 13 ms 9728 KB Output is correct
6 Correct 11 ms 9856 KB Output is correct
7 Correct 11 ms 9728 KB Output is correct
8 Correct 10 ms 9856 KB Output is correct
9 Correct 10 ms 9728 KB Output is correct
10 Correct 11 ms 9856 KB Output is correct
11 Correct 16 ms 9984 KB Output is correct
12 Correct 12 ms 9984 KB Output is correct
13 Correct 14 ms 9984 KB Output is correct
14 Correct 13 ms 9984 KB Output is correct
15 Correct 12 ms 9984 KB Output is correct
16 Correct 15 ms 9984 KB Output is correct
17 Correct 11 ms 9984 KB Output is correct
18 Correct 11 ms 9984 KB Output is correct
19 Correct 13 ms 9984 KB Output is correct
20 Correct 13 ms 9984 KB Output is correct
21 Correct 16 ms 9984 KB Output is correct
22 Correct 15 ms 10152 KB Output is correct
23 Correct 13 ms 9984 KB Output is correct
24 Correct 12 ms 9984 KB Output is correct
25 Correct 14 ms 10112 KB Output is correct
26 Correct 12 ms 10112 KB Output is correct
27 Correct 14 ms 10112 KB Output is correct
28 Correct 15 ms 9984 KB Output is correct
29 Correct 12 ms 9984 KB Output is correct
30 Correct 12 ms 10112 KB Output is correct
31 Correct 835 ms 46304 KB Output is correct
32 Correct 274 ms 46816 KB Output is correct
33 Correct 722 ms 45848 KB Output is correct
34 Correct 275 ms 46888 KB Output is correct
35 Correct 784 ms 42464 KB Output is correct
36 Correct 282 ms 46816 KB Output is correct
37 Correct 890 ms 43000 KB Output is correct
38 Correct 264 ms 46708 KB Output is correct
39 Correct 696 ms 41096 KB Output is correct
40 Correct 728 ms 38076 KB Output is correct
41 Correct 816 ms 41576 KB Output is correct
42 Correct 756 ms 44664 KB Output is correct
43 Correct 763 ms 37544 KB Output is correct
44 Correct 790 ms 45088 KB Output is correct
45 Correct 792 ms 44848 KB Output is correct
46 Correct 771 ms 38740 KB Output is correct
47 Correct 841 ms 39512 KB Output is correct
48 Correct 729 ms 39628 KB Output is correct
49 Correct 739 ms 43616 KB Output is correct
50 Correct 760 ms 46088 KB Output is correct
51 Correct 10 ms 9728 KB Output is correct
52 Correct 597 ms 26684 KB Output is correct
53 Correct 672 ms 26876 KB Output is correct
54 Correct 578 ms 26808 KB Output is correct
55 Correct 602 ms 26912 KB Output is correct
56 Correct 634 ms 27064 KB Output is correct
57 Correct 559 ms 26872 KB Output is correct
58 Correct 504 ms 30812 KB Output is correct
59 Correct 515 ms 31224 KB Output is correct
60 Correct 692 ms 30452 KB Output is correct
61 Correct 619 ms 29816 KB Output is correct
62 Correct 259 ms 46840 KB Output is correct
63 Correct 270 ms 46712 KB Output is correct
64 Correct 262 ms 46584 KB Output is correct
65 Correct 266 ms 46840 KB Output is correct
66 Correct 469 ms 36844 KB Output is correct
67 Correct 421 ms 36848 KB Output is correct
68 Correct 448 ms 36976 KB Output is correct
69 Correct 419 ms 36848 KB Output is correct
70 Correct 419 ms 36720 KB Output is correct
71 Correct 411 ms 36720 KB Output is correct
72 Correct 416 ms 36848 KB Output is correct
73 Correct 502 ms 35824 KB Output is correct
74 Correct 409 ms 36976 KB Output is correct
75 Correct 491 ms 36848 KB Output is correct
76 Correct 595 ms 35376 KB Output is correct
77 Correct 613 ms 32876 KB Output is correct
78 Correct 723 ms 39476 KB Output is correct
79 Correct 709 ms 36916 KB Output is correct
80 Correct 724 ms 45552 KB Output is correct
81 Correct 712 ms 41208 KB Output is correct
82 Correct 699 ms 41388 KB Output is correct
83 Correct 697 ms 37264 KB Output is correct
84 Correct 755 ms 44604 KB Output is correct
85 Correct 747 ms 42664 KB Output is correct
86 Correct 704 ms 36872 KB Output is correct
87 Correct 706 ms 39004 KB Output is correct
88 Correct 643 ms 40312 KB Output is correct
89 Correct 622 ms 35412 KB Output is correct
90 Correct 594 ms 35448 KB Output is correct
91 Correct 607 ms 38136 KB Output is correct
92 Correct 600 ms 36472 KB Output is correct
93 Correct 606 ms 36088 KB Output is correct
94 Correct 614 ms 35320 KB Output is correct
95 Correct 678 ms 37112 KB Output is correct
96 Correct 607 ms 35576 KB Output is correct
97 Correct 606 ms 37880 KB Output is correct