# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
914994 | xynex | 봉쇄 시간 (IOI23_closing) | C++17 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/**
* Author: Tenjin
* Created: 27.04.2022 18:58
* Why am I so stupid? :c
* Slishkom slab
**/
#include <bits/stdc++.h>
// #pragma GCC optimize("inline")
// #pragma GCC optimize("-fgcse,-fgcse-lm")
// #pragma GCC optimize("-ftree-pre,-ftree-vrp")
// #pragma GCC optimize("-ffast-math")
// #pragma GCC optimize("-fipa-sra")
// #pragma GCC optimize("-fpeephole2")
// #pragma GCC optimize("-fsched-spec")
// #pragma GCC optimize("Ofast,no-stack-protector")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
// #pragma GCC optimize("unroll-loops")
using namespace std;
#define ll long long
#define dl double long
#define ull unsigned long long
#define pr pair
#define vt vector
#define ff first
#define ss second
#define mp make_pair
#define sz(a) (int)a.size()
#define pb push_back
#define pf push_front
#define popB pop_back
#define popF pop_front
#define bit_count __builtin_popcount
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sp(x) fixed << setprecision(x)
template<typename T> T get_rand(T l, T r) {
random_device rd;
mt19937 gen(rd());
return uniform_int_distribution<T>(l, r)(gen);
}
template<typename T> T lcm(T a, T b) { return a * (b / __gcd(a, b)); }
template<class A> void read(vt<A>& v);
template<class A, size_t S> void read(array<A, S>& a);
template<class T> void read(T& x) { cin >> x; }
void read(double& d) { string t; read(t); d = stod(t); }
void read(long double& d) { string t; read(t); d = stold(t); }
template<class H, class... T> void read(H& h, T&... t) { read(h); read(t...); }
template<class A> void read(vt<A>& x) { for (auto& a : x) read(a); }
template<class A, size_t S> void read(array<A, S>& x) { for (auto& a : x) read(a); }
string to_string(char c) { return string(1, c); }
string to_string(bool b) { return b ? "true" : "false"; }
string to_string(const char* s) { return string(s); }
string to_string(string s) { return s; }
string to_string(vt<bool> v) { string res; for (int i = 0; i < sz(v); ++i) res += char('0' + v[i]); return res; }
template<size_t S> string to_string(bitset<S> b) { string res; for (int i = 0; i < S; ++i) res += char('0' + b[i]); return res; }
template<class T> string to_string(T v) { bool f = 1; string res; for (auto x : v) { if (!f) res += ' '; f = 0; res += to_string(x); } return res; }
template<class A> void write(A x) { cout << to_string(x); }
template<class H, class... T> void write(const H& h, const T&... t) { write(h); write(t...); }
void print() { write("\n"); }
template<class H, class... T> void print(const H& h, const T&... t) { write(h); if (sizeof...(t)) write(' '); print(t...); }
void freop(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
const int MOD = 1e9 + 7;
const ll INF = 1e14;
const int M = 2e5 + 5;
const dl pi = acos(-1);
const dl eps = 1e-12;
const int sq = 700;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
/* ll vs int*/
vt<pr<int, int>> g[M];
ll distX[M], distY[M];
void dfsX(int v, int p = -1, ll cnt = 0) {
distX[v] = cnt;
for(auto to : g[v]) {
if(to.ff == p) continue;
dfsX(to.ff, v, cnt + to.ss);
}
}
void dfsY(int v, int p = -1, ll cnt = 0) {
distY[v] = cnt;
for(auto to : g[v]) {
if(to.ff == p) continue;
dfsY(to.ff, v, cnt + to.ss);
}
}
int t[M];
int ok[M];
void dfs1(int v, int p = -1) {
ok[v] = 1;
for(auto to : g[v]) {
if(to.ff == p || ok[to.ff]) continue;
dfs1(to.ff, v);
}
}
int max_score(int N, int X, int Y, ll K, vt<int> U, vt<int> V, vt<int> W) {
for(int i = 0; i < N; ++i) {
g[i].clear();
distX[i] = distY[i] = 0;
t[i] = 0;
ok[i] = 0;
}
for(int i = 0; i < N - 1; ++i) {
g[U[i]].pb({V[i], W[i]});
g[V[i]].pb({U[i], W[i]});
}
dfsX(X);
dfsY(Y);
for(int i = 0; i < N; ++i) {
if(distX[i] + distY[i] == distX[Y]) {
ok[i] = 1;
//write(i, ' ');
}
}
for(int i = 0; i < N; ++i) {
if(i == X || i == Y || !ok[i]) continue;
dfs1(i);
}
vt<ll> ord, ord1;
for(int i = 0; i < N; ++i) {
if(!ok[i]) {
if(distX[i] > distY[i]) {
ord.pb(distY[i]);
}
else {
ord.pb(distX[i]);
}
}
}
if(!ord.empty()) {
sort(all(ord));
ord1[0] = ord[0];
for(int i = 1; i < sz(ord); ++i) {
ord1[i] = ord1[i - 1] + ord[i];
}
}
set<pr<pr<ll, ll>, int> > F = {{{0, MOD}, X}}, S = {{{0, MOD}, Y}};
vt<int> usd(N), usd1(N);
bool Num1 = 0, Num2 = 0;
int ans = 0, res = 0;
while(!F.empty() || !S.empty()) {
/*while(!F.empty() && usd[(*F.begin()).ss] == 2) F.erase(F.begin());
while(!S.empty() && usd1[(*S.begin()).ss] == 2) S.erase(S.begin());*/
if(F.empty()) {
auto cur1 = *S.begin();
S.erase(S.begin());
if(cur1.ff.ff <= K) {
K -= cur1.ff.ff;
t[cur1.ss] += cur1.ff.ff;
usd1[cur1.ss] = 2;
if(cur1.ss == X) Num2 = 1;
ans++;
for(auto to : g[cur1.ss]) {
if(!usd1[to.ff]) {
if(!ok[to.ff]) continue;
usd1[to.ff] = 1;
if(usd[to.ff] == 1) {
auto fn = F.lower_bound({{distX[to.ff], MOD}, to.ff});
if(fn != F.end() && (*fn).ss == to.ff) {
F.erase(fn);
F.insert({{(*fn).ff.ff, abs(distX[to.ff] - distY[to.ff])}, to.ff});
}
S.insert({{distY[to.ff], abs(distX[to.ff] - distY[to.ff])}, to.ff});
}
else {
S.insert({{distY[to.ff] - t[to.ff], MOD}, to.ff});
}
}
}
}
}
else if(S.empty()) {
auto cur = *F.begin();
F.erase(F.begin());
if(cur.ff.ff <= K) {
K -= cur.ff.ff;
t[cur.ss] += cur.ff.ff;
usd[cur.ss] = 2;
if(cur.ss == Y) Num1 = 1;
ans++;
for(auto to : g[cur.ss]) {
if(!usd[to.ff]) {
if(!ok[to.ff]) continue;
usd[to.ff] = 1;
if(usd1[to.ff]) {
auto fn = S.lower_bound({{distY[to.ff], MOD}, to.ff});
if(fn != S.end() && (*fn).ss == to.ff) {
S.erase(fn);
S.insert({{(*fn).ff.ff, abs(distX[to.ff] - distY[to.ff])}, to.ff});
}
F.insert({{distX[to.ff], abs(distX[to.ff] - distY[to.ff])}, to.ff});
}
else {
F.insert({{distX[to.ff] - t[to.ff], MOD}, to.ff});
}
}
}
}
}
else {
auto cur = *F.begin(), cur1 = *S.begin();
/*print("F", cur.ff.ff, cur.ff.ss, cur.ss, "S", cur1.ff.ff, cur1.ff.ss, cur1.ss, "K", K);
for(auto curr : F) print(curr.ff.ff, curr.ff.ss, curr.ss);
print("===");
for(auto curr : S) print(curr.ff.ff, curr.ff.ss, curr.ss);*/
if(cur.ff.ff > cur1.ff.ff) {
//print("S", cur1.ss);
S.erase(S.begin());
if(usd[cur1.ss] == 1) {
auto fn = F.lower_bound({{distX[cur1.ss], abs(distX[cur1.ss] - distY[cur1.ss])}, cur1.ss});
if(fn != F.end() && (*fn).ss == cur1.ss) {
F.erase(fn);
F.insert({{(*fn).ff.ff - cur1.ff.ff, MOD}, cur1.ss});
}
}
if(cur1.ff.ff <= K) {
K -= cur1.ff.ff;
t[cur1.ss] += cur1.ff.ff;
usd1[cur1.ss] = 2;
if(cur1.ss == X) Num2 = 1;
ans++;
for(auto to : g[cur1.ss]) {
if(!usd1[to.ff]) {
if(!ok[to.ff]) continue;
usd1[to.ff] = 1;
if(usd[to.ff] == 1) {
auto fn = F.lower_bound({{distX[to.ff], MOD}, to.ff});
if(fn != F.end() && (*fn).ss == to.ff) {
F.erase(fn);
F.insert({{(*fn).ff.ff, abs(distX[to.ff] - distY[to.ff])}, to.ff});
}
S.insert({{distY[to.ff], abs(distX[to.ff] - distY[to.ff])}, to.ff});
}
else {
S.insert({{distY[to.ff] - t[to.ff], MOD}, to.ff});
}
}
}
}
}
else if(cur.ff.ff < cur1.ff.ff) {
// print("F", cur.ss);
F.erase(F.begin());
if(usd1[cur.ss] == 1) {
auto fn = S.lower_bound({{distY[cur.ss], abs(distX[cur.ss] - distY[cur.ss])}, cur.ss});
if(fn != S.end() && (*fn).ss == cur.ss) {
S.erase(fn);
S.insert({{(*fn).ff.ff - cur.ff.ff, MOD}, cur.ss});
}
}
if(cur.ff.ff <= K) {
K -= cur.ff.ff;
t[cur.ss] += cur.ff.ff;
usd[cur.ss] = 2;
if(cur.ss == Y) Num1 = 1;
ans++;
for(auto to : g[cur.ss]) {
if(!usd[to.ff]) {
if(!ok[to.ff]) continue;
usd[to.ff] = 1;
if(usd1[to.ff]) {
auto fn = S.lower_bound({{distY[to.ff], MOD}, to.ff});
if(fn != S.end() && (*fn).ss == to.ff) {
S.erase(fn);
S.insert({{(*fn).ff.ff, abs(distX[to.ff] - distY[to.ff])}, to.ff});
}
F.insert({{distX[to.ff], abs(distX[to.ff] - distY[to.ff])}, to.ff});
}
else {
F.insert({{distX[to.ff] - t[to.ff], MOD}, to.ff});
}
}
}
}
}
else if(cur.ff.ff == cur1.ff.ff) {
if(cur.ff.ss > cur1.ff.ss) {
// print("F1", cur.ss);
S.erase(S.begin());
if(usd[cur1.ss] == 1) {
auto fn = F.lower_bound({{distX[cur1.ss], abs(distX[cur1.ss] - distY[cur1.ss])}, cur1.ss});
if(fn != F.end() && (*fn).ss == cur1.ss) {
F.erase(fn);
F.insert({{(*fn).ff.ff - cur1.ff.ff, MOD}, cur1.ss});
}
}
if(cur1.ff.ff <= K) {
K -= cur1.ff.ff;
t[cur1.ss] += cur1.ff.ff;
usd1[cur1.ss] = 2;
if(cur1.ss == X) Num2 = 1;
ans++;
for(auto to : g[cur1.ss]) {
if(!usd1[to.ff]) {
if(!ok[to.ff]) continue;
usd1[to.ff] = 1;
if(usd[to.ff] == 1) {
auto fn = F.lower_bound({{distX[to.ff], MOD}, to.ff});
if(fn != F.end() && (*fn).ss == to.ff) {
F.erase(fn);
F.insert({{(*fn).ff.ff, abs(distX[to.ff] - distY[to.ff])}, to.ff});
}
S.insert({{distY[to.ff], abs(distX[to.ff] - distY[to.ff])}, to.ff});
}
else {
S.insert({{distY[to.ff] - t[to.ff], MOD}, to.ff});
}
}
}
}
}
else {
// print("S1", cur1.ss);
F.erase(F.begin());
if(usd1[cur.ss] == 1) {
auto fn = S.lower_bound({{distY[cur.ss], abs(distX[cur.ss] - distY[cur.ss])}, cur.ss});
if(fn != S.end() && (*fn).ss == cur.ss) {
S.erase(fn);
S.insert({{(*fn).ff.ff - cur.ff.ff, MOD}, cur.ss});
}
}
if(cur.ff.ff <= K){
K -= cur.ff.ff;
t[cur.ss] += cur.ff.ff;
usd[cur.ss] = 2;
if(cur.ss == Y) Num1 = 1;
ans++;
for(auto to : g[cur.ss]) {
if(!usd[to.ff]) {
if(!ok[to.ff]) continue;
usd[to.ff] = 1;
if(usd1[to.ff]) {
auto fn = S.lower_bound({{distY[to.ff], MOD}, to.ff});
if(fn != S.end() && (*fn).ss == to.ff) {
S.erase(fn);
S.insert({{(*fn).ff.ff, abs(distX[to.ff] - distY[to.ff])}, to.ff});
}
F.insert({{distX[to.ff], abs(distX[to.ff] - distY[to.ff])}, to.ff});
}
else {
F.insert({{distX[to.ff] - t[to.ff], MOD}, to.ff});
}
}
}
}
}
}
}
if(Num1) {
Num1 = 0;
for(int i = 0; i < N; ++i) {
if(distY[i] < distX[i] && !ok[i]) {
ord.pb(distX[i] + distX[Y]);
}
}
if(!ord.empty()) {
sort(all(ord));
ord1.resize(sz(ord));
ord1[0] = ord[0];
for(int i = 1; i < sz(ord); ++i) {
ord1[i] = ord1[i - 1] + ord[i];
}
}
}
if(Num2) {
Num2 = 0;
for(int i = 0; i < N; ++i) {
if(distX[i] < distY[i] && !ok[i]) {
ord.pb(distY[i] + distX[Y]);
}
}
if(!ord.empty()) {
sort(all(ord));
ord1.resize(sz(ord));
ord1[0] = ord[0];
for(int i = 1; i < sz(ord); ++i) {
ord1[i] = ord1[i - 1] + ord[i];
}
}
}
int l = 0, r = sz(ord1) - 1;
while((r - l) >= 0) {
int mid = (r + l) >> 1;
if(K >= ord1[mid]) l = mid + 1;
else r = mid - 1;
}
if(l) l--;
if(!ord1.empty() && K >= ord1[l]) {
res = max(res, ans + l + 1);
}
else {
res = max(res, ans);
}
}
//print("");
return res;
}
int main() {
int Q;
assert(1 == scanf("%d", &Q));
std::vector<int> N(Q), X(Q), Y(Q);
std::vector<long long> K(Q);
std::vector<std::vector<int>> U(Q), V(Q), W(Q);
for (int q = 0; q < Q; q++) {
assert(4 == scanf("%d %d %d %lld", &N[q], &X[q], &Y[q], &K[q]));
U[q].resize(N[q] - 1);
V[q].resize(N[q] - 1);
W[q].resize(N[q] - 1);
for (int i = 0; i < N[q] - 1; ++i) {
assert(3 == scanf("%d %d %d", &U[q][i], &V[q][i], &W[q][i]));
}
}
std::vector<int> result(Q);
for (int q = 0; q < Q; q++) {
result[q] = max_score(N[q], X[q], Y[q], K[q], U[q], V[q], W[q]);
}
for (int q = 0; q < Q; q++) {
printf("%d\n", result[q]);
}
return 0;
}
/*
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//freop("");
int t = 1;
//read(t);
for (int i = 1; i <= t; ++i) {
//write("Case #" + to_string(i) + ": ");
solve();
}
return 0;
}*/