#include "closing.h"
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define ii pair<int,long long>
#define ll long long
const long long inf=1e18+7;
using Graph = vector<vector<pair<int, long long>>> ;
template<typename T>
bool maximize(T &a,T b)
{
if(a<b)
{
a=b;
return true;
}
return false;
}
void dfs(int x,int par,Graph &edge,vector<ll>&dist)
{
for(ii need:edge[x])
{
int node=need.x;
ll value=need.y;
if(node==par)continue;
dist[node]=dist[x]+value;
dfs(node,x,edge,dist);
}
}
int Sub1(int N,long long K,vector<ll>distX,vector<ll>distY)
{
vector<ll>que;
for(int i=1; i<=N; i++)
{
que.push_back(distX[i]);
que.push_back(distY[i]);
}
sort(que.begin(),que.end());
ll res=K;
int ans=0;
for(int value:que)
{
if(res<value)break;
res-=value;
ans++;
}
return ans;
}
bool checkLinear(int N,Graph &edge,vector<int>°)
{
int maxv=0;
for(int i=1;i<=N;i++)
{
for(ii need:edge[i])
{
int node=need.x;
deg[node]++;
maxv=max(maxv,deg[node]);
}
}
return maxv<=2;
}
int Sub2(int N,int X,int Y,long long K,Graph &edge,vector<int>deg,vector<long long>distX,vector<long long>distY)
{
int need_node;
if(deg[X]>deg[Y])swap(X,Y);
for(int i=1;i<=N;i++)if(deg[i]==1)
{
need_node=i;
break;
}
vector<int>beg(N+1,0);
vector<int>pos(N+1);
beg[need_node]=1;
queue<int>q;
q.push(need_node);
while(!q.empty())
{
int x=q.front();
pos[beg[x]]=x;
q.pop();
for(ii need:edge[x])
{
int node=need.x;
int value=need.y;
if(!beg[node])
{
beg[node]=beg[x]+1;
q.push(node);
}
}
}
vector<long long>sumX(N+1);
vector<long long>sumY(N+1);
for(int i=1;i<=N;i++)
{
sumX[i]=sumX[i-1]+min(distX[i],distY[i]);
sumY[i]=sumY[i-1]+max(distX[i],distY[i]);
}
vector<int>que;
for(int i=1;i<=N;i++)
{
que.push_back(distX[i]);
que.push_back(distY[i]);
}
sort(que.begin(),que.end());
int ans=0;
long long res=K;
for(long long value:que)
{
if(value>res)break;
ans++;
res-=value;
}
for(int i=1;i<=N;i++)
{
for(int j=i;j<=N;j++)
{
long long value=sumY[j]-sumY[i-1];
int total=(j-i+1)*2;
if(i-1>=beg[X])
{
total+=(i-beg[X]);
value+=sumX[i-1]-sumX[beg[X]-1];
}
if(j<=beg[Y])
{
total+=(beg[Y]-j);
value+=sumX[beg[Y]]-sumX[j];
}
if(value>K)continue;
vector<long long>block;
for(int cnt=1;cnt<=min(beg[X]-1,i-1);cnt++)block.push_back(distX[pos[cnt]]);
for(int cnt=max(beg[Y]+1,j+1);cnt<=N;cnt++)block.push_back(distY[pos[cnt]]);
sort(block.begin(),block.end());
long long used=K-value;
for(long long need:block)
{
if(used<need)break;
total+=1;
used-=need;
}
ans=max(ans,total);
}
}
return ans;
}
int max_score(int N,int X,int Y,long long K,vector<int>U,vector<int>V,vector<int>W)
{
X+=1;
Y+=1;
Graph edge(N+1);
for(int i=1; i<=N-1; i++)
{
int x=U[i-1]+1;
int y=V[i-1]+1;
int value=W[i-1];
edge[x].push_back({y,value});
edge[y].push_back({x,value});
}
vector<ll>distX(N+1);
vector<ll>distY(N+1);
dfs(X,0,edge,distX);
dfs(Y,0,edge,distY);
if(distX[Y]>2*K)return Sub1(N,K,distX,distY);
vector<int>deg(N+1);
if(checkLinear(N,edge,deg))return Sub2(N,X,Y,K,edge,deg,distX,distY);
}
/*signed main()
{
int n,x,y,k;
vector<int>U;
vector<int>V;
vector<int>W;
cin>>n>>x>>y>>k;
for(int i=1; i<=n-1; i++)
{
int x;
cin>>x;
U.push_back(x);
}
for(int i=1; i<=n-1; i++)
{
int y;
cin>>y;
V.push_back(y);
}
for(int i=1; i<=n-1; i++)
{
int value;
cin>>value;
W.push_back(value);
}
cout<<max_score(n,x,y,k,U,V,W);
}*/
Compilation message
closing.cpp: In function 'int Sub2(int, int, int, long long int, Graph&, std::vector<int>, std::vector<long long int>, std::vector<long long int>)':
closing.cpp:87:17: warning: unused variable 'value' [-Wunused-variable]
87 | int value=need.y;
| ^~~~~
closing.cpp: In function 'int max_score(int, int, int, long long int, std::vector<int>, std::vector<int>, std::vector<int>)':
closing.cpp:156:19: warning: control reaches end of non-void function [-Wreturn-type]
156 | Graph edge(N+1);
| ^
closing.cpp: In function 'int Sub2(int, int, int, long long int, Graph&, std::vector<int>, std::vector<long long int>, std::vector<long long int>)':
closing.cpp:82:18: warning: 'need_node' may be used uninitialized in this function [-Wmaybe-uninitialized]
82 | pos[beg[x]]=x;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
2 ms |
600 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
131 ms |
36688 KB |
Output is correct |
2 |
Correct |
144 ms |
44104 KB |
Output is correct |
3 |
Correct |
73 ms |
3016 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
2 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
2 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
600 KB |
Output is correct |
18 |
Correct |
2 ms |
436 KB |
Output is correct |
19 |
Correct |
345 ms |
544 KB |
Output is correct |
20 |
Correct |
2 ms |
344 KB |
Output is correct |
21 |
Correct |
195 ms |
524 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
2 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
2 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
600 KB |
Output is correct |
18 |
Correct |
2 ms |
436 KB |
Output is correct |
19 |
Correct |
345 ms |
544 KB |
Output is correct |
20 |
Correct |
2 ms |
344 KB |
Output is correct |
21 |
Correct |
195 ms |
524 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
23 ms |
500 KB |
Output is correct |
26 |
Execution timed out |
1082 ms |
1116 KB |
Time limit exceeded |
27 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
2 ms |
600 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
2 ms |
600 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
2 ms |
600 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
2 ms |
600 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
2 ms |
600 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |