This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#include<fstream>
using namespace std;
ifstream fin("VNOICUP.INP");
ofstream fout("VNOICUP.OUT");
#define sz(a) (int)a.size()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
#include <stdio.h>
#include <stdlib.h>
const ll inf = 1e9 + 6;
#include "dreaming.h"
const int mxn = 1e5 + 5;
vt<pii>adj[mxn + 1];
int n;
bool vis[mxn + 1];
vt<int>comp;
vt<int>bfs(int s){
// bfs works since it is a tree
vt<int>dis(n + 1, -1);
queue<int>q; q.push(s); dis[s] = 0;
while(!q.empty()){
int nw = q.front(); q.pop();
for(auto [v, w]: adj[nw]){
if(dis[v] == -1){
dis[v] = dis[nw] + w;
q.push(v);
}
}
}
return(dis);
}
void dfs(int s){
vis[s] = 1; comp.pb(s);
for(auto [v, w]: adj[s]){
if(!vis[v]){
dfs(v);
}
}
}
int travelTime(int N, int m, int L, int A[], int B[], int T[]) {
n = N;
forr(i, 0, m){
int u = A[i], v = B[i], w = T[i];
adj[u].pb({v, w}); adj[v].pb({u, w});
}
int ans = 0, diam = 0;
vt<int>cand;
forr(i, 0, n){
if(!vis[i]){
comp.clear();
vt<int>d1 = bfs(i);
int id = max_element(d1.begin(), d1.end()) - d1.begin();
vt<int>d2 = bfs(id);
int id2 = max_element(d2.begin(), d2.end()) - d2.begin();
vt<int>d3 = bfs(id2);
diam = max(diam, *max_element(d3.begin(), d3.end()));
dfs(i);
ll mn = inf, idd = -1;
for(auto j: comp){
cout << j << " ";
ll val = max(d2[j], d3[j]);
if(val < mn){
mn = val; idd = j;
}
}
cand.pb(mn);
}
}
sort(cand.rbegin(), cand.rend());
if(sz(cand) > 1)ans = cand[0] + cand[1] + L;
if(sz(cand) > 2)ans = max(ans, cand[1] + cand[2] + 2 * L);
return(max(ans, diam));
}
/*
#define fail(s, x...) do { \
fprintf(stderr, s "\n", ## x); \
exit(1); \
} while(0)
#define MAX_N 100000
static int A[MAX_N];
static int B[MAX_N];
static int T[MAX_N];
int main() {
int N, M, L, i;
int res;
FILE *f = fopen("dreaming.in", "r");
if (!f)
fail("Failed to open input file.");
res = scanf("%d%d%d", &N, &M, &L);
for (i = 0; i < M; i++)
res = scanf("%d%d%d", &A[i], &B[i], &T[i]);
int answer = travelTime(N, M, L, A, B, T);
printf("%d\n", answer);
return 0;
}
*/
Compilation message (stderr)
dreaming.cpp: In function 'std::vector<int> bfs(int)':
dreaming.cpp:33:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
33 | for(auto [v, w]: adj[nw]){
| ^
dreaming.cpp: In function 'void dfs(int)':
dreaming.cpp:44:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
44 | for(auto [v, w]: adj[s]){
| ^
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:68:26: warning: variable 'idd' set but not used [-Wunused-but-set-variable]
68 | ll mn = inf, idd = -1;
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |