Submission #120475

#TimeUsernameProblemLanguageResultExecution timeMemory
120475nvmdavaParachute rings (IOI12_rings)C++17
100 / 100
2658 ms196828 KiB
#include <bits/stdc++.h>
#define ff first
#define ss second
using namespace std;

vector<pair<int, int> > con;
vector<int> adj[1000005];
set<int> pos, t;
int N;
int p[1000005][5];
void Init(int N_) {
  N = N_;
  for(int i = 0; i < N; i++)
   for(int j = 0; j < 5; j++)
      p[i][j] = i;
   for(int i = 0; i < N; i++)
      pos.insert(i);
}
int put = 0;
int v[10];

int find(int v, int i){
   if(v == p[v][i]) return v;
   return p[v][i] = find(p[v][i], i);
}

bool merge(int v, int u, int i){
   if(i){
      if(v == ::v[i]) return 0;
      if(u == ::v[i]) return 0;
   }
   v = find(v, i);
   u = find(u, i);
   if(v == u) return 1;
   p[v][i] = u;
   return 0;
}

bool dfs(int A, int B){
   t.insert(A);
   for(int x : adj[A]){
      if(x == B) continue;
      if(t.count(x)) return 1;
      if(dfs(x, A)) return 1;
   }
   t.erase(A);
   return 0;
}

void Link(int A, int B){
   if(pos.empty()) return;
   adj[A].push_back(B);
   adj[B].push_back(A);
   if(adj[A].size() == 4){
      t.clear();
      if(pos.count(A))
         t.insert(A);
      swap(t, pos);
   } else if(adj[A].size() == 3){
      t.clear();
      if(pos.count(A))
         t.insert(A);
      for(int x : adj[A])
         if(pos.count(x))
            t.insert(x);
      swap(t, pos);
   }
   if(adj[B].size() == 4){
      t.clear();
      if(pos.count(B))
         t.insert(B);
      swap(t, pos);
   } else if(adj[B].size() == 3){
      t.clear();
      if(pos.count(B))
         t.insert(B);
      for(int x : adj[B])
         if(pos.count(x))
            t.insert(x);
      swap(t, pos);
   }
   if(put == 0 && pos.size() <= 4){
      for(int x : pos){
         v[++put] = x;
      }
      for(auto& x : con){
         for(int j = 1; j <= put; j++){
            if(v[j] == -1 || merge(x.ff, x.ss, j)) v[j] = -1;
         }
      }
   }
   con.push_back({A, B});
   if(put){
      t.clear();
      for(int j = 1; j <= put; j++){
         if(v[j] == -1 || merge(A, B, j)) v[j] = -1;
         if(pos.count(v[j])) t.insert(v[j]);
      }
      swap(t, pos);
      return;
   }
   if(pos.empty()) return;
   if(put || merge(A, B, 0) == 0) return;
   t.clear();
   dfs(A, B);
   set<int> lol;
   for(int x : t)
      if(pos.count(x))
         lol.insert(x);
   swap(lol, pos);
}

int CountCritical() {
  return pos.size();
}

Compilation message (stderr)

rings.cpp: In function 'void Init(int)':
rings.cpp:13:3: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   for(int i = 0; i < N; i++)
   ^~~
rings.cpp:16:4: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
    for(int i = 0; i < N; i++)
    ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...