답안 #169256

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
169256 2019-12-19T10:05:01 Z AlexLuchianov 낙하산 고리들 (IOI12_rings) C++14
0 / 100
11 ms 7672 KB
#include <vector>
#include <algorithm>
#include <iostream>
#include <cassert>

using namespace std;
int const nmax = 100000;
using ll = long long;
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))

vector<int> g[1 + nmax];
int type[1 + nmax];

namespace Dsu{
  int mult[1 + nmax];
  int sz[1 + nmax];
  void initialize(int n){
    for(int i = 0; i < n; i++) {
      mult[i] = i;
      sz[i] = 1;
    }
  }
  int jump(int gala){
    if(mult[gala] != gala)
      mult[gala] = jump(mult[gala]);
    return mult[gala];
  }
  void unite(int gala, int galb, int newtype){
    gala = jump(gala);
    galb = jump(galb);
    if(sz[galb] < sz[gala])
      swap(gala, galb);
    if(gala != galb) {
      mult[gala] = galb;
      sz[galb] += sz[gala];
    }
    type[galb] = newtype;
  }
}

int N, result;
int phase = 1;

vector<int> candidates;
int seen[1 + nmax];

void dfs(int node, int parent, bool &cycle, int deleted){
  seen[node] = 1;
  for(int h = 0; h < g[node].size(); h++){
    int to = g[node][h];
    if(to != parent && to != deleted) {
      if(seen[to] == 1)
        cycle = 1;
      else
        dfs(to, node, cycle, deleted);
    }
  }
}

bool test(int deleted){
  for(int i = 0; i < N; i++)
    seen[i] = 0;
  bool cycle = 0;
  for(int i = 0; i < N; i++)
    if(i != deleted && seen[i] == 0)
      dfs(i, -1, cycle, deleted);
  if(cycle == 1)
    return false;

  for(int i = 0; i < N; i++) {
    int degree = 0;
    if(i != deleted){
      for(int h = 0; h < g[i].size(); h++)
        if(g[i][h] != deleted)
          degree++;
      if(2 < degree)
        return false;
    }
  }
  return true;
}

void computecandidates(){
  sort(candidates.begin(), candidates.end());
  candidates.erase(unique(candidates.begin(), candidates.end()), candidates.end());
  assert(candidates.size() < 6);
  vector<int> newcand;
  for(int i = 0; i < candidates.size(); i++)
    if(test(candidates[i]) == 1)
      newcand.push_back(candidates[i]);
  candidates = newcand;
  result = candidates.size();
}

void Init(int N_) {
  cout << ":";
  N = N_;
  Dsu::initialize(N);
  for(int i = 0; i < N; i++)
    type[i] = 1;
  result = N;
}

void Link(int a, int b) {
  if(result == 0)
    return ;

  int gala = Dsu::jump(a);
  int galb = Dsu::jump(b);
  g[a].push_back(b);
  g[b].push_back(a);
  if(type[galb] < type[gala]){
    swap(a, b);
    swap(gala, galb);
  }

  if(gala != galb && g[a].size() <= 2 && g[b].size() <= 2) {
    Dsu::unite(gala, galb, type[galb]);
    return ;
  }

  if(phase == 1){
    if(gala == galb && g[a].size() <= 2 && g[b].size() <= 2){
      Dsu::unite(gala, galb, 2);
      phase = 2;
      result = Dsu::sz[galb];
    } else {
      Dsu::unite(gala, galb, 3);
      phase = 3;
      for(int h = 0; h < g[a].size(); h++)
        candidates.push_back(g[a][h]);
      for(int h = 0; h < g[b].size(); h++)
        candidates.push_back(g[b][h]);
      computecandidates();
    }
  } else if(phase == 2){
    Dsu::unite(gala, galb, 3);
    phase = 3;
    for(int h = 0; h < g[a].size(); h++)
      candidates.push_back(g[a][h]);
    for(int h = 0; h < g[b].size(); h++)
      candidates.push_back(g[b][h]);
    computecandidates();
  } else if(phase == 3){
    Dsu::unite(gala, galb, 3);
    if(1 < candidates.size())
      computecandidates();
    else if(candidates[0] == b)
      return ;
  }
}

int CountCritical() {
  return result;
}

Compilation message

rings.cpp: In function 'void dfs(int, int, bool&, int)':
rings.cpp:50:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int h = 0; h < g[node].size(); h++){
                  ~~^~~~~~~~~~~~~~~~
rings.cpp: In function 'bool test(int)':
rings.cpp:74:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
       for(int h = 0; h < g[i].size(); h++)
                      ~~^~~~~~~~~~~~~
rings.cpp: In function 'void computecandidates()':
rings.cpp:89:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0; i < candidates.size(); i++)
                  ~~^~~~~~~~~~~~~~~~~~~
rings.cpp: In function 'void Link(int, int)':
rings.cpp:131:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
       for(int h = 0; h < g[a].size(); h++)
                      ~~^~~~~~~~~~~~~
rings.cpp:133:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
       for(int h = 0; h < g[b].size(); h++)
                      ~~^~~~~~~~~~~~~
rings.cpp:140:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int h = 0; h < g[a].size(); h++)
                    ~~^~~~~~~~~~~~~
rings.cpp:142:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int h = 0; h < g[b].size(); h++)
                    ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 2680 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 11 ms 7672 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 2680 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 2680 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 2680 KB Output isn't correct
2 Halted 0 ms 0 KB -