이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "doll.h"
#include <bits/stdc++.h>
using namespace std;
int i, n, m, s, g, j, kk;
vector<int> C;
vector<int> X;
vector<int> Y;
vector<int> adj[100005];
bool visited[100005];
bool finished[200005];
vector<vector<int> > groups;
vector<int> tmp;
bool vis;
vector<int> sw;
vector<int> st;
void new_switch() {
  sw.push_back(--s);
  st.push_back(0);
  X.push_back(sw.back());
  Y.push_back(sw.front());
}
void dfs(int x, int code) {
  if (st[-x-1] == 0) {
    st[-x-1] ^= 1;
    if (X[-x-1] == x) X[-x-1] = adj[i][code];
    else dfs(X[-x-1], code);
  }
  else {
    st[-x-1] ^= 1;
    if (Y[-x-1] == sw.front()) Y[-x-1] = adj[i][code];
    else dfs(Y[-x-1], code);
  }
}
void create_switch(int i) {
  int num = (int)adj[i].size();
  if (num == 0) C[i] = i;
  else if (num == 1) C[i] = adj[i][0];
  else {
    sw.clear();
    kk = 1;
    while (kk < num) kk *= 2;
    --kk;
    new_switch();
    C[i] = s;
    for (int j=1; j < kk; ++j) {
      new_switch();
      if (j&1) X[-sw[j>>1]-1] = s;
      else Y[-sw[(j-1)>>1]-1] = s;
    }
    for (int j=0; j < num; ++j) dfs(sw.front(), j);
  }
}
void create_circuit(int M, vector<int> A) {
  m = M;
  n = (int)A.size();
  fill(finished, finished+n+1, false);
  C.resize(M+1);
  X.clear();
  Y.clear();
  st.clear();
  s = 0;
  do {
    vis = false;
    tmp.clear();
    fill(visited, visited+m+1, false);
    for (i=0; i < n-1; ++i) {
      if (!finished[i] && !visited[A[i]]) {
        finished[i] = true;
        visited[A[i]] = true;
        tmp.push_back(i);
        vis = true;
      }
    }
    groups.push_back(tmp);
  } while(vis);
  g = (int)groups.size();
  for (i=0; i < g; ++i) {
    kk = (int)groups[i].size();
    for (j=0; j < kk; ++j) adj[A[groups[i][j]]].push_back(A[groups[i][j]+1]);
  }
  adj[A[n-1]].push_back(0);
  
  C[0] = A[0];
  for (i=1; i <= M; ++i) create_switch(i);
  answer(C, X, Y);
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |