#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
44 ms |
16020 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
2636 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
44 ms |
16020 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
5272 KB |
Output is correct |
2 |
Correct |
15 ms |
5248 KB |
Output is correct |
3 |
Correct |
16 ms |
5188 KB |
Output is correct |
4 |
Correct |
16 ms |
5196 KB |
Output is correct |
5 |
Correct |
16 ms |
5204 KB |
Output is correct |
6 |
Correct |
19 ms |
5400 KB |
Output is correct |
7 |
Correct |
21 ms |
5364 KB |
Output is correct |
8 |
Correct |
20 ms |
5164 KB |
Output is correct |
9 |
Correct |
17 ms |
5168 KB |
Output is correct |
10 |
Correct |
20 ms |
5328 KB |
Output is correct |
11 |
Correct |
1 ms |
2636 KB |
Output is correct |
12 |
Correct |
3 ms |
2764 KB |
Output is correct |
13 |
Correct |
3 ms |
2764 KB |
Output is correct |
14 |
Correct |
3 ms |
2764 KB |
Output is correct |
15 |
Correct |
4 ms |
2784 KB |
Output is correct |
16 |
Correct |
3 ms |
2764 KB |
Output is correct |
17 |
Correct |
3 ms |
2764 KB |
Output is correct |
18 |
Correct |
4 ms |
2764 KB |
Output is correct |
19 |
Correct |
3 ms |
2764 KB |
Output is correct |
20 |
Correct |
1 ms |
2636 KB |
Output is correct |
21 |
Correct |
1 ms |
2636 KB |
Output is correct |
22 |
Correct |
2 ms |
2636 KB |
Output is correct |
23 |
Correct |
3 ms |
2764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
2636 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
44 ms |
16020 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |