제출 #119809

#제출 시각아이디문제언어결과실행 시간메모리
119809dolphingarlic꿈 (IOI13_dreaming)C++14
0 / 100
53 ms10616 KiB
#include "dreaming.h" #include <bits/stdc++.h> #pragma GCC optimize("O3") #define FOR(i, x, y) for (int i = x; i < y; i++) typedef long long ll; using namespace std; vector<pair<int, int>> graph[100001]; bool visited[100001]; struct Node { int path, child, edge; } dp[100001]; int curr_max = -1, curr_max1_c = -1, curr_max2_c = -1, curr_max_node = -1, curr_max1_edge = 0, curr_max2_edge = 0; int r1 = -1, r2 = -1, r3 = -1; void dfs(int node) { visited[node] = true; int max1 = 0, max2 = 0, max1_c = -1, max2_c = -1, max1_edge = 0, max2_edge = 0; for (auto& i : graph[node]) { if (visited[i.first]) continue; dfs(i.first); if (dp[i.first].path + i.second >= max1) { max2 = max1; max2_c = max1_c; max2_edge = max1_edge; max1 = dp[i.first].path + i.second; max1_c = i.first; max1_edge = i.second; } else if (dp[i.first].path + i.second >= max2) { max2 = dp[i.first].path + i.second; max2_c = i.first; max2_edge = i.second; } } dp[node] = {max1, max1_c, max1_edge}; if (max1 + max2 > curr_max) { curr_max = max1 + max2; curr_max1_c = max1_c; curr_max2_c = max2_c; curr_max_node = node; curr_max1_edge = max1_edge; curr_max2_edge = max2_edge; } } int travelTime(int N, int M, int L, int A[], int B[], int T[]) { fill(visited, visited + N + 1, false); FOR(i, 0, M) { graph[A[i]].push_back({B[i], T[i]}); graph[B[i]].push_back({A[i], T[i]}); } FOR(i, 0, N) { if (!visited[i]) { curr_max = -1, curr_max1_c = -1, curr_max2_c = -1, curr_max_node = -1, curr_max1_edge = 0, curr_max2_edge = 0; dfs(i); int x = dp[curr_max1_c].path + curr_max1_edge; int y = (curr_max2_c == -1 ? 0 : dp[curr_max2_c].path + curr_max2_edge); // cout << curr_max << ' ' << curr_max1_c << ' ' << curr_max2_c // << ' ' << curr_max_node << ' ' << curr_max1_edge << ' ' << curr_max2_edge << '\n'; while (x > y && y + dp[curr_max_node].edge < x) { y += dp[curr_max_node].edge; x -= dp[curr_max_node].edge; curr_max_node = dp[curr_max_node].child; } int r = max(y, x); if (r > r1) { r3 = r2; r2 = r1; r1 = r; } else if (r > r2) { r3 = r2; r2 = r; } else if (r > r3) { r3 = r; } } } // cout << r1 << ' ' << r2 << ' ' << r3 << '\n'; return max(r1 + r2 + (r2 != -1) * L, r2 + r3 + 2 * (r2 != -1 && r3 != -1) * L); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...