답안 #120152

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
120152 2019-06-23T15:10:56 Z Just_Solve_The_Problem Unique Cities (JOI19_ho_t5) C++11
100 / 100
715 ms 49252 KB
#include <bits/stdc++.h>

#define ll long long
#define ok puts("OK");

using namespace std;

const int N = (int)2e5 + 7;

int n, m, A, B;
vector <int> gr[N];
int c[N], h[N], mxhe[N], cnt[N], ans[N];
vector <int> st;

int dfs1(int v, int pr) {
  int big = v;
  for (int to : gr[v]) {
    if (to == pr) continue;
    h[to] = h[v] + 1;
    int temp = dfs1(to, v);
    if (big == -1 || h[temp] > h[big]) {
      big = temp;
    }
  }
  return big;
}

int res;

void add(int x) {
  res += (cnt[x]++ == 0);
}

void del(int x) {
  res -= (--cnt[x] == 0);
}

void predfs(int v, int pr = 0) {
  mxhe[v] = h[v];
  for (int to : gr[v]) {
    if (to == pr) continue;
    h[to] = h[v] + 1;
    predfs(to, v);
    mxhe[v] = max(mxhe[v], mxhe[to]);
  }
}

void dfs(int v, int pr) {
  sort(gr[v].begin(), gr[v].end(), [&](const int &a1, const int &b1) {
    if (pr == b1) return true;
    if (pr == a1) return false;
    return mxhe[a1] > mxhe[b1];
  });
  vector <int> vec;
  for (int to : gr[v]) {
    if (to == pr) continue;
    vec.push_back(mxhe[to]);
  }
  int cnt = 0;
  for (int to : gr[v]) {
    if (to == pr) continue;
    if (!cnt && vec.size() > 1) {
      while (!st.empty() && abs(h[v] - h[st.back()]) <= vec[1] - h[v]) {
        del(c[st.back()]);
        st.pop_back();
      }
    } 
    add(c[v]);
    st.push_back(v);
    dfs(to, v);
    while (!st.empty() && abs(h[v] - h[st.back()]) <= vec[0] - h[v]) {
      del(c[st.back()]);
      st.pop_back();
    }
    cnt++;
  }
  ans[v] = max(ans[v], res);
}

main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d %d", &u, &v);
    gr[u].push_back(v);
    gr[v].push_back(u);
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", &c[i]);
  }
  h[1] = 0;
  A = dfs1(1, 1);
  h[A] = 0;
  B = dfs1(A, A);
  h[A] = 0;
  predfs(A, A);
  dfs(A, A);
  /*for (int i = 1; i <= n; i++) {
    cout << ans[i] << ' ';
  }
  cout << endl;
  return 0;
  */
  while (!st.empty()) {
    del(c[st.back()]);
    st.pop_back();
  }
  h[B] = 0; predfs(B, B);
  dfs(B, B);
  for (int i = 1; i <= n; i++) {
    printf("%d\n", ans[i]);
  }
}
/*
5 4
1 2
2 3
3 4
3 5
1 2 1 2 4
5 4
1 2
2 3
3 4
3 5
1 2 3 4 5

*/

Compilation message

joi2019_ho_t5.cpp:80:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main() {
      ^
joi2019_ho_t5.cpp: In function 'int main()':
joi2019_ho_t5.cpp:81:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &n, &m);
   ~~~~~^~~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:84:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d", &u, &v);
     ~~~~~^~~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:89:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &c[i]);
     ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5120 KB Output is correct
2 Correct 8 ms 5248 KB Output is correct
3 Correct 7 ms 5248 KB Output is correct
4 Correct 7 ms 5376 KB Output is correct
5 Correct 8 ms 5248 KB Output is correct
6 Correct 8 ms 5376 KB Output is correct
7 Correct 7 ms 5376 KB Output is correct
8 Correct 9 ms 5248 KB Output is correct
9 Correct 8 ms 5248 KB Output is correct
10 Correct 8 ms 5120 KB Output is correct
11 Correct 9 ms 5120 KB Output is correct
12 Correct 7 ms 5248 KB Output is correct
13 Correct 7 ms 5504 KB Output is correct
14 Correct 8 ms 5248 KB Output is correct
15 Correct 8 ms 5248 KB Output is correct
16 Correct 7 ms 5120 KB Output is correct
17 Correct 8 ms 5376 KB Output is correct
18 Correct 7 ms 5248 KB Output is correct
19 Correct 7 ms 5120 KB Output is correct
20 Correct 8 ms 5504 KB Output is correct
21 Correct 7 ms 5248 KB Output is correct
22 Correct 9 ms 5120 KB Output is correct
23 Correct 8 ms 5248 KB Output is correct
24 Correct 8 ms 5248 KB Output is correct
25 Correct 8 ms 5248 KB Output is correct
26 Correct 7 ms 5120 KB Output is correct
27 Correct 9 ms 5376 KB Output is correct
28 Correct 8 ms 5376 KB Output is correct
29 Correct 8 ms 5376 KB Output is correct
30 Correct 8 ms 5120 KB Output is correct
31 Correct 8 ms 5376 KB Output is correct
32 Correct 8 ms 5356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 263 ms 12428 KB Output is correct
2 Correct 336 ms 28908 KB Output is correct
3 Correct 41 ms 8824 KB Output is correct
4 Correct 540 ms 18168 KB Output is correct
5 Correct 640 ms 46828 KB Output is correct
6 Correct 630 ms 32076 KB Output is correct
7 Correct 473 ms 18148 KB Output is correct
8 Correct 543 ms 20760 KB Output is correct
9 Correct 517 ms 19824 KB Output is correct
10 Correct 568 ms 19936 KB Output is correct
11 Correct 257 ms 19964 KB Output is correct
12 Correct 672 ms 35796 KB Output is correct
13 Correct 564 ms 31744 KB Output is correct
14 Correct 589 ms 30836 KB Output is correct
15 Correct 331 ms 20048 KB Output is correct
16 Correct 542 ms 37272 KB Output is correct
17 Correct 560 ms 32232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 391 ms 16504 KB Output is correct
2 Correct 655 ms 47340 KB Output is correct
3 Correct 43 ms 9592 KB Output is correct
4 Correct 550 ms 19620 KB Output is correct
5 Correct 651 ms 49252 KB Output is correct
6 Correct 665 ms 34036 KB Output is correct
7 Correct 533 ms 19936 KB Output is correct
8 Correct 525 ms 25464 KB Output is correct
9 Correct 489 ms 23424 KB Output is correct
10 Correct 523 ms 21848 KB Output is correct
11 Correct 370 ms 20448 KB Output is correct
12 Correct 610 ms 43244 KB Output is correct
13 Correct 555 ms 31276 KB Output is correct
14 Correct 540 ms 31940 KB Output is correct
15 Correct 246 ms 20956 KB Output is correct
16 Correct 552 ms 38892 KB Output is correct
17 Correct 590 ms 33696 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5120 KB Output is correct
2 Correct 8 ms 5248 KB Output is correct
3 Correct 7 ms 5248 KB Output is correct
4 Correct 7 ms 5376 KB Output is correct
5 Correct 8 ms 5248 KB Output is correct
6 Correct 8 ms 5376 KB Output is correct
7 Correct 7 ms 5376 KB Output is correct
8 Correct 9 ms 5248 KB Output is correct
9 Correct 8 ms 5248 KB Output is correct
10 Correct 8 ms 5120 KB Output is correct
11 Correct 9 ms 5120 KB Output is correct
12 Correct 7 ms 5248 KB Output is correct
13 Correct 7 ms 5504 KB Output is correct
14 Correct 8 ms 5248 KB Output is correct
15 Correct 8 ms 5248 KB Output is correct
16 Correct 7 ms 5120 KB Output is correct
17 Correct 8 ms 5376 KB Output is correct
18 Correct 7 ms 5248 KB Output is correct
19 Correct 7 ms 5120 KB Output is correct
20 Correct 8 ms 5504 KB Output is correct
21 Correct 7 ms 5248 KB Output is correct
22 Correct 9 ms 5120 KB Output is correct
23 Correct 8 ms 5248 KB Output is correct
24 Correct 8 ms 5248 KB Output is correct
25 Correct 8 ms 5248 KB Output is correct
26 Correct 7 ms 5120 KB Output is correct
27 Correct 9 ms 5376 KB Output is correct
28 Correct 8 ms 5376 KB Output is correct
29 Correct 8 ms 5376 KB Output is correct
30 Correct 8 ms 5120 KB Output is correct
31 Correct 8 ms 5376 KB Output is correct
32 Correct 8 ms 5356 KB Output is correct
33 Correct 263 ms 12428 KB Output is correct
34 Correct 336 ms 28908 KB Output is correct
35 Correct 41 ms 8824 KB Output is correct
36 Correct 540 ms 18168 KB Output is correct
37 Correct 640 ms 46828 KB Output is correct
38 Correct 630 ms 32076 KB Output is correct
39 Correct 473 ms 18148 KB Output is correct
40 Correct 543 ms 20760 KB Output is correct
41 Correct 517 ms 19824 KB Output is correct
42 Correct 568 ms 19936 KB Output is correct
43 Correct 257 ms 19964 KB Output is correct
44 Correct 672 ms 35796 KB Output is correct
45 Correct 564 ms 31744 KB Output is correct
46 Correct 589 ms 30836 KB Output is correct
47 Correct 331 ms 20048 KB Output is correct
48 Correct 542 ms 37272 KB Output is correct
49 Correct 560 ms 32232 KB Output is correct
50 Correct 391 ms 16504 KB Output is correct
51 Correct 655 ms 47340 KB Output is correct
52 Correct 43 ms 9592 KB Output is correct
53 Correct 550 ms 19620 KB Output is correct
54 Correct 651 ms 49252 KB Output is correct
55 Correct 665 ms 34036 KB Output is correct
56 Correct 533 ms 19936 KB Output is correct
57 Correct 525 ms 25464 KB Output is correct
58 Correct 489 ms 23424 KB Output is correct
59 Correct 523 ms 21848 KB Output is correct
60 Correct 370 ms 20448 KB Output is correct
61 Correct 610 ms 43244 KB Output is correct
62 Correct 555 ms 31276 KB Output is correct
63 Correct 540 ms 31940 KB Output is correct
64 Correct 246 ms 20956 KB Output is correct
65 Correct 552 ms 38892 KB Output is correct
66 Correct 590 ms 33696 KB Output is correct
67 Correct 34 ms 6904 KB Output is correct
68 Correct 224 ms 23728 KB Output is correct
69 Correct 337 ms 22404 KB Output is correct
70 Correct 508 ms 17940 KB Output is correct
71 Correct 629 ms 46712 KB Output is correct
72 Correct 590 ms 32220 KB Output is correct
73 Correct 470 ms 18112 KB Output is correct
74 Correct 525 ms 23416 KB Output is correct
75 Correct 512 ms 20552 KB Output is correct
76 Correct 530 ms 20088 KB Output is correct
77 Correct 377 ms 18864 KB Output is correct
78 Correct 658 ms 39180 KB Output is correct
79 Correct 547 ms 35128 KB Output is correct
80 Correct 550 ms 29116 KB Output is correct
81 Correct 218 ms 19800 KB Output is correct
82 Correct 516 ms 37144 KB Output is correct
83 Correct 568 ms 32184 KB Output is correct
84 Correct 508 ms 18204 KB Output is correct
85 Correct 661 ms 47340 KB Output is correct
86 Correct 609 ms 32576 KB Output is correct
87 Correct 499 ms 18472 KB Output is correct
88 Correct 554 ms 24184 KB Output is correct
89 Correct 530 ms 21184 KB Output is correct
90 Correct 525 ms 20576 KB Output is correct
91 Correct 371 ms 19244 KB Output is correct
92 Correct 626 ms 46460 KB Output is correct
93 Correct 530 ms 27628 KB Output is correct
94 Correct 550 ms 25088 KB Output is correct
95 Correct 231 ms 20388 KB Output is correct
96 Correct 551 ms 37648 KB Output is correct
97 Correct 583 ms 32580 KB Output is correct
98 Correct 563 ms 19532 KB Output is correct
99 Correct 666 ms 47976 KB Output is correct
100 Correct 633 ms 33872 KB Output is correct
101 Correct 472 ms 19000 KB Output is correct
102 Correct 565 ms 23544 KB Output is correct
103 Correct 530 ms 21736 KB Output is correct
104 Correct 534 ms 20972 KB Output is correct
105 Correct 366 ms 19584 KB Output is correct
106 Correct 715 ms 35328 KB Output is correct
107 Correct 541 ms 35196 KB Output is correct
108 Correct 544 ms 28320 KB Output is correct
109 Correct 226 ms 20916 KB Output is correct
110 Correct 509 ms 38092 KB Output is correct
111 Correct 548 ms 33204 KB Output is correct