Problem:
Ninja Map
Intersections of Crossing Path City are aligned to a grid. There are $N$ east-west streets which are numbered from 1 to $N$, from north to south. There are also $N$ north-south streets which are numbered from 1 to $N$, from west to east. Every pair of east-west and north-south streets has an intersection; therefore there are $N^2$ intersections which are numbered from 1 to $N^2$.
Surprisingly, all of the residents in the city are Ninja. To prevent outsiders from knowing their locations, the numbering of intersections is shuffled.
You know the connections between the intersections and try to deduce their positions from the information. If there are more than one possible set of positions, you can output any of them.
Input
The input consists of a single test case formatted as follows.
$N$
$a_1$ $b_1$
...
$a_{2N^2−2N}$ $\;$ $b_{2N^2−2N}$
The first line consists of an integer $N$ ($2 \leq N \leq 100$). The following $2N^2 - 2N$ lines represent connections between intersections. The ($i+1$)-th line consists of two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq N^2, a_i \ne b_i$), which represent that the $a_i$-th and $b_i$-th intersections are adjacent. More precisely, let's denote by ($r, c$) the intersection of the $r$-th east-west street and the $c$-th north-south street. If the intersection number of ($r,c$) is $a_i$ for some $r$ and $c$, then the intersection
  number of either ($r-1, c$), ($r+1, c$), ($r, c-1$) or ($r, c+1$) must be $b_i$. All inputs of adjacencies are different, i.e., ($a_i, b_i$) $\ne$ ($a_j, b_j$) and ($a_i, b_i$) $\ne$ ($b_j, a_j$) for all $1 \leq i < j \leq 2N^2-2N$. This means that you are given information of all adjacencies on the grid.
The input is guaranteed to describe a valid map.
Output
Print a possible set of positions of the intersections. More precisely, the output consists of $N$ lines each of which has space-separated $N$ integers. The $c$-th integer of the $r$-th line should be the intersection number of ($r, c$).
If there are more than one possible set of positions, you can output any of them.
Sample Input 1
3
1 2
4 7
8 6
2 3
8 9
5 3
4 6
5 6
7 8
1 4
2 6
5 9
Output for Sample Input 1
7 4 1
8 6 2
9 5 3
The following output will also be accepted.
1 2 3
4 6 5
7 8 9
Sample Input 2
4
12 1
3 8
10 7
13 14
8 2
9 12
6 14
11 3
3 13
1 10
11 15
4 15
4 9
14 10
5 7
2 5
6 1
14 5
16 11
15 6
15 13
9 6
16 4
13 2
Output for Sample Input 2
8 2 5 7
3 13 14 10
11 15 6 1
16 4 9 12

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define loop(i, a, b) for (ll i = a; i <= b; ++i)
constexpr ll INF = 9009009009009009009LL;

int main() {
    ll n; cin >> n;
    vector<set<ll>> g(n * n, set<ll>());
    rep(i, 2 * n * n - 2 * n) {
        ll a, b; cin >> a >> b;
        --a, --b;
        g[a].insert(b);
        g[b].insert(a);
    }

    vector<vector<ll>> grid(n, vector<ll>(n + n - 1, -INF));
    rep(r, n) {
        loop(c, r, r + n - 1) {
            grid[r][c] = INF;
        }
    }

    set<ll> used;
    rep(i, n * n) {
        if (g[i].size() == 2) {
            grid[0][0] = i;
            used.insert(i);
            break;
        }
    }

    set<ll> all;
    rep(i, n * n) {
        all.insert(i);
    }

    loop(c, 1, n + n - 2) {
        loop(r, 1, n - 1) {
            if (grid[r][c] == -INF) continue;

            set<ll> a, b;
            a = (grid[r][c - 1] == -INF ? all : g[grid[r][c - 1]]);
            b = (grid[r - 1][c - 1] == -INF ? all : g[grid[r - 1][c - 1]]);

            if (a.size() > b.size()) {
                swap(a, b);
            }

            set<ll> intersect;
            for (ll av : a) {
                if (b.count(av) && !used.count(av)) intersect.insert(av);
            }
            assert(intersect.size() != 0);

            grid[r][c] = *intersect.begin();
            used.insert(*intersect.begin());
        }

        if (grid[0][c] == -INF) continue;
        for (ll unused : g[grid[0][c - 1]]) {
            if (!used.count(unused)) {
                grid[0][c] = unused;
                used.insert(unused);
            }
        }
    }

    rep(r, n) {
        loop(c, r, r + n - 1) {
            cout << grid[r][c] + 1 << " ";
        }
        cout << endl;
    }
}
accuracy: 1
time_ms: 30
memory_kb: 6784
final_rank: 14

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)
using ll = long long;
template<class T> bool chmin(T &a,const T b){if(a>b){a=b;return 1;}return 0;}
template<class T> bool chmax(T &a,const T b){if(a<b){a=b;return 1;}return 0;}
const int INF = (1<<30)-1;
#define all(p) p.begin(),p.end()
const int mod=998244353;
using ld = long double;

int main(){
	int N;
	cin>>N;
	vector<set<int>> p(N*N);
	auto f=[&](int a,int b)->void{
		assert(p[a].count(b)&&p[b].count(a));
		p[a].erase(b);
		p[b].erase(a);
	};
	rep(i,0,2*N*N-2*N){
		int a,b;
		cin>>a>>b;
		a--,b--;
		p[a].insert(b);
		p[b].insert(a);
	}
	vector<vector<int>> ans(N,vector<int>(N));
	rep(i,0,N*N){
		if((int)(p[i].size())==2){
			ans[0][0]=i;
			rep(j,0,2){
				int v=(*p[i].begin());
				ans[j][j^1]=v;
				f(v,i);
			}
			break;
		}
	}
	rep(sum,2,N*2-1){
		int l=max(0,sum-N+1);
		int r=min(N,sum+1);
		rep(x,l,r){
			int y=sum-x;
			if(min(x,y)!=0){
				for(auto v:p[ans[x-1][y]]){
					if(p[ans[x][y-1]].count(v)){
						f(ans[x][y-1],v);
						f(ans[x-1][y],v);
						ans[x][y]=v;
						break;
					}
				}
			}
			else if(x==0){
				for(auto v:p[ans[x][y-1]]){
					if(!p[ans[x+1][y-2]].count(v)){
						f(ans[x][y-1],v);
						ans[x][y]=v;
						break;
					}
				}
			}
			else if(y==0){
				int v=(*p[ans[x-1][y]].begin());
				ans[x][y]=v;
				f(v,ans[x-1][y]);
			}
		}
	}
	rep(i,0,N){
		rep(j,0,N){
			if(j) cout<<" ";
			cout<<ans[i][j]+1;
		}
		cout<<"\n";
	}
}
accuracy: 1
time_ms: 10
memory_kb: 5492
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;

struct Edge {
    int from, to;
    int cost;
    Edge rev() {
        auto ret = *this;
        swap(ret.from, ret.to);
        return ret;
    }
    Edge(int from, int to, int cost) : from(from), to(to), cost(cost) {}
};
vector<vector<Edge>> G;

vector<int> Dijkstra(const vector<vector<Edge>>& G, int s) {
    int N = (int)G.size();
    vector<int> dist(N, 1 << 30);
    dist[s] = 0;
    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> que;
    que.push({0, s});
    while (!que.empty()) {
        auto [d_now, now] = que.top();
        que.pop();
        if (dist[now] < d_now)
            continue;
        for (auto&& e : G[now]) {
            if (dist[e.to] > d_now + e.cost) {
                dist[e.to] = d_now + e.cost;
                que.push({dist[e.to], e.to});
            }
        }
    }
    return dist;
}

int main() {
    int N;
    cin >> N;
    int M = 2 * N * N - 2 * N;
    G.resize(N * N);
    while (M--) {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        G[a].emplace_back(a, b, 1);
        G[b].emplace_back(b, a, 1);
    }
    int l;
    for (int i = 0; i < N * N; i++) {
        if (G[i].size() == 2) {
            l = i;
            break;
        }
    }
    auto L = Dijkstra(G, l);
    int r;
    for (int i = 0; i < N * N; i++) {
        if (G[i].size() == 2 && L[i] == N - 1) {
            r = i;
            break;
        }
    }
    auto R = Dijkstra(G, r);
    vector ans(N, vector(N, 0));
    for (int i = 0; i < N * N; i++) {
        int dl = L[i], dr = R[i];
        int x = (dl + dr - (N - 1)) / 2;
        int y = (dl - dr + (N - 1)) / 2;
        ans[x][y] = i + 1;
    }
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            cout << ans[i][j] << " \n"[j == N - 1];
        }
    }
}
accuracy: 1
time_ms: 10
memory_kb: 4272
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;

int n;
vector<set<int>> g;

vector<vector<int>> solve();

int main() {
  cin >> n;
  g.resize(n * n);
  for (int i = 0; i < 2 * n * n - 2 * n; ++i) {
    int x, y;
    cin >> x >> y;
    g[--x].insert(--y);
    g[y].insert(x);
  }
  auto res = solve();
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j) cout << res[i][j] + 1 << " \n"[j == n - 1];
  return 0;
}

vector<vector<int>> solve() {
  vector<vector<int>> res(n, vector<int>(n, 0));
  for (int i = 0; i < n * n; ++i)
    if (g[i].size() == 2) {
      auto it = g[i].begin();
      res[0][0] = i, res[0][1] = *it, res[1][0] = *(++it);
      g[i].clear(), g[res[0][1]].erase(i), g[res[1][0]].erase(i);
      break;
    }
  for (int i = 1; i < n; ++i) {
    int now = res[0][i], ld = res[1][i - 1];
    for (auto p : g[now])
      if (g[ld].count(p)) res[1][i] = p;
    g[ld].erase(res[1][i]), g[now].erase(res[1][i]);
    g[res[1][i]].erase(now), g[res[1][i]].erase(ld);
    if (i != n - 1) {
      res[0][i + 1] = *g[now].begin();
      g[now].clear(), g[res[0][i + 1]].erase(now);
    }
  }
  for (int i = 2; i < n; ++i)
    for (int j = 0; j < n; ++j) {
      res[i][j] = *(g[res[i - 1][j]].begin());
      g[res[i][j]].erase(res[i - 1][j]);
      g[res[i - 1][j]].clear();
      if (j)
        g[res[i][j]].erase(res[i][j - 1]), g[res[i][j - 1]].erase(res[i][j]);
    }
  return res;
}
accuracy: 1
time_ms: 10
memory_kb: 5412
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define R cin>>
#define Z class
#define ll long long
#define ln cout<<'\n'
#define in(a) insert(a)
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\n",a)
#define mem(a) memset(a,0,sizeof(a))
#define all(c) (c).begin(),(c).end()
#define iter(c) __typeof((c).begin())
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define REP(i,m,n) for(int i=(int)(m);i<(int)(n);i++)
#define rep(i,n) REP(i,0,n)
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
template<Z A>void pr(A a){cout<<a;ln;}
template<Z A,Z B>void pr(A a,B b){cout<<a<<' ';pr(b);}
template<Z A,Z B,Z C>void pr(A a,B b,C c){cout<<a<<' ';pr(b,c);}
template<Z A,Z B,Z C,Z D>void pr(A a,B b,C c,D d){cout<<a<<' ';pr(b,c,d);}
template<Z A>void PR(A a,ll n){rep(i,n){if(i)cout<<' ';cout<<a[i];}ln;}
ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}
const ll MAX=1000000007,MAXL=1LL<<61,dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
typedef pair<int,int> P;

void Main() {
  ll n;
  R n;
  P d[n*n];
  int c[n*n];
  mem(c);
  rep(i,n*n) d[i]=P(0,i);
  vector<ll> v[n*n];
  set<int> se[n*n];
  for(int i=0; i<2*n*n-n*2; i++) {
    ll x,y;
    cin >> x >> y;
    x--,y--;
    v[x].pb(y);
    v[y].pb(x);
    se[x].in(y);
    se[y].in(x);
    c[x]++;
    c[y]++;
    d[x].F++;
    d[y].F++;
  }
  sort(d,d+n*n);
  int a[n][n];
  memset(a,-1,sizeof(a));
  queue<int> que;
  que.push(d[0].S);
  c[d[0].S]=-1;
  rep(i,n) {
    int x=que.front();que.pop();
    c[x]=-1;
    a[i][0]=x;
    REP(j,1,n) {
      ll z=-1;
      rep(k,v[x].size()) {
        int y=v[x][k];
        c[y]--;
        if(c[y]>=0&&c[y]<=2) {
          if(i&&!se[a[i-1][j]].count(y)) {
            if(j==1) que.push(y);
            continue;
          } else if(j==1&&!i&&z!=-1) que.push(z);
          z=y;
        }
      }
      c[z]=-1;
      a[i][j]=z;
      x=z;
    }
  }
  rep(i,n) {
    rep(j,n) {
      if(j) cout << " ";
      cout << a[i][j]+1;
    }
    ln;
  }
}

int main(){ios::sync_with_stdio(0);cin.tie(0);Main();return 0;}
accuracy: 1
time_ms: 10
memory_kb: 6520
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main() {
    int n;
    cin >> n;
    vector<set<int>> g(n*n);
    for (int i = 0; i < 2*n*n - 2*n; ++i) {
        int a, b;
        cin >> a >> b;
        a--; b--;
        g[a].insert(b);
        g[b].insert(a);
    }
    vector ans(n, vector<int>(n));
    vector<bool> used(n*n);
    for (int i = 0; i < n*n; ++i) {
        if (g[i].size() == 2) {
            ans[0][0] = i;
            used[i] = true;
            break;
        }
    }

    for (int j = 1; j < n; ++j) {
        for (int x : g[ans[0][j - 1]]) {
            if (used[x]) continue;
            if (g[x].size() == (j == n - 1 ? 2 : 3)) {
                ans[0][j] = x;
                used[x] = true;
                break;
            }
        }
    }

    for (int i = 1; i < n; ++i) {
        for (int x : g[ans[i - 1][0]]) {
            if (used[x]) continue;
            if (g[x].size() == (i == n - 1 ? 2 : 3)) {
                ans[i][0] = x;
                used[x] = true;
                break;
            }
        }
    }

    for (int i = 1; i < n; ++i) {
        for (int j = 1; j < n; ++j) {
            for (int x : g[ans[i - 1][j]]) {
                if (g[ans[i][j - 1]].count(x)) {
                    if (used[x]) continue;
                    ans[i][j] = x;
                    used[x] = true;
                    break;
                }
            }
        }
    }

    for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) cout << ans[i][j] + 1 << (j == n - 1 ? '\n' : ' ');
}
accuracy: 1
time_ms: 10
memory_kb: 5492
final_rank: 9

code_snippet: #include <iostream>
#include <set>
using namespace std;

int main()
{
    int n;
    cin >> n;
    int a[20005], b[20005];
    int d[10005]{0};
    set<int> st[10005];
    for(int i = 0; i < n * n * 2 - n * 2; i++){
        cin >> a[i] >> b[i];
        d[a[i]]++;
        d[b[i]]++;
        st[a[i]].insert(b[i]);
        st[b[i]].insert(a[i]);
    }
    int r;
    for(r = 1; r <= n * n; r++){
        if(d[r] == 2) break;
    }
    int ans[102][102];
    ans[0][0] = r;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            if(i == n - 1){
                if(j == n - 1) continue;
                ans[i][j + 1] = *st[ans[i][j]].begin();
                st[ans[i][j + 1]].erase(ans[i][j]);
            }
            else if(j == n - 1){
                ans[i + 1][j] = *st[ans[i][j]].begin();
                st[ans[i + 1][j]].erase(ans[i][j]);
            }
            else{
                auto itr = st[ans[i][j]].begin();
                int u = *itr;
                int v = *++itr;
                if(j == 0 || (i == n - 2 && j != n - 2)){
                    if(d[u] < d[v]) swap(u, v);
                }
                else{
                    if((int)st[u].size() > (int)st[v].size()) swap(u, v);
                }
                ans[i][j + 1] = u;
                st[u].erase(ans[i][j]);
                ans[i + 1][j] = v;
                st[v].erase(ans[i][j]);
            }
        }
    }
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++) cout << ans[i][j] << " ";
        cout << endl;
    }
}
accuracy: 1
time_ms: 10
memory_kb: 5656
final_rank: 11

code_snippet: #pragma GCC target("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
#define DEBUG
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for(int i = 0; i < (int)v.size(); i++) {
        if(i) { os << ','; }
        os << v[i];
    }
    os << '}';
    return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
    cerr << " " << x;
    debugg(args...);
}
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<29;
const ll linf = 1LL<<60;
const int MAX = 4020000;
// int dy[8] = {0,1,0,-1,1,-1,-1,1};
// int dx[8] = {-1,0,1,0,1,-1,1,-1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    int sz = a.size();
    for(auto itr = a.begin(); itr != a.end(); itr++){
		cout << *itr;
        sz--;
        if(sz) cout << " ";
	}
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){
	cout << a << " " << b << "\n";
}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << a << " " << b << " " << c << "\n";
}
void mark() {cout << "#" << "\n";}
ll pcount(ll x) {return __builtin_popcountll(x);}
const int mod = 1e9 + 7;
//const int mod = 998244353;

vector<set<int>> G(10005);
int ans[105][105];

int main(){
	int n; cin >> n;
	int N = n*n;
	int m = 2*N - 2*n;
	rep(i,m){
		int a,b; cin >> a >> b; a--; b--;
		G[a].insert(b);
		G[b].insert(a);
	}
	set<int> ls;
	int s = 0;
	rep(i,N){
		if(G[i].size() <= 3) ls.insert(i);
		if(G[i].size() == 2) s = i;
	}
	ans[0][0] = s;
	int x = 0, y = 0;
	int dx = 1, dy = 0;
	int now = s;
	while(!ls.empty()){
		if(x == n-1 && y == 0) dx = 0, dy = 1;
		if(x == n-1 && y == n-1) dx = -1, dy = 0;
		if(x == 0 && y == n-1) dx = 0, dy = -1;
		x += dx, y += dy;
		for(auto v : G[now]){
			if(ls.count(v)){
				ans[y][x] = v;
				G[now].erase(v);
				G[v].erase(now);
				ls.erase(v);
				now = v;
				break; 
			}
		}
	}
	for(int i=1; i<n-1; i++){
		for(int j=1; j<n-1; j++){
			ans[i][j] = *G[ans[i-1][j]].begin();
			G[ans[i][j]].erase(ans[i-1][j]);
		}
		for(int j=0; j<n-1; j++){
			G[ans[i][j]].erase(ans[i][j+1]);
			G[ans[i][j+1]].erase(ans[i][j]);
		}
	}
	rep(i,n) rep(j,n) cout << ans[i][j]+1 << " \n"[j==n-1];
}
accuracy: 1
time_ms: 10
memory_kb: 5208
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for(int64 i = 0;i < (n);i++)
#define FOR(i, a, b) for(int64 i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}
template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}

template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
	return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value!=0>::type
fill_v(U &u,const V... v){u=U(v...);}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value==0>::type
fill_v(U &u,const V... v){
	for(auto &e:u) fill_v<T>(e,v...);
}

template<::std::uint_fast64_t mod>
class ModInt{
private:
  using value_type = ::std::uint_fast64_t;
  value_type n;
public:
  ModInt() : n(0) {}
  ModInt(value_type n_) : n(n_ % mod) {}
  ModInt(const ModInt& m) : n(m.n) {}

  template<typename T>
  explicit operator T() const { return static_cast<T>(n); }
  value_type get() const { return n; }

  friend ::std::ostream& operator<<(::std::ostream &os, const ModInt<mod> &a) {
    return os << a.n;
  }

  friend ::std::istream& operator>>(::std::istream &is, ModInt<mod> &a) {
    value_type x;
    is >> x;
    a = ModInt<mod>(x);
    return is;
  }

  bool operator==(const ModInt& m) const { return n == m.n; }
  bool operator!=(const ModInt& m) const { return n != m.n; }
  ModInt& operator*=(const ModInt& m){ n = n * m.n % mod; return *this; }

  ModInt pow(value_type b) const{
    ModInt ans = 1, m = ModInt(*this);
    while(b){
      if(b & 1) ans *= m;
      m *= m;
      b >>= 1;
    }
    return ans;
  }

  ModInt inv() const { return (*this).pow(mod-2); }
  ModInt& operator+=(const ModInt& m){ n += m.n; n = (n < mod ? n : n - mod); return *this; }
  ModInt& operator-=(const ModInt& m){ n += mod - m.n; n = (n < mod ? n : n - mod); return *this; }
  ModInt& operator/=(const ModInt& m){ *this *= m.inv(); return *this; }
  ModInt operator+(const ModInt& m) const { return ModInt(*this) += m; }
  ModInt operator-(const ModInt& m) const { return ModInt(*this) -= m; }
  ModInt operator*(const ModInt& m) const { return ModInt(*this) *= m; }
  ModInt operator/(const ModInt& m) const { return ModInt(*this) /= m; }
  ModInt& operator++(){ n += 1; return *this; }
  ModInt& operator--(){ n -= 1; return *this; }
  ModInt operator++(int){
    ModInt old(n);
    n += 1;
    return old;
  }
  ModInt operator--(int){
    ModInt old(n);
    n -= 1;
    return old;
  }
  ModInt operator-() const { return ModInt(mod-n); }
};

template<::std::size_t size, ::std::uint_fast64_t mod=1000000007>
class Factorial{
private:
  using value_type = ModInt<mod>;
  ::std::vector<value_type> fact, inv;
public:
  Factorial() : fact(size+1, 1), inv(size+1, 1){
    for(::std::size_t i = 1; i <= size; ++i){
      fact[i] = fact[i-1] * value_type(i);
      inv[i] = fact[i].inv();
    }
  }

  value_type comb(::std::int64_t a, ::std::int64_t b){
    assert(a >= b);
    assert(b >= 0);
    return fact[a]*inv[b]*inv[a-b];
  }

  value_type& operator[](::std::size_t k){ return fact[k]; }
};

constexpr int64 mod = 998244353;
using Mint = ModInt<mod>;
Factorial<412345, mod> f;

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

  int N;
  cin >> N;
  vector<vector<int>> G(N*N+1);
  vector<int> used(N*N+1, 0);
  auto res = make_v<int>(N, N);

  REP(i, 2*N*N-2*N) {
    int a, b;
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  FOR(i, 1, N*N+1) {
    if (G[i].size() == 2) {
      int col = 0, now = i, nxt = -1;
      res[0][col++] = now;
      used[now] = 1;
      while (now == i || G[now].size() > 2) {
        nxt = -1;
        for (auto& u : G[now]) {
          if (!used[u] && (nxt == -1 || (G[nxt].size() > G[u].size()))) {
            nxt = u;
          }
        }
        now = nxt;
        res[0][col++] = now;
        used[now] = 1;
      }
      break;
    }
  }

  REP(i, N-1) {
    REP(j, N) {
      for (auto& u : G[res[i][j]]) {
        if (used[u]) continue;
        res[i+1][j] = u;
        used[u] = 1;
      }
    }
  }

  REP(i, N) {
    REP(j, N) {
      cout << res[i][j] << " ";
    }
    cout << endl;
  }
}
accuracy: 1
time_ms: 40
memory_kb: 9988
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for(int64 i = 0;i < (n);i++)
#define FOR(i, a, b) for(int64 i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}
template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}

template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
	return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value!=0>::type
fill_v(U &u,const V... v){u=U(v...);}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value==0>::type
fill_v(U &u,const V... v){
	for(auto &e:u) fill_v<T>(e,v...);
}

template<::std::uint_fast64_t mod>
class ModInt{
private:
  using value_type = ::std::uint_fast64_t;
  value_type n;
public:
  ModInt() : n(0) {}
  ModInt(value_type n_) : n(n_ % mod) {}
  ModInt(const ModInt& m) : n(m.n) {}

  template<typename T>
  explicit operator T() const { return static_cast<T>(n); }
  value_type get() const { return n; }

  friend ::std::ostream& operator<<(::std::ostream &os, const ModInt<mod> &a) {
    return os << a.n;
  }

  friend ::std::istream& operator>>(::std::istream &is, ModInt<mod> &a) {
    value_type x;
    is >> x;
    a = ModInt<mod>(x);
    return is;
  }

  bool operator==(const ModInt& m) const { return n == m.n; }
  bool operator!=(const ModInt& m) const { return n != m.n; }
  ModInt& operator*=(const ModInt& m){ n = n * m.n % mod; return *this; }

  ModInt pow(value_type b) const{
    ModInt ans = 1, m = ModInt(*this);
    while(b){
      if(b & 1) ans *= m;
      m *= m;
      b >>= 1;
    }
    return ans;
  }

  ModInt inv() const { return (*this).pow(mod-2); }
  ModInt& operator+=(const ModInt& m){ n += m.n; n = (n < mod ? n : n - mod); return *this; }
  ModInt& operator-=(const ModInt& m){ n += mod - m.n; n = (n < mod ? n : n - mod); return *this; }
  ModInt& operator/=(const ModInt& m){ *this *= m.inv(); return *this; }
  ModInt operator+(const ModInt& m) const { return ModInt(*this) += m; }
  ModInt operator-(const ModInt& m) const { return ModInt(*this) -= m; }
  ModInt operator*(const ModInt& m) const { return ModInt(*this) *= m; }
  ModInt operator/(const ModInt& m) const { return ModInt(*this) /= m; }
  ModInt& operator++(){ n += 1; return *this; }
  ModInt& operator--(){ n -= 1; return *this; }
  ModInt operator++(int){
    ModInt old(n);
    n += 1;
    return old;
  }
  ModInt operator--(int){
    ModInt old(n);
    n -= 1;
    return old;
  }
  ModInt operator-() const { return ModInt(mod-n); }
};

template<::std::size_t size, ::std::uint_fast64_t mod=1000000007>
class Factorial{
private:
  using value_type = ModInt<mod>;
  ::std::vector<value_type> fact, inv;
public:
  Factorial() : fact(size+1, 1), inv(size+1, 1){
    for(::std::size_t i = 1; i <= size; ++i){
      fact[i] = fact[i-1] * value_type(i);
      inv[i] = fact[i].inv();
    }
  }

  value_type comb(::std::int64_t a, ::std::int64_t b){
    assert(a >= b);
    assert(b >= 0);
    return fact[a]*inv[b]*inv[a-b];
  }

  value_type& operator[](::std::size_t k){ return fact[k]; }
};

constexpr int64 mod = 998244353;
using Mint = ModInt<mod>;
Factorial<412345, mod> f;

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

  int N;
  cin >> N;
  vector<vector<int>> G(N*N+1);
  vector<int> used(N*N+1, 0);
  auto res = make_v<int>(N, N);

  REP(i, 2*N*N-2*N) {
    int a, b;
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  FOR(i, 1, N*N+1) {
    if (G[i].size() == 2) {
      int col = 0, now = i, nxt = -1;
      res[0][col++] = now;
      used[now] = 1;
      while (now == i || G[now].size() > 2) {
        for (auto& u : G[now]) {
          if (nxt == -1 || (!used[u] && G[nxt].size() > G[u].size())) {
            nxt = u;
          }
        }
        now = nxt;
        res[0][col++] = now;
        used[now] = 1;
      }
      break;
    }
  }

  REP(i, N-1) {
    REP(j, N) {
      for (auto& u : G[res[i][j]]) {
        if (used[u]) continue;
        res[i+1][j] = u;
        used[u] = 1;
      }
    }
  }

  REP(i, N) {
    REP(j, N) {
      cout << res[i][j] << " ";
    }
    cout << endl;
  }
}
accuracy: 0.023255813953488372
time_ms: 40
memory_kb: 8956
final_rank: 20

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for(int64 i = 0;i < (n);i++)
#define FOR(i, a, b) for(int64 i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}
template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}

template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
	return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value!=0>::type
fill_v(U &u,const V... v){u=U(v...);}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value==0>::type
fill_v(U &u,const V... v){
	for(auto &e:u) fill_v<T>(e,v...);
}

template<::std::uint_fast64_t mod>
class ModInt{
private:
  using value_type = ::std::uint_fast64_t;
  value_type n;
public:
  ModInt() : n(0) {}
  ModInt(value_type n_) : n(n_ % mod) {}
  ModInt(const ModInt& m) : n(m.n) {}

  template<typename T>
  explicit operator T() const { return static_cast<T>(n); }
  value_type get() const { return n; }

  friend ::std::ostream& operator<<(::std::ostream &os, const ModInt<mod> &a) {
    return os << a.n;
  }

  friend ::std::istream& operator>>(::std::istream &is, ModInt<mod> &a) {
    value_type x;
    is >> x;
    a = ModInt<mod>(x);
    return is;
  }

  bool operator==(const ModInt& m) const { return n == m.n; }
  bool operator!=(const ModInt& m) const { return n != m.n; }
  ModInt& operator*=(const ModInt& m){ n = n * m.n % mod; return *this; }

  ModInt pow(value_type b) const{
    ModInt ans = 1, m = ModInt(*this);
    while(b){
      if(b & 1) ans *= m;
      m *= m;
      b >>= 1;
    }
    return ans;
  }

  ModInt inv() const { return (*this).pow(mod-2); }
  ModInt& operator+=(const ModInt& m){ n += m.n; n = (n < mod ? n : n - mod); return *this; }
  ModInt& operator-=(const ModInt& m){ n += mod - m.n; n = (n < mod ? n : n - mod); return *this; }
  ModInt& operator/=(const ModInt& m){ *this *= m.inv(); return *this; }
  ModInt operator+(const ModInt& m) const { return ModInt(*this) += m; }
  ModInt operator-(const ModInt& m) const { return ModInt(*this) -= m; }
  ModInt operator*(const ModInt& m) const { return ModInt(*this) *= m; }
  ModInt operator/(const ModInt& m) const { return ModInt(*this) /= m; }
  ModInt& operator++(){ n += 1; return *this; }
  ModInt& operator--(){ n -= 1; return *this; }
  ModInt operator++(int){
    ModInt old(n);
    n += 1;
    return old;
  }
  ModInt operator--(int){
    ModInt old(n);
    n -= 1;
    return old;
  }
  ModInt operator-() const { return ModInt(mod-n); }
};

template<::std::size_t size, ::std::uint_fast64_t mod=1000000007>
class Factorial{
private:
  using value_type = ModInt<mod>;
  ::std::vector<value_type> fact, inv;
public:
  Factorial() : fact(size+1, 1), inv(size+1, 1){
    for(::std::size_t i = 1; i <= size; ++i){
      fact[i] = fact[i-1] * value_type(i);
      inv[i] = fact[i].inv();
    }
  }

  value_type comb(::std::int64_t a, ::std::int64_t b){
    assert(a >= b);
    assert(b >= 0);
    return fact[a]*inv[b]*inv[a-b];
  }

  value_type& operator[](::std::size_t k){ return fact[k]; }
};

constexpr int64 mod = 998244353;
using Mint = ModInt<mod>;
Factorial<412345, mod> f;

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

  int64 N;
  cin >> N;
  vector<vector<int>> G(N*N+1);
  vector<int> used_id(N*N+1, 0);
  auto res = make_v<int>(N, N);
  auto deg = make_v<int>(N, N);
  auto used = make_v<int>(N, N);
  fill_v<int>(deg, 4);

  int dy[4] = {-1, 0, 1, 0};
  int dx[4] = {0, -1, 0, 1};

  auto in = [&](int y, int x) {
    return 0<=y&&y<N&&0<=x&&x<N;
  };

  REP(i, N) {
    deg[0][i] = deg[i][0] = deg[N-1][i] = deg[i][N-1] = 3;
  }
  deg[0][0] = deg[N-1][0] = deg[0][N-1] = deg[N-1][N-1] = 2;

  REP(i, 2*N*N - 2*N) {
    int a, b;
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  using T= tuple<int, int, int, int>; // deg, y, x, id

  priority_queue<T, vector<T>, greater<T>> pq;
  REP(i, N*N) {
    if (G[i+1].size() == 2) {
      pq.emplace(2, 0, 0, i+1);
      res[0][0] = i+1;
      used[0][0] = 1;
      used_id[i+1] = 1;
      break;
    }
  }

  while (pq.size()) {
    int d, y, x, id;
    tie(d, y, x, id) = pq.top(); pq.pop();

    map<int, stack<PII>> mp;
    int cnt = 0;

    REP(i, 4) {
      int yy = y + dy[i], xx =x + dx[i];
      if (!in(yy, xx) || used[yy][xx]) continue;
      mp[deg[yy][xx]].push(PII(yy, xx));
      cnt++;
    }

    if (d == 2 || d == 3 || cnt == 1) {
      for (auto& u : G[id]) {
        if (used_id[u]) continue;
        int du = G[u].size();
        int yy, xx;
        tie(yy, xx) = mp[du].top(); mp[du].pop();
        res[yy][xx] = u;
        used[yy][xx] = u;
        used_id[u] = 1;
        pq.emplace(du, yy, xx, u);
      }
    } else {
      for (auto& u : G[id]) {
        if (used_id[u]) continue;
        int du = G[u].size();
        map<int, int> cnt_id;
        for (auto &v : G[u]) cnt_id[v]++;

        REP(k, 4) {
          int yy = y + dy[k], xx = x + dx[k];
          if (!in(yy, xx) || used[yy][xx]) continue;

          int ok = 0;
          REP(k2, 4) {
            int yyy = yy+dy[k], xxx =xx + dx[k];
            if (!in(yyy, xxx) || !used[yyy][xxx]) continue;

            ok += cnt_id[res[yyy][xxx]];
          }
          if (ok == 2) {
            res[yy][xx] = u;
            used[yy][xx] = u;
            used_id[u] = 1;
            pq.emplace(du, yy, xx, u);
            break;
          }
        }
      }
    }
  }
  REP(i, N) {
    REP(j, N) {
      cout << res[i][j];
      if (j < N-1) cout << " ";
    }
    cout << endl;
  }
}
accuracy: 1
time_ms: 50
memory_kb: 9940
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
using ll = long long;
using P = pair<ll, ll>;
const long double PI = acos(-1.0L);
ll GCD(ll a, ll b) { return b?GCD(b, a%b):a; }
ll LCM(ll a, ll b) { return a/GCD(a, b)*b; }

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

int n;
struct Data {
    int no, cnt, pcnt;
    bool operator<(const Data &r) const {
        return cnt == r.cnt ? pcnt < r.pcnt : cnt < r.cnt;
    };
};

struct pqdata {
    int x, y;
    int no, cnt, pcnt;
    bool operator<(const pqdata &r) const {
        return cnt == r.cnt ? pcnt > r.pcnt : cnt > r.cnt;
    };
};

int main() {
    cin >> n;
    int nn = n*n;
    vector< vector<int> > graph(nn+1, vector<int>());
    vector<Data> gcnt;
    for(int i = 1; i <= nn; ++i) {
        Data indata; indata.no = i; indata.cnt = 0;
        indata.pcnt = 0;
        gcnt.emplace_back(indata);
    }
    int ab = 2*n*n-2*n;
    for(int i = 0; i < ab; ++i) {
        int a, b; cin >> a >> b;
        gcnt[a-1].cnt++; gcnt[b-1].cnt++;
        gcnt[a-1].pcnt++; gcnt[b-1].pcnt++;
        graph[a].emplace_back(b);
        graph[b].emplace_back(a);
    }
    sort(gcnt.begin(), gcnt.end());

    /*cout << endl;
    for(int i = 0; i <= nn; ++i) {
        cout << "now " << i << " ";
        for(int j = 0; j < (int)graph[i].size(); ++j) {
            cout << graph[i][j] << " ";
        }
        cout << endl;
    }*/

    // gcntの先頭を採用する
    vector< vector<int> > ans(n, vector<int>(n, -1));
    ans[0][0] = gcnt[0].no;
    pqdata first; first.x = 0; first.y = 0;
    first.no = gcnt[0].no; first.cnt = gcnt[0].cnt;
    first.pcnt = gcnt[0].cnt;
    
    vector<bool> numch(nn+1, false);
    priority_queue<pqdata> pque;
    pque.push(first);
    numch[gcnt[0].no] = true;
    // cout << endl;
    while(!pque.empty()) {
        pqdata now = pque.top();
        // cout << "now " << now.x << " " << now.y << " no " << now.no << " " << now.cnt << endl;
        pque.pop();
        // 移動先で一番候補が少ないところを探す
        vector<pqdata> pos;
        for(int i = 0; i < 4; ++i) {
            int cx = now.x+dx[i], cy = now.y+dy[i];
            if(cx >= 0 && cx < n && cy >= 0 && cy < n) {
                if(ans[cy][cx] == -1) {
                    int ccnt = 0;
                    int cpcnt = 0;
                    for(int j = 0; j < 4; ++j) {
                        int ccx = cx+dx[j], ccy = cy+dy[j];
                        if(ccx >= 0 && ccx < n && ccy >= 0 && ccy < n) {
                            cpcnt++;
                            if(ans[ccy][ccx] == -1) ccnt++;
                        }
                    }
                    pqdata inpq;
                    inpq.x = cx; inpq.y = cy;
                    inpq.cnt = ccnt; inpq.no = 0;
                    inpq.pcnt = cpcnt;
                    pos.emplace_back(inpq);
                }
            }
        }
        int poslen = pos.size();
        sort(pos.begin(), pos.end());
        // 後ろが一番cntが小さい

        // 隣接候補のうち一番候補が少ない数を探す
        vector<Data> num;
        int glen = graph[now.no].size();
        for(int i = 0; i < glen; ++i) {
            int ch = graph[now.no][i];
            int chlen = graph[ch].size();
            int chcnt = 0;
            if(!numch[ch]) {
                for(int j = 0; j < chlen; ++j) {
                    if(!numch[graph[ch][j]]) chcnt++;
                }
                Data innum;
                innum.no = ch; innum.cnt = chcnt;
                innum.pcnt = chlen;
                num.emplace_back(innum);
            }
        }
        int numlen = num.size();
        sort(num.begin(), num.end());
        // 先頭に一番cntが少ない

        if(poslen == 0 || numlen == 0) continue;

        /*for(int i = 0; i < poslen; ++i) {
            cout << "cnt " << pos[i].cnt << " p " << pos[i].pcnt << " pos " << pos[i].x << " " << pos[i].y << endl;
        }
        for(int i = 0; i < numlen; ++i) {
            cout << "no " << num[i].no << " " << num[i].cnt << " " << num[i].pcnt << endl;
        }*/

        // cout << endl;

        int nno = num[0].no;
        int ncnt = num[0].cnt;

        set<int> check;
        for(int i = 0; i < (int)graph[nno].size(); ++i) check.insert(graph[nno][i]);

        for(int i = poslen-1; i >= 0; --i) {
            int nx = pos[i].x, ny = pos[i].y;
            bool flg = true;
            for(int j = 0; j < 4; ++j) {
                int nnx = nx+dx[j], nny = ny+dy[j];
                if(nnx >= 0 && nnx < n && nny >= 0 && nny < n) {
                    if(ans[nny][nnx] != -1) {
                        if(check.find(ans[nny][nnx]) == check.end()) flg = false;
                    }
                }
            }
            if(flg) {
                // cout << "next " << nx << " " << ny << " no " << nno << " " << ncnt << endl;
                numch[nno] = true;
                ans[ny][nx] = nno;
                pqdata next;
                next.x = nx; next.y = ny; next.no = nno; next.cnt = ncnt;
                pque.push(next);
                break;
            }
        }
    }
    
    for(int i = 0; i < n; ++i) {
        for(int j = 0; j < n; ++j) {
            if(j == n-1) cout << ans[i][j] << endl;
            else cout << ans[i][j] << " ";
        }
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3680
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ll = long long;
#define var auto

const char newl = '\n';

template< typename T1, typename T2 > inline void chmin(T1 &a, T2 b) { if (a > b) a = b; }
template< typename T1, typename T2 > inline void chmax(T1 &a, T2 b) { if (a < b) a = b; }

signed main() {
  int n;
  cin >> n;
  int N = n * n;
  int M = 2 * n * n - 2 * n;
  vector<vector<int>> g(N);
  for (int i = 0; i < M; i++){
    int r, c;
    cin >> r >> c;
    r--; c--;
    g[r].push_back(c);
    g[c].push_back(r);
  }
  vector<bool> used(N);

  queue<int> q{};
  int prev;
  for (int i = 0; i < N; i++){
    if (g[i].size() == 2){
      prev = i;
    }
  }
  q.push(prev);
  used[prev] = true;
  prev = g[prev][0];
  q.push(prev);
  used[prev] = true;
  while (g[prev].size() != 2){
    for (var&& elem : g[prev]){
      if (used[elem] || g[elem].size() == 4) continue;
      prev = elem;
      break;
    }
    q.push(prev);
    used[prev] = true;
  }
  
  for (int i = 0; i < n; i++){
    for (int j = 0; j < n; j++){
      var elem = q.front(); q.pop();
      if (j != 0) cout << ' ';
      cout << (elem + 1);
      for (var&& item : g[elem]){
        if (used[item]) continue;
        q.push(item);
        used[item] = true;
      }
    }
    cout << endl;
  }
}
accuracy: 1
time_ms: 10
memory_kb: 3480
final_rank: 1

code_snippet: #include <bits/stdc++.h>
//#include <atcoder/all>
//using namespace atcoder;
const double pi = 3.141592653589793238462643383279;
using namespace std;
//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;

//container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SQ(a) ((a) * (a))
#define EACH(i, c) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

//repetition
//------------------------------------------
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define MOD 1000000007

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
const double EPS = 1e-11, PI = acos(-1);
//ここから編集

int ans[101][101];
int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  
  int n; cin >> n;
  int n0 = 2*n*(n-1);
  vector<int> a(n0), b(n0);
  vector<vector<int>> g(n*n);
  REP(i,n0){
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    g[a[i]].push_back(b[i]);
    g[b[i]].push_back(a[i]);
  }

  vector<bool> used(n0, 0);
  for(int i=0; i<n; i++){
    for(int j=0; j<n; j++){
      int c = 2;
      if(i%(n-1) != 0) c++;
      if(j%(n-1) != 0) c++;
      for(int k=0; k<n*n; k++){
        if(g[k].size() != c || used[k]) continue;

        bool ok = true;
        if(i>0 && find(all(g[k]), ans[i-1][j]) == g[k].end()) ok = false;
        if(j>0 && find(all(g[k]), ans[i][j-1]) == g[k].end()) ok = false;
        if(ok){
          used[k] = true;
          ans[i][j] = k;
          break;
        }
      }
    }
  }
  REP(i,n){
    REP(j,n){
      if(j) cout << " ";
      cout << ans[i][j]+1;
    }
    cout << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 420
memory_kb: 3684
final_rank: 15

code_snippet: #include <bits/stdc++.h>
//#include <atcoder/all>
//using namespace atcoder;
const double pi = 3.141592653589793238462643383279;
using namespace std;
//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;

//container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SQ(a) ((a) * (a))
#define EACH(i, c) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

//repetition
//------------------------------------------
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define MOD 1000000007

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
const double EPS = 1e-11, PI = acos(-1);
//ここから編集

int ans[101][101];
ll cnt[10101];
bool memo[10101];
bool checked[10101];


vector<vector<int>> g(10101);
int s;
vector<int> path;
bool used[10101];
void dfs(int pos, int p){

  if(p != -1 && pos == s){
    return;
  }
  int nx = -1;
  for(int i=0; i<g[pos].size(); i++){
    int u = g[pos][i];
    if(u == p) continue;
    if(cnt[u] == 2){
      /* 角にたどり着いたとき */
      nx = u;
    }
    if(cnt[u] == 3){
      nx = u;
    }
  }
  path.push_back(nx);
  dfs(nx, pos);
}
bool tmp = false;
void dfs2(int pos, int y, int x, int stmt, int p){
  if(p != -1 && pos == s) {
    tmp = true;
    return;
  }
  int nx = -1;
  if(tmp) return;
  for(int i=0; i<g[pos].size(); i++){
    if(tmp) break;
    int u = g[pos][i];
    if(u == p) continue;
    if(cnt[u] == 2){
      if(stmt == 0){

        nx = u;
        path.push_back(nx);
        dfs2(u, y+1, x, stmt+1, pos);
        
      }
      else if(stmt == 1){

        nx = u;
        path.push_back(nx);
        dfs2(u, y, x+1, stmt+1, pos);
        
      }
      else if(stmt == 2){
        nx = u;
        path.push_back(nx);
        dfs2(u, y-1, x, stmt+1, pos);
        
      }else if(stmt == 3){
        nx = u;
        path.push_back(nx);
        dfs2(u, y, x-1, (stmt+1)%4, pos);
      }
    }else if(cnt[u] == 3){
      if(stmt == 0){
        int vali = ans[y+1][x-1];
        bool check = false;
        for(int j=0; j<g[u].size(); j++){
          if(g[u][j] == vali) check = true;
        }

        if(check){
          nx = u;
          path.push_back(nx);
          dfs2(u, y+1, x, stmt, pos);
        }
      }
      else if(stmt == 1){
        int vali = ans[y+1][x+1];
        bool check = false;
        for(int j=0; j<g[u].size(); j++){
          if(g[u][j] == vali) check = true;
        }

        if(check){
          nx = u;
          path.push_back(nx);
          dfs2(u, y, x+1, stmt, pos);
        }
      }
      else if(stmt == 2){
        int vali = ans[y-1][x+1];
        bool check = false;
        for(int j=0; j<g[u].size(); j++){
          if(g[u][j] == vali) check = true;
        }

        if(check){
          nx = u;
          path.push_back(nx);
          dfs2(u, y-1, x, stmt, pos);
        }
      }
      else if(stmt == 3){
        int vali = ans[y-1][x-1];
        bool check = false;
        for(int j=0; j<g[u].size(); j++){
          if(g[u][j] == vali) check = true;
        }

        if(check){
          nx = u;
          path.push_back(nx);
          dfs2(u, y, x-1, stmt, pos);
        }
      }
    }
  }
}
int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  
  int N; cin >> N;
  int N0 = 2*N*(N-1);

  vector<int> a(N0), b(N0);
  REP(i,N0) {
    cin >> a[i] >> b[i];
    a[i]--; b[i]--;
    g[a[i]].push_back(b[i]);
    g[b[i]].push_back(a[i]);
  }

  REP(i,N0) {
    cnt[a[i]]++;
    cnt[b[i]]++;
  }
  // REP(i,N*N){
  //   cout << cnt[i] << " ";
  // }
  // cout << endl;
  for(int t=0; t<N/2; t++){
    memset(memo, false, sizeof(memo));
    int pos = -1;
    if(t == 0){
    REP(i,N*N){
      if(cnt[i] == 2){
        memo[i] = true;
        pos = i;
      }
    }
    }else{
      int p1 = ans[t][t-1], p2 = ans[t-1][t];
      for(int i=0; i<N*N; i++){
        if(used[i]) continue;
        bool f1=false, f2=false;
        for(int j=0; j<g[i].size(); j++){
          if(g[i][j] == p1) f1 = true;
          if(g[i][j] == p2) f2 = true;
        }
        if(f1&&f2) pos = i;
      }
    }

    s = pos;
    path.clear();tmp = false;
    if(t == 0)dfs(pos, -1);
    else dfs2(pos, t, t, 0, -1);
    // for(int i=0; i<path.size(); i++){
    //   cout << path[i] << " ";
    // }
    // cout << endl;

    reverse(all(path));

    REP(i,path.size()) used[path[i]] = true;
    int idx = 0;
    /*  */
    
    for(int i=t; i<N-t; i++){
      ans[t][i] = path[idx++];
    }
    for(int i=1+t; i<N-t; i++){
      ans[i][N-1-t] = path[idx++];
    }
    for(int i=N-2-t; i>=t; i--){
      ans[N-1-t][i] = path[idx++];
    }
    for(int i=N-2-t; i>=1+t; i--){
      ans[i][t] = path[idx++];
    }

    memset(memo, false, sizeof(memo));

    for(int i=0; i<path.size(); i++){
      int idx = path[i];
      for(int j=0; j<N0; j++){
        if(a[j] == idx){
          cnt[b[j]]--;
        }else if(b[j] == idx){
          cnt[a[j]]--;
        }
      }
    }

    // for(int i=0; i<N0; i++){
    //   cout << cnt[i] << " ";
    // }
    // cout << endl;
  }
  if(N%2 == 1){
    int tmp = 0;
    REP(i,N*N){
      if(!used[i]) tmp = i;
    }

    ans[N/2][N/2] = tmp;
  }

      for(int i=0; i<N; i++){
      for(int j=0; j<N; j++){
        if(j) cout <<  " ";
        cout << ans[i][j]+1;
      }
      cout << endl;
    }
  return 0;
}
accuracy: 0.046511627906976744
time_ms: 80
memory_kb: 3756
final_rank: 18

code_snippet: #include <bits/stdc++.h>
//#include <atcoder/all>
//using namespace atcoder;
const double pi = 3.141592653589793238462643383279;
using namespace std;
//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;

//container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SQ(a) ((a) * (a))
#define EACH(i, c) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

//repetition
//------------------------------------------
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define MOD 1000000007

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
const double EPS = 1e-11, PI = acos(-1);
//ここから編集

int ans[101][101];
int cnt[10101];
bool memo[10101];
bool checked[10101];


vector<vector<int>> g(10101);
int s;
vector<int> path;
bool used[10101];
void dfs(int pos, int p){

  if(p != -1 && pos == s){
    return;
  }
  int nx = -1;
  for(int i=0; i<g[pos].size(); i++){
    int u = g[pos][i];
    if(u == p) continue;
    if(cnt[u] == 2){
      /* 角にたどり着いたとき */
      nx = u;
    }
    if(cnt[u] == 3){
      nx = u;
    }
  }
  path.push_back(nx);
  dfs(nx, pos);
}
bool tmp = false;
void dfs2(int pos, int y, int x, int stmt, int p){
  if(p != -1 && pos == s) {
    tmp = true;
    return;
  }
  int nx = -1;
  if(tmp) return;
  for(int i=0; i<g[pos].size(); i++){
    if(tmp) break;
    int u = g[pos][i];
    if(u == p) continue;
    if(cnt[u] == 2){
      if(stmt == 0){

        nx = u;
        path.push_back(nx);
        dfs2(u, y+1, x, stmt+1, pos);
        
      }
      else if(stmt == 1){

        nx = u;
        path.push_back(nx);
        dfs2(u, y, x+1, stmt+1, pos);
        
      }
      else if(stmt == 2){
        nx = u;
        path.push_back(nx);
        dfs2(u, y-1, x, stmt+1, pos);
        
      }else if(stmt == 3){
        nx = u;
        path.push_back(nx);
        dfs2(u, y, x-1, (stmt+1)%4, pos);
      }
    }else if(cnt[u] == 3){
      if(stmt == 0){
        int vali = ans[y+1][x-1];
        bool check = false;
        for(int j=0; j<g[u].size(); j++){
          if(g[u][j] == vali) check = true;
        }

        if(check){
          nx = u;
          path.push_back(nx);
          dfs2(u, y+1, x, stmt, pos);
        }
      }
      else if(stmt == 1){
        int vali = ans[y+1][x+1];
        bool check = false;
        for(int j=0; j<g[u].size(); j++){
          if(g[u][j] == vali) check = true;
        }

        if(check){
          nx = u;
          path.push_back(nx);
          dfs2(u, y, x+1, stmt, pos);
        }
      }
      else if(stmt == 2){
        int vali = ans[y-1][x+1];
        bool check = false;
        for(int j=0; j<g[u].size(); j++){
          if(g[u][j] == vali) check = true;
        }

        if(check){
          nx = u;
          path.push_back(nx);
          dfs2(u, y-1, x, stmt, pos);
        }
      }
      else if(stmt == 3){
        int vali = ans[y-1][x-1];
        bool check = false;
        for(int j=0; j<g[u].size(); j++){
          if(g[u][j] == vali) check = true;
        }

        if(check){
          nx = u;
          path.push_back(nx);
          dfs2(u, y, x-1, stmt, pos);
        }
      }
    }
  }
}
int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  
  int N; cin >> N;
  int N0 = 2*N*(N-1);

  vector<int> a(N0), b(N0);
  REP(i,N0) {
    cin >> a[i] >> b[i];
    a[i]--; b[i]--;
    g[a[i]].push_back(b[i]);
    g[b[i]].push_back(a[i]);
  }

  REP(i,N0) {
    cnt[a[i]]++;
    cnt[b[i]]++;
  }
  // REP(i,N*N){
  //   cout << cnt[i] << " ";
  // }
  // cout << endl;
  for(int t=0; t<N/2; t++){
    memset(memo, false, sizeof(memo));
    int pos = -1;
    if(t == 0){
    REP(i,N*N){
      if(cnt[i] == 2){
        memo[i] = true;
        pos = i;
      }
    }
    }else{
      int p1 = ans[t][t-1], p2 = ans[t-1][t];
      for(int i=0; i<N*N; i++){
        if(used[i]) continue;
        bool f1=false, f2=false;
        for(int j=0; j<g[i].size(); j++){
          if(g[i][j] == p1) f1 = true;
          if(g[i][j] == p2) f2 = true;
        }
        if(f1&&f2) pos = i;
      }
    }

    s = pos;
    path.clear();tmp = false;
    if(t == 0)dfs(pos, -1);
    else dfs2(pos, t, t, 0, -1);
    // for(int i=0; i<path.size(); i++){
    //   cout << path[i] << " ";
    // }
    // cout << endl;

    reverse(all(path));

    REP(i,path.size()) used[path[i]] = true;
    int idx = 0;
    /*  */
    
    for(int i=t; i<N-t; i++){
      ans[t][i] = path[idx++];
    }
    for(int i=1+t; i<N-t; i++){
      ans[i][N-1-t] = path[idx++];
    }
    for(int i=N-2-t; i>=t; i--){
      ans[N-1-t][i] = path[idx++];
    }
    for(int i=N-2-t; i>=1+t; i--){
      ans[i][t] = path[idx++];
    }

    memset(memo, false, sizeof(memo));

    for(int i=0; i<path.size(); i++){
      int idx = path[i];
      for(int j=0; j<N0; j++){
        if(a[j] == idx){
          cnt[b[j]]--;
        }else if(b[j] == idx){
          cnt[a[j]]--;
        }
      }
    }

    // for(int i=0; i<N0; i++){
    //   cout << cnt[i] << " ";
    // }
    // cout << endl;
  }
  if(N%2 == 1){
    int tmp = 0;
    REP(i,N*N){
      if(!used[i]) tmp = i;
    }

    ans[N/2][N/2] = tmp;
  }

      for(int i=0; i<N; i++){
      for(int j=0; j<N; j++){
        if(j) cout <<  " ";
        cout << ans[i][j]+1;
      }
      cout << endl;
    }
  return 0;
}
accuracy: 0.046511627906976744
time_ms: 90
memory_kb: 3844
final_rank: 19

code_snippet: #include <random>
#include "bits/stdc++.h"
//#include <atcoder/all>

using namespace std;
// using namespace atcoder;

using ll = long long;
using ld = long double;
using P = pair<int, string>;
constexpr ld eps = 1e-12;
constexpr int inf = numeric_limits<int>::max() / 2;
constexpr ll mod = 1e9 + 7;
mt19937_64 rnd{random_device()()};

template <typename T>
void printv(const vector<T> &v) {
  int sz = v.size();
  for (int i = 0; i < sz; i++) {
    cout << v[i] << " \n"[i == sz - 1];
  }
}

map<string, vector<string>> g_to_m;
map<string, set<string>> members;

void rec(const string &name) {
  if (members.count(name)) {
    return;
  }
  if (g_to_m.count(name) == 0) {
    members[name].insert(name);
    return;
  }
  for (auto member : g_to_m[name]) {
    rec(member);
    for (auto m : members[member]) {
      members[name].insert(m);
    }
  }
}

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

  int n;
  cin >> n;
  vector<vector<int>> ans(n, vector<int>(n));
  vector<int> usd(n * n);
  vector<vector<int>> g(n * n);
  for (int i = 0; i < 2 * n * n - 2 * n; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  int cur = -1;
  for (int i = 0; i < n * n; i++) {
    if (g[i].size() != 2) continue;
    cur = i;
    break;
  }
  int st = cur;
  ans[0][0] = cur + 1;
  usd[cur] = 1;
  for (int i = 0; i < n - 2; i++) {
    for (auto v : g[cur]) {
      if (usd[v]) continue;
      if (g[v].size() != 3) continue;
      cur = v;
      ans[0][i + 1] = cur + 1;
      usd[cur] = 1;
      break;
    }
  }
  for (auto v : g[cur]) {
    if (usd[v]) continue;
    if (g[v].size() != 2) continue;
    cur = v;
    ans[0][n - 1] = cur + 1;
    usd[cur] = 1;
    break;
  }

  cur = st;
  for (int i = 0; i < n - 2; i++) {
    for (auto v : g[cur]) {
      if (usd[v]) continue;
      if (g[v].size() != 3) continue;
      cur = v;
      ans[i + 1][0] = cur + 1;
      usd[cur] = 1;
      break;
    }
  }
  for (auto v : g[cur]) {
    if (usd[v]) continue;
    if (g[v].size() != 2) continue;
    cur = v;
    ans[n - 1][0] = cur + 1;
    usd[cur] = 1;
    break;
  }

  for (int i = 1; i < n; i++) {
    for (int j = 1; j < n; j++) {
      int up = ans[i - 1][j] - 1, left = ans[i][j - 1] - 1;
      for (int k = 0; k < n * n; k++) {
        if (usd[k]) continue;
        bool ok1 = false, ok2 = false;
        for (auto v : g[k]) {
          if (v == up) ok1 = true;
          if (v == left) ok2 = true;
        }
        if (ok1 && ok2) {
          ans[i][j] = k + 1;
          usd[k] = 1;
          break;
        }
      }
    }
  }
  for (int i = 0; i < n; i++) {
    printv(ans[i]);
  }
}
accuracy: 1
time_ms: 160
memory_kb: 3912
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long lint;
#define rep(i,n) for(lint (i)=0;(i)<(n);(i)++)
#define repp(i,m,n) for(lint (i)=(m);(i)<(n);(i)++)
#define repm(i,n) for(lint (i)=(n-1);(i)>=0;(i)--)
#define INF (1ll<<60)
#define all(x) (x).begin(),(x).end()
const lint MOD =1000000007;
const lint MAX = 1000000;
using Graph =vector<vector<lint>>;
typedef pair<lint,lint> P;
typedef map<lint,lint> M;
#define chmax(x,y) x=max(x,y)
#define chmin(x,y) x=min(x,y)

 
lint fac[MAX], finv[MAX], inv[MAX];
 
void COMinit() 
{
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (lint i = 2; i < MAX; i++)
    {
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}
 
long long COM(lint n, lint k)
{
    if (n < k)
        return 0;
    if (n < 0 || k < 0)
        return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
 
lint primary(lint num)
{
    if (num < 2) return 0;
    else if (num == 2) return 1;
    else if (num % 2 == 0) return 0;
 
    double sqrtNum = sqrt(num);
    for (int i = 3; i <= sqrtNum; i += 2)
    {
        if (num % i == 0)
        {
            return 0;
        }
    }
 
    return 1;
}
   long long modpow(long long a, long long n, long long mod) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}
    lint lcm(lint a,lint b){
        return a/__gcd(a,b)*b;
    }
     lint gcd(lint a,lint b){
        return __gcd(a,b);
    } 
   
    int main(){
        lint n;
        cin>>n;
        vector<vector<lint>> x(n*n+1);
        rep(i,2*n*n-2*n){
            lint a,b;
            cin>>a>>b;
            x[a].push_back(b);
            x[b].push_back(a);
        }
        lint h;
        rep(i,n*n)if(x[i+1].size()==2){
            h=i+1;
            break;
        }
        lint ans[n][n];
        bool isd[n*n+1];
        fill(isd,isd+n*n+1,false);
        isd[h]=true;
        ans[0][0]=h;
        ans[0][1]=x[h][0];
        ans[1][0]=x[h][1];
        isd[ans[1][0]]=true;
        isd[ans[0][1]]=true;
        rep(i,n-1){
            if(i==0){
                lint k;
                rep(j,x[ans[0][1]].size()){
                    rep(l,x[ans[1][0]].size()){
                        if(x[ans[0][1]][j]==x[ans[1][0]][l]&&!isd[x[ans[1][0]][l]])k=x[ans[1][0]][l];
                    }
                }
                ans[1][1]=k;
                isd[k]=true;
            }else{
                lint l=ans[0][i],r=ans[1][i],k;
                rep(j,x[l].size()){
                    if(!isd[x[l][j]])k=x[l][j];
                }
                ans[0][i+1]=k;
                isd[k]=true;
                l=ans[0][i+1];
                rep(j,x[l].size()){
                    rep(p,x[r].size()){
                        if(x[r][p]==x[l][j]&&!isd[x[l][j]])k=x[l][j];
                    }
                }
                ans[1][i+1]=k;
                isd[k]=true;
            }
        }
        rep(i,n-2){
            lint l;
            rep(j,n){
                lint o=ans[i+1][j];
                rep(k,x[o].size()){
                    if(!isd[x[o][k]])l=x[o][k];
                }
                 isd[l]=true;
                ans[i+2][j]=l;
            }
        }
        rep(i,n){
            rep(j,n){
                cout<<ans[i][j]<<" ";
            }
            cout<<endl;
        }
    
    }
accuracy: 1
time_ms: 10
memory_kb: 3776
final_rank: 4

code_snippet: #include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define modulo 1000000007
#define mod(mod_x) ((((long long)mod_x+modulo))%modulo)
#define Inf 1000000000

int main(){
	
	int N;
	cin>>N;
	
	vector<int> c(N*N+1,0);
	vector<pair<int,int>> X(N*N*2 - N*2);
	
	rep(i,X.size()){
		cin>>X[i].first>>X[i].second;
		c[X[i].first]++;
		c[X[i].second]++;
	}
	
	vector<vector<int>> ans(1,vector<int>(1));
	rep(i,c.size()){
		if(c[i]==2){
			ans[0][0] = i;
			break;
		}
	}
	
	while(ans[0].size()!=N){
		rep(i,X.size()){
			if(X[i].first==ans[0].back()&&c[X[i].second]<=3){
				ans[0].push_back(X[i].second);
				X.erase(X.begin()+i);
				break;
			}
			if(X[i].second==ans[0].back()&&c[X[i].first]<=3){
				ans[0].push_back(X[i].first);
				X.erase(X.begin()+i);
				break;
			}
		}
	}
	
	for(int i=1;i<N;i++){
		ans.push_back(vector<int>());
		rep(j,N){
			rep(k,X.size()){
				if(X[k].first==ans[i-1][j]){
					ans.back().push_back(X[k].second);
					X.erase(X.begin()+k);
					break;
				}
				if(X[k].second==ans[i-1][j]){
					ans.back().push_back(X[k].first);
					X.erase(X.begin()+k);
					break;
				}
			}
			
			if(j!=0){
				rep(k,X.size()){
					if(X[k].first==ans[i][j]&&X[k].second==ans[i][j-1]){
						X.erase(X.begin()+k);
						break;
					}
					if(X[k].first==ans[i][j-1]&&X[k].second==ans[i][j]){
						X.erase(X.begin()+k);
						break;
					}
				}
			}
			
			
		}
	}
	
	rep(i,N){
		rep(j,N){
			if(j!=0)cout<<' ';
			cout<<ans[i][j];
		}
		cout<<endl;
	}
	
    return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 3536
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using P = pair<int,int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vll = vector<ll>;
using vvll = vector<vll>;

#define REP(i,n) for (int i=0; i<(n); ++i)
#define RREP(i,n) for (int i=(int)(n)-1; i>=0; --i)
#define FOR(i,a,n) for (int i=(a); i<(n); ++i)
#define RFOR(i,a,n) for (int i=(int)(n)-1; i>=(a); --i)

#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(),(x).end()

#define DUMP(x) cerr<<#x<<" = "<<(x)<<endl
#define DEBUG(x) cerr<<#x<<" = "<<(x)<<" (L"<<__LINE__<<")"<<endl;

template<typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    os << "[";
    REP (i, SZ(v)) {
        if (i) os << ", ";
        os << v[i];
    }
    return os << "]";
}

template<typename T, typename U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
    return os << "(" << p.first << " " << p.second << ")";
}

template<class T>
bool chmax(T& a, const T& b) {
    if (a < b) { a = b; return true; }
    return false;
}
template<class T>
bool chmin(T& a, const T& b) {
    if (b < a) { a = b; return true; }
    return false;
}

const ll MOD = 1e9+7;
const ld EPS = 1e-9;
const int INF = INT_MAX / 2;
const ll LINF = LLONG_MAX / 2;

template<typename T>
struct edge {
    int src, to;
    T cost;

    friend ostream &operator<<(ostream &os, const edge &e) {
        return os << "(" << e.src << "->" << e.to << ":" << e.cost << ")";
    }
};

template<typename T>
using Graph = vector<vector<edge<T>>>;

template<int64_t mod>
class modint {
    int64_t x;

public:
    modint(int64_t x = 0) : x(x < 0 ? ((x % mod) + mod) % mod : x % mod) {}

    const modint operator-() const { return x ? mod - x : 0; }

    modint &operator+=(const modint &rhs) {
        if ((x += rhs.x) >= mod) x -= mod;
        return *this;
    }

    modint &operator-=(const modint &rhs) {
        return *this += -rhs;
    }

    modint &operator*=(const modint &rhs) {
        (x *= rhs.x) %= mod;
        return *this;
    }

    modint &operator/=(const modint &rhs) {
        return *this *= rhs.pow(mod - 2);
    }

    friend const modint operator+(modint lhs, const modint &rhs) {
        return lhs += rhs;
    }

    friend const modint operator-(modint lhs, const modint &rhs) {
        return lhs -= rhs;
    }

    friend const modint operator*(modint lhs, const modint &rhs) {
        return lhs *= rhs;
    }

    friend const modint operator/(modint lhs, const modint &rhs) {
        return lhs /= rhs;
    }

    const modint pow(int64_t n) const {
        modint ret = 1, tmp = *this;
        while (n) {
            if (n & 1) ret *= tmp;
            tmp *= tmp;
            n >>= 1;
        }
        return ret;
    }

    friend bool operator==(const modint &lhs, const modint &rhs) {
        return lhs.x == rhs.x;
    }

    friend bool operator!=(const modint &lhs, const modint &rhs) {
        return !(lhs == rhs);
    }

    friend ostream &operator<<(ostream &os, const modint &a) {
        return os << a.x;
    }

    friend istream &operator>>(istream &is, modint &a) {
        int64_t tmp;
        is >> tmp;
        a = tmp;
        return is;
    }
};


int main() {
    //cin.tie(0);
    //ios::sync_with_stdio(false);
    //cout << fixed << setprecision(10);

    int n; cin >> n;
    vvi g(n * n);
    REP(i, 2 * n * (n - 1)) {
        int a, b; cin >> a >> b; --a, --b;
        g[a].push_back(b);
        g[b].push_back(a);
    }

    vvi ans(n, vi(n, -1));
    vi used(n * n);

    REP(i, n * n) {
        if (SZ(g[i]) == 2) {
            ans[0][0] = i;
            used[i] = 1;
            break;
        }
    }

    REP(i, n - 1) {
        int nxt = -1;
        for (int id : g[ans[0][i]]) {
            if (SZ(g[id]) <= 3 and !used[id]) {
                nxt = id;
                break;
            }
        }
        assert(nxt != -1);
        ans[0][i + 1] = nxt;
        used[nxt] = 1;
    }

    REP(i, n - 1) {
        REP(j, n) {
            for (int id : g[ans[i][j]]) {
                if (!used[id]) {
                    ans[i + 1][j] = id;
                    used[id] = 1;
                    break;
                }
            }
        }
    }

    REP(i, n) {
        REP(j, n) {
            if (j) cout << ' ';
            cout << ans[i][j] + 1;
        }
        cout << endl;
    }


    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3672
final_rank: 2