#include <bits/stdc++.h>
#include "dreaming.h"
#define MAXN 100000
using namespace std;
vector<pair<int, int>> nodi[MAXN];
vector<bool> proc;
vector<int> dist;
void dfs(int v, vector<int>& gruppo)
{
proc[v] = true;
gruppo.push_back(v);
for (auto [u, w]: nodi[v])
{
if (!proc[u])
{
dist[u] = dist[v] + w;
dfs(u, gruppo);
}
}
}
void dfs2(int v)
{
proc[v] = true;
for (auto [u, w]: nodi[v])
{
if (!proc[u])
{
dist[u] = dist[v] + w;
dfs2(u);
}
}
}
bool dfs3(int v, int p, int y, vector<int>& diam)
{
if (v == y)
return true;
bool ok = false;
for (auto [u, w]: nodi[v])
{
if (u != p)
{
if (dfs3(u, v, y, diam))
{
ok = true;
diam.push_back(w);
break;
}
}
}
return ok;
}
vector<int> trova_diametro(int v)
{
vector<int> gruppo;
dist[v] = 0;
dfs(v, gruppo);
sort(gruppo.begin(), gruppo.end(), [&](int a, int b){return dist[a] > dist[b];});
for (int i: gruppo)
proc[i] = false;
int x, y;
x = gruppo[0];
dist[x] = 0;
dfs2(x);
sort(gruppo.begin(), gruppo.end(), [&](int a, int b){return dist[a] > dist[b];});
y = gruppo[0];
vector<int> diam;
dfs3(x, -1, y, diam);
return diam;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[])
{
for (int i = 0; i < M; i++)
{
nodi[A[i]].push_back({B[i], T[i]});
nodi[B[i]].push_back({A[i], T[i]});
}
vector<int> valori;
vector<int> diam;
dist.resize(N, 0);
proc.resize(N, false);
for (int i = 0; i < N; i++)
{
if (!proc[i])
{
vector<int> diametro = trova_diametro(i);
/*
cout << "\n\n";
for (int i: diametro)
cout << i << " ";
cout << "\n\n";
*/
if (diametro.empty())
{
valori.push_back(0);
diam.push_back(0);
continue;
}
int sx = 0, dx = 0;
for (int j: diametro)
dx += j;
int minimo = dx;
diam.push_back(dx);
for (int j = 0; j < diametro.size(); j++)
{
sx += diametro[j];
dx -= diametro[j];
minimo = min(minimo, max(sx, dx));
}
valori.push_back(minimo);
}
}
sort(valori.begin(), valori.end());
sort(diam.begin(), diam.end());
return max(diam.back(), valori.back() + valori[valori.size() - 1] + L);
}
Compilation message
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:129:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
129 | for (int j = 0; j < diametro.size(); j++)
| ~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
56 ms |
16616 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2636 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
56 ms |
16616 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
25 ms |
6072 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2636 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
56 ms |
16616 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |