Submission #128295

# Submission time Handle Problem Language Result Execution time Memory
128295 2019-07-10T16:05:03 Z IOrtroiii Unique Cities (JOI19_ho_t5) C++14
100 / 100
854 ms 46200 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 200200;

vector<int> g[N];
int color[N];
int ans[N];
int dep[N], down[N];

void dfs(int u, int p) {
   for (int v : g[u]) {
      if (v != p) {
         dep[v] = dep[u] + 1;
         dfs(v, u);
      }
   }
}

void dfsDown(int u, int p) {
   down[u] = 0;
   for (int v : g[u]) {
      if (v != p) {
         dfsDown(v, u);
         down[u] = max(down[u], down[v] + 1);
      }
   }
}

struct {
   int stSz;
   int st[N];
   int cnt[N];
   int curAns;
   bool empty() {
      return stSz == 0;
   }
   int top() {
      return st[stSz - 1];
   }
   void push(int u) {
      if (++cnt[color[u]] == 1) {
         ++curAns;
      }
      st[stSz++] = u;
   }
   void pop() {
      int u = top();
      if (--cnt[color[u]] == 0) {
         --curAns;
      }
      stSz--;
   }
} ms;

void solve(int u, int p) {
   vector<pair<int, int>> nxt;
   for (int v : g[u]) {
      if (v != p) {
         nxt.emplace_back(down[v] + 1, v);
      }
   }
   if (!nxt.empty()) {
      swap(nxt[0], nxt[max_element(nxt.begin(), nxt.end()) - nxt.begin()]);
      int len = 0;
      if (nxt.size() > 1) {
         len = max_element(nxt.begin() + 1, nxt.end())->first;
      }
      for (auto p : nxt) {
         int v = p.second;
         while (!ms.empty() && dep[ms.top()] >= dep[u] - len) {
            ms.pop();
         }
         ms.push(u);
         solve(v, u);
         if (!ms.empty() && ms.top() == u) {
            ms.pop();
         }
         len = max(len, p.first);
      }
      while (!ms.empty() && dep[ms.top()] >= dep[u] - len) {
         ms.pop();
      }
   }
   ans[u] = max(ans[u], ms.curAns);
}

int main() {
   int n, m;
   scanf("%d %d", &n, &m);
   for (int i = 0; i < n - 1; ++i) {
      int u, v;
      scanf("%d %d", &u, &v);
      g[u].push_back(v);
      g[v].push_back(u);
   }
   for (int i = 1; i <= n; ++i) {
      scanf("%d", color + i);
   }
   int root = 1;
   dfs(1, 1);
   for (int rot = 0; rot < 2; ++rot) {;
      root = max_element(dep + 1, dep + 1 + n) - dep;
      dep[root] = 0;
      dfs(root, -1);
      dfsDown(root, -1);
      solve(root, -1);
   }
   for (int i = 1; i <= n; ++i) {
      printf("%d\n", ans[i]);
   }
}

Compilation message

joi2019_ho_t5.cpp: In function 'int main()':
joi2019_ho_t5.cpp:91:9: 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:94:12: 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:99:12: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
       scanf("%d", color + i);
       ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5112 KB Output is correct
