QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#636844 | #33. Cat in a tree | maspy | 100 ✓ | 218ms | 83144kb | C++20 | 33.7kb | 2024-10-13 03:22:42 | 2024-10-13 03:22:42 |
Judging History
answer
#line 1 "/home/maspy/compro/library/my_template.hpp"
#if defined(LOCAL)
#include <my_template_compiled.hpp>
#else
// https://codeforces.com/blog/entry/96344
#pragma GCC optimize("Ofast,unroll-loops")
// いまの CF だとこれ入れると動かない?
// #pragma GCC target("avx2,popcnt")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using i128 = __int128;
using u128 = unsigned __int128;
using f128 = __float128;
template <class T>
constexpr T infty = 0;
template <>
constexpr int infty<int> = 1'010'000'000;
template <>
constexpr ll infty<ll> = 2'020'000'000'000'000'000;
template <>
constexpr u32 infty<u32> = infty<int>;
template <>
constexpr u64 infty<u64> = infty<ll>;
template <>
constexpr i128 infty<i128> = i128(infty<ll>) * 2'000'000'000'000'000'000;
template <>
constexpr double infty<double> = infty<ll>;
template <>
constexpr long double infty<long double> = infty<ll>;
using pi = pair<ll, ll>;
using vi = vector<ll>;
template <class T>
using vc = vector<T>;
template <class T>
using vvc = vector<vc<T>>;
template <class T>
using vvvc = vector<vvc<T>>;
template <class T>
using vvvvc = vector<vvvc<T>>;
template <class T>
using vvvvvc = vector<vvvvc<T>>;
template <class T>
using pq = priority_queue<T>;
template <class T>
using pqg = priority_queue<T, vector<T>, greater<T>>;
#define vv(type, name, h, ...) vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define vvv(type, name, h, w, ...) vector<vector<vector<type>>> name(h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...) \
vector<vector<vector<vector<type>>>> name(a, vector<vector<vector<type>>>(b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))
// https://trap.jp/post/1224/
#define FOR1(a) for (ll _ = 0; _ < ll(a); ++_)
#define FOR2(i, a) for (ll i = 0; i < ll(a); ++i)
#define FOR3(i, a, b) for (ll i = a; i < ll(b); ++i)
#define FOR4(i, a, b, c) for (ll i = a; i < ll(b); i += (c))
#define FOR1_R(a) for (ll i = (a)-1; i >= ll(0); --i)
#define FOR2_R(i, a) for (ll i = (a)-1; i >= ll(0); --i)
#define FOR3_R(i, a, b) for (ll i = (b)-1; i >= ll(a); --i)
#define overload4(a, b, c, d, e, ...) e
#define overload3(a, b, c, d, ...) d
#define FOR(...) overload4(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__)
#define FOR_R(...) overload3(__VA_ARGS__, FOR3_R, FOR2_R, FOR1_R)(__VA_ARGS__)
#define FOR_subset(t, s) for (ll t = (s); t >= 0; t = (t == 0 ? -1 : (t - 1) & (s)))
#define all(x) x.begin(), x.end()
#define len(x) ll(x.size())
#define elif else if
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define stoi stoll
int popcnt(int x) { return __builtin_popcount(x); }
int popcnt(u32 x) { return __builtin_popcount(x); }
int popcnt(ll x) { return __builtin_popcountll(x); }
int popcnt(u64 x) { return __builtin_popcountll(x); }
int popcnt_mod_2(int x) { return __builtin_parity(x); }
int popcnt_mod_2(u32 x) { return __builtin_parity(x); }
int popcnt_mod_2(ll x) { return __builtin_parityll(x); }
int popcnt_mod_2(u64 x) { return __builtin_parityll(x); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2)
int topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(u32 x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
int topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 0, 2)
int lowbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(u32 x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
int lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
template <typename T>
T floor(T a, T b) {
return a / b - (a % b && (a ^ b) < 0);
}
template <typename T>
T ceil(T x, T y) {
return floor(x + y - 1, y);
}
template <typename T>
T bmod(T x, T y) {
return x - y * floor(x, y);
}
template <typename T>
pair<T, T> divmod(T x, T y) {
T q = floor(x, y);
return {q, x - q * y};
}
template <typename T, typename U>
T SUM(const vector<U> &A) {
T sm = 0;
for (auto &&a: A) sm += a;
return sm;
}
#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define UNIQUE(x) sort(all(x)), x.erase(unique(all(x)), x.end()), x.shrink_to_fit()
template <typename T>
T POP(deque<T> &que) {
T a = que.front();
que.pop_front();
return a;
}
template <typename T>
T POP(pq<T> &que) {
T a = que.top();
que.pop();
return a;
}
template <typename T>
T POP(pqg<T> &que) {
T a = que.top();
que.pop();
return a;
}
template <typename T>
T POP(vc<T> &que) {
T a = que.back();
que.pop_back();
return a;
}
template <typename F>
ll binary_search(F check, ll ok, ll ng, bool check_ok = true) {
if (check_ok) assert(check(ok));
while (abs(ok - ng) > 1) {
auto x = (ng + ok) / 2;
(check(x) ? ok : ng) = x;
}
return ok;
}
template <typename F>
double binary_search_real(F check, double ok, double ng, int iter = 100) {
FOR(iter) {
double x = (ok + ng) / 2;
(check(x) ? ok : ng) = x;
}
return (ok + ng) / 2;
}
template <class T, class S>
inline bool chmax(T &a, const S &b) {
return (a < b ? a = b, 1 : 0);
}
template <class T, class S>
inline bool chmin(T &a, const S &b) {
return (a > b ? a = b, 1 : 0);
}
// ? は -1
vc<int> s_to_vi(const string &S, char first_char) {
vc<int> A(S.size());
FOR(i, S.size()) { A[i] = (S[i] != '?' ? S[i] - first_char : -1); }
return A;
}
template <typename T, typename U>
vector<T> cumsum(vector<U> &A, int off = 1) {
int N = A.size();
vector<T> B(N + 1);
FOR(i, N) { B[i + 1] = B[i] + A[i]; }
if (off == 0) B.erase(B.begin());
return B;
}
// stable sort
template <typename T>
vector<int> argsort(const vector<T> &A) {
vector<int> ids(len(A));
iota(all(ids), 0);
sort(all(ids), [&](int i, int j) { return (A[i] == A[j] ? i < j : A[i] < A[j]); });
return ids;
}
// A[I[0]], A[I[1]], ...
template <typename T>
vc<T> rearrange(const vc<T> &A, const vc<int> &I) {
vc<T> B(len(I));
FOR(i, len(I)) B[i] = A[I[i]];
return B;
}
template <typename T, typename... Vectors>
void concat(vc<T> &first, const Vectors &... others) {
vc<T> &res = first;
(res.insert(res.end(), others.begin(), others.end()), ...);
}
#endif
#line 1 "/home/maspy/compro/library/other/io.hpp"
#define FASTIO
#include <unistd.h>
// https://judge.yosupo.jp/submission/21623
namespace fastio {
static constexpr uint32_t SZ = 1 << 17;
char ibuf[SZ];
char obuf[SZ];
char out[100];
// pointer of ibuf, obuf
uint32_t pil = 0, pir = 0, por = 0;
struct Pre {
char num[10000][4];
constexpr Pre() : num() {
for (int i = 0; i < 10000; i++) {
int n = i;
for (int j = 3; j >= 0; j--) {
num[i][j] = n % 10 | '0';
n /= 10;
}
}
}
} constexpr pre;
inline void load() {
memcpy(ibuf, ibuf + pil, pir - pil);
pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);
pil = 0;
if (pir < SZ) ibuf[pir++] = '\n';
}
inline void flush() {
fwrite(obuf, 1, por, stdout);
por = 0;
}
void rd(char &c) {
do {
if (pil + 1 > pir) load();
c = ibuf[pil++];
} while (isspace(c));
}
void rd(string &x) {
x.clear();
char c;
do {
if (pil + 1 > pir) load();
c = ibuf[pil++];
} while (isspace(c));
do {
x += c;
if (pil == pir) load();
c = ibuf[pil++];
} while (!isspace(c));
}
template <typename T>
void rd_real(T &x) {
string s;
rd(s);
x = stod(s);
}
template <typename T>
void rd_integer(T &x) {
if (pil + 100 > pir) load();
char c;
do
c = ibuf[pil++];
while (c < '-');
bool minus = 0;
if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
if (c == '-') { minus = 1, c = ibuf[pil++]; }
}
x = 0;
while ('0' <= c) { x = x * 10 + (c & 15), c = ibuf[pil++]; }
if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
if (minus) x = -x;
}
}
void rd(int &x) { rd_integer(x); }
void rd(ll &x) { rd_integer(x); }
void rd(i128 &x) { rd_integer(x); }
void rd(u32 &x) { rd_integer(x); }
void rd(u64 &x) { rd_integer(x); }
void rd(u128 &x) { rd_integer(x); }
void rd(double &x) { rd_real(x); }
void rd(long double &x) { rd_real(x); }
void rd(f128 &x) { rd_real(x); }
template <class T, class U>
void rd(pair<T, U> &p) {
return rd(p.first), rd(p.second);
}
template <size_t N = 0, typename T>
void rd_tuple(T &t) {
if constexpr (N < std::tuple_size<T>::value) {
auto &x = std::get<N>(t);
rd(x);
rd_tuple<N + 1>(t);
}
}
template <class... T>
void rd(tuple<T...> &tpl) {
rd_tuple(tpl);
}
template <size_t N = 0, typename T>
void rd(array<T, N> &x) {
for (auto &d: x) rd(d);
}
template <class T>
void rd(vc<T> &x) {
for (auto &d: x) rd(d);
}
void read() {}
template <class H, class... T>
void read(H &h, T &... t) {
rd(h), read(t...);
}
void wt(const char c) {
if (por == SZ) flush();
obuf[por++] = c;
}
void wt(const string s) {
for (char c: s) wt(c);
}
void wt(const char *s) {
size_t len = strlen(s);
for (size_t i = 0; i < len; i++) wt(s[i]);
}
template <typename T>
void wt_integer(T x) {
if (por > SZ - 100) flush();
if (x < 0) { obuf[por++] = '-', x = -x; }
int outi;
for (outi = 96; x >= 10000; outi -= 4) {
memcpy(out + outi, pre.num[x % 10000], 4);
x /= 10000;
}
if (x >= 1000) {
memcpy(obuf + por, pre.num[x], 4);
por += 4;
} else if (x >= 100) {
memcpy(obuf + por, pre.num[x] + 1, 3);
por += 3;
} else if (x >= 10) {
int q = (x * 103) >> 10;
obuf[por] = q | '0';
obuf[por + 1] = (x - q * 10) | '0';
por += 2;
} else
obuf[por++] = x | '0';
memcpy(obuf + por, out + outi + 4, 96 - outi);
por += 96 - outi;
}
template <typename T>
void wt_real(T x) {
ostringstream oss;
oss << fixed << setprecision(15) << double(x);
string s = oss.str();
wt(s);
}
void wt(int x) { wt_integer(x); }
void wt(ll x) { wt_integer(x); }
void wt(i128 x) { wt_integer(x); }
void wt(u32 x) { wt_integer(x); }
void wt(u64 x) { wt_integer(x); }
void wt(u128 x) { wt_integer(x); }
void wt(double x) { wt_real(x); }
void wt(long double x) { wt_real(x); }
void wt(f128 x) { wt_real(x); }
template <class T, class U>
void wt(const pair<T, U> val) {
wt(val.first);
wt(' ');
wt(val.second);
}
template <size_t N = 0, typename T>
void wt_tuple(const T t) {
if constexpr (N < std::tuple_size<T>::value) {
if constexpr (N > 0) { wt(' '); }
const auto x = std::get<N>(t);
wt(x);
wt_tuple<N + 1>(t);
}
}
template <class... T>
void wt(tuple<T...> tpl) {
wt_tuple(tpl);
}
template <class T, size_t S>
void wt(const array<T, S> val) {
auto n = val.size();
for (size_t i = 0; i < n; i++) {
if (i) wt(' ');
wt(val[i]);
}
}
template <class T>
void wt(const vector<T> val) {
auto n = val.size();
for (size_t i = 0; i < n; i++) {
if (i) wt(' ');
wt(val[i]);
}
}
void print() { wt('\n'); }
template <class Head, class... Tail>
void print(Head &&head, Tail &&... tail) {
wt(head);
if (sizeof...(Tail)) wt(' ');
print(forward<Tail>(tail)...);
}
// gcc expansion. called automaticall after main.
void __attribute__((destructor)) _d() { flush(); }
} // namespace fastio
using fastio::read;
using fastio::print;
using fastio::flush;
#if defined(LOCAL)
#define SHOW(...) SHOW_IMPL(__VA_ARGS__, SHOW6, SHOW5, SHOW4, SHOW3, SHOW2, SHOW1)(__VA_ARGS__)
#define SHOW_IMPL(_1, _2, _3, _4, _5, _6, NAME, ...) NAME
#define SHOW1(x) print(#x, "=", (x)), flush()
#define SHOW2(x, y) print(#x, "=", (x), #y, "=", (y)), flush()
#define SHOW3(x, y, z) print(#x, "=", (x), #y, "=", (y), #z, "=", (z)), flush()
#define SHOW4(x, y, z, w) print(#x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w)), flush()
#define SHOW5(x, y, z, w, v) print(#x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w), #v, "=", (v)), flush()
#define SHOW6(x, y, z, w, v, u) print(#x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w), #v, "=", (v), #u, "=", (u)), flush()
#else
#define SHOW(...)
#endif
#define INT(...) \
int __VA_ARGS__; \
read(__VA_ARGS__)
#define LL(...) \
ll __VA_ARGS__; \
read(__VA_ARGS__)
#define U32(...) \
u32 __VA_ARGS__; \
read(__VA_ARGS__)
#define U64(...) \
u64 __VA_ARGS__; \
read(__VA_ARGS__)
#define STR(...) \
string __VA_ARGS__; \
read(__VA_ARGS__)
#define CHAR(...) \
char __VA_ARGS__; \
read(__VA_ARGS__)
#define DBL(...) \
double __VA_ARGS__; \
read(__VA_ARGS__)
#define VEC(type, name, size) \
vector<type> name(size); \
read(name)
#define VV(type, name, h, w) \
vector<vector<type>> name(h, vector<type>(w)); \
read(name)
void YES(bool t = 1) { print(t ? "YES" : "NO"); }
void NO(bool t = 1) { YES(!t); }
void Yes(bool t = 1) { print(t ? "Yes" : "No"); }
void No(bool t = 1) { Yes(!t); }
void yes(bool t = 1) { print(t ? "yes" : "no"); }
void no(bool t = 1) { yes(!t); }
#line 2 "/home/maspy/compro/library/graph/tree.hpp"
#line 2 "/home/maspy/compro/library/graph/base.hpp"
template <typename T>
struct Edge {
int frm, to;
T cost;
int id;
};
template <typename T = int, bool directed = false>
struct Graph {
static constexpr bool is_directed = directed;
int N, M;
using cost_type = T;
using edge_type = Edge<T>;
vector<edge_type> edges;
vector<int> indptr;
vector<edge_type> csr_edges;
vc<int> vc_deg, vc_indeg, vc_outdeg;
bool prepared;
class OutgoingEdges {
public:
OutgoingEdges(const Graph* G, int l, int r) : G(G), l(l), r(r) {}
const edge_type* begin() const {
if (l == r) { return 0; }
return &G->csr_edges[l];
}
const edge_type* end() const {
if (l == r) { return 0; }
return &G->csr_edges[r];
}
private:
const Graph* G;
int l, r;
};
bool is_prepared() { return prepared; }
Graph() : N(0), M(0), prepared(0) {}
Graph(int N) : N(N), M(0), prepared(0) {}
void build(int n) {
N = n, M = 0;
prepared = 0;
edges.clear();
indptr.clear();
csr_edges.clear();
vc_deg.clear();
vc_indeg.clear();
vc_outdeg.clear();
}
void add(int frm, int to, T cost = 1, int i = -1) {
assert(!prepared);
assert(0 <= frm && 0 <= to && to < N);
if (i == -1) i = M;
auto e = edge_type({frm, to, cost, i});
edges.eb(e);
++M;
}
#ifdef FASTIO
// wt, off
void read_tree(bool wt = false, int off = 1) { read_graph(N - 1, wt, off); }
void read_graph(int M, bool wt = false, int off = 1) {
for (int m = 0; m < M; ++m) {
INT(a, b);
a -= off, b -= off;
if (!wt) {
add(a, b);
} else {
T c;
read(c);
add(a, b, c);
}
}
build();
}
#endif
void build() {
assert(!prepared);
prepared = true;
indptr.assign(N + 1, 0);
for (auto&& e: edges) {
indptr[e.frm + 1]++;
if (!directed) indptr[e.to + 1]++;
}
for (int v = 0; v < N; ++v) { indptr[v + 1] += indptr[v]; }
auto counter = indptr;
csr_edges.resize(indptr.back() + 1);
for (auto&& e: edges) {
csr_edges[counter[e.frm]++] = e;
if (!directed)
csr_edges[counter[e.to]++] = edge_type({e.to, e.frm, e.cost, e.id});
}
}
OutgoingEdges operator[](int v) const {
assert(prepared);
return {this, indptr[v], indptr[v + 1]};
}
vc<int> deg_array() {
if (vc_deg.empty()) calc_deg();
return vc_deg;
}
pair<vc<int>, vc<int>> deg_array_inout() {
if (vc_indeg.empty()) calc_deg_inout();
return {vc_indeg, vc_outdeg};
}
int deg(int v) {
if (vc_deg.empty()) calc_deg();
return vc_deg[v];
}
int in_deg(int v) {
if (vc_indeg.empty()) calc_deg_inout();
return vc_indeg[v];
}
int out_deg(int v) {
if (vc_outdeg.empty()) calc_deg_inout();
return vc_outdeg[v];
}
#ifdef FASTIO
void debug() {
print("Graph");
if (!prepared) {
print("frm to cost id");
for (auto&& e: edges) print(e.frm, e.to, e.cost, e.id);
} else {
print("indptr", indptr);
print("frm to cost id");
FOR(v, N) for (auto&& e: (*this)[v]) print(e.frm, e.to, e.cost, e.id);
}
}
#endif
vc<int> new_idx;
vc<bool> used_e;
// G における頂点 V[i] が、新しいグラフで i になるようにする
// {G, es}
// sum(deg(v)) の計算量になっていて、
// 新しいグラフの n+m より大きい可能性があるので注意
Graph<T, directed> rearrange(vc<int> V, bool keep_eid = 0) {
if (len(new_idx) != N) new_idx.assign(N, -1);
int n = len(V);
FOR(i, n) new_idx[V[i]] = i;
Graph<T, directed> G(n);
vc<int> history;
FOR(i, n) {
for (auto&& e: (*this)[V[i]]) {
if (len(used_e) <= e.id) used_e.resize(e.id + 1);
if (used_e[e.id]) continue;
int a = e.frm, b = e.to;
if (new_idx[a] != -1 && new_idx[b] != -1) {
history.eb(e.id);
used_e[e.id] = 1;
int eid = (keep_eid ? e.id : -1);
G.add(new_idx[a], new_idx[b], e.cost, eid);
}
}
}
FOR(i, n) new_idx[V[i]] = -1;
for (auto&& eid: history) used_e[eid] = 0;
G.build();
return G;
}
Graph<T, true> to_directed_tree(int root = -1) {
if (root == -1) root = 0;
assert(!is_directed && prepared && M == N - 1);
Graph<T, true> G1(N);
vc<int> par(N, -1);
auto dfs = [&](auto& dfs, int v) -> void {
for (auto& e: (*this)[v]) {
if (e.to == par[v]) continue;
par[e.to] = v, dfs(dfs, e.to);
}
};
dfs(dfs, root);
for (auto& e: edges) {
int a = e.frm, b = e.to;
if (par[a] == b) swap(a, b);
assert(par[b] == a);
G1.add(a, b, e.cost);
}
G1.build();
return G1;
}
private:
void calc_deg() {
assert(vc_deg.empty());
vc_deg.resize(N);
for (auto&& e: edges) vc_deg[e.frm]++, vc_deg[e.to]++;
}
void calc_deg_inout() {
assert(vc_indeg.empty());
vc_indeg.resize(N);
vc_outdeg.resize(N);
for (auto&& e: edges) { vc_indeg[e.to]++, vc_outdeg[e.frm]++; }
}
};
#line 4 "/home/maspy/compro/library/graph/tree.hpp"
// HLD euler tour をとっていろいろ。
template <typename GT>
struct Tree {
using Graph_type = GT;
GT &G;
using WT = typename GT::cost_type;
int N;
vector<int> LID, RID, head, V, parent, VtoE;
vc<int> depth;
vc<WT> depth_weighted;
Tree(GT &G, int r = 0, bool hld = 1) : G(G) { build(r, hld); }
void build(int r = 0, bool hld = 1) {
if (r == -1) return; // build を遅延したいとき
N = G.N;
LID.assign(N, -1), RID.assign(N, -1), head.assign(N, r);
V.assign(N, -1), parent.assign(N, -1), VtoE.assign(N, -1);
depth.assign(N, -1), depth_weighted.assign(N, 0);
assert(G.is_prepared());
int t1 = 0;
dfs_sz(r, -1, hld);
dfs_hld(r, t1);
}
void dfs_sz(int v, int p, bool hld) {
auto &sz = RID;
parent[v] = p;
depth[v] = (p == -1 ? 0 : depth[p] + 1);
sz[v] = 1;
int l = G.indptr[v], r = G.indptr[v + 1];
auto &csr = G.csr_edges;
// 使う辺があれば先頭にする
for (int i = r - 2; i >= l; --i) {
if (hld && depth[csr[i + 1].to] == -1) swap(csr[i], csr[i + 1]);
}
int hld_sz = 0;
for (int i = l; i < r; ++i) {
auto e = csr[i];
if (depth[e.to] != -1) continue;
depth_weighted[e.to] = depth_weighted[v] + e.cost;
VtoE[e.to] = e.id;
dfs_sz(e.to, v, hld);
sz[v] += sz[e.to];
if (hld && chmax(hld_sz, sz[e.to]) && l < i) { swap(csr[l], csr[i]); }
}
}
void dfs_hld(int v, int ×) {
LID[v] = times++;
RID[v] += LID[v];
V[LID[v]] = v;
bool heavy = true;
for (auto &&e: G[v]) {
if (depth[e.to] <= depth[v]) continue;
head[e.to] = (heavy ? head[v] : e.to);
heavy = false;
dfs_hld(e.to, times);
}
}
vc<int> heavy_path_at(int v) {
vc<int> P = {v};
while (1) {
int a = P.back();
for (auto &&e: G[a]) {
if (e.to != parent[a] && head[e.to] == v) {
P.eb(e.to);
break;
}
}
if (P.back() == a) break;
}
return P;
}
int heavy_child(int v) {
int k = LID[v] + 1;
if (k == N) return -1;
int w = V[k];
return (parent[w] == v ? w : -1);
}
int e_to_v(int eid) {
auto e = G.edges[eid];
return (parent[e.frm] == e.to ? e.frm : e.to);
}
int v_to_e(int v) { return VtoE[v]; }
int get_eid(int u, int v) {
if (parent[u] != v) swap(u, v);
assert(parent[u] == v);
return VtoE[u];
}
int ELID(int v) { return 2 * LID[v] - depth[v]; }
int ERID(int v) { return 2 * RID[v] - depth[v] - 1; }
// 目標地点へ進む個数が k
int LA(int v, int k) {
assert(k <= depth[v]);
while (1) {
int u = head[v];
if (LID[v] - k >= LID[u]) return V[LID[v] - k];
k -= LID[v] - LID[u] + 1;
v = parent[u];
}
}
int la(int u, int v) { return LA(u, v); }
int LCA(int u, int v) {
for (;; v = parent[head[v]]) {
if (LID[u] > LID[v]) swap(u, v);
if (head[u] == head[v]) return u;
}
}
int meet(int a, int b, int c) { return LCA(a, b) ^ LCA(a, c) ^ LCA(b, c); }
int lca(int u, int v) { return LCA(u, v); }
int subtree_size(int v, int root = -1) {
if (root == -1) return RID[v] - LID[v];
if (v == root) return N;
int x = jump(v, root, 1);
if (in_subtree(v, x)) return RID[v] - LID[v];
return N - RID[x] + LID[x];
}
int dist(int a, int b) {
int c = LCA(a, b);
return depth[a] + depth[b] - 2 * depth[c];
}
WT dist_weighted(int a, int b) {
int c = LCA(a, b);
return depth_weighted[a] + depth_weighted[b] - WT(2) * depth_weighted[c];
}
// a is in b
bool in_subtree(int a, int b) { return LID[b] <= LID[a] && LID[a] < RID[b]; }
int jump(int a, int b, ll k) {
if (k == 1) {
if (a == b) return -1;
return (in_subtree(b, a) ? LA(b, depth[b] - depth[a] - 1) : parent[a]);
}
int c = LCA(a, b);
int d_ac = depth[a] - depth[c];
int d_bc = depth[b] - depth[c];
if (k > d_ac + d_bc) return -1;
if (k <= d_ac) return LA(a, k);
return LA(b, d_ac + d_bc - k);
}
vc<int> collect_child(int v) {
vc<int> res;
for (auto &&e: G[v])
if (e.to != parent[v]) res.eb(e.to);
return res;
}
vc<int> collect_light(int v) {
vc<int> res;
bool skip = true;
for (auto &&e: G[v])
if (e.to != parent[v]) {
if (!skip) res.eb(e.to);
skip = false;
}
return res;
}
vc<pair<int, int>> get_path_decomposition(int u, int v, bool edge) {
// [始点, 終点] の"閉"区間列。
vc<pair<int, int>> up, down;
while (1) {
if (head[u] == head[v]) break;
if (LID[u] < LID[v]) {
down.eb(LID[head[v]], LID[v]);
v = parent[head[v]];
} else {
up.eb(LID[u], LID[head[u]]);
u = parent[head[u]];
}
}
if (LID[u] < LID[v]) down.eb(LID[u] + edge, LID[v]);
elif (LID[v] + edge <= LID[u]) up.eb(LID[u], LID[v] + edge);
reverse(all(down));
up.insert(up.end(), all(down));
return up;
}
// 辺の列の情報 (frm,to,str)
// str = "heavy_up", "heavy_down", "light_up", "light_down"
vc<tuple<int, int, string>> get_path_decomposition_detail(int u, int v) {
vc<tuple<int, int, string>> up, down;
while (1) {
if (head[u] == head[v]) break;
if (LID[u] < LID[v]) {
if (v != head[v]) down.eb(head[v], v, "heavy_down"), v = head[v];
down.eb(parent[v], v, "light_down"), v = parent[v];
} else {
if (u != head[u]) up.eb(u, head[u], "heavy_up"), u = head[u];
up.eb(u, parent[u], "light_up"), u = parent[u];
}
}
if (LID[u] < LID[v]) down.eb(u, v, "heavy_down");
elif (LID[v] < LID[u]) up.eb(u, v, "heavy_up");
reverse(all(down));
concat(up, down);
return up;
}
vc<int> restore_path(int u, int v) {
vc<int> P;
for (auto &&[a, b]: get_path_decomposition(u, v, 0)) {
if (a <= b) {
FOR(i, a, b + 1) P.eb(V[i]);
} else {
FOR_R(i, b, a + 1) P.eb(V[i]);
}
}
return P;
}
// path [a,b] と [c,d] の交わり. 空ならば {-1,-1}.
// https://codeforces.com/problemset/problem/500/G
pair<int, int> path_intersection(int a, int b, int c, int d) {
int ab = lca(a, b), ac = lca(a, c), ad = lca(a, d);
int bc = lca(b, c), bd = lca(b, d), cd = lca(c, d);
int x = ab ^ ac ^ bc, y = ab ^ ad ^ bd; // meet(a,b,c), meet(a,b,d)
if (x != y) return {x, y};
int z = ac ^ ad ^ cd;
if (x != z) x = -1;
return {x, x};
}
};
#line 3 "/home/maspy/compro/library/graph/shortest_path/bfs01.hpp"
template <typename T, typename GT>
pair<vc<T>, vc<int>> bfs01(GT& G, int v) {
assert(G.is_prepared());
int N = G.N;
vc<T> dist(N, infty<T>);
vc<int> par(N, -1);
deque<int> que;
dist[v] = 0;
que.push_front(v);
while (!que.empty()) {
auto v = que.front();
que.pop_front();
for (auto&& e: G[v]) {
if (dist[e.to] == infty<T> || dist[e.to] > dist[e.frm] + e.cost) {
dist[e.to] = dist[e.frm] + e.cost;
par[e.to] = e.frm;
if (e.cost == 0)
que.push_front(e.to);
else
que.push_back(e.to);
}
}
}
return {dist, par};
}
// 多点スタート。[dist, par, root]
template <typename T, typename GT>
tuple<vc<T>, vc<int>, vc<int>> bfs01(GT& G, vc<int> vs) {
assert(G.is_prepared());
int N = G.N;
vc<T> dist(N, infty<T>);
vc<int> par(N, -1);
vc<int> root(N, -1);
deque<int> que;
for (auto&& v: vs) {
dist[v] = 0;
root[v] = v;
que.push_front(v);
}
while (!que.empty()) {
auto v = que.front();
que.pop_front();
for (auto&& e: G[v]) {
if (dist[e.to] == infty<T> || dist[e.to] > dist[e.frm] + e.cost) {
dist[e.to] = dist[e.frm] + e.cost;
root[e.to] = root[e.frm];
par[e.to] = e.frm;
if (e.cost == 0)
que.push_front(e.to);
else
que.push_back(e.to);
}
}
}
return {dist, par, root};
}
#line 3 "/home/maspy/compro/library/graph/centroid_decomposition.hpp"
// 頂点ベースの重心分解
// f(par, V, indptr)
template <typename F>
void centroid_decomposition_0_dfs(vc<int>& par, vc<int>& vs, F f) {
const int N = len(par);
assert(N >= 1);
int c = -1;
vc<int> sz(N, 1);
FOR_R(i, N) {
if (sz[i] >= ceil<int>(N, 2)) {
c = i;
break;
}
sz[par[i]] += sz[i];
}
vc<int> color(N);
vc<int> V = {c};
int nc = 1;
FOR(v, 1, N) {
if (par[v] == c) { V.eb(v), color[v] = nc++; }
}
if (c > 0) {
for (int a = par[c]; a != -1; a = par[a]) { color[a] = nc, V.eb(a); }
++nc;
}
FOR(i, N) {
if (i != c && color[i] == 0) color[i] = color[par[i]], V.eb(i);
}
vc<int> indptr(nc + 1);
FOR(i, N) indptr[1 + color[i]]++;
FOR(i, nc) indptr[i + 1] += indptr[i];
vc<int> counter = indptr;
vc<int> ord(N);
for (auto& v: V) { ord[counter[color[v]]++] = v; }
vc<int> new_idx(N);
FOR(i, N) new_idx[ord[i]] = i;
vc<int> name(N);
FOR(i, N) name[new_idx[i]] = vs[i];
{
vc<int> tmp(N, -1);
FOR(i, 1, N) {
int a = new_idx[i], b = new_idx[par[i]];
if (a > b) swap(a, b);
tmp[b] = a;
}
swap(par, tmp);
}
f(par, name, indptr);
FOR(k, 1, nc) {
int L = indptr[k], R = indptr[k + 1];
vc<int> par1(R - L, -1);
vc<int> name1(R - L, -1);
name1[0] = name[0];
FOR(i, L, R) name1[i - L] = name[i];
FOR(i, L, R) { par1[i - L] = max(par[i] - L, -1); }
centroid_decomposition_0_dfs(par1, name1, f);
}
}
/*
https://maspypy.com/%e9%87%8d%e5%bf%83%e5%88%86%e8%a7%a3%e3%83%bb1-3%e9%87%8d%e5%bf%83%e5%88%86%e8%a7%a3%e3%81%ae%e3%81%8a%e7%b5%b5%e6%8f%8f%e3%81%8d
centroid_decomposition_1:長さ 1 以上のパス全体
f(par, V, L1, R1, L2, R2)
[L1, R1): color 1 / [L2, R2): color 2
*/
template <typename F>
void centroid_decomposition_1_dfs(vc<int>& par, vc<int> vs, F f) {
const int N = len(par);
assert(N > 1);
if (N == 2) {
vc<int> p = {-1, 0};
vc<int> v = {vs[0], vs[1]};
f(p, vs, 0, 1, 1, 2);
return;
}
int c = -1;
vc<int> sz(N, 1);
FOR_R(i, N) {
if (sz[i] >= ceil<int>(N, 2)) {
c = i;
break;
}
sz[par[i]] += sz[i];
}
vc<int> color(N, -1);
int take = 0;
vc<int> ord(N, -1);
ord[c] = 0;
int p = 1;
FOR(v, 1, N) {
if (par[v] == c && take + sz[v] <= floor<int>(N - 1, 2)) { color[v] = 0, ord[v] = p++, take += sz[v]; }
}
FOR(i, 1, N) {
if (color[par[i]] == 0) color[i] = 0, ord[i] = p++;
}
int n0 = p - 1;
for (int a = par[c]; a != -1; a = par[a]) { color[a] = 1, ord[a] = p++; }
FOR(i, N) {
if (i != c && color[i] == -1) color[i] = 1, ord[i] = p++;
}
assert(p == N);
int n1 = N - 1 - n0;
vc<int> par0(n0 + 1, -1), par1(n1 + 1, -1), par2(N, -1);
vc<int> V0(n0 + 1), V1(n1 + 1), V2(N);
FOR(v, N) {
int i = ord[v];
V2[i] = vs[v];
if (color[v] != 1) { V0[i] = vs[v]; }
if (color[v] != 0) { V1[max(i - n0, 0)] = vs[v]; }
}
FOR(v, 1, N) {
int a = ord[v], b = ord[par[v]];
if (a > b) swap(a, b);
par2[b] = a;
if (color[v] != 1 && color[par[v]] != 1) par0[b] = a;
if (color[v] != 0 && color[par[v]] != 0) par1[max(b - n0, 0)] = max(a - n0, 0);
}
f(par2, V2, 1, 1 + n0, 1 + n0, 1 + n0 + n1);
centroid_decomposition_1_dfs(par0, V0, f);
centroid_decomposition_1_dfs(par1, V1, f);
}
/*
https://maspypy.com/%e9%87%8d%e5%bf%83%e5%88%86%e8%a7%a3%e3%83%bb1-3%e9%87%8d%e5%bf%83%e5%88%86%e8%a7%a3%e3%81%ae%e3%81%8a%e7%b5%b5%e6%8f%8f%e3%81%8d
f(par, V, color)
color in [-1,0,1], -1 is virtual.
*/
template <typename F>
void centroid_decomposition_2_dfs(vc<int>& par, vc<int>& vs, vc<int>& real, F f) {
const int N = len(par);
assert(N > 1);
if (N == 2) {
if (real[0] && real[1]) {
vc<int> color = {0, 1};
f(par, vs, color);
}
return;
}
int c = -1;
vc<int> sz(N, 1);
FOR_R(i, N) {
if (sz[i] >= ceil<int>(N, 2)) {
c = i;
break;
}
sz[par[i]] += sz[i];
}
vc<int> color(N, -1);
int take = 0;
vc<int> ord(N, -1);
ord[c] = 0;
int p = 1;
FOR(v, 1, N) {
if (par[v] == c && take + sz[v] <= floor<int>(N - 1, 2)) { color[v] = 0, ord[v] = p++, take += sz[v]; }
}
FOR(i, 1, N) {
if (color[par[i]] == 0) color[i] = 0, ord[i] = p++;
}
int n0 = p - 1;
for (int a = par[c]; a != -1; a = par[a]) { color[a] = 1, ord[a] = p++; }
FOR(i, N) {
if (i != c && color[i] == -1) color[i] = 1, ord[i] = p++;
}
assert(p == N);
int n1 = N - 1 - n0;
vc<int> par0(n0 + 1, -1), par1(n1 + 1, -1), par2(N, -1);
vc<int> V0(n0 + 1), V1(n1 + 1), V2(N);
vc<int> rea0(n0 + 1), rea1(n1 + 1), rea2(N);
FOR(v, N) {
int i = ord[v];
V2[i] = vs[v], rea2[i] = real[v];
if (color[v] != 1) { V0[i] = vs[v], rea0[i] = real[v]; }
if (color[v] != 0) { V1[max(i - n0, 0)] = vs[v], rea1[max(i - n0, 0)] = real[v]; }
}
FOR(v, 1, N) {
int a = ord[v], b = ord[par[v]];
if (a > b) swap(a, b);
par2[b] = a;
if (color[v] != 1 && color[par[v]] != 1) par0[b] = a;
if (color[v] != 0 && color[par[v]] != 0) par1[max(b - n0, 0)] = max(a - n0, 0);
}
color.assign(N, -1);
FOR(i, 1, N) if (rea2[i]) color[i] = (i <= n0 ? 0 : 1);
if (real[c]) color[0] = 2, rea0[0] = rea1[0] = rea2[0] = 0;
f(par2, V2, color);
centroid_decomposition_2_dfs(par0, V0, rea0, f);
centroid_decomposition_2_dfs(par1, V1, rea1, f);
}
// 0: f(par, V, indptr)
// 1: f(par, V, L1, R1, L2, R2)
// 2: f(par, V, color)
template <int MODE, typename GT, typename F>
void centroid_decomposition(GT& G, F f) {
static_assert(!GT::is_directed);
const int N = G.N;
if (MODE != 0 && N == 1) return;
vc<int> V(N), par(N, -1);
int l = 0, r = 0;
V[r++] = 0;
while (l < r) {
int v = V[l++];
for (auto& e: G[v]) {
if (e.to != par[v]) V[r++] = e.to, par[e.to] = v;
}
}
assert(r == N);
vc<int> new_idx(N);
FOR(i, N) new_idx[V[i]] = i;
vc<int> tmp(N, -1);
FOR(i, 1, N) {
int j = par[i];
tmp[new_idx[i]] = new_idx[j];
}
swap(par, tmp);
static_assert(MODE == 0 || MODE == 1 || MODE == 2);
if constexpr (MODE == 0) { centroid_decomposition_0_dfs(par, V, f); }
elif constexpr(MODE == 1) { centroid_decomposition_1_dfs(par, V, f); }
else {
vc<int> real(N, 1);
centroid_decomposition_2_dfs(par, V, real, f);
}
}
#line 5 "main.cpp"
void solve() {
LL(N, K);
vvc<pair<int, int>> dat(N);
vi A(N, infty<ll>);
Graph<int, 0> G(N);
FOR(v, 1, N) {
INT(p);
G.add(p, v);
}
G.build();
auto f = [&](vc<int> par, vc<int> V, vc<int> indptr) -> void {
int n = len(V);
vc<int> D(n);
FOR(i, 1, n) D[i] = D[par[i]] + 1;
FOR(i, n) { dat[V[i]].eb(V[0], D[i]); }
};
centroid_decomposition<0>(G, f);
Tree<decltype(G)> tree(G);
auto V = argsort(tree.depth);
reverse(all(V));
ll ANS = 0;
for (auto& v: V) {
ll d = infty<ll>;
for (auto& [i, x]: dat[v]) chmin(d, A[i] + x);
if (d < K) continue;
++ANS;
for (auto& [i, x]: dat[v]) chmin(A[i], x);
}
print(ANS);
}
signed main() { solve(); }
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 11
Accepted
Test #1:
score: 11
Accepted
time: 1ms
memory: 3800kb
input:
10 2 0 0 2 2 0 3 4 7 2
output:
6
result:
ok single line: '6'
Test #2:
score: 11
Accepted
time: 0ms
memory: 3804kb
input:
10 3 0 0 2 1 4 4 3 1 7
output:
4
result:
ok single line: '4'
Test #3:
score: 11
Accepted
time: 0ms
memory: 3728kb
input:
12 1 0 0 1 3 1 0 2 1 3 2 2
output:
12
result:
ok single line: '12'
Test #4:
score: 11
Accepted
time: 0ms
memory: 4044kb
input:
12 4 0 0 1 2 0 0 4 7 7 9 10
output:
3
result:
ok single line: '3'
Test #5:
score: 11
Accepted
time: 0ms
memory: 3972kb
input:
14 2 0 1 2 3 0 1 5 1 4 0 3 11 9
output:
8
result:
ok single line: '8'
Test #6:
score: 11
Accepted
time: 0ms
memory: 3692kb
input:
14 4 0 0 2 3 4 2 6 3 6 9 3 3 2
output:
3
result:
ok single line: '3'
Test #7:
score: 11
Accepted
time: 0ms
memory: 3736kb
input:
16 2 0 0 0 1 2 3 2 6 6 0 4 11 0 12 2
output:
11
result:
ok single line: '11'
Test #8:
score: 11
Accepted
time: 0ms
memory: 3800kb
input:
16 3 0 0 0 3 2 2 0 2 8 4 6 11 8 3 5
output:
6
result:
ok single line: '6'
Test #9:
score: 11
Accepted
time: 0ms
memory: 3676kb
input:
18 1 0 0 2 2 2 1 0 3 1 4 2 2 12 0 6 10 13
output:
18
result:
ok single line: '18'
Test #10:
score: 11
Accepted
time: 0ms
memory: 3716kb
input:
18 5 0 0 2 1 0 2 6 5 8 5 0 8 5 4 12 7 10
output:
4
result:
ok single line: '4'
Test #11:
score: 11
Accepted
time: 0ms
memory: 3736kb
input:
18 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
output:
18
result:
ok single line: '18'
Test #12:
score: 11
Accepted
time: 0ms
memory: 3740kb
input:
18 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
output:
17
result:
ok single line: '17'
Test #13:
score: 11
Accepted
time: 0ms
memory: 3736kb
input:
17 1 0 1 1 0 0 1 2 0 1 0 2 1 2 2 2 2
output:
17
result:
ok single line: '17'
Test #14:
score: 11
Accepted
time: 0ms
memory: 4044kb
input:
17 2 0 1 0 1 1 0 2 1 2 1 1 0 1 2 0 0
output:
14
result:
ok single line: '14'
Test #15:
score: 11
Accepted
time: 0ms
memory: 3720kb
input:
18 5 0 0 1 1 0 1 2 2 2 1 0 0 1 0 1 1 0
output:
1
result:
ok single line: '1'
Test #16:
score: 11
Accepted
time: 0ms
memory: 3720kb
input:
18 1 0 1 2 0 1 0 4 0 2 3 0 3 2 1 3 4 2
output:
18
result:
ok single line: '18'
Test #17:
score: 11
Accepted
time: 0ms
memory: 3804kb
input:
18 2 0 0 0 1 2 2 3 3 1 3 4 1 1 4 2 4 0
output:
13
result:
ok single line: '13'
Test #18:
score: 11
Accepted
time: 0ms
memory: 3696kb
input:
18 3 0 1 1 3 1 4 2 1 0 2 0 0 1 1 3 0 3
output:
5
result:
ok single line: '5'
Test #19:
score: 11
Accepted
time: 0ms
memory: 3752kb
input:
18 6 0 0 2 3 4 3 3 1 6 1 2 0 2 0 5 2 4
output:
2
result:
ok single line: '2'
Test #20:
score: 11
Accepted
time: 0ms
memory: 3648kb
input:
18 7 0 1 0 3 2 0 6 3 4 4 3 2 6 3 6 0 0
output:
1
result:
ok single line: '1'
Subtask #2:
score: 40
Accepted
Dependency #1:
100%
Accepted
Test #21:
score: 40
Accepted
time: 2ms
memory: 4316kb
input:
1500 1000 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 ...
output:
2
result:
ok single line: '2'
Test #22:
score: 40
Accepted
time: 1ms
memory: 3916kb
input:
500 1 0 1 1 1 1 5 5 2 4 4 10 0 0 11 12 6 13 5 17 2 13 12 19 12 8 7 18 5 9 10 15 1 25 21 7 32 29 14 22 12 34 13 1 10 0 20 4 6 44 38 19 12 9 17 20 51 5 3 23 46 39 25 7 42 37 55 13 14 30 46 9 71 47 55 61 43 39 1 76 0 49 8 80 51 51 71 55 44 79 25 80 24 68 15 41 88 72 76 72 38 83 94 88 69 17 9 49 75 64 3...
output:
500
result:
ok single line: '500'
Test #23:
score: 40
Accepted
time: 1ms
memory: 3848kb
input:
500 5 0 1 2 0 3 5 0 2 0 3 10 1 3 5 13 8 8 16 16 4 17 5 1 17 6 1 17 9 20 0 27 27 1 14 24 30 17 5 37 37 9 33 7 20 39 24 29 20 33 19 32 26 13 33 31 21 31 11 20 50 35 34 16 47 17 5 48 55 60 66 57 43 10 68 69 69 34 75 27 41 3 56 8 32 41 85 55 70 5 57 48 61 41 78 19 12 85 73 62 18 79 95 47 39 61 67 88 18 ...
output:
89
result:
ok single line: '89'
Test #24:
score: 40
Accepted
time: 1ms
memory: 3944kb
input:
500 10 0 1 1 0 1 0 6 6 8 1 4 0 10 0 13 8 11 16 18 1 17 7 21 22 1 22 9 26 1 4 1 21 5 18 6 7 35 4 31 5 38 36 31 37 25 17 8 28 36 12 26 20 9 11 20 42 38 17 29 42 29 38 28 39 46 39 3 46 22 16 34 25 14 3 21 73 62 77 78 29 43 3 25 48 3 47 17 54 3 43 47 43 42 62 12 44 38 29 75 62 37 96 91 47 97 104 15 84 4...
output:
23
result:
ok single line: '23'
Test #25:
score: 40
Accepted
time: 1ms
memory: 4236kb
input:
1000 1 0 0 0 2 0 4 3 4 4 2 9 4 1 12 2 12 7 12 13 1 12 20 17 5 8 3 7 0 11 18 28 2 29 26 34 24 5 24 22 38 30 0 30 19 17 9 45 32 14 47 42 29 47 5 2 1 44 23 16 48 3 51 28 44 16 24 31 8 49 0 6 12 39 6 13 2 74 77 0 25 78 67 43 83 32 22 69 38 57 32 62 23 12 22 53 85 88 91 3 4 42 96 93 20 59 44 59 102 95 86...
output:
1000
result:
ok single line: '1000'
Test #26:
score: 40
Accepted
time: 1ms
memory: 3836kb
input:
1000 5 0 0 1 1 2 3 0 2 0 1 6 3 10 13 14 1 12 0 2 0 0 16 0 14 13 11 1 4 2 15 21 10 24 4 0 18 16 29 24 2 38 40 17 31 17 30 8 18 12 49 16 47 31 52 53 10 41 28 39 57 20 49 43 1 46 27 59 38 7 37 24 38 36 13 59 34 25 31 63 36 4 32 72 9 17 4 80 87 63 20 68 43 42 64 80 8 88 2 24 68 6 41 34 68 63 66 92 32 93...
output:
191
result:
ok single line: '191'
Test #27:
score: 40
Accepted
time: 1ms
memory: 4000kb
input:
1000 10 0 0 1 2 2 2 4 2 3 9 10 5 7 3 4 6 7 2 8 16 9 5 4 3 17 17 23 10 11 26 15 30 20 32 27 12 5 29 10 15 22 9 40 19 30 1 23 33 16 5 0 5 18 18 5 48 5 50 27 39 35 60 54 11 29 25 43 13 62 29 48 39 13 49 44 66 52 65 54 29 66 15 32 35 76 7 53 40 57 7 7 77 82 83 43 31 28 85 69 1 9 72 32 29 53 57 5 94 57 6...
output:
44
result:
ok single line: '44'
Test #28:
score: 40
Accepted
time: 2ms
memory: 4400kb
input:
1500 12 0 1 2 0 4 2 0 4 6 2 3 10 4 2 12 2 10 1 5 7 7 10 3 10 0 20 1 15 24 25 0 10 30 13 0 14 24 20 7 37 25 6 20 34 37 0 27 28 25 26 28 8 2 25 50 19 19 55 33 26 53 1 53 23 20 23 27 36 17 63 22 39 10 22 26 68 62 26 1 56 11 1 51 12 19 84 42 59 65 73 9 61 63 65 27 44 34 85 31 79 98 72 60 57 92 10 53 45 ...
output:
41
result:
ok single line: '41'
Test #29:
score: 40
Accepted
time: 2ms
memory: 4444kb
input:
1500 5 0 0 2 2 3 0 5 0 0 6 2 8 0 12 8 0 2 5 4 4 4 0 0 10 16 4 26 27 10 17 23 21 5 24 27 6 22 28 2 38 24 11 38 25 19 20 22 35 21 7 6 42 21 19 30 30 56 10 23 31 5 35 49 25 2 28 31 56 43 65 55 62 15 45 10 70 30 56 67 28 35 56 4 25 42 34 41 76 12 81 66 66 8 57 47 80 75 14 68 97 12 58 10 75 86 64 101 104...
output:
293
result:
ok single line: '293'
Test #30:
score: 40
Accepted
time: 2ms
memory: 4112kb
input:
1500 15 0 1 2 3 0 0 5 1 8 0 10 1 1 0 4 6 4 2 5 18 1 8 3 8 3 1 20 22 23 28 25 31 3 28 27 8 36 25 3 30 8 29 41 21 41 34 46 37 24 6 33 15 44 44 18 14 37 55 43 59 10 3 40 27 55 44 40 3 22 53 18 11 17 35 1 41 53 41 22 40 29 35 21 26 37 23 38 74 33 84 2 63 82 53 45 24 43 75 1 66 80 52 63 31 63 46 71 65 48...
output:
19
result:
ok single line: '19'
Test #31:
score: 40
Accepted
time: 1ms
memory: 4124kb
input:
1500 40 0 1 0 0 1 1 0 7 2 5 1 9 11 9 12 3 14 8 10 11 3 5 16 5 9 20 18 21 17 8 30 13 28 9 9 30 9 2 9 6 25 28 8 33 43 19 37 41 14 10 44 38 8 53 47 1 46 32 14 24 23 51 14 46 32 32 66 60 49 53 45 35 47 24 72 52 56 22 77 65 23 3 61 33 36 48 12 79 21 46 74 9 24 23 25 36 2 63 95 49 45 68 18 51 16 1 29 48 1...
output:
1
result:
ok single line: '1'
Test #32:
score: 40
Accepted
time: 1ms
memory: 4080kb
input:
1470 4 0 0 1 1 3 4 1 2 5 8 2 8 11 11 9 5 1 0 11 19 8 7 6 1 9 3 13 0 0 16 14 11 12 11 19 14 13 1 12 5 2 6 18 17 12 18 3 7 11 3 15 7 4 19 17 14 17 6 3 11 16 8 3 4 3 7 3 4 17 7 9 7 8 15 6 10 19 12 17 10 10 2 3 4 8 7 4 5 9 1 17 12 10 0 2 13 3 5 4 19 15 10 9 6 7 13 7 5 1 17 18 6 11 12 5 4 17 15 5 0 16 4 ...
output:
13
result:
ok single line: '13'
Test #33:
score: 40
Accepted
time: 1ms
memory: 3936kb
input:
1470 10 0 1 2 1 0 3 2 6 5 6 0 0 7 7 3 2 13 5 12 1 5 18 9 5 18 0 18 9 10 19 1 14 15 12 7 7 6 3 8 5 2 17 3 10 6 2 2 12 1 17 19 9 6 7 3 10 18 5 12 5 10 11 2 8 3 19 6 15 19 13 17 6 9 5 1 1 2 10 7 17 4 18 14 7 18 12 0 12 13 17 14 12 12 13 11 0 2 18 9 8 6 3 16 5 9 10 0 13 0 10 6 5 17 0 4 8 3 6 18 14 15 5 ...
output:
1
result:
ok single line: '1'
Test #34:
score: 40
Accepted
time: 1ms
memory: 4184kb
input:
1400 10 0 1 1 1 3 3 1 0 7 1 5 3 8 0 6 11 6 13 4 5 5 17 4 6 13 21 3 24 19 21 21 13 0 29 5 2 29 23 15 4 14 24 33 15 44 4 5 13 7 10 46 24 25 15 48 43 16 0 57 35 43 35 33 50 20 46 21 4 6 1 1 42 8 44 8 17 30 70 40 0 8 3 80 10 14 26 66 2 45 1 46 73 0 8 89 25 4 60 51 26 61 50 85 29 38 17 84 88 30 44 1 64 1...
output:
5
result:
ok single line: '5'
Test #35:
score: 40
Accepted
time: 0ms
memory: 4080kb
input:
1400 12 0 0 0 1 0 1 3 2 1 1 4 7 5 0 7 9 13 8 2 18 5 2 14 18 11 12 5 0 9 27 5 1 9 21 4 10 21 25 15 26 23 8 36 15 37 0 1 38 27 49 26 9 20 45 6 48 36 24 34 16 34 11 36 57 28 21 11 53 32 53 44 22 17 24 52 28 9 36 64 71 20 74 23 15 45 0 31 87 43 22 88 21 67 31 12 18 73 67 30 8 7 74 60 85 18 94 16 20 15 6...
output:
5
result:
ok single line: '5'
Test #36:
score: 40
Accepted
time: 1ms
memory: 4236kb
input:
1500 10 0 1 0 2 3 4 5 5 6 1 10 4 6 11 12 7 11 6 7 1 19 14 20 12 21 12 6 1 18 9 16 16 25 19 18 21 22 4 18 24 25 7 14 8 31 12 29 46 40 5 43 4 7 52 12 17 11 42 43 32 9 48 24 17 24 63 8 10 48 1 9 33 28 25 64 20 71 49 69 5 26 1 73 19 42 78 24 84 9 21 39 65 80 12 80 57 50 36 68 92 97 57 46 7 88 72 21 50 5...
output:
7
result:
ok single line: '7'
Test #37:
score: 40
Accepted
time: 1ms
memory: 4196kb
input:
1500 4 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 79 93 ...
output:
50
result:
ok single line: '50'
Test #38:
score: 40
Accepted
time: 2ms
memory: 4216kb
input:
1500 50 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 10...
output:
7
result:
ok single line: '7'
Test #39:
score: 40
Accepted
time: 2ms
memory: 4292kb
input:
1500 70 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 10...
output:
8
result:
ok single line: '8'
Test #40:
score: 40
Accepted
time: 2ms
memory: 4548kb
input:
1500 5 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100...
output:
301
result:
ok single line: '301'
Subtask #3:
score: 49
Accepted
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Test #41:
score: 49
Accepted
time: 107ms
memory: 35880kb
input:
190001 5 0 1 2 0 4 5 0 7 8 0 10 11 0 13 14 0 16 17 0 19 20 0 22 23 0 25 26 0 28 29 0 31 32 0 34 35 0 37 38 0 40 41 0 43 44 0 46 47 0 49 50 0 52 53 0 55 56 0 58 59 0 61 62 0 64 65 0 67 68 0 70 71 0 73 74 0 76 77 0 79 80 0 82 83 0 85 86 0 88 89 0 91 92 0 94 95 0 97 98 0 100 101 0 103 104 0 106 107 0 1...
output:
50462
result:
ok single line: '50462'
Test #42:
score: 49
Accepted
time: 99ms
memory: 29300kb
input:
100000 1 0 1 0 2 2 5 6 5 5 2 8 4 7 3 2 4 3 1 13 17 3 18 11 23 20 9 3 18 26 15 2 11 19 27 5 0 19 9 31 17 2 7 12 2 1 5 40 6 4 33 16 30 8 40 2 26 4 23 0 32 39 32 43 11 52 8 61 15 0 62 16 56 11 34 21 14 64 23 11 71 33 78 60 76 65 19 45 9 38 79 74 45 39 49 55 17 64 53 90 78 70 4 8 13 73 26 4 98 63 70 105...
output:
100000
result:
ok single line: '100000'
Test #43:
score: 49
Accepted
time: 97ms
memory: 29744kb
input:
100000 20 0 1 1 0 2 5 6 3 5 5 2 3 5 13 0 2 8 16 8 5 16 12 4 22 2 7 26 23 26 28 12 17 23 21 4 17 1 31 14 4 24 21 28 33 33 24 35 27 15 33 42 32 32 7 46 39 20 44 52 0 13 5 54 11 43 0 45 43 25 2 44 66 50 20 18 34 28 75 28 21 24 23 10 62 23 79 53 81 71 77 29 43 42 83 86 13 24 87 10 67 60 77 54 74 74 52 8...
output:
454
result:
ok single line: '454'
Test #44:
score: 49
Accepted
time: 92ms
memory: 29860kb
input:
100000 50 0 1 1 2 0 3 5 3 7 9 5 6 4 6 8 7 15 3 10 1 17 15 14 22 2 4 19 9 5 12 2 4 20 18 8 8 35 15 26 13 19 35 30 34 20 42 27 0 26 39 41 43 51 3 39 24 38 56 48 32 59 50 56 33 1 65 61 14 19 61 40 6 50 47 59 54 48 69 29 40 61 76 11 37 16 46 53 23 53 40 63 4 12 50 44 55 89 10 85 9 72 95 81 85 18 85 36 6...
output:
1
result:
ok single line: '1'
Test #45:
score: 49
Accepted
time: 87ms
memory: 30192kb
input:
100000 100 0 1 0 3 1 2 2 7 6 9 7 11 3 12 14 1 2 2 2 14 5 7 15 21 18 0 18 6 18 16 1 7 4 26 34 29 31 37 36 11 38 18 7 29 37 45 4 40 37 4 0 14 9 2 25 36 36 45 40 56 34 41 61 37 32 52 59 65 40 19 67 41 41 43 14 72 49 73 69 67 59 32 50 16 35 44 4 2 25 0 49 4 85 68 57 43 78 96 36 93 23 9 24 47 92 55 54 56...
output:
1
result:
ok single line: '1'
Test #46:
score: 49
Accepted
time: 205ms
memory: 55140kb
input:
200000 2 0 0 0 0 3 4 1 0 0 9 7 7 1 11 3 9 4 12 18 7 14 13 22 9 18 0 6 15 17 19 8 31 5 30 1 23 1 10 38 33 32 2 27 38 8 21 29 33 8 27 28 15 9 1 29 25 32 45 55 26 15 50 17 38 3 22 38 7 64 39 10 67 37 8 44 15 45 40 53 11 16 44 9 51 30 44 8 35 85 42 53 58 36 67 25 78 75 9 42 52 54 50 101 97 10 25 26 62 7...
output:
119331
result:
ok single line: '119331'
Test #47:
score: 49
Accepted
time: 207ms
memory: 54872kb
input:
200000 40 0 1 2 2 0 2 0 1 2 3 2 9 11 3 11 15 14 12 16 8 11 6 0 19 11 16 14 8 21 18 12 3 14 31 23 19 9 19 13 23 32 29 42 16 27 7 26 7 37 38 44 33 8 33 54 43 34 3 16 16 13 14 11 38 46 19 66 30 19 31 33 71 41 41 32 42 8 74 4 79 79 34 35 52 74 68 11 4 1 4 5 42 28 24 1 11 93 4 0 68 61 51 35 83 5 14 102 5...
output:
14
result:
ok single line: '14'
Test #48:
score: 49
Accepted
time: 213ms
memory: 55092kb
input:
200000 80 0 1 0 0 0 2 6 5 6 3 9 4 7 8 14 2 0 17 2 16 2 4 13 8 23 8 3 11 24 27 21 18 6 26 14 27 2 25 13 39 8 1 36 15 25 19 21 14 30 17 46 32 45 19 1 25 30 24 50 48 52 38 51 56 26 3 8 58 17 68 48 61 54 6 28 48 41 51 14 33 34 25 30 35 20 68 26 30 4 86 87 26 8 80 62 85 54 81 48 59 45 11 77 82 5 28 78 84...
output:
1
result:
ok single line: '1'
Test #49:
score: 49
Accepted
time: 196ms
memory: 56912kb
input:
200000 200 0 1 1 3 2 0 4 6 6 5 6 5 6 6 11 6 9 3 13 5 17 5 4 21 5 2 7 0 3 10 27 21 10 24 33 25 19 13 25 17 0 5 31 10 23 43 34 18 25 44 42 39 28 4 23 7 10 17 23 0 42 48 18 35 22 5 44 53 35 16 40 71 38 72 74 68 57 19 33 51 72 45 26 27 72 11 29 42 36 48 50 49 35 83 66 26 58 65 55 25 49 62 6 89 36 97 61 ...
output:
1
result:
ok single line: '1'
Test #50:
score: 49
Accepted
time: 65ms
memory: 24536kb
input:
100000 3 0 1 1 0 0 0 1 0 5 4 6 3 3 4 11 5 10 15 16 4 5 16 18 20 11 15 8 25 5 14 21 2 4 30 2 20 27 22 23 31 7 8 24 27 32 8 31 5 35 7 32 1 4 40 9 28 47 11 5 56 36 1 43 32 63 55 33 30 60 35 55 40 23 14 30 18 2 34 36 23 65 20 31 82 47 63 54 52 6 62 84 28 6 56 11 21 17 16 61 47 27 51 27 34 14 37 42 89 14...
output:
1000
result:
ok single line: '1000'
Test #51:
score: 49
Accepted
time: 73ms
memory: 26240kb
input:
100000 10 0 1 2 1 2 1 4 4 8 7 3 9 8 9 4 3 16 3 0 9 7 12 14 23 8 15 20 20 7 27 22 28 2 17 25 34 28 26 3 0 39 39 5 34 34 31 26 45 45 43 1 9 51 44 20 46 30 19 40 51 3 6 49 62 49 46 51 61 22 41 20 40 71 27 17 69 51 5 6 72 79 22 41 76 13 14 35 40 41 78 71 51 91 54 24 82 2 61 44 16 96 25 47 62 99 72 52 85...
output:
79
result:
ok single line: '79'
Test #52:
score: 49
Accepted
time: 70ms
memory: 25624kb
input:
100000 15 0 0 0 0 1 1 6 1 1 3 9 6 0 9 10 1 14 1 4 8 13 3 17 3 2 2 12 15 28 0 21 4 11 14 16 6 23 12 16 1 10 8 22 7 43 15 38 40 33 35 31 0 37 41 3 55 13 20 31 27 18 40 17 49 61 35 64 35 0 14 19 16 29 44 15 5 29 20 9 13 0 46 27 46 69 22 80 56 7 20 80 82 35 85 16 66 70 45 21 68 14 16 20 95 92 23 46 90 9...
output:
23
result:
ok single line: '23'
Test #53:
score: 49
Accepted
time: 143ms
memory: 50776kb
input:
200000 4 0 0 2 0 1 5 5 5 6 2 3 5 11 9 2 0 2 8 4 11 5 6 12 8 15 11 11 1 2 6 7 29 12 4 1 17 24 23 14 2 29 16 9 20 42 4 40 47 38 37 23 10 0 28 9 55 51 45 56 32 31 31 12 39 43 35 47 16 44 7 51 34 30 7 10 1 59 65 59 34 32 7 74 82 60 43 25 80 10 73 41 59 81 34 81 80 62 92 51 84 70 97 81 94 53 30 36 3 27 4...
output:
600
result:
ok single line: '600'
Test #54:
score: 49
Accepted
time: 138ms
memory: 49124kb
input:
200000 12 0 1 0 2 1 3 5 1 4 1 4 5 3 9 2 7 15 13 14 3 19 21 2 11 12 1 3 4 15 27 0 28 10 25 30 13 21 28 26 17 0 17 35 10 17 37 14 15 39 24 33 36 32 27 31 1 45 0 0 6 21 3 3 19 37 18 41 48 8 6 40 66 29 8 72 47 21 46 1 38 14 46 64 57 5 44 48 86 56 42 83 51 89 25 15 65 83 66 98 85 40 56 26 74 101 38 69 52...
output:
48
result:
ok single line: '48'
Test #55:
score: 49
Accepted
time: 154ms
memory: 48796kb
input:
200000 17 0 1 1 0 1 5 5 1 7 9 0 4 6 3 6 6 7 12 10 0 7 19 3 9 3 18 9 16 16 1 5 21 14 21 9 2 24 18 11 20 37 5 34 13 10 3 11 31 13 37 32 26 22 14 51 8 56 35 46 58 57 28 26 26 18 65 37 62 16 45 38 10 18 28 0 21 59 25 18 25 42 7 23 17 63 16 60 45 77 57 81 54 31 28 89 78 91 77 21 89 42 36 38 7 94 70 4 34 ...
output:
12
result:
ok single line: '12'
Test #56:
score: 49
Accepted
time: 3ms
memory: 4656kb
input:
2400 4 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100...
output:
435
result:
ok single line: '435'
Test #57:
score: 49
Accepted
time: 19ms
memory: 11492kb
input:
21200 2000 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99...
output:
11
result:
ok single line: '11'
Test #58:
score: 49
Accepted
time: 82ms
memory: 44344kb
input:
100000 10000 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 ...
output:
9
result:
ok single line: '9'
Test #59:
score: 49
Accepted
time: 218ms
memory: 83144kb
input:
199999 10000 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 ...
output:
11
result:
ok single line: '11'
Test #60:
score: 49
Accepted
time: 106ms
memory: 36800kb
input:
195001 7 0 1 2 3 0 5 6 7 0 9 10 11 0 13 14 15 0 17 18 19 0 21 22 23 0 25 26 27 0 29 30 31 0 33 34 35 0 37 38 39 0 41 42 43 0 45 46 47 0 49 50 51 0 53 54 55 0 57 58 59 0 61 62 63 0 65 66 67 0 69 70 71 0 73 74 75 0 77 78 79 0 81 82 83 0 85 86 87 0 89 90 91 0 93 94 95 0 97 98 99 0 101 102 103 0 105 106...
output:
40021
result:
ok single line: '40021'
Test #61:
score: 49
Accepted
time: 158ms
memory: 42868kb
input:
190001 200 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99...
output:
1000
result:
ok single line: '1000'