답안 #284156

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
284156 2020-08-26T23:46:35 Z Haunted_Cpp Islands (IOI08_islands) C++17
80 / 100
2000 ms 131076 KB
/**
 *  author: Haunted_Cpp
**/
 
#include <bits/stdc++.h>
using namespace std;
 
#pragma GCC optimize("Ofast")
#pragma GCC target("fma,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma GCC optimize("unroll-loops")
 
template<typename T> ostream &operator << (ostream &os, const vector<T> &v) { os << '{'; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << '}'; }
template<typename T, size_t size> ostream &operator << (ostream &os, const array<T, size> &arr) { os << '{'; string sep; for (const auto &x : arr) os << sep << x, sep = ", "; return os << '}'; }
template<typename A, typename B> ostream &operator << (ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
 
void debug_out() { cerr << endl; }
template<typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << ' ' << H; debug_out(T...); }
 
#ifdef LOCAL
#define debug(...) cerr << "(" << #__VA_ARGS__ << "):", debug_out(__VA_ARGS__)
#else
#define debug(...) 47
#endif
 
typedef long long i64;
 
const int MAX_N = 1e6 + 5;
 
vector<vector<int>> g(MAX_N);
 
int Time = 0;
 
stack<int> stk;
vector<int> low(MAX_N), disc(MAX_N), cycle;
vector<int> nxt(MAX_N), cost(MAX_N);

bitset<MAX_N> is_cycle;
bitset<MAX_N> vis;
 
void find_cycle(int node, int p) {
  low[node] = disc[node] = ++Time;
  stk.push(node);
  for (auto et : g[node]) {
    //~ const int et = to.first;
    if (et == p && !(nxt[p] == node && nxt[node] == p)) continue;
    if (!disc[et]) {
      find_cycle(et, node);
      low[node] = min(low[node], low[et]);
    } else {
      low[node] = min(low[node], disc[et]);
    }
  }
  if (low[node] == disc[node]) {
    vector<int> cur_cycle;
    while(true) {
      const int cur = stk.top();
      stk.pop();
      cur_cycle.emplace_back(cur);
      if (cur == node) break;
    }
    if(cur_cycle.size() > 1) {
      for (auto to : cur_cycle) is_cycle[to] = 1;
      cycle = cur_cycle;
    }
  }
}

int root;
 
pair<i64, int> dfs(int node, int p) {
  pair<i64, int> best_way = {0, node};
  for (auto et : g[node]) {
    //~ const int et = get<0>(to);
    const int w = (nxt[node] == et ? cost[node] : cost[et]);
    if (et != root && is_cycle[et]) continue;
    if (et != p) {
      pair<i64, int> cur = dfs(et, node);
      cur.first += w;
      best_way = max(best_way, cur);
    }
  } 
  return best_way;
}
 
i64 get_diameter(int node) {
  pair<i64, int> cur = dfs(node, -1);
  return dfs(cur.second, -1).first;
}

i64 get_depth(int node, int p) {
  i64 mx = 0;
  for (auto et : g[node]) {
    //~ const int et = get<0>(to);
    const int w = (nxt[node] == et ? cost[node] : cost[et]);
    if (is_cycle[et]) continue;
    if (et != p) {
      mx = max(mx, w + get_depth(et, node));
    }
  }
  return mx;
}

i64 find_cost(int node) {
  vis[node] = true;
  i64 res = 0;
  for (auto et : g[node]) {
    //~ const int et = get<0>(to);
    const int w = (nxt[node] == et ? cost[node] : cost[et]);
    if (is_cycle[node] && is_cycle[et]) {
      res += w;
    }
    if (!vis[et]) {
      vis[et] = true;
      res += find_cost(et);
    }
  }
  return res;
}
 
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0); 
  int n;
  cin >> n;
  for (int st = 0; st < n; st++) {
    int et, w;
    cin >> et >> w;
    --et;
    nxt[st] = et;
    cost[st] = w;
    g[st].emplace_back(et);
    g[et].emplace_back(st);
  }
  
  i64 ans = 0;
  for (int i = 0; i < n; i++) {
    if (disc[i]) continue;
    cycle.clear();
    find_cycle(i, -1);
    i64 res = 0;
    for (auto to : cycle) {
      root = to;
      res = max(res, get_diameter(root));
    }
    i64 cycle_cost = find_cost(i) / 2;
    i64 clockwise = get_depth(cycle[0], -1);
    i64 start = 0;
    for (int j = 1; j < (int) cycle.size(); j++) {
      const int st = cycle[j - 1];
      const int et = cycle[j];
      const int w = (nxt[st] == et ? cost[st] : cost[et]);
      start += w;
      res = max(res, clockwise + start + get_depth(et, -1));
      clockwise = max(clockwise, -start + get_depth(et, -1));
    }
    res = max(res, clockwise);
    clockwise = get_depth(cycle[0], -1);
    start = 0;   
    for (int j = 1; j < (int) cycle.size(); j++) {
      const int st = cycle[j - 1];
      const int et = cycle[j];
      const int w = (nxt[st] == et ? cost[st] : cost[et]);
      start += w;
      const i64 other = cycle_cost - start;
      res = max(res, clockwise + other + get_depth(et, -1));
      clockwise = max(clockwise, start + get_depth(et, -1));
    }
    res = max(res, clockwise);
    ans += res;
  }
  cout << ans << '\n';
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 39552 KB Output is correct
2 Correct 26 ms 39552 KB Output is correct
3 Correct 26 ms 39552 KB Output is correct
4 Correct 25 ms 39424 KB Output is correct
5 Correct 26 ms 39680 KB Output is correct
6 Correct 25 ms 39544 KB Output is correct
7 Correct 26 ms 39548 KB Output is correct
8 Correct 26 ms 39544 KB Output is correct
9 Correct 26 ms 39552 KB Output is correct
10 Correct 26 ms 39552 KB Output is correct
11 Correct 26 ms 39544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 39544 KB Output is correct
2 Correct 27 ms 39552 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 39552 KB Output is correct
2 Correct 29 ms 39808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 40184 KB Output is correct
2 Correct 50 ms 42240 KB Output is correct
3 Correct 48 ms 40184 KB Output is correct
4 Correct 32 ms 39808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 69 ms 43256 KB Output is correct
2 Correct 70 ms 44836 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 144 ms 48248 KB Output is correct
2 Correct 173 ms 55252 KB Output is correct
3 Correct 164 ms 63256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 197 ms 53880 KB Output is correct
2 Correct 267 ms 76300 KB Output is correct
3 Correct 301 ms 87020 KB Output is correct
4 Correct 353 ms 100368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 427 ms 74976 KB Output is correct
2 Correct 1391 ms 131072 KB Output is correct
3 Correct 378 ms 77560 KB Output is correct
4 Correct 509 ms 115608 KB Output is correct
5 Correct 500 ms 114964 KB Output is correct
6 Execution timed out 2075 ms 85624 KB Time limit exceeded
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 408 ms 131076 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -