Submission #216891

# Submission time Handle Problem Language Result Execution time Memory
216891 2020-03-28T09:46:03 Z _7_7_ Capital City (JOI20_capital_city) C++14
100 / 100
801 ms 36012 KB
#include <bits/stdc++.h>                                           
 
//#define int long long
//#pragma GCC optimize("Ofast")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
 
 
#define file(s) freopen(s".in","r",stdin); freopen(s".out","w",stdout);
#define forev(i, b, a) for(int i = (b); i >= (a); --i)
#define forn(i, a, b) for(int i = (a); i <= (b); ++i)
#define all(x) x.begin(), x.end()
#define sz(s) (int)s.size()
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define s second
#define f first
 
 
using namespace std;
 
 
typedef pair < long long, long long > pll;    
typedef pair < int, int > pii;
typedef unsigned long long ull;         
typedef vector < pii > vpii;
typedef vector < int > vi;
typedef long double ldb;  
typedef long long ll;  
typedef double db;                             
 
 
const int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1}, block = 555;
const pii base = mp(1171, 3307), Mod = mp(1e9 + 7, 1e9 + 9);
const int inf = 1e9, maxn = 4e5 + 148, mod = 1e9 + 7, N = 2e5 + 53;
const db eps = 1e-12, pi = 3.14159265359;
const ll INF = 1e18;

int n, k, a, b, c[N], d[N], cnt[N], par[N], ans = inf, Sz;
bool was[N], ok, used[N], us[N], was1[N];
vi g[N], gg[N], cur;
queue < int > q;


void dfs (int v, int p = 0) {
    cur.pb(v);
    cnt[v] = 1;
    
    for (auto to : g[v])
        if (to != p && !was[to]) {
            dfs(to, v);
            cnt[v] += cnt[to];
        }
}

int Find (int v) {
    for (auto to : g[v])
        if (!was[to] && cnt[to] < cnt[v] && Sz - cnt[to] < cnt[to]) 
            return Find(to);
        
    return v;            
}

void dfs1 (int v, int p = 0) {
    par[v] = p;
    
    for (auto to : g[v])
        if (!was[to] && to != p)
            dfs1(to, v);
}


void dfs2 (int v) {
    was1[v] = 1;
    for (auto to : g[v])
        if (!was[to] && !was1[to])
            dfs2(to);
}

void Centroid (int v) {
    cur.clear();
    
    dfs(v);
    for (auto i : cur) {
        used[i] = us[c[i]] = 0;
        was1[i] = 0;
        gg[c[i]].clear();
    }
    
    for (auto i : cur)
        gg[c[i]].pb(i);
    
    Sz = cnt[v];
    v = Find(v);
    dfs1(v);
    
    q.push(c[v]);
    used[v] = 1;
    us[c[v]] = 1;
    
    int res = 0;
    ok = 1;
    while (sz(q)) {
        ++res;        
        int C = q.front();
        if (sz(gg[C]) != d[C]) 
            ok = 0;
        
        q.pop();
        
        for (auto i : gg[C]) {
            while (!used[i]) {
                used[i] = 1;
                if (!us[c[i]]) {
                    us[c[i]] = 1;
                    q.push(c[i]);
                }
                i = par[i];
            }
        }
    }
    
    if (ok) 
        ans = min(ans, res - 1);
    for (auto i : gg[c[v]])
        was[i] = 1;
    
    vi roots;
    for (auto i : cur)
        if (!was[i] && !was1[i]) {
            dfs2(i); 
            roots.pb(i);
        }
    for (auto i : roots)
        Centroid(i);
}


main () {
    scanf("%d%d", &n, &k);
    for (int i = 1; i < n; ++i) {
        scanf("%d%d", &a, &b);
        g[a].pb(b);
        g[b].pb(a);
    }
    
    for (int i = 1; i <= n; ++i) {
        scanf("%d", &c[i]); 
        ++d[c[i]];
    }
    
    Centroid(1);
    cout << ans << endl;
}

Compilation message

capital_city.cpp:140:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main () {
       ^
capital_city.cpp: In function 'int main()':
capital_city.cpp:141:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d", &n, &k);
     ~~~~~^~~~~~~~~~~~~~~~
capital_city.cpp:143:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d", &a, &b);
         ~~~~~^~~~~~~~~~~~~~~~
capital_city.cpp:149:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &c[i]); 
         ~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9728 KB Output is correct
