#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
#include "dreaming.h"
const int maxn = 1e5 + 5;
vector<pair<int,int>> G[maxn];
int getDiameter(int n) {
queue<int> q;
vector<int> dis(maxn, -1);
q.push(1);
dis[1] = 0;
while (q.size()) {
int u = q.front();
q.pop();
for (auto e : G[u]) {
int v = e.first;
if (dis[v] == -1) {
dis[v] = dis[u] + e.second;
q.push(v);
}
}
}
int now = 0, mx = 0;
for (int i = 1; i <= n; i++) {
if (mx < dis[i]) {
mx = dis[i];
now = i;
}
dis[i] = -1;
}
q.push(now);
dis[now] = 0;
while (q.size()) {
int u = q.front();
q.pop();
for (auto e : G[u]) {
int v = e.first;
if (dis[v] == -1) {
dis[v] = dis[u] + e.second;
q.push(v);
}
}
}
return *max_element(dis.begin(), dis.end());
}
vector<int> dp(maxn, 0);
vector<int> in(maxn, 0);
vector<int> vis(maxn, 0);
int dfs(int now, int fa) {
int mx = dp[now];
vis[now] = true;
for (auto e : G[now]) {
int v = e.first;
if (v == fa) {
continue;
}
mx = max(mx, dfs(v, now));
}
return mx;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
cout << N << " " << M << " " << L << "\n";
for (int i = 0; i < M; i++) {
// cout << A[i] << " " << B[i];
G[++A[i]].emplace_back(++B[i], T[i]);
}
if (N == 1) {
return 0;
}
if (N == M + 1) {
return getDiameter(N);
}
if (N == 2) {
return L;
}
if (M == 0) {
return L * 2;
}
queue<int> q;
for (int i = 1; i <= N; i++) {
if (G[i].size() == 1) {
q.push(i);
}
in[i] = G[i].size();
}
while (q.size()) {
int u = q.front();
q.pop();
in[u] = 0;
cout << u << "\n";
for (auto e : G[u]) {
int v = e.first;
dp[v] = max(dp[v], dp[u] + e.second);
in[v]--;
if (in[v] == 1) {
q.push(v);
}
}
}
int ans = 0;
for (int i = 1; i <= N; i++) {
if (!vis[i]) {
ans = ans + dfs(i, 0);
}
}
return ans + L;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
51 ms |
13152 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
4952 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
51 ms |
13152 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
13 ms |
6236 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
4952 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
51 ms |
13152 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |