#include "closing.h"
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
const long long INF = 1e18+1;
typedef long long ll;
const ll MOD = 1e9+7;
#define F first
#define P pair
#define S second
#define pb push_back
#define all(v) v.begin(), v.end()
typedef tree<int,null_type,less<int>,rb_tree_tag,
tree_order_statistics_node_update> indexed_set;
void file() {
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
}
vector<P<int,ll>>adj[(int)2e5+1];
bool b[(int)2e5+1];
ll d1[(int)2e5+1];
ll d2[(int)2e5+1];
void dfs1(int n,int p,int y) {
if (n==y) {
b[n]=true;
return;
}
for (auto [u,w]:adj[n]) {
if (u==p)continue;
dfs1(u,n,y);
b[n]|=b[u];
}
}
void dfs2(int n,int p) {
for (auto [u,w]:adj[n]) {
if (u==p)continue;
d1[u]=d1[n]+w;
dfs2(u,n);
}
}
void dfs3(int n,int p) {
for (auto [u,w]:adj[n]) {
if (u==p)continue;
d2[u]=d2[n]+w;
dfs3(u,n);
}
}
int max_score(int N, int X, int Y, long long K,
std::vector<int> U, std::vector<int> V, std::vector<int> W)
{
for (int i=0;i<=N;i++) {
adj[i].clear();
b[i]=false;
}
d1[X]=0;d2[Y]=0;
for (int i=0;i<N-1;i++) {
adj[U[i]].pb({V[i],W[i]});
adj[V[i]].pb({U[i],W[i]});
}
dfs1(X,-1,Y);
dfs2(X,-1);
dfs3(Y,-1);
for (int i=0;i<N;i++) {
if (d1[i]>d2[i])swap(d1[i],d2[i]);
}
ll v[N];
for (int i=0;i<N;i++) {
v[i]=d1[i];
}
sort(v,v+N);
ll z=0;
int ans=0;
for (int i=0;i<N;i++) {
z+=v[i];
if (z<=K)ans++;
}
multiset<P<ll,int>>st1,st2;
int c=0,ans1=0;
ll cst=0;
for (int i=0;i<N;i++) {
if (b[i]) {
cst+=d1[i];
c++;
}
}
if (cst<=K) {
ans1=c;
}
for (int i=0;i<N;i++) {
if (b[i]) {
st1.insert({d2[i]-d1[i],i});
}
else {
st1.insert({d1[i],i});
st2.insert({d2[i],i});
}
}
while (true) {
if (cst<=K) {
ans1=c;
}
if (st1.empty() && st2.empty()) {
break;
}
bool f1=false;
bool f2=false;
bool f3=false;
if (st2.empty()) {
f3=true;
}
else {
if (st1.size()==1) {
f3=true;
}
else {
if (min(st1.begin()->F+next(st1.begin())->F,st2.begin()->F)+cst>K) {
f3=true;
}
else {
if (st1.begin()->F+next(st1.begin())->F>st2.begin()->F) {
f2=true;
}
else
f1=true;
}
}
}
if (f1) {
auto it1=st1.begin();
auto it2=next(st1.begin());
auto p1=*st1.begin();
auto p2=*next(st1.begin());
cst+=it1->F+it2->F;
c+=2;
auto g=st2.find({d2[it1->S],it1->S});
if (g!=st2.end()) {
st2.erase(g);
}
g=st2.find({d2[it2->S],it2->S});
if (g!=st2.end()) {
st2.erase(g);
}
st1.erase(it1);
st1.erase(it2);
if (!b[p1.S]) {
st1.insert({d2[p1.S]-d1[p1.S],p1.S});
}
if (!b[p2.S]) {
st1.insert({d2[p2.S]-d1[p2.S],p2.S});
}
b[p1.S]=true;
b[p2.S]=true;
}
if (f2){
auto it=st2.begin();
cst+=it->F;
c+=2;
auto g=st1.find({d1[it->S],it->S});
st1.erase(g);
st2.erase(it);
}
if (f3) {
auto it=st1.begin();
auto p=*st1.begin();
cst+=it->F;
c++;
break;
}
}
if (cst<=K) {
ans1=c;
}
return max(ans,ans1);
}
Compilation message (stderr)
closing.cpp: In function 'void file()':
closing.cpp:17:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
17 | freopen("input.txt","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
closing.cpp:18:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
18 | freopen("output.txt","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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |