#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
#define REP(i,a,b) for(int i=a;i<b;i++)
#define F first
#define S second
#define PB push_back
#define MP make_pair
vi leaf;vector<vi> grid;
int findLeaf(int a,vector<vi> &c,vi &p,vi &toLeaf,int &i,vi &up){
if(toLeaf[a]!=-1)return toLeaf[a];
up[a]=i;
if(c[a].size()==0){
toLeaf[a]=0;
return 0;
}
if(c[a].size()<2&&p[a]!=a){
toLeaf[a]=0;
return 0;
}
for(auto u:c[a]){
if(u==p[a])continue;
p[u]=a;
toLeaf[a]=max(toLeaf[a],findLeaf(u,c,p,toLeaf,i,up)+grid[a][u]);
if(toLeaf[a]==toLeaf[u]+grid[u][a])leaf[a]=u;
}
return toLeaf[a];
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
vi p(N,-1);
vi toLeaf(N,-1);
vi maxLength(N,0);
vi up(N);
vi parents;
leaf.resize(N,-1);
grid.resize(N,vi(N));
vector<vi> c(N);
REP(i,0,M){
c[A[i]].PB(B[i]);
c[B[i]].PB(A[i]);
grid[A[i]][B[i]]=T[i];
grid[B[i]][A[i]]=T[i];
}
REP(i,0,N){
if(toLeaf[i]!=-1)continue;
p[i]=i;parents.PB(i);up[i]=i;
findLeaf(i,c,p,toLeaf,i,up);
}
// cout<<"lol";
REP(i,0,N){
// cout<<i<<" "<<c[i].size()<<" ";
if(c[i].size()==0){
maxLength[i]=0;
continue;
}
if(c[i].size()==1){
maxLength[i]=toLeaf[i];
continue;
}
if(c[i].size()<3&&p[i]!=i)continue;
priority_queue<int> pq;
for(auto u:c[i]){
if(p[i]==u)continue;
// if(toLeaf[u]==-1)pq.push(grid[i][u]);
else pq.push(toLeaf[u]+grid[i][u]);
}
maxLength[i]+=pq.top();
pq.pop();;
maxLength[i]+=pq.top();
}
map<int,pi> m;
REP(i,0,N){
if(m.count(up[i])){
if(m[up[i]].F<maxLength[i]){
m[up[i]]=make_pair(maxLength[i],i);
}
}
else{
m[up[i]]=make_pair(maxLength[i],i);
}
}
priority_queue<pi> pq2;
priority_queue<int> maxVals;
for(auto z:parents){
int u=m[z].S;int u2=u;
vi tempVal1,tempVal2;
if(c[u].size()==0){
maxVals.push(0);
}
else if(c[u].size()==1){
while(leaf[u]!=-1){
tempVal1.PB(grid[u][leaf[u]]);
u=leaf[u];
}
// cout<<"lol\n";
int sum=0;int pos=0;
while(sum<maxLength[u2]/2){
sum+=tempVal1[pos];
pos++;
}
int sum2=0;pos=0;
while(sum<maxLength[u2]/2){
sum2+=tempVal1[pos];
pos++;
}
sum=min(max(sum,maxLength[u2]-sum),max(sum2,maxLength[u2]-sum2));
maxVals.push(sum);
}
else{
for(auto g:c[u]){
if(g==p[u])continue;
pq2.push({toLeaf[g]+grid[g][u],g});
}
int x1=pq2.top().S;pq2.pop();
int x2=pq2.top().S;
tempVal1.PB(grid[u][x1]);
tempVal2.PB(grid[u][x2]);
while(leaf[x1]!=-1){
tempVal1.PB(grid[x1][leaf[x1]]);
x1=leaf[x1];
}
while(leaf[x2]!=-1){
tempVal1.PB(grid[x2][leaf[x2]]);
x2=leaf[x2];
}
reverse(tempVal1.begin(),tempVal1.end());
for(auto y:tempVal2)tempVal1.PB(y);
int sum=0;int pos=0;
while(sum<maxLength[u2]/2){
sum+=tempVal1[pos];
pos++;
}
reverse(tempVal1.begin(),tempVal1.end());
int sum2=0;pos=0;
while(sum<maxLength[u2]/2){
sum2+=tempVal1[pos];
pos++;
}
sum=min(max(sum,maxLength[u2]-sum),max(sum2,maxLength[u2]-sum2));
maxVals.push(sum);
}
}
//REP(i,0,N)cout<<toLeaf[i]<<" ";
//cout<<"\n";
//REP(i,0,N)cout<<maxLength[i]<<" ";
//cout<<"\n";
//for(auto g1:maxVals)cout<<g1<<" ";
// cout<<"\n";
int r1=maxVals.top();
maxVals.pop();
int r2=maxVals.top();
maxVals.pop();
int r3=maxVals.top();
int ans=r1+r2+L;
// cout<<r1<<" "<<r2<<" "<<r3<<"\n";
ans=max(ans,r2+r3+2*L);
for(auto z:parents){
int k1=m[z].F;
ans=max(ans,k1);
}
return ans;
return 42;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
44 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
44 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
32 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
44 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |