// Art - Bui Hai Dang
// Keep typing, keep loving
// #pragma GCC optimize("02,unroll-loops")
#include <bits/stdc++.h>
#define el cout << '\n'
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define REV(i, b, a) for (int i = (b), _a = (a); i >= _a; --i)
#define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i)
#define __Art__ int main()
const int N = 1e5 + 10;
using namespace std;
int binpow(int a, int b) {
int res = 1;
while (b) {
if (b & 1) res = 1ll * res * a;
a = 1ll * a * a; b >>= 1;
}
return res;
}
struct convex_hull_trick {
struct line {
long long a;
long long b;
long long calc(long long x) {
return a * x + b;
}
};
double slope(line x, line y) {
return (double)(y.b - x.b) / (double)(x.a - y.a);
}
bool bad(line l1, line l2, line l3) { // cmp (l1, l3) vs (l1, l2)
return (l3.b - l1.b) * (l1.a - l2.a) <= (l2.b - l1.b) * (l1.a - l3.a); // tránh sai số do chia (nếu số quá lớn thì không dùng)
// return slope(l1, l3) <= slope(l1, l2); // get minimum
}
// dùng khi xác định được thứ tự add, get tuân theo tăng / giảm liên tiếp
deque<line> dq;
void add(line new_line) {
while (dq.size() >= 2 && bad(dq.end()[-2], dq.end()[-1], new_line)) {
dq.pop_back();
}
dq.push_back(new_line);
}
long long get(long long x) {
while (dq.size() >= 2 && dq[0].calc(x) >= dq[1].calc(x)) { // get minimum, if (<=): get maximum
dq.pop_front();
}
return dq[0].calc(x);
}
// struct operation {
// int pos, top;
// line overwrite;
// }; // phục vụ thao tác xóa một đường thẳng, trả cấu trúc dữ liệu về trạng thái ban đầu.
// vector<operation> undoLst;
// // không theo thứ tự
// line dq[N];
// int top = 0;
// void add(line new_line) {
// int l = 1, r = top - 1, k = top;
// while (l <= r) {
// int m = l + (r - l) / 2;
// if (bad(dq[m - 1], dq[m], new_line)) {
// k = m; r = m - 1;
// }
// else {
// l = m + 1;
// }
// }
// // undoLst.push_back({k, top, dq[k]}); for rollback
// top = k + 1;
// dq[k] = new_line;
// }
// long long get(long long x) {
// int l = 0, r = top - 1;
// long long res = dq[0].calc(x);
// while (l < r) {
// int m = l + (r - l) / 2;
// long long a = dq[m].calc(x);
// long long b = dq[m + 1].calc(x);
// if (a > b) {
// l = m + 1;
// }
// else {
// r = m;
// }
// res = min({res, a, b});
// }
// return res;
// }
// void rollback() { // nếu thêm đường thẳng mới, sau khi xét xong thì xóa và trả lại về danh sách góc
// operation back = undoLst.back();
// undoLst.pop_back();
// top = back.top;
// dq[back.pos] = back.overwrite;
// }
} chts;
// sort (a, b) của yi = ai * x + bi
// bool cmp_slope_D(const line &a, const line &b) {
// if (a.a == b.a) {
// return a.b < b.b;
// }
// return a.a > b.a;
// }
bool _Line_Comp_State;
struct Line { //
mutable long long k, m, p;
bool operator < (const Line &o) const {
return _Line_Comp_State ? p < o.p : k < o.k;
}
};
struct LineContainer : multiset<Line> { // (for doubles, use inf = 1/.0, div(a,b) = a/b)
long long div (long long a, long long b) {
return a / b - ((a ^ b) < 0 && a % b);
}
bool isect(iterator x, iterator y) {
if (y == end()) {
x->p = LLONG_MAX;
return false;
}
if (x->k == y->k) {
x->p = x->m > y->m ? LLONG_MAX : -LLONG_MAX;
}
else {
x->p = div(y->m - x->m, x->k - y->k);
}
return x->p >= y->p;
}
void add(long long k, long long m) {
auto z = insert({k, m, 0}), y = z++, x = y;
while (isect(y, z)) {
z = erase(z);
}
if (x != begin() && isect(--x, y)) {
isect(x, y = erase(y));
}
while ((y = x) != begin() && (--x)->p >= y->p) {
isect(x, erase(y));
}
}
long long get(long long x) {
assert(!empty());
_Line_Comp_State = 1;
auto l = *lower_bound({0, 0, x});
_Line_Comp_State = 0;
return l.k * x + l.m;
}
} cht;
template<class T> inline void Write(T x) {
if (x < 0) {
putchar('-');
x *= -1;
}
if (x > 9) Write(x / 10);
putchar(x % 10 + '0');
}
template<class T> inline void Read(T &x) {
char c;
for (c = getchar(); (c > '9' | c < '0') && c != '-'; c = getchar());
T y;
if (c == '-') x = 0, y = -1;
else x = c - '0', y = 1;
for(c = getchar(); c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - '0';
x *= y;
}
struct IOPre {
static constexpr int TEN = 10, SZ = TEN * TEN * TEN * TEN;
std::array<char, 4 * SZ> num;
constexpr IOPre() : num{} {
for (int i = 0; i < SZ; i++) {
int n = i;
for (int j = 3; j >= 0; j--) {
num[i * 4 + j] = static_cast<char>(n % TEN + '0');
n /= TEN;
}
}
}
};
struct IO {
#if !HAVE_DECL_FREAD_UNLOCKED
#define fread_unlocked fread
#endif
#if !HAVE_DECL_FWRITE_UNLOCKED
#define fwrite_unlocked fwrite
#endif
static constexpr int SZ = 1 << 17, LEN = 32, TEN = 10, HUNDRED = TEN * TEN,
THOUSAND = HUNDRED * TEN, TENTHOUSAND = THOUSAND * TEN,
MAGIC_MULTIPLY = 205, MAGIC_SHIFT = 11, MASK = 15,
TWELVE = 12, SIXTEEN = 16;
static constexpr IOPre io_pre = {};
std::array<char, SZ> input_buffer, output_buffer;
int input_ptr_left, input_ptr_right, output_ptr_right;
IO()
: input_buffer{},
output_buffer{},
input_ptr_left{},
input_ptr_right{},
output_ptr_right{} {}
IO(const IO&) = delete;
IO(IO&&) = delete;
IO& operator=(const IO&) = delete;
IO& operator=(IO&&) = delete;
~IO() { flush(); }
template <class T>
struct is_char {
static constexpr bool value = std::is_same_v<T, char>;
};
template <class T>
struct is_bool {
static constexpr bool value = std::is_same_v<T, bool>;
};
template <class T>
struct is_string {
static constexpr bool value =
std::is_same_v<T, std::string> || std::is_same_v<T, const char*> ||
std::is_same_v<T, char*> || std::is_same_v<std::decay_t<T>, char*>;
};
template <class T, class D = void>
struct is_custom {
static constexpr bool value = false;
};
template <class T>
struct is_custom<T, std::void_t<typename T::internal_value_type>> {
static constexpr bool value = true;
};
template <class T>
struct is_default {
static constexpr bool value = is_char<T>::value || is_bool<T>::value ||
is_string<T>::value ||
std::is_integral_v<T>;
};
template <class T, class D = void>
struct is_iterable {
static constexpr bool value = false;
};
template <class T>
struct is_iterable<
T, typename std::void_t<decltype(std::begin(std::declval<T>()))>> {
static constexpr bool value = true;
};
template <class T, class D = void, class E = void>
struct is_applyable {
static constexpr bool value = false;
};
template <class T>
struct is_applyable<T, std::void_t<typename std::tuple_size<T>::type>,
std::void_t<decltype(std::get<0>(std::declval<T>()))>> {
static constexpr bool value = true;
};
template <class T>
static constexpr bool needs_newline = (is_iterable<T>::value ||
is_applyable<T>::value) &&
(!is_default<T>::value);
template <typename T, typename U>
struct any_needs_newline {
static constexpr bool value = false;
};
template <typename T>
struct any_needs_newline<T, std::index_sequence<>> {
static constexpr bool value = false;
};
template <typename T, std::size_t I, std::size_t... Is>
struct any_needs_newline<T, std::index_sequence<I, Is...>> {
static constexpr bool value =
needs_newline<decltype(std::get<I>(std::declval<T>()))> ||
any_needs_newline<T, std::index_sequence<Is...>>::value;
};
inline void load() {
memmove(std::begin(input_buffer),
std::begin(input_buffer) + input_ptr_left,
input_ptr_right - input_ptr_left);
input_ptr_right =
input_ptr_right - input_ptr_left +
static_cast<int>(fread_unlocked(
std::begin(input_buffer) + input_ptr_right - input_ptr_left, 1,
SZ - input_ptr_right + input_ptr_left, stdin));
input_ptr_left = 0;
}
inline void read_char(char& c) {
if (input_ptr_left + LEN > input_ptr_right) load();
c = input_buffer[input_ptr_left++];
}
inline void read_string(std::string& x) {
char c;
while (read_char(c), c < '!') continue;
x = c;
while (read_char(c), c >= '!') x += c;
}
template <class T>
inline std::enable_if_t<std::is_integral_v<T>, void> read_int(T& x) {
if (input_ptr_left + LEN > input_ptr_right) load();
char c = 0;
do c = input_buffer[input_ptr_left++];
while (c < '-');
[[maybe_unused]] bool minus = false;
if constexpr (std::is_signed<T>::value == true)
if (c == '-') minus = true, c = input_buffer[input_ptr_left++];
x = 0;
while (c >= '0')
x = x * TEN + (c & MASK), c = input_buffer[input_ptr_left++];
if constexpr (std::is_signed<T>::value == true)
if (minus) x = -x;
}
inline void skip_space() {
if (input_ptr_left + LEN > input_ptr_right) load();
while (input_buffer[input_ptr_left] <= ' ') input_ptr_left++;
}
inline void flush() {
fwrite_unlocked(std::begin(output_buffer), 1, output_ptr_right, stdout);
output_ptr_right = 0;
}
inline void write_char(char c) {
if (output_ptr_right > SZ - LEN) flush();
output_buffer[output_ptr_right++] = c;
}
inline void write_bool(bool b) {
if (output_ptr_right > SZ - LEN) flush();
output_buffer[output_ptr_right++] = b ? '1' : '0';
}
inline void write_string(const std::string& s) {
for (auto x : s) write_char(x);
}
inline void write_string(const char* s) {
while (*s) write_char(*s++);
}
inline void write_string(char* s) {
while (*s) write_char(*s++);
}
template <typename T>
inline std::enable_if_t<std::is_integral_v<T>, void> write_int(T x) {
if (output_ptr_right > SZ - LEN) flush();
if (!x) {
output_buffer[output_ptr_right++] = '0';
return;
}
if constexpr (std::is_signed<T>::value == true)
if (x < 0) output_buffer[output_ptr_right++] = '-', x = -x;
int i = TWELVE;
std::array<char, SIXTEEN> buf{};
while (x >= TENTHOUSAND) {
memcpy(std::begin(buf) + i,
std::begin(io_pre.num) + (x % TENTHOUSAND) * 4, 4);
x /= TENTHOUSAND;
i -= 4;
}
if (x < HUNDRED) {
if (x < TEN) {
output_buffer[output_ptr_right++] = static_cast<char>('0' + x);
} else {
std::uint32_t q =
(static_cast<std::uint32_t>(x) * MAGIC_MULTIPLY) >>
MAGIC_SHIFT;
std::uint32_t r = static_cast<std::uint32_t>(x) - q * TEN;
output_buffer[output_ptr_right] = static_cast<char>('0' + q);
output_buffer[output_ptr_right + 1] =
static_cast<char>('0' + r);
output_ptr_right += 2;
}
} else {
if (x < THOUSAND) {
memcpy(std::begin(output_buffer) + output_ptr_right,
std::begin(io_pre.num) + (x << 2) + 1, 3),
output_ptr_right += 3;
} else {
memcpy(std::begin(output_buffer) + output_ptr_right,
std::begin(io_pre.num) + (x << 2), 4),
output_ptr_right += 4;
}
}
memcpy(std::begin(output_buffer) + output_ptr_right,
std::begin(buf) + i + 4, TWELVE - i);
output_ptr_right += TWELVE - i;
}
template <typename T_>
IO& operator<<(T_&& x) {
using T = typename std::remove_cv<
typename std::remove_reference<T_>::type>::type;
static_assert(is_custom<T>::value or is_default<T>::value or
is_iterable<T>::value or is_applyable<T>::value);
if constexpr (is_custom<T>::value) {
write_int(x.get());
} else if constexpr (is_default<T>::value) {
if constexpr (is_bool<T>::value) {
write_bool(x);
} else if constexpr (is_string<T>::value) {
write_string(x);
} else if constexpr (is_char<T>::value) {
write_char(x);
} else if constexpr (std::is_integral_v<T>) {
write_int(x);
}
} else if constexpr (is_iterable<T>::value) {
// strings are immune
using E = decltype(*std::begin(x));
constexpr char sep = needs_newline<E> ? '\n' : ' ';
int i = 0;
for (const auto& y : x) {
if (i++) write_char(sep);
operator<<(y);
}
} else if constexpr (is_applyable<T>::value) {
// strings are immune
constexpr char sep =
(any_needs_newline<
T, std::make_index_sequence<std::tuple_size_v<T>>>::value)
? '\n'
: ' ';
int i = 0;
std::apply(
[this, &sep, &i](auto const&... y) {
(((i++ ? write_char(sep) : void()), this->operator<<(y)),
...);
},
x);
}
return *this;
}
template <typename T>
IO& operator>>(T& x) {
static_assert(is_custom<T>::value or is_default<T>::value or
is_iterable<T>::value or is_applyable<T>::value);
static_assert(!is_bool<T>::value);
if constexpr (is_custom<T>::value) {
typename T::internal_value_type y;
read_int(y);
x = y;
} else if constexpr (is_default<T>::value) {
if constexpr (is_string<T>::value) {
read_string(x);
} else if constexpr (is_char<T>::value) {
read_char(x);
} else if constexpr (std::is_integral_v<T>) {
read_int(x);
}
} else if constexpr (is_iterable<T>::value) {
for (auto& y : x) operator>>(y);
} else if constexpr (is_applyable<T>::value) {
std::apply([this](auto&... y) { ((this->operator>>(y)), ...); }, x);
}
return *this;
}
IO* tie(std::nullptr_t) { return this; }
void sync_with_stdio(bool) {}
};
IO io;
// #define cin io
// #define cout io
// dont use ios_base, cin.tie, cout.tie
const int M = 1e3 + 5;
const int base = 1000000000; const int base_digits = 9;
struct bigint {
vector<int> a; int sign;
bigint() :
sign(1) {
}
bigint(long long v) {
*this = v;
}
bigint(const string &s) {
read(s);
}
void operator=(const bigint &v) {
sign = v.sign;
a = v.a;
}
void operator=(long long v) {
sign = 1;
if (v < 0)
sign = -1, v = -v;
for (; v > 0; v = v / base)
a.push_back(v % base);
}
bigint operator+(const bigint &v) const {
if (sign == v.sign) {
bigint res = v;
for (int i = 0, carry = 0; i < (int) max(a.size(), v.a.size()) | carry; ++i) {
if (i == (int) res.a.size())
res.a.push_back(0);
res.a[i] += carry + (i < (int) a.size() ? a[i] : 0);
carry = res.a[i] >= base;
if (carry)
res.a[i] -= base;
}
return res;
}
return *this - (-v);
}
bigint operator-(const bigint &v) const {
if (sign == v.sign) {
if (abs() >= v.abs()) {
bigint res = *this;
for (int i = 0, carry = 0; i < (int) v.a.size() || carry; ++i) {
res.a[i] -= carry + (i < (int) v.a.size() ? v.a[i] : 0);
carry = res.a[i] < 0;
if (carry)
res.a[i] += base;
}
res.trim();
return res;
}
return -(v - *this);
}
return *this + (-v);
}
void operator*=(int v) {
if (v < 0)
sign = -sign, v = -v;
for (int i = 0, carry = 0; i < (int) a.size() || carry; ++i) {
if (i == (int) a.size())
a.push_back(0);
long long cur = a[i] * (long long) v + carry;
carry = (int) (cur / base);
a[i] = (int) (cur % base);
//asm("divl %ecx" : "=a"(carry), "=d"(a[i]) : "A"(cur), "c"(base));
}
trim();
}
bigint operator*(int v) const {
bigint res = *this;
res *= v;
return res;
}
friend pair<bigint, bigint> divmod(const bigint &a1, const bigint &b1) {
int norm = base / (b1.a.back() + 1);
bigint a = a1.abs() * norm;
bigint b = b1.abs() * norm;
bigint q, r;
q.a.resize(a.a.size());
for (int i = a.a.size() - 1; i >= 0; i--) {
r *= base;
r += a.a[i];
int s1 = r.a.size() <= b.a.size() ? 0 : r.a[b.a.size()];
int s2 = r.a.size() <= b.a.size() - 1 ? 0 : r.a[b.a.size() - 1];
int d = ((long long) base * s1 + s2) / b.a.back();
r -= b * d;
while (r < 0)
r += b, --d;
q.a[i] = d;
}
q.sign = a1.sign * b1.sign;
r.sign = a1.sign;
q.trim();
r.trim();
return make_pair(q, r / norm);
}
bigint operator/(const bigint &v) const {
return divmod(*this, v).first;
}
bigint operator%(const bigint &v) const {
return divmod(*this, v).second;
}
void operator/=(int v) {
if (v < 0)
sign = -sign, v = -v;
for (int i = (int) a.size() - 1, rem = 0; i >= 0; --i) {
long long cur = a[i] + rem * (long long) base;
a[i] = (int) (cur / v);
rem = (int) (cur % v);
}
trim();
}
bigint operator/(int v) const {
bigint res = *this;
res /= v;
return res;
}
int operator%(int v) const {
if (v < 0)
v = -v;
int m = 0;
for (int i = a.size() - 1; i >= 0; --i)
m = (a[i] + m * (long long) base) % v;
return m * sign;
}
void operator+=(const bigint &v) {
*this = *this + v;
}
void operator-=(const bigint &v) {
*this = *this - v;
}
void operator*=(const bigint &v) {
*this = *this * v;
}
void operator/=(const bigint &v) {
*this = *this / v;
}
bool operator<(const bigint &v) const {
if (sign != v.sign)
return sign < v.sign;
if (a.size() != v.a.size())
return a.size() * sign < v.a.size() * v.sign;
for (int i = a.size() - 1; i >= 0; i--)
if (a[i] != v.a[i])
return a[i] * sign < v.a[i] * sign;
return false;
}
bool operator>(const bigint &v) const {
return v < *this;
}
bool operator<=(const bigint &v) const {
return !(v < *this);
}
bool operator>=(const bigint &v) const {
return !(*this < v);
}
bool operator==(const bigint &v) const {
return !(*this < v) && !(v < *this);
}
bool operator!=(const bigint &v) const {
return *this < v || v < *this;
}
void trim() {
while (!a.empty() && !a.back())
a.pop_back();
if (a.empty())
sign = 1;
}
bool isZero() const {
return a.empty() || (a.size() == 1 && !a[0]);
}
bigint operator-() const {
bigint res = *this;
res.sign = -sign;
return res;
}
bigint abs() const {
bigint res = *this;
res.sign *= res.sign;
return res;
}
long long longValue() const {
long long res = 0;
for (int i = a.size() - 1; i >= 0; i--)
res = res * base + a[i];
return res * sign;
}
friend bigint gcd(const bigint &a, const bigint &b) {
return b.isZero() ? a : gcd(b, a % b);
}
friend bigint lcm(const bigint &a, const bigint &b) {
return a / gcd(a, b) * b;
}
void read(const string &s) {
sign = 1;
a.clear();
int pos = 0;
while (pos < (int) s.size() && (s[pos] == '-' || s[pos] == '+')) {
if (s[pos] == '-')
sign = -sign;
++pos;
}
for (int i = s.size() - 1; i >= pos; i -= base_digits) {
int x = 0;
for (int j = max(pos, i - base_digits + 1); j <= i; j++)
x = x * 10 + s[j] - '0';
a.push_back(x);
}
trim();
}
friend istream& operator>>(istream &stream, bigint &v) {
string s;
stream >> s;
v.read(s);
return stream;
}
friend ostream& operator<<(ostream &stream, const bigint &v) {
if (v.sign == -1)
stream << '-';
stream << (v.a.empty() ? 0 : v.a.back());
for (int i = (int) v.a.size() - 2; i >= 0; --i)
stream << setw(base_digits) << setfill('0') << v.a[i];
return stream;
}
static vector<int> convert_base(const vector<int> &a, int old_digits, int new_digits) {
vector<long long> p(max(old_digits, new_digits) + 1);
p[0] = 1;
for (int i = 1; i < (int) p.size(); i++)
p[i] = p[i - 1] * 10;
vector<int> res;
long long cur = 0;
int cur_digits = 0;
for (int i = 0; i < (int) a.size(); i++) {
cur += a[i] * p[cur_digits];
cur_digits += old_digits;
while (cur_digits >= new_digits) {
res.push_back(int(cur % p[new_digits]));
cur /= p[new_digits];
cur_digits -= new_digits;
}
}
res.push_back((int) cur);
while (!res.empty() && !res.back())
res.pop_back();
return res;
}
typedef vector<long long> vll;
static vll karatsubaMultiply(const vll &a, const vll &b) {
int n = a.size();
vll res(n + n);
if (n <= 32) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
res[i + j] += a[i] * b[j];
return res;
}
int k = n >> 1;
vll a1(a.begin(), a.begin() + k);
vll a2(a.begin() + k, a.end());
vll b1(b.begin(), b.begin() + k);
vll b2(b.begin() + k, b.end());
vll a1b1 = karatsubaMultiply(a1, b1);
vll a2b2 = karatsubaMultiply(a2, b2);
for (int i = 0; i < k; i++)
a2[i] += a1[i];
for (int i = 0; i < k; i++)
b2[i] += b1[i];
vll r = karatsubaMultiply(a2, b2);
for (int i = 0; i < (int) a1b1.size(); i++)
r[i] -= a1b1[i];
for (int i = 0; i < (int) a2b2.size(); i++)
r[i] -= a2b2[i];
for (int i = 0; i < (int) r.size(); i++)
res[i + k] += r[i];
for (int i = 0; i < (int) a1b1.size(); i++)
res[i] += a1b1[i];
for (int i = 0; i < (int) a2b2.size(); i++)
res[i + n] += a2b2[i];
return res;
}
bigint operator*(const bigint &v) const {
vector<int> a6 = convert_base(this->a, base_digits, 6);
vector<int> b6 = convert_base(v.a, base_digits, 6);
vll a(a6.begin(), a6.end());
vll b(b6.begin(), b6.end());
while (a.size() < b.size())
a.push_back(0);
while (b.size() < a.size())
b.push_back(0);
while (a.size() & (a.size() - 1))
a.push_back(0), b.push_back(0);
vll c = karatsubaMultiply(a, b);
bigint res;
res.sign = sign * v.sign;
for (int i = 0, carry = 0; i < (int) c.size(); i++) {
long long cur = c[i] + carry;
res.a.push_back((int) (cur % 1000000));
carry = (int) (cur / 1000000);
}
res.a = convert_base(res.a, 6, base_digits);
res.trim();
return res;
}
};
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
int n;
vector<int> g[N];
array<int, 3> edges[N];
int convert(const int &ID, const int &v) {
return v ^ edges[ID][0] ^ edges[ID][1];
}
vector<long long> dijkstra(const int &s) {
vector<long long> d(n, 1e18);
d[s] = 0;
set<pair<long long, long long>> st;
st.insert({d[s], s});
while (!st.empty()) {
int v = st.begin()->second;
st.erase(st.begin());
for (auto &id : g[v]) {
int u = convert(id, v);
int w = edges[id][2];
if (d[u] > d[v] + w) {
st.erase({d[u], u});
d[u] = d[v] + w;
st.insert({d[u], u});
}
}
}
return d;
}
__Art__ {
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
if (fopen("art.inp", "r")) {
freopen("art.inp", "r", stdin);
freopen("art.out", "w", stdout);
}
if (fopen("INCGRAPH.inp", "r")) {
freopen("INCGRAPH.inp", "r", stdin);
freopen("INCGRAPH.out", "w", stdout);
}
int m;
cin >> n >> m;
REP (i, m) {
int v, u, w;
cin >> v >> u >> w;
edges[i] = {--v, --u, w};
g[v].emplace_back(i);
g[u].emplace_back(i);
}
vector<int> fs(m);
for (int i = m - 2; ~i; --i) {
fs[i] = max(fs[i + 1], edges[i + 1][2]);
}
auto d0 = dijkstra(0);
auto d1 = dijkstra(n - 1);
auto can = [&](const long long &x) -> bool {
int timer = 0;
vector<int> num(n), low(n), used(n);
bool ok = 0;
function<void(int,int)> dfs = [&](int v, int par) {
used[v] = 1;
num[v] = low[v] = timer++;
for (auto &id : g[v]) {
int u = convert(id, v);
auto &[A, B, w] = edges[id];
long long cs = min(d0[A] + d1[B], d1[A] + d0[B]) + w;
if (cs > x || id == par) continue;
if (used[u] == 1) {
minimize(low[v], num[u]);
}
else if (!used[u]) {
dfs(u, id);
minimize(low[v], low[u]);
if (low[u] > num[v] && num[n - 1] >= num[u] && cs + fs[id] > x) {
ok = 1;
}
}
}
used[v] = 2;
};
dfs(0, -1);
return ok;
};
long long l = d0[n - 1];
long long r = d0[n - 1] + fs[0] + 10;
long long res = l;
while (l <= r) {
long long m = l + (r - l) / 2;
if (can(m)) {
l = m + 1;
}
else {
res = m;
r = m - 1;
}
}
cout << res;
// cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s\n";
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
Aesthetic.cpp: In function 'int main()':
Aesthetic.cpp:900:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
900 | freopen("art.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
Aesthetic.cpp:901:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
901 | freopen("art.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Aesthetic.cpp:905:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
905 | freopen("INCGRAPH.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
Aesthetic.cpp:906:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
906 | freopen("INCGRAPH.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |