QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#113982 | #6561. Fail Fast | maspy | AC ✓ | 74ms | 31160kb | C++23 | 26.6kb | 2023-06-20 12:06:07 | 2023-06-20 12:06:09 |
Judging History
answer
#line 1 "library/my_template.hpp"
#if defined(LOCAL)
#include <my_template_compiled.hpp>
#else
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using u32 = unsigned int;
using u64 = unsigned long long;
using i128 = __int128;
template <class T>
constexpr T infty = 0;
template <>
constexpr int infty<int> = 1'000'000'000;
template <>
constexpr ll infty<ll> = ll(infty<int>) * infty<int> * 2;
template <>
constexpr u32 infty<u32> = infty<int>;
template <>
constexpr u64 infty<u64> = infty<ll>;
template <>
constexpr i128 infty<i128> = i128(infty<ll>) * infty<ll>;
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); }
// (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, typename U>
T ceil(T x, U y) {
return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U>
T floor(T x, U y) {
return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T, typename U>
pair<T, T> divmod(T x, U y) {
T q = floor(x, y);
return {q, x - q * y};
}
template <typename T, typename U>
T SUM(const vector<U> &A) {
T sum = 0;
for (auto &&a: A) sum += a;
return sum;
}
#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) {
assert(!que.empty());
T a = que.top();
que.pop();
return a;
}
template <typename T>
T POP(vc<T> &que) {
assert(!que.empty());
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;
tie(ok, ng) = (check(x) ? mp(x, ng) : mp(ok, 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;
tie(ok, ng) = (check(x) ? mp(x, ng) : mp(ok, 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;
}
#endif
#line 1 "library/other/io.hpp"
// based on yosupo's fastio
#include <unistd.h>
namespace fastio {
#define FASTIO
// クラスが read(), print() を持っているかを判定するメタ関数
struct has_write_impl {
template <class T>
static auto check(T &&x) -> decltype(x.write(), std::true_type{});
template <class T>
static auto check(...) -> std::false_type;
};
template <class T>
class has_write : public decltype(has_write_impl::check<T>(std::declval<T>())) {
};
struct has_read_impl {
template <class T>
static auto check(T &&x) -> decltype(x.read(), std::true_type{});
template <class T>
static auto check(...) -> std::false_type;
};
template <class T>
class has_read : public decltype(has_read_impl::check<T>(std::declval<T>())) {};
struct Scanner {
FILE *fp;
char line[(1 << 15) + 1];
size_t st = 0, ed = 0;
void reread() {
memmove(line, line + st, ed - st);
ed -= st;
st = 0;
ed += fread(line + ed, 1, (1 << 15) - ed, fp);
line[ed] = '\0';
}
bool succ() {
while (true) {
if (st == ed) {
reread();
if (st == ed) return false;
}
while (st != ed && isspace(line[st])) st++;
if (st != ed) break;
}
if (ed - st <= 50) {
bool sep = false;
for (size_t i = st; i < ed; i++) {
if (isspace(line[i])) {
sep = true;
break;
}
}
if (!sep) reread();
}
return true;
}
template <class T, enable_if_t<is_same<T, string>::value, int> = 0>
bool read_single(T &ref) {
if (!succ()) return false;
while (true) {
size_t sz = 0;
while (st + sz < ed && !isspace(line[st + sz])) sz++;
ref.append(line + st, sz);
st += sz;
if (!sz || st != ed) break;
reread();
}
return true;
}
template <class T, enable_if_t<is_integral<T>::value, int> = 0>
bool read_single(T &ref) {
if (!succ()) return false;
bool neg = false;
if (line[st] == '-') {
neg = true;
st++;
}
ref = T(0);
while (isdigit(line[st])) { ref = 10 * ref + (line[st++] & 0xf); }
if (neg) ref = -ref;
return true;
}
template <typename T,
typename enable_if<has_read<T>::value>::type * = nullptr>
inline bool read_single(T &x) {
x.read();
return true;
}
bool read_single(double &ref) {
string s;
if (!read_single(s)) return false;
ref = std::stod(s);
return true;
}
bool read_single(char &ref) {
string s;
if (!read_single(s) || s.size() != 1) return false;
ref = s[0];
return true;
}
template <class T>
bool read_single(vector<T> &ref) {
for (auto &d: ref) {
if (!read_single(d)) return false;
}
return true;
}
template <class T, class U>
bool read_single(pair<T, U> &p) {
return (read_single(p.first) && read_single(p.second));
}
template <size_t N = 0, typename T>
void read_single_tuple(T &t) {
if constexpr (N < std::tuple_size<T>::value) {
auto &x = std::get<N>(t);
read_single(x);
read_single_tuple<N + 1>(t);
}
}
template <class... T>
bool read_single(tuple<T...> &tpl) {
read_single_tuple(tpl);
return true;
}
void read() {}
template <class H, class... T>
void read(H &h, T &... t) {
bool f = read_single(h);
assert(f);
read(t...);
}
Scanner(FILE *fp) : fp(fp) {}
};
struct Printer {
Printer(FILE *_fp) : fp(_fp) {}
~Printer() { flush(); }
static constexpr size_t SIZE = 1 << 15;
FILE *fp;
char line[SIZE], small[50];
size_t pos = 0;
void flush() {
fwrite(line, 1, pos, fp);
pos = 0;
}
void write(const char val) {
if (pos == SIZE) flush();
line[pos++] = val;
}
template <class T, enable_if_t<is_integral<T>::value, int> = 0>
void write(T val) {
if (pos > (1 << 15) - 50) flush();
if (val == 0) {
write('0');
return;
}
if (val < 0) {
write('-');
val = -val; // todo min
}
size_t len = 0;
while (val) {
small[len++] = char(0x30 | (val % 10));
val /= 10;
}
for (size_t i = 0; i < len; i++) { line[pos + i] = small[len - 1 - i]; }
pos += len;
}
void write(const string s) {
for (char c: s) write(c);
}
void write(const char *s) {
size_t len = strlen(s);
for (size_t i = 0; i < len; i++) write(s[i]);
}
void write(const double x) {
ostringstream oss;
oss << fixed << setprecision(15) << x;
string s = oss.str();
write(s);
}
void write(const long double x) {
ostringstream oss;
oss << fixed << setprecision(15) << x;
string s = oss.str();
write(s);
}
template <typename T,
typename enable_if<has_write<T>::value>::type * = nullptr>
inline void write(T x) {
x.write();
}
template <class T>
void write(const vector<T> val) {
auto n = val.size();
for (size_t i = 0; i < n; i++) {
if (i) write(' ');
write(val[i]);
}
}
template <class T, class U>
void write(const pair<T, U> val) {
write(val.first);
write(' ');
write(val.second);
}
template <size_t N = 0, typename T>
void write_tuple(const T t) {
if constexpr (N < std::tuple_size<T>::value) {
if constexpr (N > 0) { write(' '); }
const auto x = std::get<N>(t);
write(x);
write_tuple<N + 1>(t);
}
}
template <class... T>
bool write(tuple<T...> tpl) {
write_tuple(tpl);
return true;
}
template <class T, size_t S>
void write(const array<T, S> val) {
auto n = val.size();
for (size_t i = 0; i < n; i++) {
if (i) write(' ');
write(val[i]);
}
}
void write(i128 val) {
string s;
bool negative = 0;
if (val < 0) {
negative = 1;
val = -val;
}
while (val) {
s += '0' + int(val % 10);
val /= 10;
}
if (negative) s += "-";
reverse(all(s));
if (len(s) == 0) s = "0";
write(s);
}
};
Scanner scanner = Scanner(stdin);
Printer printer = Printer(stdout);
void flush() { printer.flush(); }
void print() { printer.write('\n'); }
template <class Head, class... Tail>
void print(Head &&head, Tail &&... tail) {
printer.write(head);
if (sizeof...(Tail)) printer.write(' ');
print(forward<Tail>(tail)...);
}
void read() {}
template <class Head, class... Tail>
void read(Head &head, Tail &... tail) {
scanner.read(head);
read(tail...);
}
} // namespace fastio
using fastio::print;
using fastio::flush;
using fastio::read;
#define INT(...) \
int __VA_ARGS__; \
read(__VA_ARGS__)
#define LL(...) \
ll __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 "library/graph/tree.hpp"
#line 2 "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 {
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; }
constexpr bool is_directed() { return directed; }
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;
}
// 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();
}
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];
}
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);
}
}
vc<int> new_idx;
vc<bool> used_e;
// G における頂点 V[i] が、新しいグラフで i になるようにする
// {G, es}
pair<Graph<T, directed>, vc<int>> rearrange(vc<int> V) {
if (len(new_idx) != N) new_idx.assign(N, -1);
if (len(used_e) != M) used_e.assign(M, 0);
int n = len(V);
FOR(i, n) new_idx[V[i]] = i;
Graph<T, directed> G(n);
vc<int> es;
FOR(i, n) {
for (auto&& e: (*this)[V[i]]) {
if (used_e[e.id]) continue;
int a = e.frm, b = e.to;
if (new_idx[a] != -1 && new_idx[b] != -1) {
used_e[e.id] = 1;
G.add(new_idx[a], new_idx[b], e.cost);
es.eb(e.id);
}
}
}
FOR(i, n) new_idx[V[i]] = -1;
for (auto&& eid: es) used_e[eid] = 0;
G.build();
return {G, es};
}
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 "library/graph/tree.hpp"
// HLD euler tour をとっていろいろ。
// 木以外、非連結でも dfs 順序や親がとれる。
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 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 ELID(int v) { return 2 * LID[v] - depth[v]; }
int ERID(int v) { return 2 * RID[v] - depth[v] - 1; }
/* k: 0-indexed */
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;
}
}
// root を根とした場合の lca
int LCA_root(int u, int v, int root) {
return LCA(u, v) ^ LCA(u, root) ^ LCA(v, root);
}
int lca(int u, int v) { return LCA(u, v); }
int lca_root(int u, int v, int root) { return LCA_root(u, v, root); }
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<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;
}
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;
}
};
#line 2 "library/ds/unionfind/unionfind.hpp"
struct UnionFind {
int n, n_comp;
vc<int> dat; // par or (-size)
UnionFind(int n = 0) { build(n); }
void build(int m) {
n = m, n_comp = m;
dat.assign(n, -1);
}
void reset() { build(n); }
int operator[](int x) {
while (dat[x] >= 0) {
int pp = dat[dat[x]];
if (pp < 0) { return dat[x]; }
x = dat[x] = pp;
}
return x;
}
ll size(int x) {
x = (*this)[x];
return -dat[x];
}
bool merge(int x, int y) {
x = (*this)[x], y = (*this)[y];
if (x == y) return false;
if (-dat[x] < -dat[y]) swap(x, y);
dat[x] += dat[y], dat[y] = x, n_comp--;
return true;
}
};
#line 3 "library/graph/optimal_product_on_tree.hpp"
// 木の各頂点に全順序モノイドの元が置かれている(x<=y -> axb<=ayb)
// topological 順序でかけるとき,結果を最大化する
// https://atcoder.jp/contests/agc023/tasks/agc023_f
template <typename TREE, typename Monoid, bool MINIMIZE, typename F>
pair<vc<int>, typename Monoid::value_type> optimal_product_on_tree(
TREE& tree, vc<typename Monoid::value_type> A, F compare) {
const int N = tree.N;
const int root = tree.V[0];
using X = typename Monoid::value_type;
vc<int> head(N), tail(N), nxt(N);
FOR(v, N) head[v] = v, tail[v] = v, nxt[v] = -1;
UnionFind uf(N);
// (x, v, size v)
using T = tuple<X, int, int>;
auto my_compare = [&](T& t1, T& t2) -> bool {
X& x1 = get<0>(t1);
X& x2 = get<0>(t2);
if constexpr (MINIMIZE) return compare(x2, x1);
if constexpr (!MINIMIZE) return compare(x1, x2);
};
priority_queue<T, vc<T>, decltype(my_compare)> que(my_compare);
FOR(v, N) if (v != root) que.emplace(A[v], v, 1);
while (len(que)) {
auto [_, v, sv] = que.top();
que.pop();
if (uf.size(v) != sv) continue;
int a = head[uf[v]], b = tail[uf[v]];
int p = uf[tree.parent[a]];
int c = head[p], d = tail[p];
X x = Monoid::op(A[p], A[v]);
uf.merge(p, v);
v = uf[v];
A[v] = x, head[v] = c, tail[v] = b, nxt[d] = a;
if (uf[v] == uf[root]) continue;
que.emplace(A[v], v, uf.size(v));
}
vc<int> I = {root};
while (nxt[I.back()] != -1) I.eb(nxt[I.back()]);
return {I, A[uf[root]]};
}
#line 2 "library/alg/monoid/affine.hpp"
template <typename K>
struct Monoid_Affine {
using F = pair<K, K>;
using value_type = F;
using X = value_type;
static constexpr F op(const F &x, const F &y) noexcept {
return F({x.first * y.first, x.second * y.first + y.second});
}
static constexpr F inverse(const F &x) {
auto [a, b] = x;
a = K(1) / a;
return {a, a * (-b)};
}
static constexpr K eval(const F &f, K x) noexcept {
return f.first * x + f.second;
}
static constexpr F unit() { return {K(1), K(0)}; }
static constexpr bool commute = false;
};
#line 5 "main.cpp"
using Re = double;
using AFF = pair<Re, Re>;
struct Mono {
using value_type = AFF;
using X = value_type;
static X op(X x, X y) { return {x.fi * y.fi, x.fi * y.se + x.se}; }
static constexpr X unit() { return {1.0, 0.0}; }
static constexpr bool commute = 0;
};
void solve() {
LL(N);
Graph<bool, 1> G(N + 1);
vc<AFF> dat(N + 1);
dat[0] = Mono::unit();
FOR(v, 1, N + 1) {
double x, p;
read(x), read(p);
INT(par);
G.add(par, v);
dat[v] = {p, x};
}
G.build();
Tree<decltype(G)> tree(G);
auto comp = [&](AFF x, AFF y) -> bool {
return x.fi * y.se + x.se < y.fi * x.se + y.se;
};
auto [ord, x]
= optimal_product_on_tree<decltype(tree), Mono, true>(tree, dat, comp);
ord.erase(ord.begin());
for (auto&& x: ord) print(x);
}
signed main() {
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3600kb
input:
4 100 0.5 0 200 0.1 1 10 0.5 2 10 0.9 0
output:
4 1 2 3
result:
ok correct
Test #2:
score: 0
Accepted
time: 1ms
memory: 3668kb
input:
4 84 0.716 0 91 0.115 0 19 0.640 1 103 0.455 0
output:
2 1 3 4
result:
ok correct
Test #3:
score: 0
Accepted
time: 1ms
memory: 3600kb
input:
10 18 0.574073 0 13 0.540309 0 13 0.539018 2 12 0.572910 2 15 0.570616 4 16 0.510215 3 17 0.504083 5 19 0.539297 1 19 0.577271 7 10 0.578346 1
output:
2 4 3 6 5 7 1 10 8 9
result:
ok correct
Test #4:
score: 0
Accepted
time: 0ms
memory: 3672kb
input:
20 93 0.093030 0 53 0.056639 0 39 0.021549 0 48 0.069268 3 58 0.009572 4 22 0.015083 1 27 0.024351 5 68 0.085055 7 48 0.031563 5 46 0.025067 9 15 0.095445 2 57 0.011233 6 97 0.028239 2 8 0.060758 6 59 0.097330 13 34 0.052120 3 73 0.055127 11 53 0.004135 12 24 0.051183 5 56 0.027001 13
output:
3 16 4 2 11 5 19 7 9 10 8 17 1 6 14 12 18 13 20 15
result:
ok correct
Test #5:
score: 0
Accepted
time: 1ms
memory: 3684kb
input:
20 971329 0.076234 0 996879 0.012978 0 994191 0.056803 0 978400 0.080792 1 907508 0.008858 4 992071 0.057419 2 901661 0.089621 6 912521 0.051943 5 979169 0.040201 5 904759 0.083405 7 928478 0.092658 7 980034 0.054747 3 906344 0.053231 10 907474 0.090196 8 927695 0.023153 4 995464 0.009387 2 984650 0...
output:
2 16 6 7 10 13 17 19 20 11 1 4 5 15 8 14 18 9 3 12
result:
ok correct
Test #6:
score: 0
Accepted
time: 0ms
memory: 3624kb
input:
20 54 0.952741 0 41 0.912397 0 11 0.963309 0 66 0.915806 3 47 0.919191 4 51 0.906473 5 24 0.989650 6 97 0.964070 7 56 0.997215 1 39 0.981950 2 50 0.994037 2 92 0.942000 7 97 0.900405 3 53 0.950071 6 16 0.980631 14 63 0.950876 10 49 0.995183 15 20 0.908520 5 71 0.949757 16 76 0.972330 9
output:
3 2 4 5 18 6 13 14 15 1 10 16 19 7 12 8 9 20 11 17
result:
ok correct
Test #7:
score: 0
Accepted
time: 1ms
memory: 3612kb
input:
20 933154 0.904059 0 929160 0.911627 0 999437 0.921760 0 991335 0.904136 1 903530 0.943148 4 904464 0.921035 2 944382 0.912394 6 990971 0.982189 7 941308 0.959290 4 993916 0.945081 7 924496 0.989350 1 938782 0.958578 4 964442 0.997198 11 964358 0.938647 10 911972 0.943888 5 975140 0.993873 4 995611 ...
output:
1 4 2 6 7 3 5 15 10 14 20 12 19 9 11 13 17 8 18 16
result:
ok correct
Test #8:
score: 0
Accepted
time: 1ms
memory: 3648kb
input:
1 10 0.5 0
output:
1
result:
ok correct
Test #9:
score: 0
Accepted
time: 1ms
memory: 3660kb
input:
2 100 0.8 0 200 0.7 0
output:
1 2
result:
ok correct
Test #10:
score: 0
Accepted
time: 1ms
memory: 3568kb
input:
2 10 0.5 0 5 0.4 1
output:
1 2
result:
ok correct
Test #11:
score: 0
Accepted
time: 0ms
memory: 3736kb
input:
2 1000 0.7 2 1 0.6 0
output:
2 1
result:
ok correct
Test #12:
score: 0
Accepted
time: 69ms
memory: 31160kb
input:
100000 938188 0.740681 0 575610 0.965656 1 937341 0.842524 2 817797 0.945396 3 602563 0.818956 4 893939 0.900203 5 952148 0.810399 6 538333 0.960769 7 550079 0.908188 8 676338 0.795726 9 546675 0.529045 10 542108 0.581119 11 963201 0.665127 12 564484 0.897025 13 504952 0.844118 14 673675 0.777947 15...
output:
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 101 102 ...
result:
ok correct
Test #13:
score: 0
Accepted
time: 27ms
memory: 30936kb
input:
100000 501234 0.500516 0 501214 0.503354 1 501013 0.504058 2 502546 0.502962 3 500273 0.505433 4 500197 0.505874 5 505941 0.500204 6 500455 0.506393 7 506433 0.500626 8 503652 0.503861 9 500935 0.507151 10 501370 0.506725 11 502595 0.506236 12 503444 0.505698 13 501723 0.508031 14 505738 0.504150 15...
output:
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 101 102 ...
result:
ok correct
Test #14:
score: 0
Accepted
time: 47ms
memory: 30416kb
input:
100000 768956 0.999996 0 686063 0.999982 1 817790 0.999964 2 897069 0.999958 3 940413 0.999956 4 879098 0.999957 5 687880 0.999964 6 663602 0.999959 7 816976 0.999944 8 572136 0.999958 9 653227 0.999946 10 972448 0.999914 11 836815 0.999920 12 617305 0.999941 13 934633 0.999903 14 757071 0.999917 15...
output:
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 101 102 ...
result:
ok correct
Test #15:
score: 0
Accepted
time: 61ms
memory: 19580kb
input:
100000 686602 0.755750 0 606835 0.951986 0 713656 0.504635 0 609527 0.663061 0 752558 0.613758 0 997011 0.880758 0 905135 0.574450 0 732880 0.774286 0 573515 0.609711 0 899010 0.630653 0 664787 0.949029 0 649162 0.965284 0 582075 0.957310 0 939848 0.848816 0 743139 0.738017 0 577134 0.723893 0 91476...
output:
253 67 161 137 179 1001 287 255 3403 97013 304 33 428 135 212 344 237 110 234 35 149 2447 65686 7360 281 267 357 235 11969 10350 55117 23101 44394 90 345 32287 94695 21214 188 5602 3 12430 219 17907 55 71206 152 87789 70 305 9 90656 238 210 5552 72776 205 236 6216 1136 648 1276 7802 92691 249 2763 5...
result:
ok correct
Test #16:
score: 0
Accepted
time: 48ms
memory: 18836kb
input:
100000 866461 0.563475 0 566909 0.892585 1 794999 0.608805 1 572103 0.501998 1 889513 0.669248 4 712553 0.620050 3 721255 0.898776 3 731219 0.870250 5 958886 0.933100 5 946557 0.758386 1 829823 0.901169 6 513249 0.679404 6 707379 0.965023 2 686267 0.691424 13 790432 0.772695 3 630098 0.867609 11 975...
output:
1 4 255 1463 17335 2114 14926 25657 17157 19416 25749 33301 67686 2 43930 34070 382 70 4349 739 5234 2679 10128 14274 10413 35 1243 11751 30407 49724 94468 92693 98817 28424 41338 79762 220 4584 702 10034 40074 1056 32441 2754 5745 5840 61325 61426 28243 3 6 12 9550 123 1113 15255 26543 1657 3838 31...
result:
ok correct
Test #17:
score: 0
Accepted
time: 62ms
memory: 20228kb
input:
100000 724633 0.992242 0 519908 0.739362 1 841119 0.933434 2 791058 0.900611 3 675619 0.998138 4 764793 0.750214 5 749590 0.659667 6 999818 0.893057 7 643755 0.894506 8 843096 0.848621 9 948647 0.664404 10 914991 0.753675 11 923272 0.619427 12 937334 0.894654 13 811519 0.627730 14 610149 0.981146 15...
output:
1 2 3534 1245 9813 18840 3255 17466 2329 5499 10237 28651 58928 63019 72223 3738 79615 1525 59464 8134 8727 390 71564 7842 10595 99232 89273 10764 35414 14731 20737 89154 14055 3552 69171 11710 17881 33462 11875 19460 22920 24318 14524 45990 7751 9761 8722 75995 3968 9240 65365 40991 80419 12494 262...
result:
ok correct
Test #18:
score: 0
Accepted
time: 67ms
memory: 25788kb
input:
100000 526633 0.902698 0 515821 0.957942 1 871718 0.810818 2 920847 0.633590 3 826181 0.806362 4 876673 0.742829 5 614618 0.612000 6 853505 0.831590 7 511485 0.830238 8 632217 0.794467 9 671254 0.883474 10 695783 0.701932 11 862825 0.611140 12 678224 0.856628 13 659493 0.947587 14 893183 0.970325 15...
output:
1 2 52258 3 4 5 6 7 8 9 10 63310 11 12 13 55535 70001 14 94065 87644 15 16 17 18 19 20 21 22 23 61227 24 25 26 27 54174 28 29 30 31 32 75852 69617 33 34 35 52672 95714 53846 36 60306 37 50265 38 39 63613 64784 82319 60688 40 41 42 43 80655 44 45 46 76567 47 48 49 50 76243 51 52 53 60308 54 55 56 57 ...
result:
ok correct
Test #19:
score: 0
Accepted
time: 1ms
memory: 3584kb
input:
100 456 0.123000 44 456 0.123000 0 456 0.123000 9 456 0.123000 10 456 0.123000 95 456 0.123000 100 456 0.123000 53 456 0.123000 37 456 0.123000 37 456 0.123000 42 456 0.123000 40 456 0.123000 4 456 0.123000 47 456 0.123000 4 456 0.123000 84 456 0.123000 71 456 0.123000 83 456 0.123000 2 456 0.123000...
output:
100 37 9 3 42 48 24 23 43 39 82 89 56 80 74 36 25 20 97 60 79 58 76 67 10 73 84 15 49 47 30 44 1 55 4 14 45 64 77 61 22 59 66 72 95 5 88 83 21 93 98 92 12 6 41 52 65 57 78 85 8 54 26 13 51 17 2 18 31 53 7 35 63 99 62 40 27 11 46 90 91 81 75 69 38 71 33 94 96 32 28 34 29 50 70 68 87 16 19 86
result:
ok correct
Test #20:
score: 0
Accepted
time: 65ms
memory: 24548kb
input:
100000 991626 0.995651 9504 995771 0.998261 79562 998060 0.995175 92360 992097 0.995626 3344 991954 0.991091 6502 995161 0.991812 89766 990581 0.996178 78414 991802 0.999306 97661 992640 0.995473 24083 994222 0.999294 89602 997968 0.992137 45379 993834 0.996958 76573 994607 0.993958 85053 995532 0.9...
output:
13200 75427 46826 91955 6792 58951 55107 91758 8159 83245 27728 84187 17052 32946 88578 39869 41072 87880 56112 29316 82758 58620 40149 45212 11072 78598 70427 16486 2563 43645 7366 11552 41031 13373 62192 83583 66503 67600 38542 75912 77272 84516 17222 19872 42647 62849 99825 22131 59316 54785 5510...
result:
ok correct
Test #21:
score: 0
Accepted
time: 56ms
memory: 24648kb
input:
100000 994360 0.971872 88920 993564 0.952659 41443 987411 0.953086 14489 985630 0.966869 48952 999079 0.962902 45554 992490 0.962228 48582 994303 0.999688 2207 981197 0.974874 89269 994427 0.954167 46810 982731 0.987444 19228 989277 0.968593 33423 992826 0.962838 21386 993938 0.981453 5429 989639 0....
output:
75696 30935 62149 9056 33005 89511 86903 50595 96461 51390 76049 88417 78266 66542 20035 92068 70310 90092 15209 55734 56807 74861 92709 65406 51172 94603 23029 69738 32661 26934 48828 3439 37047 20478 93906 33886 82412 19784 95442 62198 49432 14125 31214 38985 42167 65903 17693 52552 4677 87763 247...
result:
ok correct
Test #22:
score: 0
Accepted
time: 69ms
memory: 24632kb
input:
100000 985815 0.968338 16495 994819 0.979029 23173 991468 0.983509 81133 982365 0.950143 16314 983055 0.971887 62541 996806 0.971088 68267 995780 0.950299 74974 994532 0.955473 63606 989984 0.951905 81901 988566 0.998953 42305 996656 0.965468 55450 993573 0.994893 6634 997306 0.979634 16470 991636 0...
output:
13130 52362 46854 26229 51335 80347 86680 22539 74823 79936 77914 3081 32725 27753 33217 52760 95825 17422 58889 34274 49599 89422 71815 55768 83383 27789 86192 45722 41115 44524 29829 10847 62845 87416 49949 98368 61655 33583 36777 12533 97259 80536 19101 23746 98761 78888 51188 96827 62467 63281 6...
result:
ok correct
Test #23:
score: 0
Accepted
time: 52ms
memory: 18756kb
input:
100000 379221 0.374168 0 219097 0.093397 0 141258 0.041142 0 773286 0.353950 0 479988 0.960409 0 169530 0.061026 0 857709 0.406691 0 328582 0.276540 0 903160 0.739709 0 252384 0.785859 0 383381 0.117421 0 652923 0.378000 0 154387 0.849961 0 689248 0.502565 0 951646 0.417907 0 840656 0.173340 0 80522...
output:
27300 27976 10500 8980 9631 29827 42466 31708 41922 46188 34880 21131 40863 39318 44981 27847 20875 33333 3274 37915 36722 33972 39084 27339 46854 48282 27030 29281 32529 2079 30792 373 38419 47331 37775 36207 36950 10130 16372 14566 10655 3616 5900 45517 17417 38377 4372 24244 47165 11392 7150 1755...
result:
ok correct
Test #24:
score: 0
Accepted
time: 74ms
memory: 24568kb
input:
100000 210466 0.747974 14014 875274 0.741805 83670 213751 0.306945 37859 924462 0.720568 6619 216870 0.448922 14076 144128 0.273307 3318 191627 0.489542 94999 695166 0.745605 88175 700114 0.359530 1241 874007 0.041421 67065 715108 0.713048 65 400676 0.000343 41833 456908 0.421291 14481 51440 0.71410...
output:
12430 26046 74117 92942 79559 34874 33889 2103 30460 71222 55527 68745 16338 56243 84827 52383 28546 33572 27929 52035 50723 94922 13498 11155 5576 84171 30388 60198 35505 199 67796 31967 15488 12153 16903 96283 87999 99388 18362 68560 63993 39841 9243 309 9437 24831 14832 91149 42941 72482 64829 18...
result:
ok correct
Test #25:
score: 0
Accepted
time: 55ms
memory: 20052kb
input:
99856 576549 0.545246 0 430801 0.958125 0 987874 0.848851 0 783119 0.204964 0 855730 0.750672 0 310636 0.443476 0 237417 0.154278 0 78845 0.812317 0 285014 0.676250 0 525664 0.343623 0 950682 0.343284 0 522477 0.803923 0 397098 0.788337 0 837538 0.006097 0 348545 0.356729 0 509388 0.960808 0 544999 ...
output:
283 296 245 299 140 78 25 70 264 59 288 287 90407 200 63002 63003 182 165 177 72 305 224 53 41 28 231 239 308 76 315 71 179 146 106 24572 228 90722 29 166 52292 273 216 130 209 61 204 98 261 184 57962 133 267 156 286 81 188 304 52293 52294 51 119 85997 42 175 55127 7 2207 178 72767 56 17642 144 4536...
result:
ok correct
Test #26:
score: 0
Accepted
time: 1ms
memory: 3620kb
input:
5 10 0.5 0 1 0.5 1 1 0.5 1 9 0.5 0 1 0.5 4
output:
1 2 3 4 5
result:
ok correct