Submission #315355

# Submission time Handle Problem Language Result Execution time Memory
315355 2020-10-22T15:13:05 Z georgerapeanu Capital City (JOI20_capital_city) C++11
100 / 100
653 ms 40956 KB
#include <bits/stdc++.h>

using namespace std;

const int NMAX = 200000;

int n,k;
int color[NMAX + 5];
vector<int> nodes[NMAX + 5];
vector<int> graph[NMAX + 5];

bool inCentroid[NMAX + 5];
int weight[NMAX + 5];
int father[NMAX + 5];
bool viz[NMAX + 5];
int group[NMAX + 5];
int processed[NMAX + 5];
int global_ans = 1e9;

void predfs(int nod,int tata,int centroid){
    weight[nod] = 1;
    processed[color[nod]] = false;
    viz[nod] = 0;
    group[nod] = centroid;
    father[nod] = tata;

    for(auto it:graph[nod]){
        if(it == tata || inCentroid[it]){
            continue;            
        }
        predfs(it,nod,centroid);
        weight[nod] += weight[it];
    }


}

void dfs(int nod){
    int _father = 0;
    int centroid = nod;

    while(true){
        int bigChild = -1;

        for(auto it:graph[centroid]){
            if(it == _father || inCentroid[it]){
                continue;
            }
            if(bigChild == -1 || weight[bigChild] < weight[it]){
                bigChild = it;
            }
        }
        
        if(weight[bigChild] * 2 > weight[nod]){
            _father = centroid;
            centroid = bigChild;
        }
        else{
            break;    
        }
    }

    predfs(centroid,0,centroid);
    vector<int> colors = {color[centroid]};
    inCentroid[centroid] = true;

    int ans = -1;

    processed[colors.back()] = true;

    while(colors.size()){
        ans++;
        int col = colors.back();
        colors.pop_back();

        for(auto it:nodes[col]){
            if(group[it] != centroid){
                ans = 1e9;
                break;
            }
            int nod = it;
            while(nod && viz[nod] == 0){
                viz[nod] = 1;
                if(processed[color[nod]] == 0){
                    colors.push_back(color[nod]);
                    processed[color[nod]] = true;
                }
                nod = father[nod];
            }
        }

        if(ans == 1e9){
            break;
        }
    }

    global_ans = min(global_ans,ans);

    for(auto it:graph[centroid]){
        if(inCentroid[it] == 0){
            dfs(it);
        }
    }
}

int main(){

    scanf("%d %d",&n,&k);

    for(int i = 1;i < n;i++){
        int x,y;
        scanf("%d %d",&x,&y);
        graph[x].push_back(y);
        graph[y].push_back(x);
    }

    for(int i = 1;i <= n;i++){
        scanf("%d",&color[i]);
        nodes[color[i]].push_back(i);
    }

    predfs(1,0,0);
    dfs(1);

    printf("%d\n",global_ans);    

    return 0;
}

Compilation message

capital_city.cpp: In function 'int main()':
capital_city.cpp:108:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  108 |     scanf("%d %d",&n,&k);
      |     ~~~~~^~~~~~~~~~~~~~~
capital_city.cpp:112:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  112 |         scanf("%d %d",&x,&y);
      |         ~~~~~^~~~~~~~~~~~~~~
capital_city.cpp:118:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  118 |         scanf("%d",&color[i]);
      |         ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 7 ms 9728 KB Output is correct
3 Correct 7 ms 9728 KB Output is correct
4 Correct 7 ms 9808 KB Output is correct
5 Correct 7 ms 9728 KB Output is correct
6 Correct 7 ms 9728 KB Output is correct
7 Correct 7 ms 9728 KB Output is correct
8 Correct 7 ms 9728 KB Output is correct
9 Correct 7 ms 9728 KB Output is correct
10 Correct 8 ms 9728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 7 ms 9728 KB Output is correct
3 Correct 7 ms 9728 KB Output is correct
4 Correct 7 ms 9808 KB Output is correct
5 Correct 7 ms 9728 KB Output is correct
6 Correct 7 ms 9728 KB Output is correct
7 Correct 7 ms 9728 KB Output is correct
8 Correct 7 ms 9728 KB Output is correct
9 Correct 7 ms 9728 KB Output is correct
10 Correct 8 ms 9728 KB Output is correct
11 Correct 9 ms 9856 KB Output is correct
12 Correct 8 ms 9856 KB Output is correct
13 Correct 8 ms 9856 KB Output is correct
14 Correct 8 ms 9856 KB Output is correct
15 Correct 9 ms 9856 KB Output is correct
16 Correct 9 ms 9912 KB Output is correct
17 Correct 9 ms 9984 KB Output is correct
18 Correct 8 ms 9984 KB Output is correct
19 Correct 8 ms 9984 KB Output is correct
20 Correct 8 ms 9984 KB Output is correct
21 Correct 8 ms 9984 KB Output is correct
22 Correct 8 ms 9984 KB Output is correct
23 Correct 9 ms 9984 KB Output is correct
24 Correct 9 ms 9856 KB Output is correct
25 Correct 9 ms 9984 KB Output is correct
26 Correct 9 ms 9984 KB Output is correct
27 Correct 9 ms 10112 KB Output is correct
28 Correct 9 ms 9984 KB Output is correct
29 Correct 9 ms 9984 KB Output is correct
30 Correct 9 ms 9984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 577 ms 38264 KB Output is correct
2 Correct 206 ms 38648 KB Output is correct
3 Correct 586 ms 38008 KB Output is correct
4 Correct 212 ms 38776 KB Output is correct
5 Correct 590 ms 35064 KB Output is correct
6 Correct 211 ms 38648 KB Output is correct
7 Correct 584 ms 35452 KB Output is correct
8 Correct 203 ms 38392 KB Output is correct
9 Correct 653 ms 33600 KB Output is correct
10 Correct 629 ms 31224 KB Output is correct
11 Correct 641 ms 34032 KB Output is correct
12 Correct 630 ms 36728 KB Output is correct
13 Correct 628 ms 30712 KB Output is correct
14 Correct 630 ms 37012 KB Output is correct
15 Correct 627 ms 36744 KB Output is correct
16 Correct 635 ms 31792 KB Output is correct
17 Correct 613 ms 32120 KB Output is correct
18 Correct 617 ms 32504 KB Output is correct
19 Correct 626 ms 35704 KB Output is correct
20 Correct 630 ms 37908 KB Output is correct
21 Correct 6 ms 9728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 7 ms 9728 KB Output is correct
3 Correct 7 ms 9728 KB Output is correct
4 Correct 7 ms 9808 KB Output is correct
5 Correct 7 ms 9728 KB Output is correct
6 Correct 7 ms 9728 KB Output is correct
7 Correct 7 ms 9728 KB Output is correct
8 Correct 7 ms 9728 KB Output is correct
9 Correct 7 ms 9728 KB Output is correct
10 Correct 8 ms 9728 KB Output is correct
11 Correct 9 ms 9856 KB Output is correct
12 Correct 8 ms 9856 KB Output is correct
13 Correct 8 ms 9856 KB Output is correct
14 Correct 8 ms 9856 KB Output is correct
15 Correct 9 ms 9856 KB Output is correct
16 Correct 9 ms 9912 KB Output is correct
17 Correct 9 ms 9984 KB Output is correct
18 Correct 8 ms 9984 KB Output is correct
19 Correct 8 ms 9984 KB Output is correct
20 Correct 8 ms 9984 KB Output is correct
21 Correct 8 ms 9984 KB Output is correct
22 Correct 8 ms 9984 KB Output is correct
23 Correct 9 ms 9984 KB Output is correct
24 Correct 9 ms 9856 KB Output is correct
25 Correct 9 ms 9984 KB Output is correct
26 Correct 9 ms 9984 KB Output is correct
27 Correct 9 ms 10112 KB Output is correct
28 Correct 9 ms 9984 KB Output is correct
29 Correct 9 ms 9984 KB Output is correct
30 Correct 9 ms 9984 KB Output is correct
31 Correct 577 ms 38264 KB Output is correct
32 Correct 206 ms 38648 KB Output is correct
33 Correct 586 ms 38008 KB Output is correct
34 Correct 212 ms 38776 KB Output is correct
35 Correct 590 ms 35064 KB Output is correct
36 Correct 211 ms 38648 KB Output is correct
37 Correct 584 ms 35452 KB Output is correct
38 Correct 203 ms 38392 KB Output is correct
39 Correct 653 ms 33600 KB Output is correct
40 Correct 629 ms 31224 KB Output is correct
41 Correct 641 ms 34032 KB Output is correct
42 Correct 630 ms 36728 KB Output is correct
43 Correct 628 ms 30712 KB Output is correct
44 Correct 630 ms 37012 KB Output is correct
45 Correct 627 ms 36744 KB Output is correct
46 Correct 635 ms 31792 KB Output is correct
47 Correct 613 ms 32120 KB Output is correct
48 Correct 617 ms 32504 KB Output is correct
49 Correct 626 ms 35704 KB Output is correct
50 Correct 630 ms 37908 KB Output is correct
51 Correct 6 ms 9728 KB Output is correct
52 Correct 492 ms 21496 KB Output is correct
53 Correct 495 ms 21540 KB Output is correct
54 Correct 502 ms 21496 KB Output is correct
55 Correct 503 ms 21496 KB Output is correct
56 Correct 507 ms 21496 KB Output is correct
57 Correct 496 ms 21624 KB Output is correct
58 Correct 501 ms 25208 KB Output is correct
59 Correct 472 ms 25516 KB Output is correct
60 Correct 571 ms 24824 KB Output is correct
61 Correct 586 ms 24568 KB Output is correct
62 Correct 210 ms 38648 KB Output is correct
63 Correct 216 ms 38776 KB Output is correct
64 Correct 209 ms 38392 KB Output is correct
65 Correct 205 ms 38648 KB Output is correct
66 Correct 394 ms 29908 KB Output is correct
67 Correct 382 ms 33520 KB Output is correct
68 Correct 393 ms 33604 KB Output is correct
69 Correct 384 ms 33676 KB Output is correct
70 Correct 399 ms 33520 KB Output is correct
71 Correct 386 ms 33644 KB Output is correct
72 Correct 401 ms 33736 KB Output is correct
73 Correct 383 ms 32876 KB Output is correct
74 Correct 372 ms 33904 KB Output is correct
75 Correct 391 ms 33772 KB Output is correct
76 Correct 595 ms 32504 KB Output is correct
77 Correct 561 ms 30712 KB Output is correct
78 Correct 599 ms 35852 KB Output is correct
79 Correct 626 ms 33856 KB Output is correct
80 Correct 620 ms 40956 KB Output is correct
81 Correct 618 ms 37384 KB Output is correct
82 Correct 616 ms 37624 KB Output is correct
83 Correct 613 ms 34196 KB Output is correct
84 Correct 639 ms 40340 KB Output is correct
85 Correct 614 ms 38544 KB Output is correct
86 Correct 615 ms 33832 KB Output is correct
87 Correct 613 ms 35464 KB Output is correct
88 Correct 572 ms 36604 KB Output is correct
89 Correct 547 ms 32504 KB Output is correct
90 Correct 533 ms 32248 KB Output is correct
91 Correct 553 ms 34680 KB Output is correct
92 Correct 553 ms 33400 KB Output is correct
93 Correct 549 ms 33016 KB Output is correct
94 Correct 549 ms 32504 KB Output is correct
95 Correct 548 ms 33940 KB Output is correct
96 Correct 562 ms 32760 KB Output is correct
97 Correct 549 ms 34680 KB Output is correct