#include <bits/stdc++.h>
#include "dreaming.h"
using namespace std;
long long n,m,k;
vector<pair<long long,long long> > v[100001];
long long l[100001];
long long c[100001],num;
long long in;
void prec(long long i,long long p)
{
c[i]=num;
for(long long j=0; j<v[i].size(); j++)
{
long long nb=v[i][j].first;
if(nb!=p)
{
prec(nb,i);
}
}
}
long long furt[100001],minn[100001];
long long dfs(long long i,long long p)
{
if(v[i].size()==1&&p||v[i].size()==0)return 0;
long long curr=0,f1=0,f2=0;
for(long long j=0; j<v[i].size(); j++)
{
long long nb=v[i][j].first;
if(nb==p)continue;
int f=dfs(nb,i);
curr=max(curr,dfs(nb,i)+v[i][j].second);
if(f>f1)
{
f2=f1;
f1=f;
}
else if(f>f2)
{
f2=f;
}
}
in=max(in,f1+f2);
//cout<<i<<" - "<<curr<<endl;
return curr;
}
long long solve()
{
for(long long i=1; i<=num; i++)
minn[i]=1e9;
for(long long i=1; i<=n; i++)
{
//cout<<c[i]<<" "<<i<<" "<<dfs(i,0)<<endl;
minn[c[i]]=min(minn[c[i]],dfs(i,0));
}
sort(minn+1,minn+num+1);
return max(k+minn[num]+minn[num-1],in);
}
int travelTime(int N,int M,int L,int A[],int B[],int T[])
{
n=N;
m=M;
k=L;
//cout<<n<<" "<<m<<" "<<k<<endl;
for(long long i=0;i<m;i++)
{
A[i]++;
B[i]++;
//cout<<A[i]<<" "<<B[i]<<" "<<T[i]<<endl;
v[A[i]].push_back({B[i],T[i]});
v[B[i]].push_back({A[i],T[i]});
}
for(long long i=1; i<=n; i++)
if(!c[i])
{
num++;
prec(i,0);
}
return solve();
}
/*
12 8 2
0 8 4
8 2 2
2 7 4
5 11 3
5 1 7
1 3 1
1 9 5
10 6 3
*/
Compilation message
dreaming.cpp: In function 'void prec(long long int, long long int)':
dreaming.cpp:15:25: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
15 | for(long long j=0; j<v[i].size(); j++)
| ~^~~~~~~~~~~~
dreaming.cpp: In function 'long long int dfs(long long int, long long int)':
dreaming.cpp:28:22: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
28 | if(v[i].size()==1&&p||v[i].size()==0)return 0;
| ~~~~~~~~~~~~~~^~~
dreaming.cpp:30:25: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | for(long long j=0; j<v[i].size(); j++)
| ~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1041 ms |
14692 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1056 ms |
4700 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1041 ms |
14692 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
7672 KB |
Output is correct |
2 |
Incorrect |
17 ms |
7768 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1056 ms |
4700 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1041 ms |
14692 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |