Submission #468823

# Submission time Handle Problem Language Result Execution time Memory
468823 2021-08-29T18:50:06 Z hoaphat1 Unique Cities (JOI19_ho_t5) C++17
100 / 100
776 ms 55340 KB
#include <bits/stdc++.h>
 
using namespace std;
 
template <typename T>
class graph {
 public:
  struct edge {
    int from;
    int to;
    T cost;
  };
 
  vector<edge> edges;
  vector<vector<int>> g;
  int n;
 
  graph(int _n) : n(_n) {
    g.resize(n);
  }
 
  virtual int add(int from, int to, T cost) = 0;
};
 
template <typename T>
class forest : public graph<T> {
 public:
  using graph<T>::edges;
  using graph<T>::g;
  using graph<T>::n;
 
  forest(int _n) : graph<T>(_n) {
  }
 
  int add(int from, int to, T cost = 1) {
    assert(0 <= from && from < n && 0 <= to && to < n);
    int id = (int) edges.size();
    assert(id < n - 1);
    g[from].push_back(id);
    g[to].push_back(id);
    edges.push_back({from, to, cost});
    return id;
  }
};
 
template <typename T>
class dfs_forest : public forest<T> {
 public:
  using forest<T>::edges;
  using forest<T>::g;
  using forest<T>::n;
 
  vector<int> pv;
  vector<int> pe;
  vector<int> order;
  vector<int> pos;
  vector<int> end;
  vector<int> sz;
  vector<int> root;
  vector<int> depth;
  vector<T> dist;
 
  dfs_forest(int _n) : forest<T>(_n) {
  }
 
  void init() {
    pv = vector<int>(n, -1);
    pe = vector<int>(n, -1);
    order.clear();
    pos = vector<int>(n, -1);
    end = vector<int>(n, -1);
    sz = vector<int>(n, 0);
    root = vector<int>(n, -1);
    depth = vector<int>(n, -1);
    dist = vector<T>(n);
  }
 
  void clear() {
    pv.clear();
    pe.clear();
    order.clear();
    pos.clear();
    end.clear();
    sz.clear();
    root.clear();
    depth.clear();
    dist.clear();
  }
 
 private:
  void do_dfs(int v) {
    pos[v] = (int) order.size();
    order.push_back(v);
    sz[v] = 1;
    for (int id : g[v]) {
      if (id == pe[v]) {
        continue;
      }
      auto &e = edges[id];
      int to = e.from ^ e.to ^ v;
      depth[to] = depth[v] + 1;
      dist[to] = dist[v] + e.cost;
      pv[to] = v;
      pe[to] = id;
      root[to] = (root[v] != -1 ? root[v] : to);
      do_dfs(to);
      sz[v] += sz[to];
    }
    end[v] = (int) order.size() - 1;
  }
 
  void do_dfs_from(int v) {
    depth[v] = 0;
    dist[v] = T{};
    root[v] = v;
    pv[v] = pe[v] = -1;
    do_dfs(v);
  }
 
 public:
  void dfs(int v, bool clear_order = true) {
    if (pv.empty()) {
      init();
    } else {
      if (clear_order) {
        order.clear();
      }
    }
    do_dfs_from(v);
  }
 
  void dfs_all() {
    init();
    for (int v = 0; v < n; v++) {
      if (depth[v] == -1) {
        do_dfs_from(v);
      }
    }
    assert((int) order.size() == n);
  }
};
 
template <typename A, typename B>
string to_string(pair<A, B> p);
 
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
 
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
 
string to_string(const string& s) {
  return '"' + s + '"';
}
 
string to_string(const char* s) {
  return to_string((string) s);
}
 
string to_string(bool b) {
  return (b ? "true" : "false");
}
 
string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}
 
template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}
 
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
 
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
 
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}
 
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
 
void debug_out() { cerr << endl; }
 
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
 
#ifndef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
/// template from tourist
 
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  dfs_forest<int> g(n);
  for (int i = 1; i < n; i++) {
  	int u, v;
  	cin >> u >> v;
  	--u; --v;
  	g.add(u, v);
	}
	g.dfs(0);
	vector<int> dia(2);
	dia[0] = max_element(g.depth.begin(), g.depth.end()) - g.depth.begin();
	g.dfs(dia[0]);
	dia[1] = max_element(g.depth.begin(), g.depth.end()) - g.depth.begin();
	vector<int> c(n);
	for (int i = 0; i < n; i++) {
		cin >> c[i];
		--c[i];
	}
	vector<int> ans(n);
	for (int i = 0; i < 2; i++) {
		vector<pair<int, int>> dis(n);
		function<void(int, int)> dfs = [&](int v, int p) {
			dis[v] = make_pair(0, v);
			for (int id : g.g[v]) {
				auto& e = g.edges[id];
				int u = e.from ^ e.to ^ v;
				if (u == p) continue;
				dfs(u, v);
				dis[v] = max(dis[v], make_pair(dis[u].first + 1, u));
			}
		};
		vector<int> st;
		vector<int> cnt(m);
		int res = 0;
		auto add = [&](int v) {
			if (!cnt[c[v]]++) ++res;
		};
		auto del = [&](int v) {
			if (!--cnt[c[v]]) --res;
		};
		function<void(int, int)> dfs1 = [&](int v, int p) {
			if (p != -1) {
				st.push_back(p);
				add(p);
			}
			int mx = 0;
			for (int id : g.g[v]) {
				auto& e = g.edges[id];
				int u = e.from ^ e.to ^ v;
				if (u == p || u == dis[v].second) continue;
				mx = max(mx, dis[u].first + 1);
			}
			while (!st.empty() && g.depth[v] - g.depth[st.back()] <= mx) {
				del(st.back());
				st.pop_back();
			}
			if (v != dis[v].second) {
				dfs1(dis[v].second, v);
			}
			while (!st.empty() && g.depth[v] - g.depth[st.back()] <= dis[v].first) {
				del(st.back());
				st.pop_back();
			}
			for (int id : g.g[v]) {
				auto& e = g.edges[id];
				int u = e.from ^ e.to ^ v;
				if (u == p || u == dis[v].second) continue;
				dfs1(u, v);
			}
			ans[v] = max(ans[v], res);
			if (!st.empty() && st.back() == p) {
				st.pop_back();
				del(p);
			}
		};
		g.dfs(dia[i]);
		dfs(dia[i], -1);
		dfs1(dia[i], -1);
	}
	for (int i = 0; i < n; i++) cout << ans[i] <<"\n";
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 3 ms 460 KB Output is correct
3 Correct 2 ms 460 KB Output is correct
4 Correct 2 ms 588 KB Output is correct
5 Correct 3 ms 460 KB Output is correct
6 Correct 2 ms 716 KB Output is correct
7 Correct 2 ms 588 KB Output is correct
8 Correct 2 ms 460 KB Output is correct
9 Correct 3 ms 588 KB Output is correct
10 Correct 3 ms 588 KB Output is correct
11 Correct 3 ms 460 KB Output is correct
12 Correct 2 ms 460 KB Output is correct
13 Correct 3 ms 716 KB Output is correct
14 Correct 3 ms 588 KB Output is correct
15 Correct 3 ms 588 KB Output is correct
16 Correct 2 ms 460 KB Output is correct
17 Correct 2 ms 620 KB Output is correct
18 Correct 2 ms 588 KB Output is correct
19 Correct 3 ms 484 KB Output is correct
20 Correct 3 ms 716 KB Output is correct
21 Correct 3 ms 588 KB Output is correct
22 Correct 3 ms 460 KB Output is correct
23 Correct 3 ms 588 KB Output is correct
24 Correct 3 ms 588 KB Output is correct
25 Correct 3 ms 568 KB Output is correct
26 Correct 2 ms 460 KB Output is correct
27 Correct 3 ms 716 KB Output is correct
28 Correct 2 ms 716 KB Output is correct
29 Correct 3 ms 588 KB Output is correct
30 Correct 2 ms 460 KB Output is correct
31 Correct 3 ms 716 KB Output is correct
32 Correct 2 ms 588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 264 ms 15092 KB Output is correct
2 Correct 346 ms 29200 KB Output is correct
3 Correct 41 ms 5920 KB Output is correct
4 Correct 600 ms 27400 KB Output is correct
5 Correct 738 ms 53744 KB Output is correct
6 Correct 705 ms 40136 KB Output is correct
7 Correct 545 ms 27504 KB Output is correct
8 Correct 648 ms 30024 KB Output is correct
9 Correct 607 ms 29400 KB Output is correct
10 Correct 623 ms 29008 KB Output is correct
11 Correct 308 ms 28236 KB Output is correct
12 Correct 721 ms 43660 KB Output is correct
13 Correct 635 ms 40252 KB Output is correct
14 Correct 637 ms 39256 KB Output is correct
15 Correct 270 ms 28152 KB Output is correct
16 Correct 634 ms 45428 KB Output is correct
17 Correct 608 ms 40276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 432 ms 21204 KB Output is correct
2 Correct 715 ms 49312 KB Output is correct
3 Correct 49 ms 6784 KB Output is correct
4 Correct 644 ms 29240 KB Output is correct
5 Correct 741 ms 55340 KB Output is correct
6 Correct 721 ms 42232 KB Output is correct
7 Correct 578 ms 29104 KB Output is correct
8 Correct 628 ms 34392 KB Output is correct
9 Correct 635 ms 32668 KB Output is correct
10 Correct 644 ms 31120 KB Output is correct
11 Correct 486 ms 29400 KB Output is correct
12 Correct 734 ms 49608 KB Output is correct
13 Correct 677 ms 40396 KB Output is correct
14 Correct 657 ms 40656 KB Output is correct
15 Correct 278 ms 29792 KB Output is correct
16 Correct 628 ms 47000 KB Output is correct
17 Correct 655 ms 42352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 3 ms 460 KB Output is correct
3 Correct 2 ms 460 KB Output is correct
4 Correct 2 ms 588 KB Output is correct
5 Correct 3 ms 460 KB Output is correct
6 Correct 2 ms 716 KB Output is correct
7 Correct 2 ms 588 KB Output is correct
8 Correct 2 ms 460 KB Output is correct
9 Correct 3 ms 588 KB Output is correct
10 Correct 3 ms 588 KB Output is correct
11 Correct 3 ms 460 KB Output is correct
12 Correct 2 ms 460 KB Output is correct
13 Correct 3 ms 716 KB Output is correct
14 Correct 3 ms 588 KB Output is correct
15 Correct 3 ms 588 KB Output is correct
16 Correct 2 ms 460 KB Output is correct
17 Correct 2 ms 620 KB Output is correct
18 Correct 2 ms 588 KB Output is correct
19 Correct 3 ms 484 KB Output is correct
20 Correct 3 ms 716 KB Output is correct
21 Correct 3 ms 588 KB Output is correct
22 Correct 3 ms 460 KB Output is correct
23 Correct 3 ms 588 KB Output is correct
24 Correct 3 ms 588 KB Output is correct
25 Correct 3 ms 568 KB Output is correct
26 Correct 2 ms 460 KB Output is correct
27 Correct 3 ms 716 KB Output is correct
28 Correct 2 ms 716 KB Output is correct
29 Correct 3 ms 588 KB Output is correct
30 Correct 2 ms 460 KB Output is correct
31 Correct 3 ms 716 KB Output is correct
32 Correct 2 ms 588 KB Output is correct
33 Correct 264 ms 15092 KB Output is correct
34 Correct 346 ms 29200 KB Output is correct
35 Correct 41 ms 5920 KB Output is correct
36 Correct 600 ms 27400 KB Output is correct
37 Correct 738 ms 53744 KB Output is correct
38 Correct 705 ms 40136 KB Output is correct
39 Correct 545 ms 27504 KB Output is correct
40 Correct 648 ms 30024 KB Output is correct
41 Correct 607 ms 29400 KB Output is correct
42 Correct 623 ms 29008 KB Output is correct
43 Correct 308 ms 28236 KB Output is correct
44 Correct 721 ms 43660 KB Output is correct
45 Correct 635 ms 40252 KB Output is correct
46 Correct 637 ms 39256 KB Output is correct
47 Correct 270 ms 28152 KB Output is correct
48 Correct 634 ms 45428 KB Output is correct
49 Correct 608 ms 40276 KB Output is correct
50 Correct 432 ms 21204 KB Output is correct
51 Correct 715 ms 49312 KB Output is correct
52 Correct 49 ms 6784 KB Output is correct
53 Correct 644 ms 29240 KB Output is correct
54 Correct 741 ms 55340 KB Output is correct
55 Correct 721 ms 42232 KB Output is correct
56 Correct 578 ms 29104 KB Output is correct
57 Correct 628 ms 34392 KB Output is correct
58 Correct 635 ms 32668 KB Output is correct
59 Correct 644 ms 31120 KB Output is correct
60 Correct 486 ms 29400 KB Output is correct
61 Correct 734 ms 49608 KB Output is correct
62 Correct 677 ms 40396 KB Output is correct
63 Correct 657 ms 40656 KB Output is correct
64 Correct 278 ms 29792 KB Output is correct
65 Correct 628 ms 47000 KB Output is correct
66 Correct 655 ms 42352 KB Output is correct
67 Correct 36 ms 3980 KB Output is correct
68 Correct 236 ms 23768 KB Output is correct
69 Correct 392 ms 25420 KB Output is correct
70 Correct 631 ms 27492 KB Output is correct
71 Correct 743 ms 53616 KB Output is correct
72 Correct 705 ms 40184 KB Output is correct
73 Correct 644 ms 27480 KB Output is correct
74 Correct 651 ms 32320 KB Output is correct
75 Correct 605 ms 29656 KB Output is correct
76 Correct 650 ms 29380 KB Output is correct
77 Correct 464 ms 27760 KB Output is correct
78 Correct 721 ms 46508 KB Output is correct
79 Correct 626 ms 43472 KB Output is correct
80 Correct 679 ms 37664 KB Output is correct
81 Correct 265 ms 27884 KB Output is correct
82 Correct 621 ms 45352 KB Output is correct
83 Correct 630 ms 40300 KB Output is correct
84 Correct 635 ms 27764 KB Output is correct
85 Correct 757 ms 54052 KB Output is correct
86 Correct 712 ms 40644 KB Output is correct
87 Correct 620 ms 27688 KB Output is correct
88 Correct 649 ms 33172 KB Output is correct
89 Correct 661 ms 30580 KB Output is correct
90 Correct 602 ms 29912 KB Output is correct
91 Correct 463 ms 28064 KB Output is correct
92 Correct 750 ms 53084 KB Output is correct
93 Correct 678 ms 36616 KB Output is correct
94 Correct 664 ms 34416 KB Output is correct
95 Correct 269 ms 28344 KB Output is correct
96 Correct 632 ms 45632 KB Output is correct
97 Correct 658 ms 40876 KB Output is correct
98 Correct 610 ms 29224 KB Output is correct
99 Correct 776 ms 54408 KB Output is correct
100 Correct 740 ms 41888 KB Output is correct
101 Correct 592 ms 28240 KB Output is correct
102 Correct 693 ms 32336 KB Output is correct
103 Correct 661 ms 30828 KB Output is correct
104 Correct 644 ms 30236 KB Output is correct
105 Correct 481 ms 28420 KB Output is correct
106 Correct 727 ms 43212 KB Output is correct
107 Correct 663 ms 44096 KB Output is correct
108 Correct 675 ms 36880 KB Output is correct
109 Correct 276 ms 29360 KB Output is correct
110 Correct 632 ms 46144 KB Output is correct
111 Correct 687 ms 41736 KB Output is correct