답안 #501719

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
501719 2022-01-04T11:37:33 Z 600Mihnea Islands (IOI08_islands) C++17
40 / 100
2000 ms 131076 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

struct Edge {
  int a, b, cost;
  bool seen = false;
};

const int N = (int) 1e6 + 7;
int n;
int parrent[N];
Edge graph_edges[N];
vector<int> g[N], verts;
bool vis[N];
int dep[N];
int epar[N];

void build1dfsdfs(int a) {
  vis[a] = true;
  verts.push_back(a);
  for (auto &i : g[a]) {
    int b = graph_edges[i].a + graph_edges[i].b - a;
    if (!vis[b]) {
      dep[b] = 1 + dep[a];
      epar[b] = i;
      build1dfsdfs(b);
      parrent[b] = a;
      graph_edges[i].seen = true;
    }
  }
}

vector<int> geallonp(int a, int b) {
  vector<int> p1, p2;
  while (a != b) {
    assert(1 <= a && a <= n);
    assert(1 <= b && b <= n);
    if (dep[a] > dep[b]) {
      p1.push_back(a);
      a = parrent[a];
    } else {
      p2.push_back(b);
      b = parrent[b];
    }
  }
  p1.push_back(a);
  reverse(p2.begin(), p2.end());
  for (auto &x : p2) {
    p1.push_back(x);
  }
  return p1;
}

vector<int> geallonpedges(int a, int b) {
  vector<int> p1, p2;
  while (a != b) {
    assert(1 <= a && a <= n);
    assert(1 <= b && b <= n);
    if (dep[a] > dep[b]) {
      p1.push_back(epar[a]);
      a = parrent[a];
    } else {
      p2.push_back(epar[b]);
      b = parrent[b];
    }
  }
  reverse(p2.begin(), p2.end());
  for (auto &x : p2) {
    p1.push_back(x);
  }
  return p1;
}

bool blocked[N];
ll under[N];
ll bbpath[N];

void dfs(int a, int p = -1) {
  for (auto &i : g[a]) {
    int b = graph_edges[i].a + graph_edges[i].b - a;
    if (blocked[b] || b == p) {
      continue;
    }
    dfs(b, a);
    bbpath[a] = max(bbpath[a], bbpath[b]);
    bbpath[a] = max(bbpath[a], under[b] + graph_edges[i].cost + under[a]);
    under[a] = max(under[a], under[b] + graph_edges[i].cost);
  }

}

ll solveComponent(int rootVertex) {
  ll sol = 0;
  verts.clear();
  build1dfsdfs(rootVertex);
  vector<int> nnseeng;
  for (auto &v : verts) {
    for (auto &i : g[v]) {
      if (!graph_edges[i].seen) {
        graph_edges[i].seen = true;
        nnseeng.push_back(i);
      }
    }
  }
  assert((int) nnseeng.size() == 1);
  {
    int index = nnseeng[0];
    int a = graph_edges[index].a;
    int b = graph_edges[index].b;
    int cost = graph_edges[index].cost;
    vector<int> path = geallonp(a, b);
    vector<int> edges = geallonpedges(a, b);
    vector<ll> value;
    edges.push_back(index);

    assert((int) edges.size() == (int) path.size());

    for (auto &v : path) {
      blocked[v] = true;
    }



    for (auto &v : path) {
      dfs(v);
      sol = max(sol, bbpath[v]);
      value.push_back(under[v]);
    }

    assert((int) value.size() == (int) path.size());

    ll total = 0;
    for (auto &i : edges) {
      total += graph_edges[i].cost;
    }

    int y = (int) value.size();

    for (int l = 0; l < y; l++) {
      for (int r = l + 1; r < y; r++) {
        ll tog = value[l] + value[r];
        ll way = 0;
        for (int j = l; j < r; j++) {
          way += graph_edges[edges[j]].cost;
        }
        ll other_way = total - way;
        sol = max(sol, way + tog);
        sol = max(sol, other_way + tog);
      }
    }

    if (0) {
      cout << " ---> ";
      for (auto &x : path) {
        cout << x << " ";
      }
      cout << "\n";
      cout << " ---> ";
      for (auto &x : edges) {
        cout << graph_edges[x].cost << " ";
      }
      cout << "\n\n";
    }
  }
  return sol;
}

signed main() {
  /// L < P

  ios::sync_with_stdio(0); cin.tie(0);

 /// freopen ("input", "r", stdin);

  cin >> n;
  for (int i = 1; i <= n; i++) {
    int j, c;
    cin >> j >> c;
    graph_edges[i].a = i;
    graph_edges[i].b = j;
    graph_edges[i].cost = c;
  }
  for (int i = 1; i <= n; i++) {
    g[graph_edges[i].a].push_back(i);
    g[graph_edges[i].b].push_back(i);
  }

  ll sol = 0;

  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      sol += solveComponent(i);
    }
  }

  cout << sol << "\n";

  return 0;
}

Compilation message

islands.cpp: In function 'll solveComponent(int)':
islands.cpp:113:9: warning: unused variable 'cost' [-Wunused-variable]
  113 |     int cost = graph_edges[index].cost;
      |         ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 39500 KB Output is correct
2 Correct 22 ms 39460 KB Output is correct
3 Correct 30 ms 39468 KB Output is correct
4 Correct 20 ms 39456 KB Output is correct
5 Correct 20 ms 39464 KB Output is correct
6 Correct 19 ms 39488 KB Output is correct
7 Correct 20 ms 39364 KB Output is correct
8 Correct 19 ms 39416 KB Output is correct
9 Correct 19 ms 39372 KB Output is correct
10 Correct 20 ms 39500 KB Output is correct
11 Correct 19 ms 39500 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 39620 KB Output is correct
2 Correct 21 ms 39500 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 39636 KB Output is correct
2 Correct 1181 ms 40008 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2064 ms 40800 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2087 ms 45312 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2011 ms 57092 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2017 ms 59276 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 303 ms 106996 KB Output is correct
2 Runtime error 612 ms 131076 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 301 ms 131076 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -