#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
bool viz[100005];
vector<pair<int, int>> v[100005];
pair<int, int> dfs(int p, int par)
{
viz[p]=true;
pair<int, int> ans={0, p};
for(auto it : v[p])
{
if(it.first == par)
continue;
pair<int, int> nans = dfs(it.first, p);
ans = max(ans, {nans.first + it.second, nans.second});
}
return ans;
}
bool calcPath(int p, int par, int b, vector<int> &e)
{
if(p == b)
return true;
for(auto it : v[p])
{
if(it.first == par)
continue;
if(calcPath(it.first, p, b, e))
{
e.push_back(it.second);
return true;
}
}
return false;
}
int solve(int p)
{
int a = dfs(p, 0).second;
int b = dfs(a, 0).second;
vector<int> e;
calcPath(a, 0, b, e);
if(e.empty())
return 0;
int ans=1e9, sumP=0, sumS=0;
for(int it : e)
sumS += it;
for(int it : e)
{
sumP += it;
sumS -= it;
ans=min(ans, max(sumP, sumS));
}
return ans;
}
int travelTime(int n, int m, int l, int a[], int b[], int t[])
{
for(int i=0;i<m;i++)
{
v[a[i]+1].push_back({b[i]+1, t[i]});
v[b[i]+1].push_back({a[i]+1, t[i]});
}
int nr=0;
int ans1=0, ans2=0, ans3=0;
for(int i=1;i<=n;i++)
{
if(!viz[i])
{
nr++;
int nans = solve(i);
if(nans>ans1)
{
ans3=ans2;
ans2=ans1;
ans1=nans;
}
else if(nans > ans2)
{
ans3=ans2;
ans2=nans;
}
else if(nans > ans3)
{
ans3=nans;
}
}
}
if(nr>=2)
return max(l+ans1+ans2, 2*l+ans2+ans3);
if(nr == 2)
return l+ans1+ans2;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
45 ms |
17032 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2656 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
45 ms |
17032 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
5476 KB |
Output is correct |
2 |
Correct |
16 ms |
5492 KB |
Output is correct |
3 |
Correct |
17 ms |
5440 KB |
Output is correct |
4 |
Correct |
26 ms |
5420 KB |
Output is correct |
5 |
Correct |
18 ms |
5448 KB |
Output is correct |
6 |
Correct |
17 ms |
5600 KB |
Output is correct |
7 |
Correct |
16 ms |
5596 KB |
Output is correct |
8 |
Correct |
15 ms |
5344 KB |
Output is correct |
9 |
Correct |
15 ms |
5352 KB |
Output is correct |
10 |
Correct |
17 ms |
5544 KB |
Output is correct |
11 |
Correct |
2 ms |
2636 KB |
Output is correct |
12 |
Correct |
3 ms |
2660 KB |
Output is correct |
13 |
Correct |
3 ms |
2764 KB |
Output is correct |
14 |
Correct |
3 ms |
2764 KB |
Output is correct |
15 |
Correct |
3 ms |
2784 KB |
Output is correct |
16 |
Correct |
5 ms |
2660 KB |
Output is correct |
17 |
Correct |
3 ms |
2764 KB |
Output is correct |
18 |
Correct |
3 ms |
2796 KB |
Output is correct |
19 |
Correct |
4 ms |
2764 KB |
Output is correct |
20 |
Correct |
2 ms |
2636 KB |
Output is correct |
21 |
Incorrect |
1 ms |
2636 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2656 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
45 ms |
17032 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |