message stringlengths 2 49.9k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 446 108k | cluster float64 13 13 | __index_level_0__ int64 892 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's call an undirected graph G = (V, E) relatively prime if and only if for each edge (v, u) ∈ E GCD(v, u) = 1 (the greatest common divisor of v and u is 1). If there is no edge between some pair of vertices v and u then the value of GCD(v, u) doesn't matter. The vertices are numbered from 1 to |V|.
Construct a relatively prime graph with n vertices and m edges such that it is connected and it contains neither self-loops nor multiple edges.
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
If there are multiple answers then print any of them.
Input
The only line contains two integers n and m (1 ≤ n, m ≤ 10^5) — the number of vertices and the number of edges.
Output
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
Otherwise print the answer in the following format:
The first line should contain the word "Possible".
The i-th of the next m lines should contain the i-th edge (v_i, u_i) of the resulting graph (1 ≤ v_i, u_i ≤ n, v_i ≠ u_i). For each pair (v, u) there can be no more pairs (v, u) or (u, v). The vertices are numbered from 1 to n.
If there are multiple answers then print any of them.
Examples
Input
5 6
Output
Possible
2 5
3 2
5 1
3 4
4 1
5 4
Input
6 12
Output
Impossible
Note
Here is the representation of the graph from the first example: <image> | instruction | 0 | 11,624 | 13 | 23,248 |
Tags: brute force, constructive algorithms, graphs, greedy, math
Correct Solution:
```
import time
from copy import deepcopy
import itertools
from bisect import bisect_left
from bisect import bisect_right
import math
from collections import deque
from collections import Counter
def read():
return int(input())
def readmap():
return map(int, input().split())
def readlist():
return list(map(int, input().split()))
def make_relatively_prime_list(n, m):
tree = []
for i in range(2, n+1):
tree.append((i, 1))
cnt = n - 1
new_leaf = [(2, 1)]
while cnt < m:
# tree.extend(new_leaf)
leaf = deepcopy(new_leaf)
new_leaf = []
if not leaf:
break
for (x, y) in leaf:
if max(2 * x - y, x) <= n:
# tree.append((2 * x - y, x))
new_leaf.append((2 * x - y, x))
if x > 1:
tree.append((2 * x - y, x))
cnt += 1
if cnt < m and max(2 * x + y, x) <= n:
# tree.append((2 * x + y, x))
new_leaf.append((2 * x + y, x))
if x > 1:
tree.append((2 * x + y, x))
cnt += 1
if cnt < m and max(x + 2 * y, y) <= n:
# tree.append((x + 2 * y, y))
new_leaf.append((x + 2 * y, y))
if y > 1:
tree.append((x + 2 * y, y))
cnt += 1
if n == 2:
return tree
new_leaf = [(3, 1)]
while cnt < m:
# tree.extend(new_leaf)
leaf = deepcopy(new_leaf)
new_leaf = []
if not leaf:
break
for (x, y) in leaf:
if max(2 * x - y, x) <= n:
# tree.append((2 * x - y, x))
new_leaf.append((2 * x - y, x))
if x > 1:
tree.append((2 * x - y, x))
cnt += 1
if cnt < m and max(2 * x + y, x) <= n:
# tree.append((2 * x + y, x))
new_leaf.append((2 * x + y, x))
if x > 1:
tree.append((2 * x + y, x))
cnt += 1
if cnt < m and max(x + 2 * y, y) <= n:
# tree.append((x + 2 * y, y))
new_leaf.append((x + 2 * y, y))
if y > 1:
tree.append((x + 2 * y, y))
cnt += 1
return tree
n, m = readmap()
if n == 1 or m < n - 1:
print("Impossible")
quit()
tree = make_relatively_prime_list(n, m)
if len(tree) < m:
print("Impossible")
else:
print("Possible")
for i in range(m):
print(tree[i][0], tree[i][1])
``` | output | 1 | 11,624 | 13 | 23,249 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's call an undirected graph G = (V, E) relatively prime if and only if for each edge (v, u) ∈ E GCD(v, u) = 1 (the greatest common divisor of v and u is 1). If there is no edge between some pair of vertices v and u then the value of GCD(v, u) doesn't matter. The vertices are numbered from 1 to |V|.
Construct a relatively prime graph with n vertices and m edges such that it is connected and it contains neither self-loops nor multiple edges.
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
If there are multiple answers then print any of them.
Input
The only line contains two integers n and m (1 ≤ n, m ≤ 10^5) — the number of vertices and the number of edges.
Output
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
Otherwise print the answer in the following format:
The first line should contain the word "Possible".
The i-th of the next m lines should contain the i-th edge (v_i, u_i) of the resulting graph (1 ≤ v_i, u_i ≤ n, v_i ≠ u_i). For each pair (v, u) there can be no more pairs (v, u) or (u, v). The vertices are numbered from 1 to n.
If there are multiple answers then print any of them.
Examples
Input
5 6
Output
Possible
2 5
3 2
5 1
3 4
4 1
5 4
Input
6 12
Output
Impossible
Note
Here is the representation of the graph from the first example: <image> | instruction | 0 | 11,625 | 13 | 23,250 |
Tags: brute force, constructive algorithms, graphs, greedy, math
Correct Solution:
```
from itertools import islice
def get_all(n):
q = [(0, 1, 1, 1)]
while q:
a, b, c, d = q.pop()
e, f = a + c, b + d
if f <= n:
yield e, f
q.append((a, b, e, f))
q.append((e, f, c, d))
def solve(n, m):
x = list(islice(get_all(n), m))
if len(x) == m >= n - 1:
return x
res = solve(*map(int, input().split()))
if res is not None:
print('Possible')
for e in res: print(*e)
else:
print('Impossible')
``` | output | 1 | 11,625 | 13 | 23,251 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call an undirected graph G = (V, E) relatively prime if and only if for each edge (v, u) ∈ E GCD(v, u) = 1 (the greatest common divisor of v and u is 1). If there is no edge between some pair of vertices v and u then the value of GCD(v, u) doesn't matter. The vertices are numbered from 1 to |V|.
Construct a relatively prime graph with n vertices and m edges such that it is connected and it contains neither self-loops nor multiple edges.
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
If there are multiple answers then print any of them.
Input
The only line contains two integers n and m (1 ≤ n, m ≤ 10^5) — the number of vertices and the number of edges.
Output
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
Otherwise print the answer in the following format:
The first line should contain the word "Possible".
The i-th of the next m lines should contain the i-th edge (v_i, u_i) of the resulting graph (1 ≤ v_i, u_i ≤ n, v_i ≠ u_i). For each pair (v, u) there can be no more pairs (v, u) or (u, v). The vertices are numbered from 1 to n.
If there are multiple answers then print any of them.
Examples
Input
5 6
Output
Possible
2 5
3 2
5 1
3 4
4 1
5 4
Input
6 12
Output
Impossible
Note
Here is the representation of the graph from the first example: <image>
Submitted Solution:
```
from math import gcd
n, m = map(int, input().split())
edges = []
cnt = 0
if m < n - 1:
print('Impossible')
else:
for i in range(1, n + 1):
for j in range(i + 1, n + 1):
if gcd(i, j) == 1:
edges.append((i, j))
cnt += 1
if cnt == m:
break
if cnt == m:
break
if cnt != m:
print('Impossible')
else:
print('Possible')
for pair in edges:
print(*pair)
``` | instruction | 0 | 11,626 | 13 | 23,252 |
Yes | output | 1 | 11,626 | 13 | 23,253 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call an undirected graph G = (V, E) relatively prime if and only if for each edge (v, u) ∈ E GCD(v, u) = 1 (the greatest common divisor of v and u is 1). If there is no edge between some pair of vertices v and u then the value of GCD(v, u) doesn't matter. The vertices are numbered from 1 to |V|.
Construct a relatively prime graph with n vertices and m edges such that it is connected and it contains neither self-loops nor multiple edges.
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
If there are multiple answers then print any of them.
Input
The only line contains two integers n and m (1 ≤ n, m ≤ 10^5) — the number of vertices and the number of edges.
Output
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
Otherwise print the answer in the following format:
The first line should contain the word "Possible".
The i-th of the next m lines should contain the i-th edge (v_i, u_i) of the resulting graph (1 ≤ v_i, u_i ≤ n, v_i ≠ u_i). For each pair (v, u) there can be no more pairs (v, u) or (u, v). The vertices are numbered from 1 to n.
If there are multiple answers then print any of them.
Examples
Input
5 6
Output
Possible
2 5
3 2
5 1
3 4
4 1
5 4
Input
6 12
Output
Impossible
Note
Here is the representation of the graph from the first example: <image>
Submitted Solution:
```
from fractions import gcd
import sys
n,m = list(map(int, input().split()))
if m<n-1:
print("Impossible")
sys.exit(0)
fnd = 0
res = ""
for i in range(1, n+1):
for j in range(i+1, n+1):
if gcd(i,j) == 1:
fnd+=1
res += "%d %d\n" % (i,j)
if fnd==m:
print("Possible")
print(res)
sys.exit(0)
print("Impossible")
``` | instruction | 0 | 11,627 | 13 | 23,254 |
Yes | output | 1 | 11,627 | 13 | 23,255 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call an undirected graph G = (V, E) relatively prime if and only if for each edge (v, u) ∈ E GCD(v, u) = 1 (the greatest common divisor of v and u is 1). If there is no edge between some pair of vertices v and u then the value of GCD(v, u) doesn't matter. The vertices are numbered from 1 to |V|.
Construct a relatively prime graph with n vertices and m edges such that it is connected and it contains neither self-loops nor multiple edges.
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
If there are multiple answers then print any of them.
Input
The only line contains two integers n and m (1 ≤ n, m ≤ 10^5) — the number of vertices and the number of edges.
Output
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
Otherwise print the answer in the following format:
The first line should contain the word "Possible".
The i-th of the next m lines should contain the i-th edge (v_i, u_i) of the resulting graph (1 ≤ v_i, u_i ≤ n, v_i ≠ u_i). For each pair (v, u) there can be no more pairs (v, u) or (u, v). The vertices are numbered from 1 to n.
If there are multiple answers then print any of them.
Examples
Input
5 6
Output
Possible
2 5
3 2
5 1
3 4
4 1
5 4
Input
6 12
Output
Impossible
Note
Here is the representation of the graph from the first example: <image>
Submitted Solution:
```
from math import gcd
n,m = list(map(int,input().split()))
# arr = []
# prime = [True for i in range(n+1)]
# for i in range(2,len(prime)):
# if prime[i]==True:
# for j in range(i*i,len(prime),i):
# prime[j] = False
# for i in range(2,len(prime)):
# if prime[i]:
# arr.append(i)
# cnt = 10**5
# edges = []
# for i in range(2,n+1):
# edges.append([1,i])
# cnt-=1
# for i in range(2,n+1):
# if prime[i]==True:
# for j in arr:
# if j>i:
# edges.append([i,j])
# cnt-=1
# if cnt==0:
# break
# else:
# for j in arr:
# if gcd(i,j)==1:
# edges.append([i,j])
# cnt-=1
# if cnt==0:
# break
# if cnt==0:
# break
# print(len(edges),len(arr))
edges = []
p = m
for i in range(1,n+1):
for j in range(i+1,n+1):
if gcd(i,j)==1:
m-=1
edges.append([i,j])
if m==0:
break
if m==0:
break
if len(edges)>=p and p>=n-1:
print("Possible")
for i in range(len(edges)):
print(*edges[i])
else:
print("Impossible")
``` | instruction | 0 | 11,628 | 13 | 23,256 |
Yes | output | 1 | 11,628 | 13 | 23,257 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call an undirected graph G = (V, E) relatively prime if and only if for each edge (v, u) ∈ E GCD(v, u) = 1 (the greatest common divisor of v and u is 1). If there is no edge between some pair of vertices v and u then the value of GCD(v, u) doesn't matter. The vertices are numbered from 1 to |V|.
Construct a relatively prime graph with n vertices and m edges such that it is connected and it contains neither self-loops nor multiple edges.
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
If there are multiple answers then print any of them.
Input
The only line contains two integers n and m (1 ≤ n, m ≤ 10^5) — the number of vertices and the number of edges.
Output
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
Otherwise print the answer in the following format:
The first line should contain the word "Possible".
The i-th of the next m lines should contain the i-th edge (v_i, u_i) of the resulting graph (1 ≤ v_i, u_i ≤ n, v_i ≠ u_i). For each pair (v, u) there can be no more pairs (v, u) or (u, v). The vertices are numbered from 1 to n.
If there are multiple answers then print any of them.
Examples
Input
5 6
Output
Possible
2 5
3 2
5 1
3 4
4 1
5 4
Input
6 12
Output
Impossible
Note
Here is the representation of the graph from the first example: <image>
Submitted Solution:
```
from math import gcd
n, m = map(int, input().split())
a = []
for i in range(1, n):
for j in range(i+1, n+1):
if gcd(i, j) == 1:
a.append([i, j])
if len(a) == m:
break
if len(a) == m:
break
if m < n-1 or len(a) != m:
print("Impossible")
else:
print("Possible")
for x in a:
print(x[0], x[1])
``` | instruction | 0 | 11,629 | 13 | 23,258 |
Yes | output | 1 | 11,629 | 13 | 23,259 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call an undirected graph G = (V, E) relatively prime if and only if for each edge (v, u) ∈ E GCD(v, u) = 1 (the greatest common divisor of v and u is 1). If there is no edge between some pair of vertices v and u then the value of GCD(v, u) doesn't matter. The vertices are numbered from 1 to |V|.
Construct a relatively prime graph with n vertices and m edges such that it is connected and it contains neither self-loops nor multiple edges.
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
If there are multiple answers then print any of them.
Input
The only line contains two integers n and m (1 ≤ n, m ≤ 10^5) — the number of vertices and the number of edges.
Output
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
Otherwise print the answer in the following format:
The first line should contain the word "Possible".
The i-th of the next m lines should contain the i-th edge (v_i, u_i) of the resulting graph (1 ≤ v_i, u_i ≤ n, v_i ≠ u_i). For each pair (v, u) there can be no more pairs (v, u) or (u, v). The vertices are numbered from 1 to n.
If there are multiple answers then print any of them.
Examples
Input
5 6
Output
Possible
2 5
3 2
5 1
3 4
4 1
5 4
Input
6 12
Output
Impossible
Note
Here is the representation of the graph from the first example: <image>
Submitted Solution:
```
from sys import stdin
from collections import deque
mod = 10**9 + 7
# def rl():
# return [int(w) for w in stdin.readline().split()]
from bisect import bisect_right
from bisect import bisect_left
from collections import defaultdict
from math import sqrt,factorial,gcd,log2,inf,ceil
# map(int,input().split())
# # l = list(map(int,input().split()))
# from itertools import permutations
import heapq
# la = []
# s = list(input())
# n = len(s)
#
# i = 0
# ans = []
# while i<n:
# # print(i)
# if s[i] == '1':
# cnt1 = 0
# cnt2 = 0
# while s[i] == '1' or s[i] == '0':
# if s[i] == '1':
# cnt2+=1
# if s[i] == '0':
# cnt1+=1
#
# i+=1
# if i == n:
# break
#
# for x in range(cnt1):
# ans.append('0')
# for y in range(cnt2):
# ans.append('1')
#
# if i == n:
# break
# elif s[i] == '2':
# cnt1 = 0
# cnt2 = 0
# cnt3 = 0
# st = -1
# ba = -1
# while s[i] == '2' or s[i] == '1' or s[i] == '0':
# if s[i] == '2':
# cnt2+=1
# elif s[i] == '1':
# cnt1+=1
# ba = i
# else:
# if ba == -1:
# st = i
# cnt3+=1
# else:
# break
#
#
# i+=1
# if i == n:
# break
#
# for x in range(cnt1):
# ans.append('1')
# for y in range(cnt2):
# ans.append('2')
# for y in range(cnt3):
# ans.append('0')
#
# if i == n:
# break
#
#
#
# else:
# ans.append('0')
# i+=1
#
#
#
#
# print(''.join(ans))
def check_prime(n):
for i in range(2,int(sqrt(n))+1):
if n%i == 0:
return False
return True
ba = []
for i in range(2,10**5):
if check_prime(i):
ba.append(i)
if len(ba) == 1000:
break
n,m = map(int,input().split())
hash = defaultdict(set)
cnt = n-1
for i in range(1,n):
hash[i].add(i+1)
for i in range(3,n+1):
hash[1].add(i)
cnt+=1
if n<=1000:
for i in range(2,n+1):
for j in range(i+1,n+1):
if gcd(i,j) == 1:
if j not in hash[i]:
hash[i].add(j)
cnt+=1
ha = 0
seti = set()
ans = []
if cnt>=m:
for z in hash:
for k in hash[z]:
ans.append((z,k))
ha+=1
seti.add(z)
seti.add(k)
if ha == m:
if len(seti) == n:
break
else:
print('Impossible')
exit()
if ha == m:
break
print('Possible')
for i in ans:
print(*i)
else:
print('Impossible')
else:
for i in range(len(ba)):
for j in range(i+1,len(ba)):
if ba[j] not in hash[ba[i]]:
hash[ba[i]].add(ba[j])
cnt+=1
ha = 0
seti = set()
ans = []
if cnt>=m:
for z in hash:
for k in hash[z]:
ans.append((z,k))
ha+=1
seti.add(z)
seti.add(k)
if ha == m:
if len(seti) == n:
break
else:
print('Impossible')
exit()
if ha == m:
break
print('Possible')
for i in ans:
print(*i)
else:
print('Impossible')
``` | instruction | 0 | 11,630 | 13 | 23,260 |
No | output | 1 | 11,630 | 13 | 23,261 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call an undirected graph G = (V, E) relatively prime if and only if for each edge (v, u) ∈ E GCD(v, u) = 1 (the greatest common divisor of v and u is 1). If there is no edge between some pair of vertices v and u then the value of GCD(v, u) doesn't matter. The vertices are numbered from 1 to |V|.
Construct a relatively prime graph with n vertices and m edges such that it is connected and it contains neither self-loops nor multiple edges.
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
If there are multiple answers then print any of them.
Input
The only line contains two integers n and m (1 ≤ n, m ≤ 10^5) — the number of vertices and the number of edges.
Output
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
Otherwise print the answer in the following format:
The first line should contain the word "Possible".
The i-th of the next m lines should contain the i-th edge (v_i, u_i) of the resulting graph (1 ≤ v_i, u_i ≤ n, v_i ≠ u_i). For each pair (v, u) there can be no more pairs (v, u) or (u, v). The vertices are numbered from 1 to n.
If there are multiple answers then print any of them.
Examples
Input
5 6
Output
Possible
2 5
3 2
5 1
3 4
4 1
5 4
Input
6 12
Output
Impossible
Note
Here is the representation of the graph from the first example: <image>
Submitted Solution:
```
from math import gcd
def main():
n, m = map(int, input().split())
if not 1 < n <= m + 1 <= n * (n + 1) // 2:
m = 0
res = ['Possible']
for a in range(2, min(n + 1, m + 2)):
res.append('1 %d' % a)
m -= min(n - 1, m)
if m:
for a in range(n, 2, -1):
for b in range(a - 1, 1, a % 2 - 2):
if gcd(a, b) == 1:
res.append('%d %d' % (b, a))
m -= 1
if not m:
break
else:
continue
break
print('Impossible' if m else '\n'.join(res))
if __name__ == '__main__':
main()
``` | instruction | 0 | 11,631 | 13 | 23,262 |
No | output | 1 | 11,631 | 13 | 23,263 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call an undirected graph G = (V, E) relatively prime if and only if for each edge (v, u) ∈ E GCD(v, u) = 1 (the greatest common divisor of v and u is 1). If there is no edge between some pair of vertices v and u then the value of GCD(v, u) doesn't matter. The vertices are numbered from 1 to |V|.
Construct a relatively prime graph with n vertices and m edges such that it is connected and it contains neither self-loops nor multiple edges.
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
If there are multiple answers then print any of them.
Input
The only line contains two integers n and m (1 ≤ n, m ≤ 10^5) — the number of vertices and the number of edges.
Output
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
Otherwise print the answer in the following format:
The first line should contain the word "Possible".
The i-th of the next m lines should contain the i-th edge (v_i, u_i) of the resulting graph (1 ≤ v_i, u_i ≤ n, v_i ≠ u_i). For each pair (v, u) there can be no more pairs (v, u) or (u, v). The vertices are numbered from 1 to n.
If there are multiple answers then print any of them.
Examples
Input
5 6
Output
Possible
2 5
3 2
5 1
3 4
4 1
5 4
Input
6 12
Output
Impossible
Note
Here is the representation of the graph from the first example: <image>
Submitted Solution:
```
from copy import deepcopy
nm = list(map(int, input(' ').split(' ')))
n = nm[0]
m = nm[1]
max = 0
min = n-1
edges = [[] for i in range(n-1)]
def euler(a):
result = deepcopy(a)
i = 2
while i*i <= a:
if a % i == 0:
while a % i == 0:
a /= i
result -= result / i
i += 1
if a > 1:
result -= result / a
return int(result)
def euclid(a, b):
if b > 0:
return euclid(b, a % b)
else:
return a
if m < n-1:
print('Impossible')
exit()
for i in range(2, n+1):
max += euler(i)
for j in range(i, 1, -1):
if euclid(i, j) == 1:
edges[i-2].append(j)
if m > max:
print('Impossible')
exit()
ans = []
m -= n-1
for i in range(n-1):
ans.append([i, 1])
cur = 0
while (m != 0) and (cur < len(edges)):
for i in range(len(edges[cur])):
ans.append([cur+2, edges[cur][i]])
m -= 1
if m == 0:
break
cur += 1
print('Possible\n')
for i in range(len(ans)):
for j in range(2):
print(ans[i][j], ' ', end ='')
print()
``` | instruction | 0 | 11,632 | 13 | 23,264 |
No | output | 1 | 11,632 | 13 | 23,265 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call an undirected graph G = (V, E) relatively prime if and only if for each edge (v, u) ∈ E GCD(v, u) = 1 (the greatest common divisor of v and u is 1). If there is no edge between some pair of vertices v and u then the value of GCD(v, u) doesn't matter. The vertices are numbered from 1 to |V|.
Construct a relatively prime graph with n vertices and m edges such that it is connected and it contains neither self-loops nor multiple edges.
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
If there are multiple answers then print any of them.
Input
The only line contains two integers n and m (1 ≤ n, m ≤ 10^5) — the number of vertices and the number of edges.
Output
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
Otherwise print the answer in the following format:
The first line should contain the word "Possible".
The i-th of the next m lines should contain the i-th edge (v_i, u_i) of the resulting graph (1 ≤ v_i, u_i ≤ n, v_i ≠ u_i). For each pair (v, u) there can be no more pairs (v, u) or (u, v). The vertices are numbered from 1 to n.
If there are multiple answers then print any of them.
Examples
Input
5 6
Output
Possible
2 5
3 2
5 1
3 4
4 1
5 4
Input
6 12
Output
Impossible
Note
Here is the representation of the graph from the first example: <image>
Submitted Solution:
```
from math import gcd
n,m = list(map(int,input().split()))
arr = []
prime = [True for i in range(n+1)]
for i in range(2,len(prime)):
if prime[i]==True:
for j in range(i*i,len(prime),i):
prime[j] = False
for i in range(2,len(prime)):
if prime[i]:
arr.append(i)
cnt = m
edges = []
arr = [1]+arr
for i in range(1,i+1):
for j in range(1,n+1):
if j>i and i!=j and gcd(i,j)==1:
edges.append([i,j])
cnt-=1
if cnt==0:
break
if cnt==0:
break
print(len(edges))
if len(edges)>=m and m>=n-1:
print("Possible")
for i in range(len(edges)):
print(*edges[i])
else:
print("Impossible")
``` | instruction | 0 | 11,633 | 13 | 23,266 |
No | output | 1 | 11,633 | 13 | 23,267 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a node of the tree with index 1 and with weight 0. Let cnt be the number of nodes in the tree at any instant (initially, cnt is set to 1). Support Q queries of following two types:
* <image> Add a new node (index cnt + 1) with weight W and add edge between node R and this node.
* <image> Output the maximum length of sequence of nodes which
1. starts with R.
2. Every node in the sequence is an ancestor of its predecessor.
3. Sum of weight of nodes in sequence does not exceed X.
4. For some nodes i, j that are consecutive in the sequence if i is an ancestor of j then w[i] ≥ w[j] and there should not exist a node k on simple path from i to j such that w[k] ≥ w[j]
The tree is rooted at node 1 at any instant.
Note that the queries are given in a modified way.
Input
First line containing the number of queries Q (1 ≤ Q ≤ 400000).
Let last be the answer for previous query of type 2 (initially last equals 0).
Each of the next Q lines contains a query of following form:
* 1 p q (1 ≤ p, q ≤ 1018): This is query of first type where <image> and <image>. It is guaranteed that 1 ≤ R ≤ cnt and 0 ≤ W ≤ 109.
* 2 p q (1 ≤ p, q ≤ 1018): This is query of second type where <image> and <image>. It is guaranteed that 1 ≤ R ≤ cnt and 0 ≤ X ≤ 1015.
<image> denotes bitwise XOR of a and b.
It is guaranteed that at least one query of type 2 exists.
Output
Output the answer to each query of second type in separate line.
Examples
Input
6
1 1 1
2 2 0
2 2 1
1 3 0
2 2 0
2 2 2
Output
0
1
1
2
Input
6
1 1 0
2 2 0
2 0 3
1 0 2
2 1 3
2 1 6
Output
2
2
3
2
Input
7
1 1 2
1 2 3
2 3 3
1 0 0
1 5 1
2 5 0
2 4 0
Output
1
1
2
Input
7
1 1 3
1 2 3
2 3 4
1 2 0
1 5 3
2 5 5
2 7 22
Output
1
2
3
Note
In the first example,
last = 0
- Query 1: 1 1 1, Node 2 with weight 1 is added to node 1.
- Query 2: 2 2 0, No sequence of nodes starting at 2 has weight less than or equal to 0. last = 0
- Query 3: 2 2 1, Answer is 1 as sequence will be {2}. last = 1
- Query 4: 1 2 1, Node 3 with weight 1 is added to node 2.
- Query 5: 2 3 1, Answer is 1 as sequence will be {3}. Node 2 cannot be added as sum of weights cannot be greater than 1. last = 1
- Query 6: 2 3 3, Answer is 2 as sequence will be {3, 2}. last = 2
Submitted Solution:
```
from bisect import bisect_right
def sol(Q):
last = 0
weight = [0]
parent = [0]
# id -> cumulated weight for each possible length
# (computed lazyly)
lss = {1 : [0]}
def get_ls(idx):
todo = []
i = idx
while i not in lss:
todo.append(i)
i=parent[i-1]
break
for i in reversed(todo):
cur = weight[i-1]
ls = [cur]
p = parent[i-1]
while p>0:
w = weight[p-1]
if w >= cur:
ls.append(cur+w)
cur = w
p = parent[p-1]
lss[i] = ls
return lss[idx]
for q, r, w in Q:
r ^= last
w ^= last
if q == 1:
parent.append(r)
weight.append(w)
else:
ls = get_ls(r)
last = bisect_right(ls, w)
print(last)
if 0:
n = int(input())
Q = []
for _ in range(n):
Q.append(map(int, input().strip().split()))
else:
Q = [(1,1,1),(2,2,0),(2,2,1),(1,3,0),(2,2,0),(2,2,2)]
#Q = [(1,1,0),(2,2,0),(2,0,3),(1,0,2),(2,1,3),(2,1,6)]
sol(Q)
``` | instruction | 0 | 12,259 | 13 | 24,518 |
No | output | 1 | 12,259 | 13 | 24,519 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight <image> (where <image> is the [bitwise-xor operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR)). Can you find the weight of the minimum spanning tree of that graph?
You can read about complete graphs in <https://en.wikipedia.org/wiki/Complete_graph>
You can read about the minimum spanning tree in <https://en.wikipedia.org/wiki/Minimum_spanning_tree>
The weight of the minimum spanning tree is the sum of the weights on the edges included in it.
Input
The only line contains an integer n (2 ≤ n ≤ 1012), the number of vertices in the graph.
Output
The only line contains an integer x, the weight of the graph's minimum spanning tree.
Example
Input
4
Output
4
Note
In the first sample: <image> The weight of the minimum spanning tree is 1+2+1=4. | instruction | 0 | 12,260 | 13 | 24,520 |
Tags: bitmasks, dp, graphs, implementation, math
Correct Solution:
```
f = lambda x: f(x // 2) * 2 + (x + 1) // 2 if x else 0
print(f(int(input()) - 1))
``` | output | 1 | 12,260 | 13 | 24,521 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight <image> (where <image> is the [bitwise-xor operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR)). Can you find the weight of the minimum spanning tree of that graph?
You can read about complete graphs in <https://en.wikipedia.org/wiki/Complete_graph>
You can read about the minimum spanning tree in <https://en.wikipedia.org/wiki/Minimum_spanning_tree>
The weight of the minimum spanning tree is the sum of the weights on the edges included in it.
Input
The only line contains an integer n (2 ≤ n ≤ 1012), the number of vertices in the graph.
Output
The only line contains an integer x, the weight of the graph's minimum spanning tree.
Example
Input
4
Output
4
Note
In the first sample: <image> The weight of the minimum spanning tree is 1+2+1=4. | instruction | 0 | 12,261 | 13 | 24,522 |
Tags: bitmasks, dp, graphs, implementation, math
Correct Solution:
```
def slow_cal(x):
if x == 0:
return 0
else:
return slow_cal(x - 1) + min(x ^ v for v in range(x))
def med_cal(x):
if x == 0:
return 0
return sum(min(i ^ j for j in range(i)) for i in range(1, x+1))
def fast_cal(x):
res = 0
for bit in range(50):
res += (x//(1 << (bit)) - x//(1 << (bit + 1)))*(1 << bit)
return res
"""
for i in range(700):
assert fast_cal(i) == slow_cal(i) == med_cal(i)
print("ALL_CORRECT")
"""
n = int(input()) - 1
print(fast_cal(n))
``` | output | 1 | 12,261 | 13 | 24,523 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight <image> (where <image> is the [bitwise-xor operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR)). Can you find the weight of the minimum spanning tree of that graph?
You can read about complete graphs in <https://en.wikipedia.org/wiki/Complete_graph>
You can read about the minimum spanning tree in <https://en.wikipedia.org/wiki/Minimum_spanning_tree>
The weight of the minimum spanning tree is the sum of the weights on the edges included in it.
Input
The only line contains an integer n (2 ≤ n ≤ 1012), the number of vertices in the graph.
Output
The only line contains an integer x, the weight of the graph's minimum spanning tree.
Example
Input
4
Output
4
Note
In the first sample: <image> The weight of the minimum spanning tree is 1+2+1=4. | instruction | 0 | 12,262 | 13 | 24,524 |
Tags: bitmasks, dp, graphs, implementation, math
Correct Solution:
```
n = int(input())
ans = 0
_pow = 2
while 2 * n > _pow:
ans += (_pow //2 )* (n // _pow + (1 if n % _pow > _pow // 2 else 0))
_pow *= 2
print(ans)
``` | output | 1 | 12,262 | 13 | 24,525 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight <image> (where <image> is the [bitwise-xor operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR)). Can you find the weight of the minimum spanning tree of that graph?
You can read about complete graphs in <https://en.wikipedia.org/wiki/Complete_graph>
You can read about the minimum spanning tree in <https://en.wikipedia.org/wiki/Minimum_spanning_tree>
The weight of the minimum spanning tree is the sum of the weights on the edges included in it.
Input
The only line contains an integer n (2 ≤ n ≤ 1012), the number of vertices in the graph.
Output
The only line contains an integer x, the weight of the graph's minimum spanning tree.
Example
Input
4
Output
4
Note
In the first sample: <image> The weight of the minimum spanning tree is 1+2+1=4. | instruction | 0 | 12,263 | 13 | 24,526 |
Tags: bitmasks, dp, graphs, implementation, math
Correct Solution:
```
from math import log
##n = int(input())
##
####print(n-1 + int(log(n-1)/log(2)))
##
##def ord2(n): ## max power of 2 that divides n
## ret = 1
##
## while n%ret == 0:
## ret*=2
##
## return ret//2
##
##total = 0
##
##for i in range(1, n):
## total += ord2(i)
# fast way?
n = int(input()) - 1
divider = 2**int(log(n)/log(2))
total = 0
doubled = 0
while divider > 0:
total += (n//divider - doubled)*divider
##print('A total of', n//divider, 'work, and the number of doubled', doubled)
doubled += n//divider - doubled
divider //=2
## total = 0
## power = 0
##
## while 2**power <= n:
##
## total += n//(2**power)
##
## power += 1
print(total)
``` | output | 1 | 12,263 | 13 | 24,527 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight <image> (where <image> is the [bitwise-xor operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR)). Can you find the weight of the minimum spanning tree of that graph?
You can read about complete graphs in <https://en.wikipedia.org/wiki/Complete_graph>
You can read about the minimum spanning tree in <https://en.wikipedia.org/wiki/Minimum_spanning_tree>
The weight of the minimum spanning tree is the sum of the weights on the edges included in it.
Input
The only line contains an integer n (2 ≤ n ≤ 1012), the number of vertices in the graph.
Output
The only line contains an integer x, the weight of the graph's minimum spanning tree.
Example
Input
4
Output
4
Note
In the first sample: <image> The weight of the minimum spanning tree is 1+2+1=4. | instruction | 0 | 12,264 | 13 | 24,528 |
Tags: bitmasks, dp, graphs, implementation, math
Correct Solution:
```
n=int(input())-1;a=0
for i in range(0,40):
k=1<<i;a+=k*((n+(k<<1)-k)//(k<<1))
print(a)
``` | output | 1 | 12,264 | 13 | 24,529 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight <image> (where <image> is the [bitwise-xor operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR)). Can you find the weight of the minimum spanning tree of that graph?
You can read about complete graphs in <https://en.wikipedia.org/wiki/Complete_graph>
You can read about the minimum spanning tree in <https://en.wikipedia.org/wiki/Minimum_spanning_tree>
The weight of the minimum spanning tree is the sum of the weights on the edges included in it.
Input
The only line contains an integer n (2 ≤ n ≤ 1012), the number of vertices in the graph.
Output
The only line contains an integer x, the weight of the graph's minimum spanning tree.
Example
Input
4
Output
4
Note
In the first sample: <image> The weight of the minimum spanning tree is 1+2+1=4. | instruction | 0 | 12,265 | 13 | 24,530 |
Tags: bitmasks, dp, graphs, implementation, math
Correct Solution:
```
def func(n):
if n==0:
return 0
if n==1:
return 1
if(n%2):
return func(n//2)*2+(n//2)+1
else:
return func(n//2)*2+(n//2)
n=int(input())
a=[1,2,1,4,1,2,1,8,1,2,1,4,1,2,1]
print(func(n-1))
``` | output | 1 | 12,265 | 13 | 24,531 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight <image> (where <image> is the [bitwise-xor operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR)). Can you find the weight of the minimum spanning tree of that graph?
You can read about complete graphs in <https://en.wikipedia.org/wiki/Complete_graph>
You can read about the minimum spanning tree in <https://en.wikipedia.org/wiki/Minimum_spanning_tree>
The weight of the minimum spanning tree is the sum of the weights on the edges included in it.
Input
The only line contains an integer n (2 ≤ n ≤ 1012), the number of vertices in the graph.
Output
The only line contains an integer x, the weight of the graph's minimum spanning tree.
Example
Input
4
Output
4
Note
In the first sample: <image> The weight of the minimum spanning tree is 1+2+1=4. | instruction | 0 | 12,266 | 13 | 24,532 |
Tags: bitmasks, dp, graphs, implementation, math
Correct Solution:
```
import math
n = int(input())
ans = 0
cur = 1
while cur < n:
cnt = math.ceil((n-cur)/(cur << 1))
ans += cnt*cur
cur <<= 1
print(ans)
``` | output | 1 | 12,266 | 13 | 24,533 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight <image> (where <image> is the [bitwise-xor operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR)). Can you find the weight of the minimum spanning tree of that graph?
You can read about complete graphs in <https://en.wikipedia.org/wiki/Complete_graph>
You can read about the minimum spanning tree in <https://en.wikipedia.org/wiki/Minimum_spanning_tree>
The weight of the minimum spanning tree is the sum of the weights on the edges included in it.
Input
The only line contains an integer n (2 ≤ n ≤ 1012), the number of vertices in the graph.
Output
The only line contains an integer x, the weight of the graph's minimum spanning tree.
Example
Input
4
Output
4
Note
In the first sample: <image> The weight of the minimum spanning tree is 1+2+1=4. | instruction | 0 | 12,267 | 13 | 24,534 |
Tags: bitmasks, dp, graphs, implementation, math
Correct Solution:
```
n=int(input())-1;a=0
for i in range(40):
k=1<<i;a+=(n+k)//(k<<1)*k
print(a)
``` | output | 1 | 12,267 | 13 | 24,535 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight <image> (where <image> is the [bitwise-xor operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR)). Can you find the weight of the minimum spanning tree of that graph?
You can read about complete graphs in <https://en.wikipedia.org/wiki/Complete_graph>
You can read about the minimum spanning tree in <https://en.wikipedia.org/wiki/Minimum_spanning_tree>
The weight of the minimum spanning tree is the sum of the weights on the edges included in it.
Input
The only line contains an integer n (2 ≤ n ≤ 1012), the number of vertices in the graph.
Output
The only line contains an integer x, the weight of the graph's minimum spanning tree.
Example
Input
4
Output
4
Note
In the first sample: <image> The weight of the minimum spanning tree is 1+2+1=4.
Submitted Solution:
```
L=input().split();n=int(L[0])-1;k=0
while (1<<k)<n:
k += 1
ans = 0
for i in range(0, k+1):
ans+=(1<<i)*((n+(1<<(i+1))-(1<<i))//(1<<(i+1)))
print(ans)
``` | instruction | 0 | 12,268 | 13 | 24,536 |
Yes | output | 1 | 12,268 | 13 | 24,537 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight <image> (where <image> is the [bitwise-xor operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR)). Can you find the weight of the minimum spanning tree of that graph?
You can read about complete graphs in <https://en.wikipedia.org/wiki/Complete_graph>
You can read about the minimum spanning tree in <https://en.wikipedia.org/wiki/Minimum_spanning_tree>
The weight of the minimum spanning tree is the sum of the weights on the edges included in it.
Input
The only line contains an integer n (2 ≤ n ≤ 1012), the number of vertices in the graph.
Output
The only line contains an integer x, the weight of the graph's minimum spanning tree.
Example
Input
4
Output
4
Note
In the first sample: <image> The weight of the minimum spanning tree is 1+2+1=4.
Submitted Solution:
```
def f(n):
if n == 2:
return 1
if n == 3:
return 3
if n % 2 == 0:
return 2 * f(n // 2) + n // 2
else:
return 2 * f(n // 2 + 1) + n // 2
n = int(input())
print(f(n))
``` | instruction | 0 | 12,269 | 13 | 24,538 |
Yes | output | 1 | 12,269 | 13 | 24,539 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight <image> (where <image> is the [bitwise-xor operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR)). Can you find the weight of the minimum spanning tree of that graph?
You can read about complete graphs in <https://en.wikipedia.org/wiki/Complete_graph>
You can read about the minimum spanning tree in <https://en.wikipedia.org/wiki/Minimum_spanning_tree>
The weight of the minimum spanning tree is the sum of the weights on the edges included in it.
Input
The only line contains an integer n (2 ≤ n ≤ 1012), the number of vertices in the graph.
Output
The only line contains an integer x, the weight of the graph's minimum spanning tree.
Example
Input
4
Output
4
Note
In the first sample: <image> The weight of the minimum spanning tree is 1+2+1=4.
Submitted Solution:
```
"""
ATSTNG's ejudge Python3 solution template
(actual solution is below)
"""
import sys, queue
try:
import dev_act_ffc429465ab634 # empty file in directory
DEV = True
except:
DEV = False
def log(*s):
if DEV: print('LOG', *s)
class EJudge:
def __init__(self, problem="default", reclim=1<<30):
self.problem = problem
sys.setrecursionlimit(reclim)
def use_files(self, infile='', outfile=''):
if infile!='':
self.infile = open(infile)
sys.stdin = self.infile
if outfile!='':
self.outfile = open(outfile, 'w')
sys.stdout = self.outfile
def use_bacs_files(self):
self.use_files(self.problem+'.in', self.problem+'.out')
def get_tl(self):
while True: pass
def get_ml(self):
tmp = [[[5]*100000 for _ in range(1000)]]
while True: tmp.append([[5]*100000 for _ in range(1000)])
def get_re(self):
s = (0,)[8]
def get_wa(self, wstr='blablalblah'):
for _ in range(3): print(wstr)
exit()
class IntReader:
def __init__(self):
self.ost = queue.Queue()
def get(self):
return int(self.sget())
def sget(self):
if self.ost.empty():
for el in input().split():
self.ost.put(el)
return self.ost.get()
def release(self):
res = []
while not self.ost.empty():
res.append(self.ost.get())
return res
def tokenized(s):
""" Parses given string into tokens with default rules """
word = []
for ch in s.strip():
if ch == ' ':
if word: yield ''.join(word); word = []
elif 'a' <= ch <= 'z' or 'A' <= ch <= 'Z' or '0' <= ch <= '9':
word.append(ch)
else:
if word: yield ''.join(word); word = []
yield ch
if word: yield ''.join(word); word = []
###############################################################################
ej = EJudge( )
int_reader = IntReader()
fmap = lambda f,*l: list(map(f,*l))
parse_int = lambda: fmap(int, input().split())
# input
n = bin(int(input())-1)[2:][::-1]
log(n)
bit_full_cost = [(1 << i) * (i+1) for i in range(100)]
bit_cost = [1<<i for i in range(100)]
log(bit_cost)
ans = 0
for i in range(len(n)):
if n[i] == '1':
if i > 0: ans += bit_full_cost[i-1]
ans += bit_cost[i]
print(ans)
'''
1 2 3 4 5 6 7 8
8 7
0 5
0
'''
``` | instruction | 0 | 12,270 | 13 | 24,540 |
Yes | output | 1 | 12,270 | 13 | 24,541 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight <image> (where <image> is the [bitwise-xor operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR)). Can you find the weight of the minimum spanning tree of that graph?
You can read about complete graphs in <https://en.wikipedia.org/wiki/Complete_graph>
You can read about the minimum spanning tree in <https://en.wikipedia.org/wiki/Minimum_spanning_tree>
The weight of the minimum spanning tree is the sum of the weights on the edges included in it.
Input
The only line contains an integer n (2 ≤ n ≤ 1012), the number of vertices in the graph.
Output
The only line contains an integer x, the weight of the graph's minimum spanning tree.
Example
Input
4
Output
4
Note
In the first sample: <image> The weight of the minimum spanning tree is 1+2+1=4.
Submitted Solution:
```
import sys
import math
import collections
dy = [1, 0, -1, 0]
dx = [0, 1, 0, -1]
r = sys.stdin.readline
N = int(r())
N -= 1
i = 1
ans = N
while 2**i <= N:
ans += N//(2**i)*(2**(i-1))
i += 1
print(ans)
``` | instruction | 0 | 12,271 | 13 | 24,542 |
Yes | output | 1 | 12,271 | 13 | 24,543 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight <image> (where <image> is the [bitwise-xor operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR)). Can you find the weight of the minimum spanning tree of that graph?
You can read about complete graphs in <https://en.wikipedia.org/wiki/Complete_graph>
You can read about the minimum spanning tree in <https://en.wikipedia.org/wiki/Minimum_spanning_tree>
The weight of the minimum spanning tree is the sum of the weights on the edges included in it.
Input
The only line contains an integer n (2 ≤ n ≤ 1012), the number of vertices in the graph.
Output
The only line contains an integer x, the weight of the graph's minimum spanning tree.
Example
Input
4
Output
4
Note
In the first sample: <image> The weight of the minimum spanning tree is 1+2+1=4.
Submitted Solution:
```
n = int(input())
ans = 0
cur = 1
ecnt = 0
while cur < n:
ans += cur
cur *= 2
ecnt += 1
ans += (n - 1 - ecnt)
print(ans)
``` | instruction | 0 | 12,272 | 13 | 24,544 |
No | output | 1 | 12,272 | 13 | 24,545 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight <image> (where <image> is the [bitwise-xor operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR)). Can you find the weight of the minimum spanning tree of that graph?
You can read about complete graphs in <https://en.wikipedia.org/wiki/Complete_graph>
You can read about the minimum spanning tree in <https://en.wikipedia.org/wiki/Minimum_spanning_tree>
The weight of the minimum spanning tree is the sum of the weights on the edges included in it.
Input
The only line contains an integer n (2 ≤ n ≤ 1012), the number of vertices in the graph.
Output
The only line contains an integer x, the weight of the graph's minimum spanning tree.
Example
Input
4
Output
4
Note
In the first sample: <image> The weight of the minimum spanning tree is 1+2+1=4.
Submitted Solution:
```
n = int(input())
k = (n - 1) // 2
if (n - 1) % 2 == 1:
print(k * (k + 1) + k + 1)
else:
print(k * (k + 1))
``` | instruction | 0 | 12,273 | 13 | 24,546 |
No | output | 1 | 12,273 | 13 | 24,547 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight <image> (where <image> is the [bitwise-xor operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR)). Can you find the weight of the minimum spanning tree of that graph?
You can read about complete graphs in <https://en.wikipedia.org/wiki/Complete_graph>
You can read about the minimum spanning tree in <https://en.wikipedia.org/wiki/Minimum_spanning_tree>
The weight of the minimum spanning tree is the sum of the weights on the edges included in it.
Input
The only line contains an integer n (2 ≤ n ≤ 1012), the number of vertices in the graph.
Output
The only line contains an integer x, the weight of the graph's minimum spanning tree.
Example
Input
4
Output
4
Note
In the first sample: <image> The weight of the minimum spanning tree is 1+2+1=4.
Submitted Solution:
```
n=int(input())-1;a=0
for i in range(0,35):
k=1<<i;a+=k*((n+(k<<1)-k)//(k<<1))
print(a)
``` | instruction | 0 | 12,274 | 13 | 24,548 |
No | output | 1 | 12,274 | 13 | 24,549 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight <image> (where <image> is the [bitwise-xor operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR)). Can you find the weight of the minimum spanning tree of that graph?
You can read about complete graphs in <https://en.wikipedia.org/wiki/Complete_graph>
You can read about the minimum spanning tree in <https://en.wikipedia.org/wiki/Minimum_spanning_tree>
The weight of the minimum spanning tree is the sum of the weights on the edges included in it.
Input
The only line contains an integer n (2 ≤ n ≤ 1012), the number of vertices in the graph.
Output
The only line contains an integer x, the weight of the graph's minimum spanning tree.
Example
Input
4
Output
4
Note
In the first sample: <image> The weight of the minimum spanning tree is 1+2+1=4.
Submitted Solution:
```
li = []
size, xor = 2, 1
while size <= 10**12:
li.append((size, xor))
size *= 2
xor *= 2
n = int(input())
if n == 1:
print(0)
exit(0)
ans = 0
for i in range(len(li)):
size, xor = li[i]
if size >= n:
ans += xor
break;
if not i: ans += (n//2)
else:
prev_size, nothing = li[i-1]
ans += ((n+prev_size-1)//size) * xor
print(ans)
``` | instruction | 0 | 12,275 | 13 | 24,550 |
No | output | 1 | 12,275 | 13 | 24,551 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a set of size m with integer elements between 0 and 2^{n}-1 inclusive. Let's build an undirected graph on these integers in the following way: connect two integers x and y with an edge if and only if x \& y = 0. Here \& is the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND). Count the number of connected components in that graph.
Input
In the first line of input there are two integers n and m (0 ≤ n ≤ 22, 1 ≤ m ≤ 2^{n}).
In the second line there are m integers a_1, a_2, …, a_m (0 ≤ a_{i} < 2^{n}) — the elements of the set. All a_{i} are distinct.
Output
Print the number of connected components.
Examples
Input
2 3
1 2 3
Output
2
Input
5 5
5 19 10 20 12
Output
2
Note
Graph from first sample:
<image>
Graph from second sample:
<image> | instruction | 0 | 12,276 | 13 | 24,552 |
Tags: bitmasks, dfs and similar, dsu, graphs
Correct Solution:
```
n, m = map(int, input().split())
a = set(map(int, input().split()))
y = 2 ** n
mk = [0] * (2 * y)
cur = 0
for x in a:
if mk[x]: continue
mk[x] = 1
st = [x]
while st:
u = st.pop()
if u < y:
if not mk[y + u]:
mk[y + u] = 1
st.append(y + u)
else:
for b in range(n):
v = u | 1 << b
if u < v and not mk[v]:
mk[v] = 1
st.append(v)
v = y - 1 - (u - y)
if v in a and not mk[v]:
mk[v] = 1
st.append(v)
cur += 1
print(cur)
``` | output | 1 | 12,276 | 13 | 24,553 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a set of size m with integer elements between 0 and 2^{n}-1 inclusive. Let's build an undirected graph on these integers in the following way: connect two integers x and y with an edge if and only if x \& y = 0. Here \& is the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND). Count the number of connected components in that graph.
Input
In the first line of input there are two integers n and m (0 ≤ n ≤ 22, 1 ≤ m ≤ 2^{n}).
In the second line there are m integers a_1, a_2, …, a_m (0 ≤ a_{i} < 2^{n}) — the elements of the set. All a_{i} are distinct.
Output
Print the number of connected components.
Examples
Input
2 3
1 2 3
Output
2
Input
5 5
5 19 10 20 12
Output
2
Note
Graph from first sample:
<image>
Graph from second sample:
<image> | instruction | 0 | 12,277 | 13 | 24,554 |
Tags: bitmasks, dfs and similar, dsu, graphs
Correct Solution:
```
n, m = map(int, input().split())
a = set(map(int, input().split()))
y = 2 ** n
mk = [0] * (2 * y)
cur = 0
for x in a:
if mk[x]: continue
mk[x] = 1
st = [x]
def push(v):
if not mk[v]: mk[v] = 1; st.append(v)
while st:
u = st.pop()
if u < y:
push(y + u)
else:
for b in range(n):
v = u | 1 << b
push(v)
v = y - 1 - (u - y)
if v in a: push(v)
cur += 1
print(cur)
``` | output | 1 | 12,277 | 13 | 24,555 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a set of size m with integer elements between 0 and 2^{n}-1 inclusive. Let's build an undirected graph on these integers in the following way: connect two integers x and y with an edge if and only if x \& y = 0. Here \& is the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND). Count the number of connected components in that graph.
Input
In the first line of input there are two integers n and m (0 ≤ n ≤ 22, 1 ≤ m ≤ 2^{n}).
In the second line there are m integers a_1, a_2, …, a_m (0 ≤ a_{i} < 2^{n}) — the elements of the set. All a_{i} are distinct.
Output
Print the number of connected components.
Examples
Input
2 3
1 2 3
Output
2
Input
5 5
5 19 10 20 12
Output
2
Note
Graph from first sample:
<image>
Graph from second sample:
<image> | instruction | 0 | 12,278 | 13 | 24,556 |
Tags: bitmasks, dfs and similar, dsu, graphs
Correct Solution:
```
import sys
inp = list(map(int, sys.stdin.buffer.read().split()))
n, m = inp[:2]
a = set(inp[2:])
y = 2 ** n
mk = [0] * (2 * y)
cur = 0
for x in a:
if mk[x]: continue
mk[x] = 1
st = [x]
def push(v):
if not mk[v]: mk[v] = 1; st.append(v)
while st:
u = st.pop()
if u < y:
push(y + u)
else:
for b in range(n):
v = u | 1 << b
push(v)
v = y - 1 - (u - y)
if v in a: push(v)
cur += 1
print(cur)
``` | output | 1 | 12,278 | 13 | 24,557 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a set of size m with integer elements between 0 and 2^{n}-1 inclusive. Let's build an undirected graph on these integers in the following way: connect two integers x and y with an edge if and only if x \& y = 0. Here \& is the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND). Count the number of connected components in that graph.
Input
In the first line of input there are two integers n and m (0 ≤ n ≤ 22, 1 ≤ m ≤ 2^{n}).
In the second line there are m integers a_1, a_2, …, a_m (0 ≤ a_{i} < 2^{n}) — the elements of the set. All a_{i} are distinct.
Output
Print the number of connected components.
Examples
Input
2 3
1 2 3
Output
2
Input
5 5
5 19 10 20 12
Output
2
Note
Graph from first sample:
<image>
Graph from second sample:
<image>
Submitted Solution:
```
import math
n,m=input().strip().split(' ')
n,m=[int(n),int(m)]
arr=list(map(int,input().strip().split(' ')))
alist=[]
for i in arr:
alist.append(i)
for i in range(m):
for j in range(i+1,m):
if(arr[i]&arr[j]==0):
alist[j]=alist[i]
print(len(list(set(alist))))
``` | instruction | 0 | 12,279 | 13 | 24,558 |
No | output | 1 | 12,279 | 13 | 24,559 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a set of size m with integer elements between 0 and 2^{n}-1 inclusive. Let's build an undirected graph on these integers in the following way: connect two integers x and y with an edge if and only if x \& y = 0. Here \& is the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND). Count the number of connected components in that graph.
Input
In the first line of input there are two integers n and m (0 ≤ n ≤ 22, 1 ≤ m ≤ 2^{n}).
In the second line there are m integers a_1, a_2, …, a_m (0 ≤ a_{i} < 2^{n}) — the elements of the set. All a_{i} are distinct.
Output
Print the number of connected components.
Examples
Input
2 3
1 2 3
Output
2
Input
5 5
5 19 10 20 12
Output
2
Note
Graph from first sample:
<image>
Graph from second sample:
<image>
Submitted Solution:
```
import bisect
import collections
import copy
import functools
import heapq
import itertools
import math
import random
import re
import sys
import time
import string
from typing import List
sys.setrecursionlimit(99999)
n,m = map(int,input().split())
# st = "3 8 13 16 18 19 21 22 24 25 26 28 29 31 33 42 44 46 49 50 51 53 54 57 58 59 60 61 62 63"
s = set(map(int,input().split()))
l = (1<<n)-1
ans = 0
vis = set()
def dfs(mask):
if mask in vis:
return
vis.add(mask)
if mask in s:
s.remove(mask)
dfs(l^mask)
t = mask
while t:
dfs(t)
t=mask&(t-1)
for i in range(1,l+1):
if i in s:
ans+=1
dfs(i)
print(ans)
``` | instruction | 0 | 12,280 | 13 | 24,560 |
No | output | 1 | 12,280 | 13 | 24,561 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a set of size m with integer elements between 0 and 2^{n}-1 inclusive. Let's build an undirected graph on these integers in the following way: connect two integers x and y with an edge if and only if x \& y = 0. Here \& is the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND). Count the number of connected components in that graph.
Input
In the first line of input there are two integers n and m (0 ≤ n ≤ 22, 1 ≤ m ≤ 2^{n}).
In the second line there are m integers a_1, a_2, …, a_m (0 ≤ a_{i} < 2^{n}) — the elements of the set. All a_{i} are distinct.
Output
Print the number of connected components.
Examples
Input
2 3
1 2 3
Output
2
Input
5 5
5 19 10 20 12
Output
2
Note
Graph from first sample:
<image>
Graph from second sample:
<image>
Submitted Solution:
```
import math
n,m=input().strip().split(' ')
n,m=[int(n),int(m)]
arr=list(map(int,input().strip().split(' ')))
alist=[]
for i in arr:
alist.append(i)
for i in range(m):
for j in range(i+1,m):
if(arr[i]&arr[j]==0):
if(alist[j]==arr[j]):
alist[j]=alist[i]
else:
alist[i]=alist[j]
print(len(list(set(alist))))
``` | instruction | 0 | 12,281 | 13 | 24,562 |
No | output | 1 | 12,281 | 13 | 24,563 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a set of size m with integer elements between 0 and 2^{n}-1 inclusive. Let's build an undirected graph on these integers in the following way: connect two integers x and y with an edge if and only if x \& y = 0. Here \& is the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND). Count the number of connected components in that graph.
Input
In the first line of input there are two integers n and m (0 ≤ n ≤ 22, 1 ≤ m ≤ 2^{n}).
In the second line there are m integers a_1, a_2, …, a_m (0 ≤ a_{i} < 2^{n}) — the elements of the set. All a_{i} are distinct.
Output
Print the number of connected components.
Examples
Input
2 3
1 2 3
Output
2
Input
5 5
5 19 10 20 12
Output
2
Note
Graph from first sample:
<image>
Graph from second sample:
<image>
Submitted Solution:
```
n,m = map(int , input().split(" "))
l=list(map(int ,input().split(" ")))
nombre=0
for i in range(1,m):
if l[0] & l[1]==0:
nombre+=2
l.pop(0)
print(nombre)
``` | instruction | 0 | 12,282 | 13 | 24,564 |
No | output | 1 | 12,282 | 13 | 24,565 |
Provide a correct Python 3 solution for this coding contest problem.
Rng has a connected undirected graph with N vertices. Currently, there are M edges in the graph, and the i-th edge connects Vertices A_i and B_i.
Rng will add new edges to the graph by repeating the following operation:
* Operation: Choose u and v (u \neq v) such that Vertex v can be reached by traversing exactly three edges from Vertex u, and add an edge connecting Vertices u and v. It is not allowed to add an edge if there is already an edge connecting Vertices u and v.
Find the maximum possible number of edges that can be added.
Constraints
* 2 \leq N \leq 10^5
* 1 \leq M \leq 10^5
* 1 \leq A_i,B_i \leq N
* The graph has no self-loops or multiple edges.
* The graph is connected.
Input
Input is given from Standard Input in the following format:
N M
A_1 B_1
A_2 B_2
:
A_M B_M
Output
Find the maximum possible number of edges that can be added.
Examples
Input
6 5
1 2
2 3
3 4
4 5
5 6
Output
4
Input
5 5
1 2
2 3
3 1
5 4
5 1
Output
5 | instruction | 0 | 12,365 | 13 | 24,730 |
"Correct Solution:
```
from collections import deque
import sys
sys.setrecursionlimit(10 ** 7)
n, m = map(int, input().split())
edges = [[] for _ in range(n)]
for _ in range(m):
a, b = map(int, input().split())
edges[a-1].append(b-1)
edges[b-1].append(a-1)
colors = [0] * n
def dfs(v, color):
colors[v] = color
for to in edges[v]:
if colors[to] == color:
return False
if colors[to] == 0 and (not dfs(to, -color)):
return False
return True
if dfs(0, 1):
x = colors.count(1)
print(x * (n - x) - m)
else:
print(n * (n - 1) // 2 - m)
``` | output | 1 | 12,365 | 13 | 24,731 |
Provide a correct Python 3 solution for this coding contest problem.
Rng has a connected undirected graph with N vertices. Currently, there are M edges in the graph, and the i-th edge connects Vertices A_i and B_i.
Rng will add new edges to the graph by repeating the following operation:
* Operation: Choose u and v (u \neq v) such that Vertex v can be reached by traversing exactly three edges from Vertex u, and add an edge connecting Vertices u and v. It is not allowed to add an edge if there is already an edge connecting Vertices u and v.
Find the maximum possible number of edges that can be added.
Constraints
* 2 \leq N \leq 10^5
* 1 \leq M \leq 10^5
* 1 \leq A_i,B_i \leq N
* The graph has no self-loops or multiple edges.
* The graph is connected.
Input
Input is given from Standard Input in the following format:
N M
A_1 B_1
A_2 B_2
:
A_M B_M
Output
Find the maximum possible number of edges that can be added.
Examples
Input
6 5
1 2
2 3
3 4
4 5
5 6
Output
4
Input
5 5
1 2
2 3
3 1
5 4
5 1
Output
5 | instruction | 0 | 12,366 | 13 | 24,732 |
"Correct Solution:
```
import sys
sys.setrecursionlimit(100000)
from collections import defaultdict
G = defaultdict(list)
N, M = map(int, input().split())
visited = [[False for i in range(N)] for j in range(5)]
for i in range(M):
v,u = map(int, input().split())
G[v-1].append(u-1)
G[u-1].append(v-1)
# 頂点間に奇数長のパスがあるかどうか確認するために 2部グラフ 判定をする
COLOR = [0 for i in range(N)]
def dfs(pos, color):
global COLOR
COLOR[pos] = color
ans = True
for to in G[pos]:
if COLOR[to] == color:
return False
elif COLOR[to] == 0:
ans &= dfs(to, -color)
return ans
if dfs(0, 1):
count = 0
for i in COLOR:
if i == -1:
count += 1
another = N-count
print(count*another-M)
else:
# 2 部グラフではなかったので、完全グラフから M を引いた値を出力する
print(N*(N-1)//2-M)
``` | output | 1 | 12,366 | 13 | 24,733 |
Provide a correct Python 3 solution for this coding contest problem.
Rng has a connected undirected graph with N vertices. Currently, there are M edges in the graph, and the i-th edge connects Vertices A_i and B_i.
Rng will add new edges to the graph by repeating the following operation:
* Operation: Choose u and v (u \neq v) such that Vertex v can be reached by traversing exactly three edges from Vertex u, and add an edge connecting Vertices u and v. It is not allowed to add an edge if there is already an edge connecting Vertices u and v.
Find the maximum possible number of edges that can be added.
Constraints
* 2 \leq N \leq 10^5
* 1 \leq M \leq 10^5
* 1 \leq A_i,B_i \leq N
* The graph has no self-loops or multiple edges.
* The graph is connected.
Input
Input is given from Standard Input in the following format:
N M
A_1 B_1
A_2 B_2
:
A_M B_M
Output
Find the maximum possible number of edges that can be added.
Examples
Input
6 5
1 2
2 3
3 4
4 5
5 6
Output
4
Input
5 5
1 2
2 3
3 1
5 4
5 1
Output
5 | instruction | 0 | 12,367 | 13 | 24,734 |
"Correct Solution:
```
from collections import deque
n,m=map(int,input().split())
e=[[] for _ in range(n+1)]
d=[-1]*(n+1)
for i in range(m):
a,b=map(int,input().split())
e[a]+=[b]
e[b]+=[a]
q=deque([(1,0)])
d[1]=0
a,b=0,0
while q:
now,par=q.popleft()
for to in e[now]:
if to==par:continue
if d[to]==-1:
d[to]=(d[now]+1)%2
a+=1
q.append((to,now))
elif d[to]!=d[now]%2:b+=1
else:
print(n*(n-1)//2-m)
exit()
p=sum(d)+1
print(p*(n-p)-a-b//2)
``` | output | 1 | 12,367 | 13 | 24,735 |
Provide a correct Python 3 solution for this coding contest problem.
Rng has a connected undirected graph with N vertices. Currently, there are M edges in the graph, and the i-th edge connects Vertices A_i and B_i.
Rng will add new edges to the graph by repeating the following operation:
* Operation: Choose u and v (u \neq v) such that Vertex v can be reached by traversing exactly three edges from Vertex u, and add an edge connecting Vertices u and v. It is not allowed to add an edge if there is already an edge connecting Vertices u and v.
Find the maximum possible number of edges that can be added.
Constraints
* 2 \leq N \leq 10^5
* 1 \leq M \leq 10^5
* 1 \leq A_i,B_i \leq N
* The graph has no self-loops or multiple edges.
* The graph is connected.
Input
Input is given from Standard Input in the following format:
N M
A_1 B_1
A_2 B_2
:
A_M B_M
Output
Find the maximum possible number of edges that can be added.
Examples
Input
6 5
1 2
2 3
3 4
4 5
5 6
Output
4
Input
5 5
1 2
2 3
3 1
5 4
5 1
Output
5 | instruction | 0 | 12,368 | 13 | 24,736 |
"Correct Solution:
```
def bipartite():
color = {v: None for v in V}
stack = [(V[0], 0)]
parts = {0: [], 1:[]}
while stack:
v, c = stack.pop()
if color[v] is not None: # consistent
continue
color[v] = c
parts[c].append(v)
for u in E[v]:
if color[u] is None: # not visited yet
stack.append((u, c^1)) # paint u with different color from v's one
elif color[u] != c: # consistent
continue
else: # inconsistent
return (None, None)
return (parts[0], parts[1])
N, M = map(int, input().split())
V = range(1, N+1)
E = {v: [] for v in V}
for _ in range(M):
a, b = map(int, input().split())
E[a].append(b)
E[b].append(a)
p1, p2 = bipartite()
if p1 is None:
print(N * (N-1) // 2 - M)
else:
print(len(p1) * len(p2) - M)
``` | output | 1 | 12,368 | 13 | 24,737 |
Provide a correct Python 3 solution for this coding contest problem.
Rng has a connected undirected graph with N vertices. Currently, there are M edges in the graph, and the i-th edge connects Vertices A_i and B_i.
Rng will add new edges to the graph by repeating the following operation:
* Operation: Choose u and v (u \neq v) such that Vertex v can be reached by traversing exactly three edges from Vertex u, and add an edge connecting Vertices u and v. It is not allowed to add an edge if there is already an edge connecting Vertices u and v.
Find the maximum possible number of edges that can be added.
Constraints
* 2 \leq N \leq 10^5
* 1 \leq M \leq 10^5
* 1 \leq A_i,B_i \leq N
* The graph has no self-loops or multiple edges.
* The graph is connected.
Input
Input is given from Standard Input in the following format:
N M
A_1 B_1
A_2 B_2
:
A_M B_M
Output
Find the maximum possible number of edges that can be added.
Examples
Input
6 5
1 2
2 3
3 4
4 5
5 6
Output
4
Input
5 5
1 2
2 3
3 1
5 4
5 1
Output
5 | instruction | 0 | 12,369 | 13 | 24,738 |
"Correct Solution:
```
N, M = map(int, input().split())
v = [set() for _ in range(N)]
for _ in range(M) :
A, B = map(int, input().split())
v[A-1].add(B-1)
v[B-1].add(A-1)
visited = [[False] * N for _ in range(2)]
visited[0][0] = True
q = [(0, 0)]
while q :
parity, cur = q.pop()
parity ^= 1
for nex in v[cur] :
if visited[parity][nex] :
continue
visited[parity][nex] = True
q.append((parity, nex))
o, e = 0, 0
for i in range(N) :
if visited[0][i] and not visited[1][i] :
e += 1
elif not visited[0][i] and visited[1][i] :
o += 1
print((N * (N - 1) - o * (o - 1) - e * (e - 1)) // 2 - M)
``` | output | 1 | 12,369 | 13 | 24,739 |
Provide a correct Python 3 solution for this coding contest problem.
Rng has a connected undirected graph with N vertices. Currently, there are M edges in the graph, and the i-th edge connects Vertices A_i and B_i.
Rng will add new edges to the graph by repeating the following operation:
* Operation: Choose u and v (u \neq v) such that Vertex v can be reached by traversing exactly three edges from Vertex u, and add an edge connecting Vertices u and v. It is not allowed to add an edge if there is already an edge connecting Vertices u and v.
Find the maximum possible number of edges that can be added.
Constraints
* 2 \leq N \leq 10^5
* 1 \leq M \leq 10^5
* 1 \leq A_i,B_i \leq N
* The graph has no self-loops or multiple edges.
* The graph is connected.
Input
Input is given from Standard Input in the following format:
N M
A_1 B_1
A_2 B_2
:
A_M B_M
Output
Find the maximum possible number of edges that can be added.
Examples
Input
6 5
1 2
2 3
3 4
4 5
5 6
Output
4
Input
5 5
1 2
2 3
3 1
5 4
5 1
Output
5 | instruction | 0 | 12,370 | 13 | 24,740 |
"Correct Solution:
```
import sys
sys.setrecursionlimit(pow(10, 7))
n, m = map(int, input().split())
G = [[] for _ in range(n)]
color = [-1]*n
for i in range(m):
a, b = map(int, input().split())
G[a-1].append(b-1)
G[b-1].append(a-1)
def dfs(v, c):
color[v] = c
ans = True
for u in G[v]:
if color[u] == c:
return False
if color[u] != -1:
continue
ans &= dfs(u, 1-c)
return ans
if dfs(v=0, c=0):
k = sum(color)
print(k*(n-k) - m)
else:
print(n*(n-1)//2 - m)
``` | output | 1 | 12,370 | 13 | 24,741 |
Provide a correct Python 3 solution for this coding contest problem.
Rng has a connected undirected graph with N vertices. Currently, there are M edges in the graph, and the i-th edge connects Vertices A_i and B_i.
Rng will add new edges to the graph by repeating the following operation:
* Operation: Choose u and v (u \neq v) such that Vertex v can be reached by traversing exactly three edges from Vertex u, and add an edge connecting Vertices u and v. It is not allowed to add an edge if there is already an edge connecting Vertices u and v.
Find the maximum possible number of edges that can be added.
Constraints
* 2 \leq N \leq 10^5
* 1 \leq M \leq 10^5
* 1 \leq A_i,B_i \leq N
* The graph has no self-loops or multiple edges.
* The graph is connected.
Input
Input is given from Standard Input in the following format:
N M
A_1 B_1
A_2 B_2
:
A_M B_M
Output
Find the maximum possible number of edges that can be added.
Examples
Input
6 5
1 2
2 3
3 4
4 5
5 6
Output
4
Input
5 5
1 2
2 3
3 1
5 4
5 1
Output
5 | instruction | 0 | 12,371 | 13 | 24,742 |
"Correct Solution:
```
n,m,*t=map(int,open(0).read().split())
e=[[]for _ in'_'*n]
for a,b in zip(*[iter(t)]*2):
e[a-1]+=b-1,
e[b-1]+=a-1,
s=[0]
f=s+[-1]*~-n
while s:
v=s.pop()
p=f[v]^1
for w in e[v]:
if-1<f[w]:
if f[w]!=p:
print(n*~-n//2-m)
exit()
else:
f[w]=p
s+=w,
r=sum(f)
print(r*(n-r)-m)
``` | output | 1 | 12,371 | 13 | 24,743 |
Provide a correct Python 3 solution for this coding contest problem.
Rng has a connected undirected graph with N vertices. Currently, there are M edges in the graph, and the i-th edge connects Vertices A_i and B_i.
Rng will add new edges to the graph by repeating the following operation:
* Operation: Choose u and v (u \neq v) such that Vertex v can be reached by traversing exactly three edges from Vertex u, and add an edge connecting Vertices u and v. It is not allowed to add an edge if there is already an edge connecting Vertices u and v.
Find the maximum possible number of edges that can be added.
Constraints
* 2 \leq N \leq 10^5
* 1 \leq M \leq 10^5
* 1 \leq A_i,B_i \leq N
* The graph has no self-loops or multiple edges.
* The graph is connected.
Input
Input is given from Standard Input in the following format:
N M
A_1 B_1
A_2 B_2
:
A_M B_M
Output
Find the maximum possible number of edges that can be added.
Examples
Input
6 5
1 2
2 3
3 4
4 5
5 6
Output
4
Input
5 5
1 2
2 3
3 1
5 4
5 1
Output
5 | instruction | 0 | 12,372 | 13 | 24,744 |
"Correct Solution:
```
import sys
sys.setrecursionlimit(10**6)
n,m=map(int,input().split())
s=[[]for i in range(n+1)]
c=[0]*(n+1)
for i in range(m):
a,b=map(int,input().split())
s[a].append(b)
s[b].append(a)
def dfs(v,t):
c[v]=t
# print('start :'+str(c))
for i in s[v]:
if c[i]==t:
return False
if c[i]==0 and not dfs(i,-t):
return False
else:
return True
if dfs(1,1):
q=c.count(1)
print((n-q)*q-m)
else:
print((n*(n-1)//2)-m)
``` | output | 1 | 12,372 | 13 | 24,745 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Rng has a connected undirected graph with N vertices. Currently, there are M edges in the graph, and the i-th edge connects Vertices A_i and B_i.
Rng will add new edges to the graph by repeating the following operation:
* Operation: Choose u and v (u \neq v) such that Vertex v can be reached by traversing exactly three edges from Vertex u, and add an edge connecting Vertices u and v. It is not allowed to add an edge if there is already an edge connecting Vertices u and v.
Find the maximum possible number of edges that can be added.
Constraints
* 2 \leq N \leq 10^5
* 1 \leq M \leq 10^5
* 1 \leq A_i,B_i \leq N
* The graph has no self-loops or multiple edges.
* The graph is connected.
Input
Input is given from Standard Input in the following format:
N M
A_1 B_1
A_2 B_2
:
A_M B_M
Output
Find the maximum possible number of edges that can be added.
Examples
Input
6 5
1 2
2 3
3 4
4 5
5 6
Output
4
Input
5 5
1 2
2 3
3 1
5 4
5 1
Output
5
Submitted Solution:
```
N, M = map(int, input().split())
A, B = (
zip(*(map(int, input().split()) for _ in range(M))) if M else
((), ())
)
# グラフが二部グラフならば二集合間に辺を張れる
# そうでなければ完全グラフになるように辺を張れる
G = [set() for _ in range(N + 1)]
for x, y in zip(A, B):
G[x].add(y)
G[y].add(x)
dp = [0 for _ in range(N + 1)]
q = []
q.append(1)
dp[1] = 1
is_bi = True
while q:
i = q.pop()
for j in G[i]:
if dp[j] == 0:
dp[j] = -dp[i]
q.append(j)
else:
is_bi &= dp[j] == -dp[i]
ans = (
sum(x == 1 for x in dp) * sum(x == -1 for x in dp) - M if is_bi else
N * (N - 1) // 2 - M
)
print(ans)
``` | instruction | 0 | 12,373 | 13 | 24,746 |
Yes | output | 1 | 12,373 | 13 | 24,747 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Rng has a connected undirected graph with N vertices. Currently, there are M edges in the graph, and the i-th edge connects Vertices A_i and B_i.
Rng will add new edges to the graph by repeating the following operation:
* Operation: Choose u and v (u \neq v) such that Vertex v can be reached by traversing exactly three edges from Vertex u, and add an edge connecting Vertices u and v. It is not allowed to add an edge if there is already an edge connecting Vertices u and v.
Find the maximum possible number of edges that can be added.
Constraints
* 2 \leq N \leq 10^5
* 1 \leq M \leq 10^5
* 1 \leq A_i,B_i \leq N
* The graph has no self-loops or multiple edges.
* The graph is connected.
Input
Input is given from Standard Input in the following format:
N M
A_1 B_1
A_2 B_2
:
A_M B_M
Output
Find the maximum possible number of edges that can be added.
Examples
Input
6 5
1 2
2 3
3 4
4 5
5 6
Output
4
Input
5 5
1 2
2 3
3 1
5 4
5 1
Output
5
Submitted Solution:
```
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")
n,m = map(int, input().split())
ns = [[] for _ in range(n)]
for i in range(m):
a,b = map(int, input().split())
a -= 1
b -= 1
ns[a].append(b)
ns[b].append(a)
def is_bip(ns):
start = 0
q = [start]
cs = [None]*n
cs[start] = 1
while q:
u = q.pop()
c = cs[u]
cc = int(not c)
for v in ns[u]:
if cs[v] is None:
cs[v] = cc
q.append(v)
elif cs[v]==c:
return False, None
return True, cs
res, cs = is_bip(ns)
if res:
n1 = sum(cs)
n2 = n - n1
ans = n1*n2 - m
else:
ans = n*(n-1)//2 - m
print(ans)
``` | instruction | 0 | 12,374 | 13 | 24,748 |
Yes | output | 1 | 12,374 | 13 | 24,749 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Rng has a connected undirected graph with N vertices. Currently, there are M edges in the graph, and the i-th edge connects Vertices A_i and B_i.
Rng will add new edges to the graph by repeating the following operation:
* Operation: Choose u and v (u \neq v) such that Vertex v can be reached by traversing exactly three edges from Vertex u, and add an edge connecting Vertices u and v. It is not allowed to add an edge if there is already an edge connecting Vertices u and v.
Find the maximum possible number of edges that can be added.
Constraints
* 2 \leq N \leq 10^5
* 1 \leq M \leq 10^5
* 1 \leq A_i,B_i \leq N
* The graph has no self-loops or multiple edges.
* The graph is connected.
Input
Input is given from Standard Input in the following format:
N M
A_1 B_1
A_2 B_2
:
A_M B_M
Output
Find the maximum possible number of edges that can be added.
Examples
Input
6 5
1 2
2 3
3 4
4 5
5 6
Output
4
Input
5 5
1 2
2 3
3 1
5 4
5 1
Output
5
Submitted Solution:
```
import sys
sys.setrecursionlimit(10**7)
N,M=map(int,input().split())
E=[[]for _ in'_'*-~N]
for _ in'_'*M:
a,b=map(int,input().split())
E[a]+=[b]
E[b]+=[a]
V=[0]*-~N
def f(i,x):
if V[i]:
return V[i]==x
V[i]=x
r=1
for j in E[i]:
r*=f(j,-x)
return r
if f(1,1):
print(V.count(1)*V.count(-1)-M)
else:
print(N*~-N//2-M)
``` | instruction | 0 | 12,375 | 13 | 24,750 |
Yes | output | 1 | 12,375 | 13 | 24,751 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Rng has a connected undirected graph with N vertices. Currently, there are M edges in the graph, and the i-th edge connects Vertices A_i and B_i.
Rng will add new edges to the graph by repeating the following operation:
* Operation: Choose u and v (u \neq v) such that Vertex v can be reached by traversing exactly three edges from Vertex u, and add an edge connecting Vertices u and v. It is not allowed to add an edge if there is already an edge connecting Vertices u and v.
Find the maximum possible number of edges that can be added.
Constraints
* 2 \leq N \leq 10^5
* 1 \leq M \leq 10^5
* 1 \leq A_i,B_i \leq N
* The graph has no self-loops or multiple edges.
* The graph is connected.
Input
Input is given from Standard Input in the following format:
N M
A_1 B_1
A_2 B_2
:
A_M B_M
Output
Find the maximum possible number of edges that can be added.
Examples
Input
6 5
1 2
2 3
3 4
4 5
5 6
Output
4
Input
5 5
1 2
2 3
3 1
5 4
5 1
Output
5
Submitted Solution:
```
# 解説AC
# 二部グラフでないグラフの性質や,パスの長さを考察する
def main():
N, M = (int(i) for i in input().split())
G = [[] for _ in range(N)]
for _ in range(M):
a, b = (int(i) for i in input().split())
G[a-1].append(b-1)
G[b-1].append(a-1)
def dfs(s):
"""
二部グラフか判定
"""
stack = [s]
color = [-1]*N
color[s] = 0
while stack:
v = stack.pop()
for u in G[v]:
if color[u] != -1:
if color[u] == color[v]:
# 二部グラフでない
return False, color
continue
color[u] = color[v] ^ 1
stack.append(u)
return True, color
is_bipartite, color = dfs(0)
if is_bipartite:
# 二部グラフである
b = sum(color)
w = N - b
print(b*w - M)
else:
# 二部グラフでない
# 完全グラフになるので,{}_N C_2 - (既に張られている辺の数)
print(N*(N-1)//2 - M)
if __name__ == '__main__':
main()
``` | instruction | 0 | 12,376 | 13 | 24,752 |
Yes | output | 1 | 12,376 | 13 | 24,753 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Rng has a connected undirected graph with N vertices. Currently, there are M edges in the graph, and the i-th edge connects Vertices A_i and B_i.
Rng will add new edges to the graph by repeating the following operation:
* Operation: Choose u and v (u \neq v) such that Vertex v can be reached by traversing exactly three edges from Vertex u, and add an edge connecting Vertices u and v. It is not allowed to add an edge if there is already an edge connecting Vertices u and v.
Find the maximum possible number of edges that can be added.
Constraints
* 2 \leq N \leq 10^5
* 1 \leq M \leq 10^5
* 1 \leq A_i,B_i \leq N
* The graph has no self-loops or multiple edges.
* The graph is connected.
Input
Input is given from Standard Input in the following format:
N M
A_1 B_1
A_2 B_2
:
A_M B_M
Output
Find the maximum possible number of edges that can be added.
Examples
Input
6 5
1 2
2 3
3 4
4 5
5 6
Output
4
Input
5 5
1 2
2 3
3 1
5 4
5 1
Output
5
Submitted Solution:
```
from collections import deque
n, m = map(int, input().split())
if n==2 or n==3:
print(0)
exit()
connect = [[] for _ in range(n)]
for _ in range(m):
a, b = map(lambda x: int(x)-1, input().split())
connect[a].append(b)
connect[b].append(a)
#print(connect)
color= [-1]*n
color[0]=0
explored= {0}
next = deque(connect[0])
exploring =deque()
Yes = True
explored.add(0)
while next:
now = next.popleft()
exploring.extend(connect[now])
while exploring:
a=exploring.popleft()
if color[a]==-1 or color[a]==(color[now]+1)%2:
color[a]=(color[now]+1)%2
else:
Yes = False
if a not in explored:
next.append(a)
explored.add(a)
Yes = False
if Yes:
s=sum(color)
print(s*(n-s)-m)
else:
print(int(n*(n-1)/2-m))
``` | instruction | 0 | 12,377 | 13 | 24,754 |
No | output | 1 | 12,377 | 13 | 24,755 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Rng has a connected undirected graph with N vertices. Currently, there are M edges in the graph, and the i-th edge connects Vertices A_i and B_i.
Rng will add new edges to the graph by repeating the following operation:
* Operation: Choose u and v (u \neq v) such that Vertex v can be reached by traversing exactly three edges from Vertex u, and add an edge connecting Vertices u and v. It is not allowed to add an edge if there is already an edge connecting Vertices u and v.
Find the maximum possible number of edges that can be added.
Constraints
* 2 \leq N \leq 10^5
* 1 \leq M \leq 10^5
* 1 \leq A_i,B_i \leq N
* The graph has no self-loops or multiple edges.
* The graph is connected.
Input
Input is given from Standard Input in the following format:
N M
A_1 B_1
A_2 B_2
:
A_M B_M
Output
Find the maximum possible number of edges that can be added.
Examples
Input
6 5
1 2
2 3
3 4
4 5
5 6
Output
4
Input
5 5
1 2
2 3
3 1
5 4
5 1
Output
5
Submitted Solution:
```
import sys
sys.setrecursionlimit(200000)
N,M = map(int,input().split())
edges = [[]for _ in range(N+1)]
for _ in range(M):
u,v = map(int,input().split())
edges[u].append(v)
edges[v].append(u)
colors = [-1]*(N+1)
def choose(n,k):
import math
return math.factorial(n)//(math.factorial(n-k)*math.factorial(k))
def dfs(n,c):
if colors[n] == -1:
colors[n] = c
for nx in edges[n]:
dfs(nx,(c+1)%2)
else:
if colors[n] != c:
print(choose(N,2)-M)
exit()
dfs(1,0)
white = colors.count(0)
black = N - white
print(white*black-M)
``` | instruction | 0 | 12,378 | 13 | 24,756 |
No | output | 1 | 12,378 | 13 | 24,757 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Rng has a connected undirected graph with N vertices. Currently, there are M edges in the graph, and the i-th edge connects Vertices A_i and B_i.
Rng will add new edges to the graph by repeating the following operation:
* Operation: Choose u and v (u \neq v) such that Vertex v can be reached by traversing exactly three edges from Vertex u, and add an edge connecting Vertices u and v. It is not allowed to add an edge if there is already an edge connecting Vertices u and v.
Find the maximum possible number of edges that can be added.
Constraints
* 2 \leq N \leq 10^5
* 1 \leq M \leq 10^5
* 1 \leq A_i,B_i \leq N
* The graph has no self-loops or multiple edges.
* The graph is connected.
Input
Input is given from Standard Input in the following format:
N M
A_1 B_1
A_2 B_2
:
A_M B_M
Output
Find the maximum possible number of edges that can be added.
Examples
Input
6 5
1 2
2 3
3 4
4 5
5 6
Output
4
Input
5 5
1 2
2 3
3 1
5 4
5 1
Output
5
Submitted Solution:
```
N, M = map(int, input().split())
G = [[] for _ in range(N)]
for _ in range(M):
a, b = map(int, input().split())
G[a-1].append(b-1)
G[b-1].append(a-1)
def is_bipartite(G):
n = len(G)
color = [-1]*n
que = deque([0])
color[0] = 0
while que:
v = que.pop()
c = color[v]
for nv in G[v]:
if color[nv]<0:
color[nv] = 1-c
que.append(nv)
elif color[nv] == c:
return False
return color
color = is_bipartite(G)
if color:
a = color.count(0)
b = N-a
print(a*b-M)
else:
print(N*(N-1)//2-M)
``` | instruction | 0 | 12,379 | 13 | 24,758 |
No | output | 1 | 12,379 | 13 | 24,759 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Rng has a connected undirected graph with N vertices. Currently, there are M edges in the graph, and the i-th edge connects Vertices A_i and B_i.
Rng will add new edges to the graph by repeating the following operation:
* Operation: Choose u and v (u \neq v) such that Vertex v can be reached by traversing exactly three edges from Vertex u, and add an edge connecting Vertices u and v. It is not allowed to add an edge if there is already an edge connecting Vertices u and v.
Find the maximum possible number of edges that can be added.
Constraints
* 2 \leq N \leq 10^5
* 1 \leq M \leq 10^5
* 1 \leq A_i,B_i \leq N
* The graph has no self-loops or multiple edges.
* The graph is connected.
Input
Input is given from Standard Input in the following format:
N M
A_1 B_1
A_2 B_2
:
A_M B_M
Output
Find the maximum possible number of edges that can be added.
Examples
Input
6 5
1 2
2 3
3 4
4 5
5 6
Output
4
Input
5 5
1 2
2 3
3 1
5 4
5 1
Output
5
Submitted Solution:
```
# coding:utf-8
class Tree(object):
def __init__(self, N):
self.tree = [[] for _ in range(N)]
self.colors = [0 for _ in range(N)]
def add_node(self, a, b):
self.tree[a].append(b)
# 二部グラフか判定する関数
def is_bipartite_graph(self, v, color):
self.colors[v] = color
for to in self.tree[v]:
if self.colors[to] == color:
return False
if self.colors[to] == 0 and not self.is_bipartite_graph(to, -color):
return False
return True
def count_node(self, color):
return self.colors.count(color)
if __name__ == "__main__":
# input data
N, M = map(int, input().split())
tree = Tree(N)
for _ in range(M):
a, b = map(int, input().split())
tree.add_node(a-1, b-1)
tree.add_node(b-1, a-1)
# tree dp
is_bpr = tree.is_bipartite_graph(0, 1)
if is_bpr:
bla = tree.count_node(1)
whi = N - bla
print(bla*whi-M)
else:
print(int(N*(N-1)/2-M))
``` | instruction | 0 | 12,380 | 13 | 24,760 |
No | output | 1 | 12,380 | 13 | 24,761 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.