# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1109845 | SonicML | Dreaming (IOI13_dreaming) | C++14 | 37 ms | 13136 KiB |
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 "dreaming.h"
#include <vector>
#include <iostream>
using namespace std;
struct Edge{
int to;
int cost;
};
int const NMAX = 1e5;
vector <Edge> g[1 + NMAX];
int below[1 + NMAX];
int above[1 + NMAX];
int visit[1 + NMAX];
void computeBelow(int node, int parent) {
visit[node] = true;
for(int i = 0;i < g[node].size();i++) {
int to = g[node][i].to, cost = g[node][i].cost;
if(to != parent) {
computeBelow(to, node);
below[node] = max(below[node], below[to] + cost);
}
}
}
int computeAbove(int node, int parent) {
int ans = max(above[node], below[node]);
//cerr << node << ' ' << above[node] << ' ' << below[node] << '\n';
visit[node] = true;
int best1 = above[node], best2 = 0;
for(int i = 0;i < g[node].size();i++) {
int to = g[node][i].to, cost = g[node][i].cost;
if(to != parent) {
if(best1 < below[to] + cost) {
best2 = best1;
best1 = below[to] + cost;
}else if(best2 < below[to] + cost) {
best2 = below[to] + cost;
}
}
}
for(int i = 0;i < g[node].size();i++) {
int to = g[node][i].to, cost = g[node][i].cost;
if(to != parent) {
if(below[to] + cost == best1) {
above[to] = best2 + cost;
}else {
above[to] = best1 + cost;
}
ans = min(ans, computeAbove(to, node));
}
}
return ans;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
for(int i = 0;i < M;i++) {
g[A[i]].push_back({B[i], T[i]});
g[B[i]].push_back({A[i], T[i]});
}
for(int i = 0;i < N;i++) {
if(!visit[i]) {
computeBelow(i, i);
}
}
for(int i = 0;i < N;i++) {
visit[i] = 0;
}
vector <int> len;
int core = 0;
for(int i = 0;i < N;i++) {
if(!visit[i]) {
int temp = computeAbove(i, i);
//cerr << temp << '\n';
len.push_back(temp);
if(len[core] < temp) {
core = len.size()-1;
}
}
}
//cerr << '\n';
int best1 = 0, best2 = 0;
for(int i = 0;i < len.size();i++) {
int temp;
if(i == core) {
temp = len[i];
}else {
temp = len[i] + L;
}
if(best1 < temp) {
best2 = best1;
best1 = temp;
}else if(best2 < temp) {
best2 = temp;
}
}
return best1 + best2;
}
Compilation message (stderr)
# | 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... |