답안 #231620

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
231620 2020-05-14T07:52:54 Z AlexLuchianov Islands (IOI08_islands) C++14
90 / 100
1484 ms 131072 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cassert>

using namespace std;

using ll = long long;
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))

int const nmax = 1000000;
int const inf = 1000000000;

int edge[5 + nmax][3], seen[5 + nmax];
vector<int> g[5 + nmax];


vector<int> st, cycle;
int in_stack[5 + nmax];

int oth(int node, int id){
  return edge[id][0] + edge[id][1] - node;
}

bool exist(int node, int id){
  return ((edge[id][0] == node) || (edge[id][1] == node));
}

void mark(int node){
  if(seen[node] == 1)
    return ;
  seen[node] = 1;
  for(int h = 0; h < g[node].size(); h++)
    mark(oth(node, g[node][h]));
}

void dfs(int node, bool &done){

  if(in_stack[node] == 1){
    cycle.push_back(st.back());
    st.pop_back();
    while(exist(node, st.back()) == 0){
      cycle.push_back(st.back());
      st.pop_back();
    }
    cycle.push_back(st.back());
    st.pop_back();
    done = 1;
  }
  in_stack[node] = 1;

  for(int h = 0; h < g[node].size(); h++){
    int id = g[node][h];
    if(done == 1)
      return ;

    if(st.size() == 0 || st.back() != id) {
      st.push_back(id);
      dfs(oth(node, id), done);
      st.pop_back();
    }
  }
  in_stack[node] = 0;
}

ll dp[5 + nmax];

ll maxdist(int node, int parent, ll &result){
  for(int h = 0; h < g[node].size(); h++){
    int id = g[node][h];
    int to = oth(node, id);
    if(seen[to] < 2 && parent != to) {
      maxdist(to, node, result);
      result = max(result, dp[node] + dp[to] + edge[id][2]);
      dp[node] = max(dp[node], dp[to] + edge[id][2]);
    }
  }
  return dp[node];
}

vector<int> path;

ll solve(int node){
  cycle.clear();
  st.clear();
  path.clear();

  mark(node);
  bool done = 0;
  dfs(node, done);

  ll sum = 0;

  for(int i = 0; i < cycle.size(); i++) {
    int id = cycle[i];
    seen[edge[id][0]] = 2;
    seen[edge[id][1]] = 2;
    sum += edge[id][2];
  }

  if(exist(edge[cycle[0]][0], cycle[1]) == 1)
    path.push_back(edge[cycle[0]][0]);
  else
    path.push_back(edge[cycle[0]][1]);

  for(int i = 1; i < cycle.size(); i++)
    path.push_back(oth(path.back(), cycle[i]));

  ll result = 0, smax1 = 1LL * 2000 * -inf, smax2 = 1LL * 2000 * -inf, part = 0;
  for(int i = 0; i < path.size(); i++){
    maxdist(path[i], 0, result);

    if(0 < i) {
      part += edge[cycle[i]][2];
      result = max(result, dp[path[i]] + part + smax1);
      result = max(result, dp[path[i]] + sum - part + smax2);
    }
    smax1 = max(smax1, dp[path[i]] - part);
    smax2 = max(smax2, dp[path[i]] + part);
  }
  return result;
}

int main()
{
  ios::sync_with_stdio(0);
  cin.tie(0);

  int n;
  cin >> n;
  for(int i = 1; i <= n; i++){
    edge[i][0] = i;
    cin >> edge[i][1] >> edge[i][2];
    g[edge[i][0]].push_back(i);
    g[edge[i][1]].push_back(i);
  }
  ll result = 0;
  for(int i = 1;i <= n; i++)
    if(seen[i] == 0) {
      result += solve(i);
    }
  cout << result;
  return 0;
}

Compilation message

islands.cpp: In function 'void mark(int)':
islands.cpp:35:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int h = 0; h < g[node].size(); h++)
                  ~~^~~~~~~~~~~~~~~~
islands.cpp: In function 'void dfs(int, bool&)':
islands.cpp:54:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int h = 0; h < g[node].size(); h++){
                  ~~^~~~~~~~~~~~~~~~
islands.cpp: In function 'll maxdist(int, int, ll&)':
islands.cpp:71:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int h = 0; h < g[node].size(); h++){
                  ~~^~~~~~~~~~~~~~~~
islands.cpp: In function 'll solve(int)':
islands.cpp:96:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0; i < cycle.size(); i++) {
                  ~~^~~~~~~~~~~~~~
islands.cpp:108:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 1; i < cycle.size(); i++)
                  ~~^~~~~~~~~~~~~~
islands.cpp:112:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0; i < path.size(); i++){
                  ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 23808 KB Output is correct
2 Correct 21 ms 23936 KB Output is correct
3 Correct 18 ms 23936 KB Output is correct
4 Correct 18 ms 23808 KB Output is correct
5 Correct 18 ms 23860 KB Output is correct
6 Correct 19 ms 23808 KB Output is correct
7 Correct 17 ms 23808 KB Output is correct
8 Correct 19 ms 23808 KB Output is correct
9 Correct 18 ms 23936 KB Output is correct
10 Correct 18 ms 23936 KB Output is correct
11 Correct 18 ms 23808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 23936 KB Output is correct
2 Correct 18 ms 23936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 24040 KB Output is correct
2 Correct 20 ms 24320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 24960 KB Output is correct
2 Correct 48 ms 27256 KB Output is correct
3 Correct 32 ms 25216 KB Output is correct
4 Correct 28 ms 24500 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 28460 KB Output is correct
2 Correct 63 ms 31100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 122 ms 37368 KB Output is correct
2 Correct 146 ms 41764 KB Output is correct
3 Correct 161 ms 50000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 189 ms 47992 KB Output is correct
2 Correct 237 ms 68380 KB Output is correct
3 Correct 287 ms 74340 KB Output is correct
4 Correct 350 ms 90620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 383 ms 77652 KB Output is correct
2 Correct 1029 ms 114980 KB Output is correct
3 Correct 342 ms 72088 KB Output is correct
4 Correct 480 ms 105640 KB Output is correct
5 Correct 452 ms 105544 KB Output is correct
6 Correct 1484 ms 81248 KB Output is correct
7 Correct 501 ms 131072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 526 ms 131072 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -