Problem p02977 (C++)

Buggy submission (status: Time Limit Exceeded):
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#pragma warning(disable : 4786)
#pragma warning(disable : 4996)
#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
  ~debug() { cerr << endl; }
  template <class c>
  typename enable_if<sizeof dud<c>(0) != 1, debug &>::type operator<<(c i) {
    cerr << boolalpha << i;
    return *this;
  }

  template <class c, int = 0>
  typename enable_if<sizeof dud<c>(0) == 1, debug &>::type operator<<(c i) {
    return *this << range(begin(i), end(i));
  }

  sim, class b dor(pair<b, c> d) {
    ris << "(" << d.first << ", " << d.second << ")";
  }
  sim dor(rge<c> d) {
    *this << "[";
    for (auto it = d.b; it != d.e; ++it)
      *this << ", " + 2 * (it == d.b) << *it;
    ris << "]";
  }
#else
  sim dor(const c &) { ris; }
#endif
};
#define watch(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

#define MEM(a, b) memset(a, (b), sizeof(a))
#define CLR(a) memset(a, 0, sizeof(a))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define ABS(X) ((X) > 0 ? (X) : (-(X)))
#define S(X) ((X) * (X))
#define SZ(V) (int)V.size()
#define FORN(i, n) for (int i = 0; i < n; i++)
#define FORAB(i, a, b) for (int i = a; i <= b; i++)
#define ALL(V) V.begin(), V.end()
#define IN(A, B, C) ((B) <= (A) && (A) <= (C))
#define AIN(A, B, C) assert(IN(A, B, C))

// typedef int LL;
typedef long long int LL;
// typedef __int128 LLL;
typedef long long LLL;

typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<double, double> PDD;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<PLL> VPL;
typedef vector<PII> VP;
typedef vector<double> VD;
typedef long double ld;

VI adj[200005];
int n;
void add(int u, int v) {
  adj[u].push_back(v);
  adj[v].push_back(u);
}

void Print(int a, int b) {
  for (int i = a; i < b; i++) {
    printf("%d %d\n", i, i + 1);
    add(i, i + 1);
  }
  printf("%d %d\n", b, n + a);
  add(b, n + a);
  for (int i = n + a; i < n + b; i++) {
    printf("%d %d\n", i, i + 1);
    add(i, i + 1);
  }
}

int vis[200005];
void dfs(int at, int par = 0) {
  vis[at] = 1;
  for (int u : adj[at]) {
    if (u == par)
      continue;
    if (vis[u])
      assert(0);
    dfs(u, at);
  }
}

void check(int a, int b, int me, int prev = -1) {
  int exp = (a <= n ? a : a - n);
  if (a == b) {
    if (me != exp)
      printf(">>%d %d %d\n", me, exp, b - n);
    assert(me == exp);
  }
  for (int u : adj[a]) {
    if (u == prev)
      continue;
    check(u, b, me ^ (u <= n ? u : u - n), a);
  }
}

void solve(int ks) {
  scanf("%d", &n);
  if (!(n & (n - 1))) {
    printf("No\n");
    return;
  }

  printf("Yes\n");
  int last = 3;
  while (last + 4 <= n)
    last += 4;
  // Print line.
  Print(1, 3);
  for (int i = 4; i <= last; i += 4) {
    printf("%d %d\n", n + i - 1, i);
    add(n + i - 1, i);
    Print(i, i + 3);
  }
  if ((last & (last + 1)) || (n - last == 0)) {
    // last+1 ... n
    if (last + 1 <= n) {
      int x = (last + 1) ^ (last - 3);
      printf("%d %d\n", n + x + 1, last + 1);
      printf("%d %d\n", last - 3, n + last + 1);
      add(n + x + 1, last + 1);
      add(last - 3, n + last + 1);
    }
    if (last + 2 <= n) {
      printf("%d %d\n", last + 1, last + 2);
      printf("1 %d\n", n + last + 2);
      add(last + 1, last + 2);
      add(1, n + last + 2);
    }
    if (last + 3 <= n) {
      printf("%d %d\n", last + 1, last + 3);
      printf("3 %d\n", n + last + 3);
      add(last + 1, last + 3);
      add(3, n + last + 3);
    }
  } else {
    int x = last + 1;
    printf("%d %d\n", x, 1);
    add(x, 1);
    printf("%d %d\n", x + 1, 1);
    add(x + 1, 1);
    printf("%d %d\n", n + x, x + 1);
    add(n + x, x + 1);
    printf("%d %d\n", x, n + x + 1);
    add(x, n + x + 1);
    if (last + 3 <= n) {
      printf("%d %d\n", x + 1, last + 3);
      add(x + 1, last + 3);
      printf("%d %d\n", 2, last + 3 + n);
      add(2, last + 3 + n);
    }
  }

  dfs(1);
  for (int i = 1; i <= 2 * n; i++)
    assert(vis[i]);
  for (int i = 1; i <= n; i++)
    check(i, i + n, i);
}

int main() {
#ifdef LOCAL
  double start_time = clock();
  freopen("C:\\Home\\ContestCodes\\sample.in", "r", stdin);
  //	freopen("out.out", "w", stdout);
#endif

  if (0) {
    int T;
    scanf("%d", &T);
    // AIN(T, 1, 25);
    for (int ks = 1; ks <= T; ks++) {
      solve(ks);
      fprintf(stderr, "%d done\n", ks);
    }
  } else {
    solve(0);
  }

#ifdef LOCAL
  double end_time = clock();
  fprintf(stderr, "Time = %lf\n", (end_time - start_time) / CLOCKS_PER_SEC);
#endif

  return 0;
}


Fixed submission:
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#pragma warning(disable : 4786)
#pragma warning(disable : 4996)
#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
  ~debug() { cerr << endl; }
  template <class c>
  typename enable_if<sizeof dud<c>(0) != 1, debug &>::type operator<<(c i) {
    cerr << boolalpha << i;
    return *this;
  }

  template <class c, int = 0>
  typename enable_if<sizeof dud<c>(0) == 1, debug &>::type operator<<(c i) {
    return *this << range(begin(i), end(i));
  }

  sim, class b dor(pair<b, c> d) {
    ris << "(" << d.first << ", " << d.second << ")";
  }
  sim dor(rge<c> d) {
    *this << "[";
    for (auto it = d.b; it != d.e; ++it)
      *this << ", " + 2 * (it == d.b) << *it;
    ris << "]";
  }
#else
  sim dor(const c &) { ris; }
#endif
};
#define watch(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

#define MEM(a, b) memset(a, (b), sizeof(a))
#define CLR(a) memset(a, 0, sizeof(a))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define ABS(X) ((X) > 0 ? (X) : (-(X)))
#define S(X) ((X) * (X))
#define SZ(V) (int)V.size()
#define FORN(i, n) for (int i = 0; i < n; i++)
#define FORAB(i, a, b) for (int i = a; i <= b; i++)
#define ALL(V) V.begin(), V.end()
#define IN(A, B, C) ((B) <= (A) && (A) <= (C))
#define AIN(A, B, C) assert(IN(A, B, C))

// typedef int LL;
typedef long long int LL;
// typedef __int128 LLL;
typedef long long LLL;

typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<double, double> PDD;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<PLL> VPL;
typedef vector<PII> VP;
typedef vector<double> VD;
typedef long double ld;

VI adj[200005];
int n;
void add(int u, int v) {
  adj[u].push_back(v);
  adj[v].push_back(u);
}

void Print(int a, int b) {
  for (int i = a; i < b; i++) {
    printf("%d %d\n", i, i + 1);
    add(i, i + 1);
  }
  printf("%d %d\n", b, n + a);
  add(b, n + a);
  for (int i = n + a; i < n + b; i++) {
    printf("%d %d\n", i, i + 1);
    add(i, i + 1);
  }
}

int vis[200005];
void dfs(int at, int par = 0) {
  vis[at] = 1;
  for (int u : adj[at]) {
    if (u == par)
      continue;
    if (vis[u])
      assert(0);
    dfs(u, at);
  }
}

void check(int a, int b, int me, int prev = -1) {
  int exp = (a <= n ? a : a - n);
  if (a == b) {
    if (me != exp)
      printf(">>%d %d %d\n", me, exp, b - n);
    assert(me == exp);
  }
  for (int u : adj[a]) {
    if (u == prev)
      continue;
    check(u, b, me ^ (u <= n ? u : u - n), a);
  }
}

void solve(int ks) {
  scanf("%d", &n);
  if (!(n & (n - 1))) {
    printf("No\n");
    return;
  }

  printf("Yes\n");
  int last = 3;
  while (last + 4 <= n)
    last += 4;
  // Print line.
  Print(1, 3);
  for (int i = 4; i <= last; i += 4) {
    printf("%d %d\n", n + i - 1, i);
    add(n + i - 1, i);
    Print(i, i + 3);
  }
  if ((last & (last + 1)) || (n - last == 0)) {
    // last+1 ... n
    if (last + 1 <= n) {
      int x = (last + 1) ^ (last - 3);
      printf("%d %d\n", n + x + 1, last + 1);
      printf("%d %d\n", last - 3, n + last + 1);
      add(n + x + 1, last + 1);
      add(last - 3, n + last + 1);
    }
    if (last + 2 <= n) {
      printf("%d %d\n", last + 1, last + 2);
      printf("1 %d\n", n + last + 2);
      add(last + 1, last + 2);
      add(1, n + last + 2);
    }
    if (last + 3 <= n) {
      printf("%d %d\n", last + 1, last + 3);
      printf("3 %d\n", n + last + 3);
      add(last + 1, last + 3);
      add(3, n + last + 3);
    }
  } else {
    int x = last + 1;
    printf("%d %d\n", x, 1);
    add(x, 1);
    printf("%d %d\n", x + 1, 1);
    add(x + 1, 1);
    printf("%d %d\n", n + x, x + 1);
    add(n + x, x + 1);
    printf("%d %d\n", x, n + x + 1);
    add(x, n + x + 1);
    if (last + 3 <= n) {
      printf("%d %d\n", x + 1, last + 3);
      add(x + 1, last + 3);
      printf("%d %d\n", 2, last + 3 + n);
      add(2, last + 3 + n);
    }
  }
}

int main() {
#ifdef LOCAL
  double start_time = clock();
  freopen("C:\\Home\\ContestCodes\\sample.in", "r", stdin);
  //	freopen("out.out", "w", stdout);
#endif

  if (0) {
    int T;
    scanf("%d", &T);
    // AIN(T, 1, 25);
    for (int ks = 1; ks <= T; ks++) {
      solve(ks);
      fprintf(stderr, "%d done\n", ks);
    }
  } else {
    solve(0);
  }

#ifdef LOCAL
  double end_time = clock();
  fprintf(stderr, "Time = %lf\n", (end_time - start_time) / CLOCKS_PER_SEC);
#endif

  return 0;
}