답안 #557879

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
557879 2022-05-06T08:43:41 Z 600Mihnea 수도 (JOI20_capital_city) C++17
100 / 100
621 ms 39124 KB
#include <bits/stdc++.h>

bool home = 1;

using namespace std;

const int N=200000+7;
const int INF=(int)1e9+7;
int n,cntcols,col[N],sub[N],last_active[N],time_step,par[N],last_added[N],tt,print=INF;
vector<int> where_col[N];
vector<int> g[N];
bool blocked[N];
int total;

void build_sub(int a,int p=-1) {
  last_active[a]=time_step;
  sub[a]=1;
  for (auto &b:g[a]) {
    if(b==p||blocked[b]) continue;
    build_sub(b,a);
    sub[a]+=sub[b];
  }
}

int fcen(int a,int p=-1) {
  int mx=total-sub[a];
  for (auto &b:g[a]) {
    if(b==p||blocked[b]) continue;
    mx=max(mx,sub[b]);
  }
  if(mx<=total/2) return a;
  for (auto &b:g[a]) {
    if(b==p||blocked[b]) continue;
    if(sub[b]==mx) return fcen(b,a);
  }
  assert(0);
}

void dfs_par(int a,int p=-1) {
  par[a]=p;
  for (auto &b:g[a]) {
    if(b==p||blocked[b]) continue;
    dfs_par(b,a);
  }
}

void solve(int a) {
  time_step++;
  build_sub(a);
  total=sub[a];

  a=fcen(a);

  queue<int> colors;
  tt++;
  dfs_par(a);

  last_added[col[a]]=tt;
  colors.push(col[a]);
  bool valid=1;
  int sol=0;

  while (!colors.empty()&&valid) {
    int c=colors.front();
    colors.pop();
    for (auto &vertex:where_col[c]) {
      if (last_active[vertex]!=time_step) {
        valid=0;
        break;
      }
      if(vertex==a) continue;
      assert(1<=par[vertex]&&par[vertex]<=n);
      if(last_added[col[par[vertex]]]<tt){
        last_added[col[par[vertex]]]=tt;
        colors.push(col[par[vertex]]);
        sol++;
      }
    }
  }

  if (valid) {
    print=min(print,sol);
  }

  blocked[a]=1;
  for (auto &b:g[a]) {
    if(blocked[b]) continue;
    solve(b);
  }
}

signed main() {
#ifdef ONLINE_JUDGE
  home = 0;
#endif

  home=0;

  if (home) {
    freopen("I_am_iron_man", "r", stdin);
  }
  else {
    ios::sync_with_stdio(0); cin.tie(0);
  }


  cin>>n>>cntcols;
  for (int i=1;i<n;i++) {
    int a,b;
    cin>>a>>b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  for (int i=1;i<=n;i++) {
    cin>>col[i];
    where_col[col[i]].push_back(i);
  }
  solve(1);
  cout<<print<<"\n";
}

Compilation message

capital_city.cpp: In function 'int main()':
capital_city.cpp:100:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  100 |     freopen("I_am_iron_man", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9728 KB Output is correct
3 Correct 7 ms 9748 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 5 ms 9684 KB Output is correct
6 Correct 5 ms 9812 KB Output is correct
7 Correct 6 ms 9684 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 5 ms 9740 KB Output is correct
10 Correct 6 ms 9684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9728 KB Output is correct
3 Correct 7 ms 9748 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 5 ms 9684 KB Output is correct
6 Correct 5 ms 9812 KB Output is correct
7 Correct 6 ms 9684 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 5 ms 9740 KB Output is correct
10 Correct 6 ms 9684 KB Output is correct
11 Correct 6 ms 9812 KB Output is correct
12 Correct 6 ms 9872 KB Output is correct
13 Correct 7 ms 9812 KB Output is correct
14 Correct 7 ms 9812 KB Output is correct
15 Correct 7 ms 9872 KB Output is correct
16 Correct 8 ms 9920 KB Output is correct
17 Correct 7 ms 9940 KB Output is correct
18 Correct 7 ms 9872 KB Output is correct
19 Correct 6 ms 9940 KB Output is correct
20 Correct 6 ms 9940 KB Output is correct
21 Correct 6 ms 9940 KB Output is correct
22 Correct 6 ms 9920 KB Output is correct
23 Correct 7 ms 9940 KB Output is correct
24 Correct 6 ms 9940 KB Output is correct
25 Correct 7 ms 9924 KB Output is correct
26 Correct 7 ms 9940 KB Output is correct
27 Correct 8 ms 9916 KB Output is correct
28 Correct 7 ms 9940 KB Output is correct
29 Correct 7 ms 9940 KB Output is correct
30 Correct 7 ms 9940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 442 ms 38620 KB Output is correct
2 Correct 193 ms 38896 KB Output is correct
3 Correct 428 ms 38356 KB Output is correct
4 Correct 197 ms 39124 KB Output is correct
5 Correct 470 ms 35916 KB Output is correct
6 Correct 187 ms 39020 KB Output is correct
7 Correct 470 ms 36092 KB Output is correct
8 Correct 171 ms 38472 KB Output is correct
9 Correct 559 ms 34420 KB Output is correct
10 Correct 581 ms 32544 KB Output is correct
11 Correct 518 ms 34872 KB Output is correct
12 Correct 545 ms 36832 KB Output is correct
13 Correct 537 ms 32120 KB Output is correct
14 Correct 607 ms 37220 KB Output is correct
15 Correct 557 ms 36896 KB Output is correct
16 Correct 587 ms 32924 KB Output is correct
17 Correct 548 ms 33484 KB Output is correct
18 Correct 558 ms 33616 KB Output is correct
19 Correct 576 ms 36044 KB Output is correct
20 Correct 542 ms 37872 KB Output is correct
21 Correct 5 ms 9684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9728 KB Output is correct
3 Correct 7 ms 9748 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 5 ms 9684 KB Output is correct
6 Correct 5 ms 9812 KB Output is correct
7 Correct 6 ms 9684 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 5 ms 9740 KB Output is correct
10 Correct 6 ms 9684 KB Output is correct
11 Correct 6 ms 9812 KB Output is correct
12 Correct 6 ms 9872 KB Output is correct
13 Correct 7 ms 9812 KB Output is correct
14 Correct 7 ms 9812 KB Output is correct
15 Correct 7 ms 9872 KB Output is correct
16 Correct 8 ms 9920 KB Output is correct
17 Correct 7 ms 9940 KB Output is correct
18 Correct 7 ms 9872 KB Output is correct
19 Correct 6 ms 9940 KB Output is correct
20 Correct 6 ms 9940 KB Output is correct
21 Correct 6 ms 9940 KB Output is correct
22 Correct 6 ms 9920 KB Output is correct
23 Correct 7 ms 9940 KB Output is correct
24 Correct 6 ms 9940 KB Output is correct
25 Correct 7 ms 9924 KB Output is correct
26 Correct 7 ms 9940 KB Output is correct
27 Correct 8 ms 9916 KB Output is correct
28 Correct 7 ms 9940 KB Output is correct
29 Correct 7 ms 9940 KB Output is correct
30 Correct 7 ms 9940 KB Output is correct
31 Correct 442 ms 38620 KB Output is correct
32 Correct 193 ms 38896 KB Output is correct
33 Correct 428 ms 38356 KB Output is correct
34 Correct 197 ms 39124 KB Output is correct
35 Correct 470 ms 35916 KB Output is correct
36 Correct 187 ms 39020 KB Output is correct
37 Correct 470 ms 36092 KB Output is correct
38 Correct 171 ms 38472 KB Output is correct
39 Correct 559 ms 34420 KB Output is correct
40 Correct 581 ms 32544 KB Output is correct
41 Correct 518 ms 34872 KB Output is correct
42 Correct 545 ms 36832 KB Output is correct
43 Correct 537 ms 32120 KB Output is correct
44 Correct 607 ms 37220 KB Output is correct
45 Correct 557 ms 36896 KB Output is correct
46 Correct 587 ms 32924 KB Output is correct
47 Correct 548 ms 33484 KB Output is correct
48 Correct 558 ms 33616 KB Output is correct
49 Correct 576 ms 36044 KB Output is correct
50 Correct 542 ms 37872 KB Output is correct
51 Correct 5 ms 9684 KB Output is correct
52 Correct 404 ms 24660 KB Output is correct
53 Correct 457 ms 24656 KB Output is correct
54 Correct 439 ms 24784 KB Output is correct
55 Correct 409 ms 24636 KB Output is correct
56 Correct 419 ms 24592 KB Output is correct
57 Correct 396 ms 24620 KB Output is correct
58 Correct 406 ms 28048 KB Output is correct
59 Correct 309 ms 28236 KB Output is correct
60 Correct 460 ms 27764 KB Output is correct
61 Correct 470 ms 27524 KB Output is correct
62 Correct 181 ms 39016 KB Output is correct
63 Correct 169 ms 38920 KB Output is correct
64 Correct 173 ms 38752 KB Output is correct
65 Correct 205 ms 39040 KB Output is correct
66 Correct 268 ms 31808 KB Output is correct
67 Correct 321 ms 31800 KB Output is correct
68 Correct 283 ms 31896 KB Output is correct
69 Correct 280 ms 31936 KB Output is correct
70 Correct 320 ms 31824 KB Output is correct
71 Correct 290 ms 31816 KB Output is correct
72 Correct 289 ms 31824 KB Output is correct
73 Correct 301 ms 31172 KB Output is correct
74 Correct 317 ms 31920 KB Output is correct
75 Correct 309 ms 31872 KB Output is correct
76 Correct 461 ms 31096 KB Output is correct
77 Correct 513 ms 29540 KB Output is correct
78 Correct 621 ms 33180 KB Output is correct
79 Correct 612 ms 31656 KB Output is correct
80 Correct 543 ms 37472 KB Output is correct
81 Correct 512 ms 34552 KB Output is correct
82 Correct 600 ms 34680 KB Output is correct
83 Correct 517 ms 32000 KB Output is correct
84 Correct 560 ms 36752 KB Output is correct
85 Correct 579 ms 35504 KB Output is correct
86 Correct 572 ms 31616 KB Output is correct
87 Correct 495 ms 33060 KB Output is correct
88 Correct 481 ms 34076 KB Output is correct
89 Correct 470 ms 30760 KB Output is correct
90 Correct 408 ms 30540 KB Output is correct
91 Correct 486 ms 32652 KB Output is correct
92 Correct 503 ms 31624 KB Output is correct
93 Correct 417 ms 31312 KB Output is correct
94 Correct 453 ms 30676 KB Output is correct
95 Correct 493 ms 31876 KB Output is correct
96 Correct 529 ms 30924 KB Output is correct
97 Correct 459 ms 32560 KB Output is correct