2 Correct 9 ms 5112 KB Output is correct
3 Correct 8 ms 5232 KB Output is correct
4 Correct 9 ms 5240 KB Output is correct
5 Correct 9 ms 5212 KB Output is correct
6 Correct 9 ms 5496 KB Output is correct
7 Correct 9 ms 5240 KB Output is correct
8 Correct 9 ms 5240 KB Output is correct
9 Correct 9 ms 5240 KB Output is correct
10 Correct 10 ms 5240 KB Output is correct
11 Correct 10 ms 5112 KB Output is correct
12 Correct 8 ms 5240 KB Output is correct
13 Correct 9 ms 5368 KB Output is correct
14 Correct 9 ms 5240 KB Output is correct
15 Correct 12 ms 5300 KB Output is correct
16 Correct 10 ms 5240 KB Output is correct
17 Correct 10 ms 5368 KB Output is correct
18 Correct 11 ms 5368 KB Output is correct
19 Correct 9 ms 5240 KB Output is correct
20 Correct 9 ms 5496 KB Output is correct
21 Correct 9 ms 5368 KB Output is correct
22 Correct 9 ms 5116 KB Output is correct
23 Correct 9 ms 5240 KB Output is correct
24 Correct 9 ms 5240 KB Output is correct
25 Correct 10 ms 5240 KB Output is correct
26 Correct 8 ms 5240 KB Output is correct
27 Correct 11 ms 5368 KB Output is correct
28 Correct 9 ms 5368 KB Output is correct
29 Correct 9 ms 5244 KB Output is correct
30 Correct 9 ms 5240 KB Output is correct
31 Correct 10 ms 5368 KB Output is correct
32 Correct 9 ms 5244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 290 ms 10744 KB Output is correct
2 Correct 365 ms 27120 KB Output is correct
3 Correct 53 ms 8696 KB Output is correct
4 Correct 538 ms 18040 KB Output is correct
5 Correct 697 ms 43608 KB Output is correct
6 Correct 714 ms 30456 KB Output is correct
7 Correct 504 ms 18392 KB Output is correct
8 Correct 549 ms 20600 KB Output is correct
9 Correct 577 ms 19704 KB Output is correct
10 Correct 541 ms 19588 KB Output is correct
11 Correct 250 ms 21668 KB Output is correct
12 Correct 633 ms 33912 KB Output is correct
13 Correct 601 ms 30332 KB Output is correct
14 Correct 599 ms 29816 KB Output is correct
15 Correct 223 ms 21564 KB Output is correct
16 Correct 557 ms 35508 KB Output is correct
17 Correct 632 ms 30688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 425 ms 13572 KB Output is correct
2 Correct 682 ms 44340 KB Output is correct
3 Correct 65 ms 9340 KB Output is correct
4 Correct 564 ms 19548 KB Output is correct
5 Correct 854 ms 46200 KB Output is correct
6 Correct 727 ms 32496 KB Output is correct
7 Correct 537 ms 19748 KB Output is correct
8 Correct 583 ms 25084 KB Output is correct
9 Correct 549 ms 23124 KB Output is correct
10 Correct 565 ms 21608 KB Output is correct
11 Correct 390 ms 21208 KB Output is correct
12 Correct 657 ms 40944 KB Output is correct
13 Correct 572 ms 30304 KB Output is correct
14 Correct 630 ms 30752 KB Output is correct
15 Correct 230 ms 22720 KB Output is correct
16 Correct 563 ms 37284 KB Output is correct
17 Correct 624 ms 32456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5112 KB Output is correct
2 Correct 9 ms 5112 KB Output is correct
3 Correct 8 ms 5232 KB Output is correct
4 Correct 9 ms 5240 KB Output is correct
5 Correct 9 ms 5212 KB Output is correct
6 Correct 9 ms 5496 KB Output is correct
7 Correct 9 ms 5240 KB Output is correct
8 Correct 9 ms 5240 KB Output is correct
9 Correct 9 ms 5240 KB Output is correct
10 Correct 10 ms 5240 KB Output is correct
11 Correct 10 ms 5112 KB Output is correct
12 Correct 8 ms 5240 KB Output is correct
13 Correct 9 ms 5368 KB Output is correct
14 Correct 9 ms 5240 KB Output is correct
15 Correct 12 ms 5300 KB Output is correct
16 Correct 10 ms 5240 KB Output is correct
17 Correct 10 ms 5368 KB Output is correct
18 Correct 11 ms 5368 KB Output is correct
19 Correct 9 ms 5240 KB Output is correct
20 Correct 9 ms 5496 KB Output is correct
21 Correct 9 ms 5368 KB Output is correct
22 Correct 9 ms 5116 KB Output is correct
23 Correct 9 ms 5240 KB Output is correct
24 Correct 9 ms 5240 KB Output is correct
25 Correct 10 ms 5240 KB Output is correct
26 Correct 8 ms 5240 KB Output is correct
27 Correct 11 ms 5368 KB Output is correct
28 Correct 9 ms 5368 KB Output is correct
29 Correct 9 ms 5244 KB Output is correct
30 Correct 9 ms 5240 KB Output is correct
31 Correct 10 ms 5368 KB Output is correct
32 Correct 9 ms 5244 KB Output is correct
33 Correct 290 ms 10744 KB Output is correct
34 Correct 365 ms 27120 KB Output is correct
35 Correct 53 ms 8696 KB Output is correct
36 Correct 538 ms 18040 KB Output is correct
37 Correct 697 ms 43608 KB Output is correct
38 Correct 714 ms 30456 KB Output is correct
39 Correct 504 ms 18392 KB Output is correct
40 Correct 549 ms 20600 KB Output is correct
41 Correct 577 ms 19704 KB Output is correct
42 Correct 541 ms 19588 KB Output is correct
43 Correct 250 ms 21668 KB Output is correct
44 Correct 633 ms 33912 KB Output is correct
45 Correct 601 ms 30332 KB Output is correct
46 Correct 599 ms 29816 KB Output is correct
47 Correct 223 ms 21564 KB Output is correct
48 Correct 557 ms 35508 KB Output is correct
49 Correct 632 ms 30688 KB Output is correct
50 Correct 425 ms 13572 KB Output is correct
51 Correct 682 ms 44340 KB Output is correct
52 Correct 65 ms 9340 KB Output is correct
53 Correct 564 ms 19548 KB Output is correct
54 Correct 854 ms 46200 KB Output is correct
55 Correct 727 ms 32496 KB Output is correct
56 Correct 537 ms 19748 KB Output is correct
57 Correct 583 ms 25084 KB Output is correct
58 Correct 549 ms 23124 KB Output is correct
59 Correct 565 ms 21608 KB Output is correct
60 Correct 390 ms 21208 KB Output is correct
61 Correct 657 ms 40944 KB Output is correct
62 Correct 572 ms 30304 KB Output is correct
63 Correct 630 ms 30752 KB Output is correct
64 Correct 230 ms 22720 KB Output is correct
65 Correct 563 ms 37284 KB Output is correct
66 Correct 624 ms 32456 KB Output is correct
67 Correct 47 ms 7004 KB Output is correct
68 Correct 305 ms 22396 KB Output is correct
69 Correct 350 ms 21368 KB Output is correct
70 Correct 524 ms 17920 KB Output is correct
71 Correct 683 ms 43628 KB Output is correct
72 Correct 783 ms 30648 KB Output is correct
73 Correct 633 ms 18032 KB Output is correct
74 Correct 635 ms 22904 KB Output is correct
75 Correct 526 ms 20076 KB Output is correct
76 Correct 531 ms 19908 KB Output is correct
77 Correct 441 ms 19660 KB Output is correct
78 Correct 669 ms 36728 KB Output is correct
79 Correct 603 ms 33480 KB Output is correct
80 Correct 572 ms 28048 KB Output is correct
81 Correct 214 ms 21584 KB Output is correct
82 Correct 620 ms 35436 KB Output is correct
83 Correct 668 ms 30576 KB Output is correct
84 Correct 551 ms 18168 KB Output is correct
85 Correct 666 ms 44280 KB Output is correct
86 Correct 659 ms 31140 KB Output is correct
87 Correct 503 ms 18432 KB Output is correct
88 Correct 579 ms 23700 KB Output is correct
89 Correct 556 ms 20832 KB Output is correct
90 Correct 560 ms 20288 KB Output is correct
91 Correct 388 ms 20220 KB Output is correct
92 Correct 775 ms 43568 KB Output is correct
93 Correct 721 ms 26968 KB Output is correct
94 Correct 610 ms 24676 KB Output is correct
95 Correct 269 ms 21992 KB Output is correct
96 Correct 557 ms 35768 KB Output is correct
97 Correct 582 ms 31092 KB Output is correct
98 Correct 695 ms 19732 KB Output is correct
99 Correct 805 ms 44792 KB Output is correct
100 Correct 660 ms 32324 KB Output is correct
101 Correct 482 ms 19356 KB Output is correct
102 Correct 600 ms 23144 KB Output is correct
103 Correct 616 ms 21300 KB Output is correct
104 Correct 667 ms 20788 KB Output is correct
105 Correct 395 ms 20720 KB Output is correct
106 Correct 655 ms 33796 KB Output is correct
107 Correct 628 ms 33792 KB Output is correct
108 Correct 716 ms 26868 KB Output is correct
109 Correct 302 ms 22732 KB Output is correct
110 Correct 637 ms 36204 KB Output is correct
111 Correct 615 ms 31940 KB Output is correct