#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;
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);
break;
}
int sx = 0, dx = 0;
for (int j: diametro)
dx += j;
int minimo = 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());
if (valori.size() == 1)
return valori[0];
else
return valori.back() + valori[valori.size() - 2] + L;
}
Compilation message
dreaming.cpp: In function 'void dfs(int, std::vector<int>&)':
dreaming.cpp:17:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
17 | for (auto [u, w]: nodi[v])
| ^
dreaming.cpp: In function 'void dfs2(int)':
dreaming.cpp:31:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
31 | for (auto [u, w]: nodi[v])
| ^
dreaming.cpp: In function 'bool dfs3(int, int, int, std::vector<int>&)':
dreaming.cpp:47:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
47 | for (auto [u, w]: nodi[v])
| ^
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:125:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
125 | for (int j = 0; j < diametro.size(); j++)
| ~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
55 ms |
16596 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
2636 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
55 ms |
16596 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
13 ms |
5324 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
2636 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
55 ms |
16596 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |