File size: 4,337 Bytes
1fd0050
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <bits/stdc++.h>
using namespace std;

vector<vector<int>> create_map(int N, int M, vector<int> A, vector<int> B) {
  vector<set<int>> adj(N + 1);
  for (int i = 0; i < M; i++) {
    int a = A[i], b = B[i];
    adj[a].insert(b);
    adj[b].insert(a);
  }
  int root = 1;
  vector<vector<int>> tree(N + 1);
  vector<bool> visited(N + 1, false);
  vector<int> parent(N + 1, -1);
  queue<int> qq;
  qq.push(root);
  visited[root] = true;
  while (!qq.empty()) {
    int u = qq.front();
    qq.pop();
    for (int v : adj[u]) {
      if (!visited[v]) {
        visited[v] = true;
        parent[v] = u;
        tree[u].push_back(v);
        qq.push(v);
      }
    }
  }
  vector<int> sub_height(N + 1), sub_width(N + 1);
  function<void(int, int)> comp_size = [&](int u, int p) {
    int numc = tree[u].size();
    int maxch = 0;
    int sumcw = 0;
    int nsep = max(0, numc - 1);
    for (int v : tree[u]) {
      comp_size(v, u);
      maxch = max(maxch, sub_height[v]);
      sumcw += sub_width[v];
    }
    int hchild = (numc == 0 ? 0 : maxch);
    sub_height[u] = 3 + hchild;
    int wchild = (numc == 0 ? 0 : sumcw + nsep);
    int deg = adj[u].size();
    int wblock = (deg == 0 ? 1 : 3 * deg);
    sub_width[u] = max(wblock, wchild);
  };
  comp_size(root, -1);
  int toth = sub_height[root];
  int totw = sub_width[root];
  int K = max(toth, totw);
  vector<vector<int>> C(K, vector<int>(K, 0));
  function<void(int, int, int)> do_place = [&](int u, int r0, int c0) {
    int sw = sub_width[u];
    int deg = adj[u].size();
    int wb = (deg == 0 ? 1 : 3 * deg);
    for (int i = 0; i < 3; i++) {
      int rr = r0 + i;
      if (rr >= K) continue;
      for (int j = 0; j < sw; j++) {
        int cc = c0 + j;
        if (cc >= K) break;
        C[rr][cc] = u;
      }
    }
    vector<int> neigh(adj[u].begin(), adj[u].end());
    int d = neigh.size();
    int pstart = c0;
    for (int k = 0; k < d; k++) {
      int v = neigh[k];
      int pc = pstart + 3 * k + 1;
      if (pc < c0 + sw && pc < K) {
        int rr = r0 + 1;
        if (rr < K) C[rr][pc] = v;
      }
    }
    auto& children = tree[u];
    int numc = children.size();
    if (numc == 0) return;
    int r1 = r0 + 3;
    if (r1 >= K) return;
    int maxch = 0;
    for (int v : children) maxch = max(maxch, sub_height[v]);
    vector<int> ch_starts(numc);
    int this_c = c0;
    int wchild_actual = 0;
    for (int i = 0; i < numc; i++) {
      int v = children[i];
      ch_starts[i] = this_c;
      do_place(v, r1, this_c);
      this_c += sub_width[v];
      wchild_actual += sub_width[v];
      if (i < numc - 1) {
        int sep_c = this_c;
        for (int rr = r1; rr < r1 + maxch && rr < K; rr++) {
          if (sep_c < K) C[rr][sep_c] = u;
        }
        this_c++;
        wchild_actual++;
      }
    }
    for (int i = 0; i < numc; i++) {
      int v = children[i];
      int startc = ch_starts[i];
      int endc = startc + sub_width[v] - 1;
      int child_h = sub_height[v];
      int gap_start_r = r1 + child_h;
      for (int rr = gap_start_r; rr < r1 + maxch && rr < K; rr++) {
        for (int cc = startc; cc <= endc && cc < K; cc++) {
          C[rr][cc] = v;
        }
      }
    }
    int children_end_c = c0 + wchild_actual;
    for (int rr = r1; rr < r1 + maxch && rr < K; rr++) {
      for (int cc = children_end_c; cc < c0 + sw && cc < K; cc++) {
        C[rr][cc] = u;
      }
    }
  };
  do_place(root, 0, 0, C);
  if (toth < K) {
    for (int r = toth; r < K; r++) {
      for (int j = 0; j < K; j++) {
        C[r][j] = C[toth - 1][j];
      }
    }
  }
  if (totw < K) {
    for (int j = totw; j < K; j++) {
      for (int i = 0; i < K; i++) {
        C[i][j] = C[i][totw - 1];
      }
    }
  }
  return C;
}

int main() {
  int T;
  cin >> T;
  for (int t = 0; t < T; t++) {
    int N, M;
    cin >> N >> M;
    vector<int> AA(M), BB(M);
    for (int i = 0; i < M; i++) {
      cin >> AA[i] >> BB[i];
    }
    auto cmap = create_map(N, M, AA, BB);
    int KK = cmap.size();
    cout << KK << endl;
    for (int q = 0; q < KK; q++) {
      if (q > 0) cout << " ";
      cout << KK;
    }
    cout << endl;
    cout << endl;
    for (int i = 0; i < KK; i++) {
      for (int j = 0; j < KK; j++) {
        if (j > 0) cout << " ";
        cout << cmap[i][j];
      }
      cout << endl;
    }
  }
  return 0;
}