2 Correct 10 ms 9728 KB Output is correct
3 Correct 10 ms 9728 KB Output is correct
4 Correct 10 ms 9728 KB Output is correct
5 Correct 10 ms 9728 KB Output is correct
6 Correct 11 ms 9728 KB Output is correct
7 Correct 10 ms 9728 KB Output is correct
8 Correct 10 ms 9728 KB Output is correct
9 Correct 10 ms 9728 KB Output is correct
10 Correct 11 ms 9728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9728 KB Output is correct
2 Correct 10 ms 9728 KB Output is correct
3 Correct 10 ms 9728 KB Output is correct
4 Correct 10 ms 9728 KB Output is correct
5 Correct 10 ms 9728 KB Output is correct
6 Correct 11 ms 9728 KB Output is correct
7 Correct 10 ms 9728 KB Output is correct
8 Correct 10 ms 9728 KB Output is correct
9 Correct 10 ms 9728 KB Output is correct
10 Correct 11 ms 9728 KB Output is correct
11 Correct 12 ms 9856 KB Output is correct
12 Correct 12 ms 9856 KB Output is correct
13 Correct 12 ms 9856 KB Output is correct
14 Correct 13 ms 9856 KB Output is correct
15 Correct 12 ms 9856 KB Output is correct
16 Correct 12 ms 9856 KB Output is correct
17 Correct 11 ms 9984 KB Output is correct
18 Correct 11 ms 9984 KB Output is correct
19 Correct 11 ms 9984 KB Output is correct
20 Correct 13 ms 9984 KB Output is correct
21 Correct 13 ms 9984 KB Output is correct
22 Correct 12 ms 9984 KB Output is correct
23 Correct 13 ms 9984 KB Output is correct
24 Correct 12 ms 9856 KB Output is correct
25 Correct 12 ms 9984 KB Output is correct
26 Correct 12 ms 9984 KB Output is correct
27 Correct 12 ms 9984 KB Output is correct
28 Correct 13 ms 9984 KB Output is correct
29 Correct 12 ms 9984 KB Output is correct
30 Correct 13 ms 9984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 799 ms 35500 KB Output is correct
2 Correct 308 ms 35816 KB Output is correct
3 Correct 801 ms 35304 KB Output is correct
4 Correct 301 ms 35816 KB Output is correct
5 Correct 732 ms 32928 KB Output is correct
6 Correct 309 ms 35816 KB Output is correct
7 Correct 671 ms 33000 KB Output is correct
8 Correct 288 ms 35432 KB Output is correct
9 Correct 337 ms 31340 KB Output is correct
10 Correct 425 ms 29440 KB Output is correct
11 Correct 429 ms 31720 KB Output is correct
12 Correct 551 ms 34008 KB Output is correct
13 Correct 563 ms 29164 KB Output is correct
14 Correct 465 ms 34028 KB Output is correct
15 Correct 519 ms 33900 KB Output is correct
16 Correct 636 ms 29804 KB Output is correct
17 Correct 486 ms 30188 KB Output is correct
18 Correct 356 ms 30576 KB Output is correct
19 Correct 403 ms 32876 KB Output is correct
20 Correct 419 ms 34816 KB Output is correct
21 Correct 10 ms 9728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9728 KB Output is correct
2 Correct 10 ms 9728 KB Output is correct
3 Correct 10 ms 9728 KB Output is correct
4 Correct 10 ms 9728 KB Output is correct
5 Correct 10 ms 9728 KB Output is correct
6 Correct 11 ms 9728 KB Output is correct
7 Correct 10 ms 9728 KB Output is correct
8 Correct 10 ms 9728 KB Output is correct
9 Correct 10 ms 9728 KB Output is correct
10 Correct 11 ms 9728 KB Output is correct
11 Correct 12 ms 9856 KB Output is correct
12 Correct 12 ms 9856 KB Output is correct
13 Correct 12 ms 9856 KB Output is correct
14 Correct 13 ms 9856 KB Output is correct
15 Correct 12 ms 9856 KB Output is correct
16 Correct 12 ms 9856 KB Output is correct
17 Correct 11 ms 9984 KB Output is correct
18 Correct 11 ms 9984 KB Output is correct
19 Correct 11 ms 9984 KB Output is correct
20 Correct 13 ms 9984 KB Output is correct
21 Correct 13 ms 9984 KB Output is correct
22 Correct 12 ms 9984 KB Output is correct
23 Correct 13 ms 9984 KB Output is correct
24 Correct 12 ms 9856 KB Output is correct
25 Correct 12 ms 9984 KB Output is correct
26 Correct 12 ms 9984 KB Output is correct
27 Correct 12 ms 9984 KB Output is correct
28 Correct 13 ms 9984 KB Output is correct
29 Correct 12 ms 9984 KB Output is correct
30 Correct 13 ms 9984 KB Output is correct
31 Correct 799 ms 35500 KB Output is correct
32 Correct 308 ms 35816 KB Output is correct
33 Correct 801 ms 35304 KB Output is correct
34 Correct 301 ms 35816 KB Output is correct
35 Correct 732 ms 32928 KB Output is correct
36 Correct 309 ms 35816 KB Output is correct
37 Correct 671 ms 33000 KB Output is correct
38 Correct 288 ms 35432 KB Output is correct
39 Correct 337 ms 31340 KB Output is correct
40 Correct 425 ms 29440 KB Output is correct
41 Correct 429 ms 31720 KB Output is correct
42 Correct 551 ms 34008 KB Output is correct
43 Correct 563 ms 29164 KB Output is correct
44 Correct 465 ms 34028 KB Output is correct
45 Correct 519 ms 33900 KB Output is correct
46 Correct 636 ms 29804 KB Output is correct
47 Correct 486 ms 30188 KB Output is correct
48 Correct 356 ms 30576 KB Output is correct
49 Correct 403 ms 32876 KB Output is correct
50 Correct 419 ms 34816 KB Output is correct
51 Correct 10 ms 9728 KB Output is correct
52 Correct 621 ms 21736 KB Output is correct
53 Correct 661 ms 21608 KB Output is correct
54 Correct 636 ms 21612 KB Output is correct
55 Correct 664 ms 21628 KB Output is correct
56 Correct 665 ms 21612 KB Output is correct
57 Correct 647 ms 21740 KB Output is correct
58 Correct 631 ms 24624 KB Output is correct
59 Correct 578 ms 24988 KB Output is correct
60 Correct 750 ms 24684 KB Output is correct
61 Correct 785 ms 24440 KB Output is correct
62 Correct 301 ms 35820 KB Output is correct
63 Correct 311 ms 36012 KB Output is correct
64 Correct 277 ms 35688 KB Output is correct
65 Correct 299 ms 35816 KB Output is correct
66 Correct 204 ms 29544 KB Output is correct
67 Correct 198 ms 29672 KB Output is correct
68 Correct 203 ms 29420 KB Output is correct
69 Correct 212 ms 29800 KB Output is correct
70 Correct 201 ms 29416 KB Output is correct
71 Correct 200 ms 29420 KB Output is correct
72 Correct 209 ms 29552 KB Output is correct
73 Correct 200 ms 28780 KB Output is correct
74 Correct 192 ms 29420 KB Output is correct
75 Correct 222 ms 29676 KB Output is correct
76 Correct 651 ms 27884 KB Output is correct
77 Correct 655 ms 26476 KB Output is correct
78 Correct 409 ms 30312 KB Output is correct
79 Correct 529 ms 28520 KB Output is correct
80 Correct 508 ms 34284 KB Output is correct
81 Correct 381 ms 31464 KB Output is correct
82 Correct 587 ms 31724 KB Output is correct
83 Correct 565 ms 29032 KB Output is correct
84 Correct 593 ms 33608 KB Output is correct
85 Correct 499 ms 32364 KB Output is correct
86 Correct 507 ms 28652 KB Output is correct
87 Correct 449 ms 29932 KB Output is correct
88 Correct 410 ms 30960 KB Output is correct
89 Correct 330 ms 27760 KB Output is correct
90 Correct 279 ms 28268 KB Output is correct
91 Correct 346 ms 29552 KB Output is correct
92 Correct 344 ms 28528 KB Output is correct
93 Correct 358 ms 28140 KB Output is correct
94 Correct 312 ms 28016 KB Output is correct
95 Correct 351 ms 28912 KB Output is correct
96 Correct 332 ms 27760 KB Output is correct
97 Correct 299 ms 29424 KB Output is correct