#!/usr/bin/python3
from collections import defaultdict
N, M = map(int, input().split())
conn = defaultdict(list)
for _ in range(M):
a, b = map(int, input().split())
conn[a].append(b)
conn[b].append(a)
def make_subtree(n, tree, only_path):
dep = 1
for i in conn[n]:
if i not in tree and not any(j in conn[i] for j in tree[n]):
tree[n].append(i)
tree[i] = []
dep = max(dep, make_subtree(i, tree, only_path) + 1)
if only_path:
break
return dep
def make_tree():
best_dep = (N+1, N+1)
best_tree = None
for root in range(N):
for subroot in conn[root]:
tree = defaultdict(list)
tree[root] = [subroot]
tree[subroot] = []
tree_dep = make_subtree(root, tree, False)
path_dep = make_subtree(subroot, tree, True)
if len(tree) == N and (path_dep, tree_dep) < best_dep:
best_dep = (path_dep, tree_dep)
best_tree = root, subroot, tree, tree_dep, path_dep
return best_tree
root, subroot, tree, tree_dep, path_dep = make_tree()
if tree_dep + path_dep == N:
def get_path(n, skip):
for i in tree[n]:
if i != skip:
return [n] + get_path(i, None)
return [n]
path = list(reversed(get_path(subroot, None))) + get_path(root, subroot)
even = list(range(N))
odd = list(range(N))
for i in range(N // 2):
even[path[2*i]] = even[path[2*i+1]]
if 2*i+2 < N:
odd[path[2*i+1]] = even[path[2*i+2]]
base_moves = [even, odd]
else:
even = list(range(N))
odd = list(range(N))
def gen_moves(n, depth):
if depth % 2 == 0:
even[n] = n
else:
odd[n] = n
for i in tree[n]:
if depth % 2 == 0:
even[i] = n
else:
odd[i] = n
gen_moves(i, depth + 1)
gen_moves(root, 0)
gen_moves(subroot, 0)
odd[subroot] = root
even[subroot] = subroot
def end_of_path(n, par):
for i in tree[n]:
return end_of_path(i, n)
return n, par
end1, end2 = end_of_path(subroot, root)
odd[end1] = end2
even[end1] = end2
base_moves = [even, odd]
def get_next(n, move):
res = set()
for i in conn[n]:
if move[i] == move[n]:
res.add(i)
if not res:
res = {n}
return res
pos = {(a, b) for a in range(N) for b in range(a)}
moves = []
while pos:
move = base_moves[len(moves) % len(base_moves)]
moves.append(move)
npos = set()
for a, b in pos:
na = get_next(a, move)
nb = get_next(b, move)
for aa in na:
for bb in nb:
if aa != bb and not (aa == b and bb == a):
npos.add((aa,bb))
pos = npos
print(len(moves))
for move in moves:
print(' '.join(map(str, move)))
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
18748 KB |
Output is correct |
2 |
Correct |
27 ms |
18536 KB |
Output is correct |
3 |
Correct |
27 ms |
18636 KB |
Output is correct |
4 |
Correct |
68 ms |
23548 KB |
Output is correct |
5 |
Correct |
65 ms |
23344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
18744 KB |
Output is correct |
2 |
Correct |
29 ms |
18672 KB |
Output is correct |
3 |
Correct |
31 ms |
18748 KB |
Output is correct |
4 |
Correct |
45 ms |
21780 KB |
Output is correct |
5 |
Correct |
62 ms |
22320 KB |
Output is correct |
6 |
Correct |
112 ms |
26160 KB |
Output is correct |
7 |
Correct |
139 ms |
27184 KB |
Output is correct |
8 |
Correct |
223 ms |
27952 KB |
Output is correct |
9 |
Correct |
664 ms |
31112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
18740 KB |
Output is correct |
2 |
Correct |
29 ms |
18740 KB |
Output is correct |
3 |
Correct |
46 ms |
18740 KB |
Output is correct |
4 |
Correct |
86 ms |
26728 KB |
Output is correct |
5 |
Correct |
157 ms |
28204 KB |
Output is correct |
6 |
Correct |
148 ms |
28356 KB |
Output is correct |
7 |
Correct |
135 ms |
30100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
18748 KB |
Output is correct |
2 |
Correct |
27 ms |
18536 KB |
Output is correct |
3 |
Correct |
27 ms |
18636 KB |
Output is correct |
4 |
Correct |
68 ms |
23548 KB |
Output is correct |
5 |
Correct |
65 ms |
23344 KB |
Output is correct |
6 |
Correct |
29 ms |
18740 KB |
Output is correct |
7 |
Correct |
29 ms |
18740 KB |
Output is correct |
8 |
Correct |
46 ms |
18740 KB |
Output is correct |
9 |
Correct |
86 ms |
26728 KB |
Output is correct |
10 |
Correct |
157 ms |
28204 KB |
Output is correct |
11 |
Correct |
148 ms |
28356 KB |
Output is correct |
12 |
Correct |
135 ms |
30100 KB |
Output is correct |
13 |
Correct |
35 ms |
18740 KB |
Output is correct |
14 |
Correct |
29 ms |
18780 KB |
Output is correct |
15 |
Correct |
28 ms |
18740 KB |
Output is correct |
16 |
Correct |
70 ms |
23600 KB |
Output is correct |
17 |
Correct |
84 ms |
23556 KB |
Output is correct |
18 |
Correct |
29 ms |
18788 KB |
Output is correct |
19 |
Correct |
31 ms |
18692 KB |
Output is correct |
20 |
Correct |
86 ms |
26672 KB |
Output is correct |
21 |
Correct |
195 ms |
28132 KB |
Output is correct |
22 |
Correct |
157 ms |
28452 KB |
Output is correct |
23 |
Correct |
129 ms |
30080 KB |
Output is correct |
24 |
Correct |
29 ms |
18768 KB |
Output is correct |
25 |
Correct |
31 ms |
18864 KB |
Output is correct |
26 |
Correct |
38 ms |
19036 KB |
Output is correct |
27 |
Correct |
149 ms |
27104 KB |
Output is correct |
28 |
Correct |
186 ms |
28444 KB |
Output is correct |
29 |
Correct |
207 ms |
28976 KB |
Output is correct |
30 |
Correct |
112 ms |
26904 KB |
Output is correct |
31 |
Correct |
188 ms |
28020 KB |
Output is correct |
32 |
Correct |
208 ms |
31608 KB |
Output is correct |
33 |
Correct |
192 ms |
28660 KB |
Output is correct |
34 |
Correct |
212 ms |
29900 KB |
Output is correct |
35 |
Correct |
199 ms |
28044 KB |
Output is correct |
36 |
Correct |
202 ms |
28136 KB |
Output is correct |
37 |
Correct |
82 ms |
26416 KB |
Output is correct |
38 |
Correct |
146 ms |
27692 KB |
Output is correct |
39 |
Correct |
178 ms |
28208 KB |
Output is correct |
40 |
Correct |
187 ms |
29164 KB |
Output is correct |
41 |
Correct |
130 ms |
27680 KB |
Output is correct |
42 |
Correct |
107 ms |
26672 KB |
Output is correct |
43 |
Correct |
109 ms |
26920 KB |
Output is correct |
44 |
Correct |
211 ms |
28524 KB |
Output is correct |
45 |
Correct |
183 ms |
28460 KB |
Output is correct |
46 |
Correct |
30 ms |
18788 KB |
Output is correct |
47 |
Correct |
29 ms |
18628 KB |
Output is correct |
48 |
Correct |
34 ms |
18740 KB |
Output is correct |
49 |
Correct |
28 ms |
18644 KB |
Output is correct |
50 |
Correct |
29 ms |
18716 KB |
Output is correct |
51 |
Correct |
28 ms |
18744 KB |
Output is correct |
52 |
Correct |
33 ms |
18736 KB |
Output is correct |
53 |
Correct |
28 ms |
18688 KB |
Output is correct |
54 |
Correct |
28 ms |
18740 KB |
Output is correct |
55 |
Correct |
27 ms |
18716 KB |
Output is correct |
56 |
Correct |
31 ms |
18748 KB |
Output is correct |
57 |
Correct |
28 ms |
18748 KB |
Output is correct |
58 |
Correct |
38 ms |
18740 KB |
Output is correct |
59 |
Correct |
41 ms |
18740 KB |
Output is correct |
60 |
Correct |
29 ms |
18748 KB |
Output is correct |
61 |
Correct |
39 ms |
18744 KB |
Output is correct |
62 |
Correct |
27 ms |
18632 KB |
Output is correct |
63 |
Correct |
28 ms |
18740 KB |
Output is correct |
64 |
Correct |
31 ms |
18740 KB |
Output is correct |
65 |
Correct |
37 ms |
18740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
18748 KB |
Output is correct |
2 |
Correct |
27 ms |
18536 KB |
Output is correct |
3 |
Correct |
27 ms |
18636 KB |
Output is correct |
4 |
Correct |
68 ms |
23548 KB |
Output is correct |
5 |
Correct |
65 ms |
23344 KB |
Output is correct |
6 |
Correct |
28 ms |
18744 KB |
Output is correct |
7 |
Correct |
29 ms |
18672 KB |
Output is correct |
8 |
Correct |
31 ms |
18748 KB |
Output is correct |
9 |
Correct |
45 ms |
21780 KB |
Output is correct |
10 |
Correct |
62 ms |
22320 KB |
Output is correct |
11 |
Correct |
112 ms |
26160 KB |
Output is correct |
12 |
Correct |
139 ms |
27184 KB |
Output is correct |
13 |
Correct |
223 ms |
27952 KB |
Output is correct |
14 |
Correct |
664 ms |
31112 KB |
Output is correct |
15 |
Correct |
29 ms |
18740 KB |
Output is correct |
16 |
Correct |
29 ms |
18740 KB |
Output is correct |
17 |
Correct |
46 ms |
18740 KB |
Output is correct |
18 |
Correct |
86 ms |
26728 KB |
Output is correct |
19 |
Correct |
157 ms |
28204 KB |
Output is correct |
20 |
Correct |
148 ms |
28356 KB |
Output is correct |
21 |
Correct |
135 ms |
30100 KB |
Output is correct |
22 |
Correct |
35 ms |
18740 KB |
Output is correct |
23 |
Correct |
29 ms |
18780 KB |
Output is correct |
24 |
Correct |
28 ms |
18740 KB |
Output is correct |
25 |
Correct |
70 ms |
23600 KB |
Output is correct |
26 |
Correct |
84 ms |
23556 KB |
Output is correct |
27 |
Correct |
29 ms |
18788 KB |
Output is correct |
28 |
Correct |
31 ms |
18692 KB |
Output is correct |
29 |
Correct |
86 ms |
26672 KB |
Output is correct |
30 |
Correct |
195 ms |
28132 KB |
Output is correct |
31 |
Correct |
157 ms |
28452 KB |
Output is correct |
32 |
Correct |
129 ms |
30080 KB |
Output is correct |
33 |
Correct |
29 ms |
18768 KB |
Output is correct |
34 |
Correct |
31 ms |
18864 KB |
Output is correct |
35 |
Correct |
38 ms |
19036 KB |
Output is correct |
36 |
Correct |
149 ms |
27104 KB |
Output is correct |
37 |
Correct |
186 ms |
28444 KB |
Output is correct |
38 |
Correct |
207 ms |
28976 KB |
Output is correct |
39 |
Correct |
112 ms |
26904 KB |
Output is correct |
40 |
Correct |
188 ms |
28020 KB |
Output is correct |
41 |
Correct |
208 ms |
31608 KB |
Output is correct |
42 |
Correct |
192 ms |
28660 KB |
Output is correct |
43 |
Correct |
212 ms |
29900 KB |
Output is correct |
44 |
Correct |
199 ms |
28044 KB |
Output is correct |
45 |
Correct |
202 ms |
28136 KB |
Output is correct |
46 |
Correct |
82 ms |
26416 KB |
Output is correct |
47 |
Correct |
146 ms |
27692 KB |
Output is correct |
48 |
Correct |
178 ms |
28208 KB |
Output is correct |
49 |
Correct |
187 ms |
29164 KB |
Output is correct |
50 |
Correct |
130 ms |
27680 KB |
Output is correct |
51 |
Correct |
107 ms |
26672 KB |
Output is correct |
52 |
Correct |
109 ms |
26920 KB |
Output is correct |
53 |
Correct |
211 ms |
28524 KB |
Output is correct |
54 |
Correct |
183 ms |
28460 KB |
Output is correct |
55 |
Correct |
30 ms |
18788 KB |
Output is correct |
56 |
Correct |
29 ms |
18628 KB |
Output is correct |
57 |
Correct |
34 ms |
18740 KB |
Output is correct |
58 |
Correct |
28 ms |
18644 KB |
Output is correct |
59 |
Correct |
29 ms |
18716 KB |
Output is correct |
60 |
Correct |
28 ms |
18744 KB |
Output is correct |
61 |
Correct |
33 ms |
18736 KB |
Output is correct |
62 |
Correct |
28 ms |
18688 KB |
Output is correct |
63 |
Correct |
28 ms |
18740 KB |
Output is correct |
64 |
Correct |
27 ms |
18716 KB |
Output is correct |
65 |
Correct |
31 ms |
18748 KB |
Output is correct |
66 |
Correct |
28 ms |
18748 KB |
Output is correct |
67 |
Correct |
38 ms |
18740 KB |
Output is correct |
68 |
Correct |
41 ms |
18740 KB |
Output is correct |
69 |
Correct |
29 ms |
18748 KB |
Output is correct |
70 |
Correct |
39 ms |
18744 KB |
Output is correct |
71 |
Correct |
27 ms |
18632 KB |
Output is correct |
72 |
Correct |
28 ms |
18740 KB |
Output is correct |
73 |
Correct |
31 ms |
18740 KB |
Output is correct |
74 |
Correct |
37 ms |
18740 KB |
Output is correct |
75 |
Correct |
32 ms |
18740 KB |
Output is correct |
76 |
Correct |
28 ms |
18740 KB |
Output is correct |
77 |
Correct |
30 ms |
18780 KB |
Output is correct |
78 |
Correct |
66 ms |
23344 KB |
Output is correct |
79 |
Correct |
71 ms |
23388 KB |
Output is correct |
80 |
Correct |
30 ms |
18656 KB |
Output is correct |
81 |
Correct |
29 ms |
18740 KB |
Output is correct |
82 |
Correct |
60 ms |
21760 KB |
Output is correct |
83 |
Correct |
52 ms |
22288 KB |
Output is correct |
84 |
Correct |
105 ms |
26152 KB |
Output is correct |
85 |
Correct |
151 ms |
27180 KB |
Output is correct |
86 |
Correct |
185 ms |
27952 KB |
Output is correct |
87 |
Correct |
592 ms |
30924 KB |
Output is correct |
88 |
Correct |
31 ms |
18648 KB |
Output is correct |
89 |
Correct |
30 ms |
18704 KB |
Output is correct |
90 |
Correct |
91 ms |
26584 KB |
Output is correct |
91 |
Correct |
164 ms |
27892 KB |
Output is correct |
92 |
Correct |
190 ms |
28300 KB |
Output is correct |
93 |
Correct |
112 ms |
29960 KB |
Output is correct |
94 |
Correct |
28 ms |
18748 KB |
Output is correct |
95 |
Correct |
28 ms |
18740 KB |
Output is correct |
96 |
Correct |
33 ms |
18992 KB |
Output is correct |
97 |
Correct |
130 ms |
26928 KB |
Output is correct |
98 |
Correct |
185 ms |
28608 KB |
Output is correct |
99 |
Correct |
186 ms |
29232 KB |
Output is correct |
100 |
Correct |
109 ms |
26704 KB |
Output is correct |
101 |
Correct |
160 ms |
27952 KB |
Output is correct |
102 |
Correct |
193 ms |
31536 KB |
Output is correct |
103 |
Correct |
189 ms |
28848 KB |
Output is correct |
104 |
Correct |
212 ms |
30000 KB |
Output is correct |
105 |
Correct |
186 ms |
28208 KB |
Output is correct |
106 |
Correct |
184 ms |
27984 KB |
Output is correct |
107 |
Correct |
87 ms |
26412 KB |
Output is correct |
108 |
Correct |
143 ms |
27676 KB |
Output is correct |
109 |
Correct |
182 ms |
28356 KB |
Output is correct |
110 |
Correct |
180 ms |
29232 KB |
Output is correct |
111 |
Correct |
132 ms |
27440 KB |
Output is correct |
112 |
Correct |
104 ms |
26668 KB |
Output is correct |
113 |
Correct |
109 ms |
26992 KB |
Output is correct |
114 |
Correct |
272 ms |
28460 KB |
Output is correct |
115 |
Correct |
195 ms |
28148 KB |
Output is correct |
116 |
Correct |
28 ms |
18684 KB |
Output is correct |
117 |
Correct |
30 ms |
18688 KB |
Output is correct |
118 |
Correct |
28 ms |
18636 KB |
Output is correct |
119 |
Correct |
37 ms |
18740 KB |
Output is correct |
120 |
Correct |
35 ms |
18584 KB |
Output is correct |
121 |
Correct |
28 ms |
18784 KB |
Output is correct |
122 |
Correct |
35 ms |
18740 KB |
Output is correct |
123 |
Correct |
28 ms |
18584 KB |
Output is correct |
124 |
Correct |
30 ms |
18596 KB |
Output is correct |
125 |
Correct |
35 ms |
18756 KB |
Output is correct |
126 |
Correct |
31 ms |
18740 KB |
Output is correct |
127 |
Correct |
28 ms |
18660 KB |
Output is correct |
128 |
Correct |
29 ms |
18740 KB |
Output is correct |
129 |
Correct |
29 ms |
18712 KB |
Output is correct |
130 |
Correct |
33 ms |
18664 KB |
Output is correct |
131 |
Correct |
31 ms |
18804 KB |
Output is correct |
132 |
Correct |
30 ms |
18568 KB |
Output is correct |
133 |
Correct |
43 ms |
18656 KB |
Output is correct |
134 |
Correct |
30 ms |
18720 KB |
Output is correct |
135 |
Correct |
29 ms |
18776 KB |
Output is correct |
136 |
Correct |
36 ms |
19544 KB |
Output is correct |
137 |
Correct |
35 ms |
19716 KB |
Output is correct |
138 |
Correct |
38 ms |
20300 KB |
Output is correct |
139 |
Correct |
32 ms |
19232 KB |
Output is correct |
140 |
Correct |
208 ms |
28572 KB |
Output is correct |
141 |
Correct |
28 ms |
18648 KB |
Output is correct |
142 |
Correct |
702 ms |
32684 KB |
Output is correct |
143 |
Correct |
744 ms |
32544 KB |
Output is correct |
144 |
Correct |
1451 ms |
30836 KB |
Output is correct |
145 |
Correct |
519 ms |
32400 KB |
Output is correct |
146 |
Correct |
354 ms |
29760 KB |
Output is correct |
147 |
Correct |
226 ms |
28508 KB |
Output is correct |
148 |
Correct |
795 ms |
28980 KB |
Output is correct |
149 |
Correct |
508 ms |
29244 KB |
Output is correct |
150 |
Correct |
338 ms |
30000 KB |
Output is correct |
151 |
Correct |
284 ms |
29340 KB |
Output is correct |
152 |
Correct |
192 ms |
28304 KB |
Output is correct |
153 |
Correct |
164 ms |
29048 KB |
Output is correct |
154 |
Correct |
577 ms |
30636 KB |
Output is correct |
155 |
Correct |
134 ms |
26880 KB |
Output is correct |
156 |
Correct |
213 ms |
28720 KB |
Output is correct |
157 |
Correct |
87 ms |
25392 KB |
Output is correct |
158 |
Correct |
217 ms |
28720 KB |
Output is correct |
159 |
Correct |
297 ms |
29488 KB |
Output is correct |
160 |
Correct |
200 ms |
28672 KB |
Output is correct |
161 |
Correct |
157 ms |
27952 KB |
Output is correct |
162 |
Correct |
32 ms |
18788 KB |
Output is correct |
163 |
Correct |
36 ms |
19500 KB |
Output is correct |
164 |
Correct |
30 ms |
18740 KB |
Output is correct |
165 |
Correct |
31 ms |
18740 KB |
Output is correct |