답안 #444642

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
444642 2021-07-14T13:51:33 Z ivan_tudor 수도 (JOI20_capital_city) C++14
100 / 100
847 ms 41872 KB
#include<bits/stdc++.h>
using namespace std;
const int N = 200005;
int sz[N];
bool wasC[N];
vector<int> gr[N];
int par[N];
int inc[N];
vector<int> col[N];
bool usedc[N];
int cl[N];
void dfs_prec(int nod, int dad, int val){
  sz[nod] = 1;

  par[nod] = dad;
  inc[nod] = val;
  usedc[cl[nod]] = false;

  for(auto x:gr[nod]){
    if(x!=dad && wasC[x] == false){
      dfs_prec(x, nod, val);
      sz[nod]+=sz[x];
    }
  }
}
int find_centro(int nod, int dad, int n){
  for(auto x:gr[nod]){
    if(x!=dad && wasC[x] == false && sz[x] > n/2)
      return find_centro(x, nod, n);
  }
  return nod;
}
int ans = N;
int k;
void centr_deco(int nod, int &cnt){
  dfs_prec(nod, 0, ++cnt);
  int C = find_centro(nod, 0, sz[nod]);
  dfs_prec(C, 0, cnt);
  queue<int> q;
  bool ok = true;
  for(auto x:col[cl[C]]){
    if(inc[x] != cnt){
      ok = false;
      break;
    }
    q.push(x);
  }
  usedc[cl[C]] = true;
  int cans = 1;
  while(q.size() && ok){
    int nod = q.front();
    q.pop();
    int colour = cl[par[nod]];
    if(colour != 0 && usedc[colour] == false){
      for(auto x:col[colour]){
        if(inc[x] != cnt){
          ok = false;
          break;
        }
        q.push(x);
      }
      usedc[colour] = true;
      cans++;
    }
  }
  if(ok == true){
    ans = min(ans, cans - 1);
  }
  wasC[C] = true;
  for(auto x:gr[C]){
    if(wasC[x] == false){
      centr_deco(x, cnt);
    }
  }
}
int main()
{
  //freopen(".in","r",stdin);
  ios::sync_with_stdio(false);
  cin.tie(0),cout.tie(0);
  int n;
  cin>>n>>k;
  for(int i =1 ; i <n; i++){
    int a, b;
    cin>>a>>b;
    gr[a].push_back(b);
    gr[b].push_back(a);
  }
  for(int i = 1; i <=n; i++){
    cin>>cl[i];
    col[cl[i]].push_back(i);
  }
  int aux = 0;
  centr_deco(1, aux);
  cout<<ans;
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9676 KB Output is correct
2 Correct 5 ms 9676 KB Output is correct
3 Correct 5 ms 9676 KB Output is correct
4 Correct 5 ms 9700 KB Output is correct
5 Correct 5 ms 9676 KB Output is correct
6 Correct 6 ms 9720 KB Output is correct
7 Correct 6 ms 9676 KB Output is correct
8 Correct 6 ms 9676 KB Output is correct
9 Correct 5 ms 9676 KB Output is correct
10 Correct 5 ms 9692 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9676 KB Output is correct
2 Correct 5 ms 9676 KB Output is correct
3 Correct 5 ms 9676 KB Output is correct
4 Correct 5 ms 9700 KB Output is correct
5 Correct 5 ms 9676 KB Output is correct
6 Correct 6 ms 9720 KB Output is correct
7 Correct 6 ms 9676 KB Output is correct
8 Correct 6 ms 9676 KB Output is correct
9 Correct 5 ms 9676 KB Output is correct
10 Correct 5 ms 9692 KB Output is correct
11 Correct 7 ms 9804 KB Output is correct
12 Correct 7 ms 9804 KB Output is correct
13 Correct 7 ms 9864 KB Output is correct
14 Correct 8 ms 9804 KB Output is correct
15 Correct 8 ms 9860 KB Output is correct
16 Correct 9 ms 9804 KB Output is correct
17 Correct 8 ms 9932 KB Output is correct
18 Correct 9 ms 9932 KB Output is correct
19 Correct 8 ms 9932 KB Output is correct
20 Correct 8 ms 9932 KB Output is correct
21 Correct 9 ms 9860 KB Output is correct
22 Correct 7 ms 9932 KB Output is correct
23 Correct 9 ms 9972 KB Output is correct
24 Correct 10 ms 9932 KB Output is correct
25 Correct 8 ms 9992 KB Output is correct
26 Correct 8 ms 9932 KB Output is correct
27 Correct 8 ms 9932 KB Output is correct
28 Correct 8 ms 9932 KB Output is correct
29 Correct 8 ms 9956 KB Output is correct
30 Correct 8 ms 9932 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 667 ms 37664 KB Output is correct
2 Correct 226 ms 41804 KB Output is correct
3 Correct 667 ms 40900 KB Output is correct
4 Correct 249 ms 41828 KB Output is correct
5 Correct 648 ms 38064 KB Output is correct
6 Correct 246 ms 41828 KB Output is correct
7 Correct 635 ms 38388 KB Output is correct
8 Correct 232 ms 41744 KB Output is correct
9 Correct 762 ms 36700 KB Output is correct
10 Correct 777 ms 34376 KB Output is correct
11 Correct 796 ms 37152 KB Output is correct
12 Correct 767 ms 39748 KB Output is correct
13 Correct 714 ms 33808 KB Output is correct
14 Correct 752 ms 40040 KB Output is correct
15 Correct 761 ms 39672 KB Output is correct
16 Correct 780 ms 34768 KB Output is correct
17 Correct 827 ms 35296 KB Output is correct
18 Correct 817 ms 35532 KB Output is correct
19 Correct 803 ms 38592 KB Output is correct
20 Correct 737 ms 41064 KB Output is correct
21 Correct 6 ms 9676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9676 KB Output is correct
2 Correct 5 ms 9676 KB Output is correct
3 Correct 5 ms 9676 KB Output is correct
4 Correct 5 ms 9700 KB Output is correct
5 Correct 5 ms 9676 KB Output is correct
6 Correct 6 ms 9720 KB Output is correct
7 Correct 6 ms 9676 KB Output is correct
8 Correct 6 ms 9676 KB Output is correct
9 Correct 5 ms 9676 KB Output is correct
10 Correct 5 ms 9692 KB Output is correct
11 Correct 7 ms 9804 KB Output is correct
12 Correct 7 ms 9804 KB Output is correct
13 Correct 7 ms 9864 KB Output is correct
14 Correct 8 ms 9804 KB Output is correct
15 Correct 8 ms 9860 KB Output is correct
16 Correct 9 ms 9804 KB Output is correct
17 Correct 8 ms 9932 KB Output is correct
18 Correct 9 ms 9932 KB Output is correct
19 Correct 8 ms 9932 KB Output is correct
20 Correct 8 ms 9932 KB Output is correct
21 Correct 9 ms 9860 KB Output is correct
22 Correct 7 ms 9932 KB Output is correct
23 Correct 9 ms 9972 KB Output is correct
24 Correct 10 ms 9932 KB Output is correct
25 Correct 8 ms 9992 KB Output is correct
26 Correct 8 ms 9932 KB Output is correct
27 Correct 8 ms 9932 KB Output is correct
28 Correct 8 ms 9932 KB Output is correct
29 Correct 8 ms 9956 KB Output is correct
30 Correct 8 ms 9932 KB Output is correct
31 Correct 667 ms 37664 KB Output is correct
32 Correct 226 ms 41804 KB Output is correct
33 Correct 667 ms 40900 KB Output is correct
34 Correct 249 ms 41828 KB Output is correct
35 Correct 648 ms 38064 KB Output is correct
36 Correct 246 ms 41828 KB Output is correct
37 Correct 635 ms 38388 KB Output is correct
38 Correct 232 ms 41744 KB Output is correct
39 Correct 762 ms 36700 KB Output is correct
40 Correct 777 ms 34376 KB Output is correct
41 Correct 796 ms 37152 KB Output is correct
42 Correct 767 ms 39748 KB Output is correct
43 Correct 714 ms 33808 KB Output is correct
44 Correct 752 ms 40040 KB Output is correct
45 Correct 761 ms 39672 KB Output is correct
46 Correct 780 ms 34768 KB Output is correct
47 Correct 827 ms 35296 KB Output is correct
48 Correct 817 ms 35532 KB Output is correct
49 Correct 803 ms 38592 KB Output is correct
50 Correct 737 ms 41064 KB Output is correct
51 Correct 6 ms 9676 KB Output is correct
52 Correct 584 ms 25112 KB Output is correct
53 Correct 531 ms 25100 KB Output is correct
54 Correct 598 ms 25108 KB Output is correct
55 Correct 545 ms 25112 KB Output is correct
56 Correct 619 ms 25152 KB Output is correct
57 Correct 540 ms 25192 KB Output is correct
58 Correct 572 ms 28312 KB Output is correct
59 Correct 549 ms 28620 KB Output is correct
60 Correct 663 ms 27920 KB Output is correct
61 Correct 716 ms 27592 KB Output is correct
62 Correct 242 ms 41828 KB Output is correct
63 Correct 254 ms 41724 KB Output is correct
64 Correct 236 ms 41872 KB Output is correct
65 Correct 229 ms 41828 KB Output is correct
66 Correct 502 ms 33668 KB Output is correct
67 Correct 401 ms 33532 KB Output is correct
68 Correct 451 ms 33628 KB Output is correct
69 Correct 422 ms 33644 KB Output is correct
70 Correct 459 ms 33580 KB Output is correct
71 Correct 454 ms 33596 KB Output is correct
72 Correct 492 ms 33580 KB Output is correct
73 Correct 524 ms 32796 KB Output is correct
74 Correct 401 ms 33592 KB Output is correct
75 Correct 473 ms 33528 KB Output is correct
76 Correct 564 ms 31872 KB Output is correct
77 Correct 595 ms 30008 KB Output is correct
78 Correct 821 ms 35344 KB Output is correct
79 Correct 781 ms 33312 KB Output is correct
80 Correct 773 ms 40512 KB Output is correct
81 Correct 731 ms 36804 KB Output is correct
82 Correct 754 ms 37168 KB Output is correct
83 Correct 769 ms 33672 KB Output is correct
84 Correct 778 ms 39564 KB Output is correct
85 Correct 783 ms 37936 KB Output is correct
86 Correct 847 ms 33180 KB Output is correct
87 Correct 695 ms 35080 KB Output is correct
88 Correct 701 ms 36164 KB Output is correct
89 Correct 743 ms 32068 KB Output is correct
90 Correct 737 ms 32068 KB Output is correct
91 Correct 722 ms 34380 KB Output is correct
92 Correct 650 ms 32944 KB Output is correct
93 Correct 666 ms 32768 KB Output is correct
94 Correct 697 ms 32132 KB Output is correct
95 Correct 734 ms 33700 KB Output is correct
96 Correct 671 ms 32128 KB Output is correct
97 Correct 686 ms 34372 KB Output is correct