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 |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a tree of n nodes. The tree is rooted at node 1, which is not considered as a leaf regardless of its degree.
Each leaf of the tree has one of the two colors: red or blue. Leaf node v initially has color s_{v}.
The color of each of the internal nodes (including the root) is determined as follows.
* Let b be the number of blue immediate children, and r be the number of red immediate children of a given vertex.
* Then the color of this vertex is blue if and only if b - r ≥ k, otherwise red.
Integer k is a parameter that is same for all the nodes.
You need to handle the following types of queries:
* 1 v: print the color of node v;
* 2 v c: change the color of leaf v to c (c = 0 means red, c = 1 means blue);
* 3 h: update the current value of k to h.
Input
The first line of the input consists of two integers n and k (2 ≤ n ≤ 10^{5}, -n ≤ k ≤ n) — the number of nodes and the initial parameter k.
Each of the next n - 1 lines contains two integers u and v (1 ≤ u,v ≤ n), denoting that there is an edge between vertices u and v.
The next line consists of n space separated integers — the initial array s (-1 ≤ s_i ≤ 1). s_{i} = 0 means that the color of node i is red. s_{i} = 1 means that the color of node i is blue. s_{i} = -1 means that the node i is not a leaf.
The next line contains an integer q (1 ≤ q ≤ 10^5), the number of queries.
q lines follow, each containing a query in one of the following queries:
* 1 v (1 ≤ v ≤ n): print the color of node v;
* 2 v c (1 ≤ v ≤ n, c = 0 or c = 1): change the color of leaf v to c (c = 0 means red, c = 1 means blue). It is guaranteed that v is a leaf;
* 3 h (-n ≤ h ≤ n): update the current value of k to h.
Output
For each query of the first type, print 0 if the color of vertex v is red, and 1 otherwise.
Example
Input
5 2
1 2
1 3
2 4
2 5
-1 -1 0 1 0
9
1 1
1 2
3 -2
1 1
1 2
3 1
2 5 1
1 1
1 2
Output
0
0
1
1
0
1
Note
Figures:
(i) The initial tree
(ii) The tree after the 3rd query
(iii) The tree after the 7th query
<image>
Submitted Solution:
```
nn,k = list(map(int,input().split()))
uv = {}
for i in range(nn-1):
u,v = list(map(int,input().split()))
if u not in uv:
uv[u] = []
uv[u].append(v)
c = list(map(int,input().split()))
c_ = c[:]
def init_color(i=0):
b,r = 0,0
childs = uv.get(i+1,[])
for child in childs:
if c[child-1] == -1:
init_color(child-1)
if c[child-1] == 0:
r += 1
if c[child-1] == 1:
b += 1
c_[i] = int(b-r>=k)
init_color()
nq = int(input())
for i in range(nq):
q = list(map(int,input().split()))
if q[0] == 1:
print(c_[q[1]-1])
if q[0] == 2:
c[q[1]-1] = q[2]
init_color()
if q[0] == 3:
k = q[1]
init_color()
``` | instruction | 0 | 20,370 | 13 | 40,740 |
No | output | 1 | 20,370 | 13 | 40,741 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a new attraction in Singapore Zoo: The Infinite Zoo.
The Infinite Zoo can be represented by a graph with an infinite number of vertices labeled 1,2,3,…. There is a directed edge from vertex u to vertex u+v if and only if u\&v=v, where \& denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND). There are no other edges in the graph.
Zookeeper has q queries. In the i-th query she will ask you if she can travel from vertex u_i to vertex v_i by going through directed edges.
Input
The first line contains an integer q (1 ≤ q ≤ 10^5) — the number of queries.
The i-th of the next q lines will contain two integers u_i, v_i (1 ≤ u_i, v_i < 2^{30}) — a query made by Zookeeper.
Output
For the i-th of the q queries, output "YES" in a single line if Zookeeper can travel from vertex u_i to vertex v_i. Otherwise, output "NO".
You can print your answer in any case. For example, if the answer is "YES", then the output "Yes" or "yeS" will also be considered as correct answer.
Example
Input
5
1 4
3 6
1 6
6 2
5 5
Output
YES
YES
NO
NO
YES
Note
The subgraph on vertices 1,2,3,4,5,6 is shown below.
<image> | instruction | 0 | 20,500 | 13 | 41,000 |
Tags: bitmasks, constructive algorithms, dp, greedy, math
Correct Solution:
```
def f(x):
a = []
while x:
a.append(x & 1)
x //= 2
return a + [0] * (30 - len(a))
for _ in range(int(input())):
u, v = map(int, input().split())
if u > v:
print("NO")
continue
a, b = f(u), f(v)
if a.count(1) < b.count(1):
print("NO")
continue
flag = 0
ac = bc = 0
for i in range(30):
bc += b[i]
ac += a[i]
if b[i] == 1 and a[i] == 0:
if bc > ac:
print("NO")
flag = 1
break
if flag == 0:
print("YES")
``` | output | 1 | 20,500 | 13 | 41,001 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a new attraction in Singapore Zoo: The Infinite Zoo.
The Infinite Zoo can be represented by a graph with an infinite number of vertices labeled 1,2,3,…. There is a directed edge from vertex u to vertex u+v if and only if u\&v=v, where \& denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND). There are no other edges in the graph.
Zookeeper has q queries. In the i-th query she will ask you if she can travel from vertex u_i to vertex v_i by going through directed edges.
Input
The first line contains an integer q (1 ≤ q ≤ 10^5) — the number of queries.
The i-th of the next q lines will contain two integers u_i, v_i (1 ≤ u_i, v_i < 2^{30}) — a query made by Zookeeper.
Output
For the i-th of the q queries, output "YES" in a single line if Zookeeper can travel from vertex u_i to vertex v_i. Otherwise, output "NO".
You can print your answer in any case. For example, if the answer is "YES", then the output "Yes" or "yeS" will also be considered as correct answer.
Example
Input
5
1 4
3 6
1 6
6 2
5 5
Output
YES
YES
NO
NO
YES
Note
The subgraph on vertices 1,2,3,4,5,6 is shown below.
<image> | instruction | 0 | 20,501 | 13 | 41,002 |
Tags: bitmasks, constructive algorithms, dp, greedy, math
Correct Solution:
```
from collections import deque
import copy
import sys
input = sys.stdin.readline
def solve(u,v):
a = []
b = []
for bit in range(30):
if u & (1<<bit):
a.append(bit)
if v & (1<<bit):
b.append(bit)
ans = True
if len(a) < len(b):
ans = False
else:
for i in range(len(b)):
if a[i] > b[i]:
ans = False
ans &= (u<=v)
if ans ==True:
return "YES"
else:
return "NO"
t = int(input())
for _ in range(t):
u, v = map(int, input().split())
print(solve(u,v))
``` | output | 1 | 20,501 | 13 | 41,003 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a new attraction in Singapore Zoo: The Infinite Zoo.
The Infinite Zoo can be represented by a graph with an infinite number of vertices labeled 1,2,3,…. There is a directed edge from vertex u to vertex u+v if and only if u\&v=v, where \& denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND). There are no other edges in the graph.
Zookeeper has q queries. In the i-th query she will ask you if she can travel from vertex u_i to vertex v_i by going through directed edges.
Input
The first line contains an integer q (1 ≤ q ≤ 10^5) — the number of queries.
The i-th of the next q lines will contain two integers u_i, v_i (1 ≤ u_i, v_i < 2^{30}) — a query made by Zookeeper.
Output
For the i-th of the q queries, output "YES" in a single line if Zookeeper can travel from vertex u_i to vertex v_i. Otherwise, output "NO".
You can print your answer in any case. For example, if the answer is "YES", then the output "Yes" or "yeS" will also be considered as correct answer.
Example
Input
5
1 4
3 6
1 6
6 2
5 5
Output
YES
YES
NO
NO
YES
Note
The subgraph on vertices 1,2,3,4,5,6 is shown below.
<image> | instruction | 0 | 20,502 | 13 | 41,004 |
Tags: bitmasks, constructive algorithms, dp, greedy, math
Correct Solution:
```
'''
Auther: ghoshashis545 Ashis Ghosh
College: jalpaiguri Govt Enggineering College
'''
from os import path
from io import BytesIO, IOBase
import sys
from heapq import heappush,heappop
from functools import cmp_to_key as ctk
from collections import deque,Counter,defaultdict as dd
from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right
from itertools import permutations
from datetime import datetime
from math import ceil,sqrt,log,gcd
def ii():return int(input())
def si():return input().rstrip()
def mi():return map(int,input().split())
def li():return list(mi())
abc='abcdefghijklmnopqrstuvwxyz'
mod=1000000007
#mod=998244353
inf = float("inf")
vow=['a','e','i','o','u']
dx,dy=[-1,1,0,0],[0,0,1,-1]
def bo(i):
return ord(i)-ord('0')
file = 1
def ceil(a,b):
return (a+b-1)//b
def solve():
for _ in range(ii()):
a,b = mi()
if a > b:
print("No")
continue
c1,c2 = 0,0
ok = True
for i in range(31):
if (1<<i)&a:
c1 +=1
if (1<<i)&b:
c2+=1
if c2 > c1:
print('No')
ok=False
break
if ok:
print('Yes')
if __name__ =="__main__":
if(file):
if path.exists('tmp/input.txt'):
sys.stdin=open('tmp/input.txt', 'r')
sys.stdout=open('tmp/output.txt','w')
else:
input=sys.stdin.readline
solve()
``` | output | 1 | 20,502 | 13 | 41,005 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a new attraction in Singapore Zoo: The Infinite Zoo.
The Infinite Zoo can be represented by a graph with an infinite number of vertices labeled 1,2,3,…. There is a directed edge from vertex u to vertex u+v if and only if u\&v=v, where \& denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND). There are no other edges in the graph.
Zookeeper has q queries. In the i-th query she will ask you if she can travel from vertex u_i to vertex v_i by going through directed edges.
Input
The first line contains an integer q (1 ≤ q ≤ 10^5) — the number of queries.
The i-th of the next q lines will contain two integers u_i, v_i (1 ≤ u_i, v_i < 2^{30}) — a query made by Zookeeper.
Output
For the i-th of the q queries, output "YES" in a single line if Zookeeper can travel from vertex u_i to vertex v_i. Otherwise, output "NO".
You can print your answer in any case. For example, if the answer is "YES", then the output "Yes" or "yeS" will also be considered as correct answer.
Example
Input
5
1 4
3 6
1 6
6 2
5 5
Output
YES
YES
NO
NO
YES
Note
The subgraph on vertices 1,2,3,4,5,6 is shown below.
<image> | instruction | 0 | 20,503 | 13 | 41,006 |
Tags: bitmasks, constructive algorithms, dp, greedy, math
Correct Solution:
```
import math
def getint():
return [int(i) for i in input().split()]
def getstr():
return [str(i) for i in input().split()]
#--------------------------------------------------------------------------
def solve():
x,y=getint()
o=[0]*35
f=[0]*35
f1=1
if x>y:
print("NO")
elif x==y:
print("YES")
else:
for i in range(31):
o[i]=x>>i&1
for i in range(1,31):
o[i]+=o[i-1]
for i in range(31):
f[i]=y>>i&1
for i in range(1,31):
f[i]+=f[i-1]
for i in range(31):
if o[i]<f[i]:
f1=0
break
if f1:
print("YES")
else:
print("NO")
#--------------------------------------------------------------------------
for _ in range(int(input())):
solve()
``` | output | 1 | 20,503 | 13 | 41,007 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a new attraction in Singapore Zoo: The Infinite Zoo.
The Infinite Zoo can be represented by a graph with an infinite number of vertices labeled 1,2,3,…. There is a directed edge from vertex u to vertex u+v if and only if u\&v=v, where \& denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND). There are no other edges in the graph.
Zookeeper has q queries. In the i-th query she will ask you if she can travel from vertex u_i to vertex v_i by going through directed edges.
Input
The first line contains an integer q (1 ≤ q ≤ 10^5) — the number of queries.
The i-th of the next q lines will contain two integers u_i, v_i (1 ≤ u_i, v_i < 2^{30}) — a query made by Zookeeper.
Output
For the i-th of the q queries, output "YES" in a single line if Zookeeper can travel from vertex u_i to vertex v_i. Otherwise, output "NO".
You can print your answer in any case. For example, if the answer is "YES", then the output "Yes" or "yeS" will also be considered as correct answer.
Example
Input
5
1 4
3 6
1 6
6 2
5 5
Output
YES
YES
NO
NO
YES
Note
The subgraph on vertices 1,2,3,4,5,6 is shown below.
<image> | instruction | 0 | 20,504 | 13 | 41,008 |
Tags: bitmasks, constructive algorithms, dp, greedy, math
Correct Solution:
```
'''Author- Akshit Monga'''
from sys import stdin, stdout
input = stdin.readline
t = int(input())
for _ in range(t):
a,b=map(int,input().split())
if b<a:
print("no")
continue
ans="yes"
x=0
y=0
for i in range(32):
if a&(1<<i):
x+=1
if b&(1<<i):
y+=1
if y>x:
ans="no"
break
print(ans)
``` | output | 1 | 20,504 | 13 | 41,009 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a new attraction in Singapore Zoo: The Infinite Zoo.
The Infinite Zoo can be represented by a graph with an infinite number of vertices labeled 1,2,3,…. There is a directed edge from vertex u to vertex u+v if and only if u\&v=v, where \& denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND). There are no other edges in the graph.
Zookeeper has q queries. In the i-th query she will ask you if she can travel from vertex u_i to vertex v_i by going through directed edges.
Input
The first line contains an integer q (1 ≤ q ≤ 10^5) — the number of queries.
The i-th of the next q lines will contain two integers u_i, v_i (1 ≤ u_i, v_i < 2^{30}) — a query made by Zookeeper.
Output
For the i-th of the q queries, output "YES" in a single line if Zookeeper can travel from vertex u_i to vertex v_i. Otherwise, output "NO".
You can print your answer in any case. For example, if the answer is "YES", then the output "Yes" or "yeS" will also be considered as correct answer.
Example
Input
5
1 4
3 6
1 6
6 2
5 5
Output
YES
YES
NO
NO
YES
Note
The subgraph on vertices 1,2,3,4,5,6 is shown below.
<image> | instruction | 0 | 20,505 | 13 | 41,010 |
Tags: bitmasks, constructive algorithms, dp, greedy, math
Correct Solution:
```
import sys
input = sys.stdin.readline
flush = sys.stdout.flush
from collections import Counter
for _ in range(int(input())):
u, v = map(int, input().split())
if u > v:
print("NO")
continue
if u == v:
print("YES")
continue
d = v - u
for i in range(30, -1, -1):
b = u & 1 << i
if b:
while d >= b:
d -= b
b <<= 1
if not d: print("YES")
else: print("NO")
``` | output | 1 | 20,505 | 13 | 41,011 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a new attraction in Singapore Zoo: The Infinite Zoo.
The Infinite Zoo can be represented by a graph with an infinite number of vertices labeled 1,2,3,…. There is a directed edge from vertex u to vertex u+v if and only if u\&v=v, where \& denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND). There are no other edges in the graph.
Zookeeper has q queries. In the i-th query she will ask you if she can travel from vertex u_i to vertex v_i by going through directed edges.
Input
The first line contains an integer q (1 ≤ q ≤ 10^5) — the number of queries.
The i-th of the next q lines will contain two integers u_i, v_i (1 ≤ u_i, v_i < 2^{30}) — a query made by Zookeeper.
Output
For the i-th of the q queries, output "YES" in a single line if Zookeeper can travel from vertex u_i to vertex v_i. Otherwise, output "NO".
You can print your answer in any case. For example, if the answer is "YES", then the output "Yes" or "yeS" will also be considered as correct answer.
Example
Input
5
1 4
3 6
1 6
6 2
5 5
Output
YES
YES
NO
NO
YES
Note
The subgraph on vertices 1,2,3,4,5,6 is shown below.
<image> | instruction | 0 | 20,506 | 13 | 41,012 |
Tags: bitmasks, constructive algorithms, dp, greedy, math
Correct Solution:
```
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
#######################################
for t in range(int(input())):
u,v=map(int,input().split())
x=bin(u).lstrip('0b')
y=bin(v).lstrip('0b')
a=x.count('1')
b=y.count('1')
if v>=u and b<=a:
ans="YES"
c=0
d=0
for i in range(-1,-min(len(x),len(y))-1,-1):
if x[i]=='1':
c+=1
if y[i]=='1':
d+=1
if d>c:
ans="NO"
break
print(ans)
else:
print("NO")
``` | output | 1 | 20,506 | 13 | 41,013 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a new attraction in Singapore Zoo: The Infinite Zoo.
The Infinite Zoo can be represented by a graph with an infinite number of vertices labeled 1,2,3,…. There is a directed edge from vertex u to vertex u+v if and only if u\&v=v, where \& denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND). There are no other edges in the graph.
Zookeeper has q queries. In the i-th query she will ask you if she can travel from vertex u_i to vertex v_i by going through directed edges.
Input
The first line contains an integer q (1 ≤ q ≤ 10^5) — the number of queries.
The i-th of the next q lines will contain two integers u_i, v_i (1 ≤ u_i, v_i < 2^{30}) — a query made by Zookeeper.
Output
For the i-th of the q queries, output "YES" in a single line if Zookeeper can travel from vertex u_i to vertex v_i. Otherwise, output "NO".
You can print your answer in any case. For example, if the answer is "YES", then the output "Yes" or "yeS" will also be considered as correct answer.
Example
Input
5
1 4
3 6
1 6
6 2
5 5
Output
YES
YES
NO
NO
YES
Note
The subgraph on vertices 1,2,3,4,5,6 is shown below.
<image> | instruction | 0 | 20,507 | 13 | 41,014 |
Tags: bitmasks, constructive algorithms, dp, greedy, math
Correct Solution:
```
for _ in range(int(input())):
u, v = list(map(int, input().split()))
if u > v:
print("NO")
elif u == v:
print("YES")
else:
fl = True
u = bin(u)
v = bin(v)
u_o = 0
v_o = 0
diff = len(v) - len(u)
for i in range(len(u)-1, 1, -1):
if u[i] == '1':
u_o += 1
if v[i+diff] == '1':
v_o += 1
if v_o > u_o:
print("NO")
fl = False
break
if fl:
for i in range(2, 2+diff):
if v[i] == '1':
v_o += 1
if v_o > u_o:
print("NO")
else:
print("YES")
``` | output | 1 | 20,507 | 13 | 41,015 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a new attraction in Singapore Zoo: The Infinite Zoo.
The Infinite Zoo can be represented by a graph with an infinite number of vertices labeled 1,2,3,…. There is a directed edge from vertex u to vertex u+v if and only if u\&v=v, where \& denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND). There are no other edges in the graph.
Zookeeper has q queries. In the i-th query she will ask you if she can travel from vertex u_i to vertex v_i by going through directed edges.
Input
The first line contains an integer q (1 ≤ q ≤ 10^5) — the number of queries.
The i-th of the next q lines will contain two integers u_i, v_i (1 ≤ u_i, v_i < 2^{30}) — a query made by Zookeeper.
Output
For the i-th of the q queries, output "YES" in a single line if Zookeeper can travel from vertex u_i to vertex v_i. Otherwise, output "NO".
You can print your answer in any case. For example, if the answer is "YES", then the output "Yes" or "yeS" will also be considered as correct answer.
Example
Input
5
1 4
3 6
1 6
6 2
5 5
Output
YES
YES
NO
NO
YES
Note
The subgraph on vertices 1,2,3,4,5,6 is shown below.
<image>
Submitted Solution:
```
q = int(input())
for _ in range(q):
u, v = map(int, input().split())
bu = bin(u)[2:][::-1]
bv = bin(v)[2:][::-1]
if u > v or bu.count('1') < bv.count('1'):
print('NO')
continue
uCounts = [i for i, c in enumerate(bu) if c == '1']
vCounts = [i for i, c in enumerate(bv) if c == '1']
for j in range(len(vCounts)):
if vCounts[j] < uCounts[j]:
print('NO')
break
else:
print('YES')
``` | instruction | 0 | 20,508 | 13 | 41,016 |
Yes | output | 1 | 20,508 | 13 | 41,017 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a new attraction in Singapore Zoo: The Infinite Zoo.
The Infinite Zoo can be represented by a graph with an infinite number of vertices labeled 1,2,3,…. There is a directed edge from vertex u to vertex u+v if and only if u\&v=v, where \& denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND). There are no other edges in the graph.
Zookeeper has q queries. In the i-th query she will ask you if she can travel from vertex u_i to vertex v_i by going through directed edges.
Input
The first line contains an integer q (1 ≤ q ≤ 10^5) — the number of queries.
The i-th of the next q lines will contain two integers u_i, v_i (1 ≤ u_i, v_i < 2^{30}) — a query made by Zookeeper.
Output
For the i-th of the q queries, output "YES" in a single line if Zookeeper can travel from vertex u_i to vertex v_i. Otherwise, output "NO".
You can print your answer in any case. For example, if the answer is "YES", then the output "Yes" or "yeS" will also be considered as correct answer.
Example
Input
5
1 4
3 6
1 6
6 2
5 5
Output
YES
YES
NO
NO
YES
Note
The subgraph on vertices 1,2,3,4,5,6 is shown below.
<image>
Submitted Solution:
```
"""
Author - Satwik Tiwari .
28th Feb , 2021 - Sunday
"""
#===============================================================================================
#importing some useful libraries.
from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from functools import cmp_to_key
# from itertools import *
from heapq import *
from math import gcd, factorial,floor,ceil,sqrt,log2
from copy import deepcopy
from collections import deque
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
#==============================================================================================
#fast I/O region
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
def print(*args, **kwargs):
"""Prints the values to a stream, or to sys.stdout by default."""
sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
at_start = True
for x in args:
if not at_start:
file.write(sep)
file.write(str(x))
at_start = False
file.write(kwargs.pop("end", "\n"))
if kwargs.pop("flush", False):
file.flush()
if sys.version_info[0] < 3:
sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
# inp = lambda: sys.stdin.readline().rstrip("\r\n")
#===============================================================================================
### START ITERATE RECURSION ###
from types import GeneratorType
def iterative(f, stack=[]):
def wrapped_func(*args, **kwargs):
if stack: return f(*args, **kwargs)
to = f(*args, **kwargs)
while True:
if type(to) is GeneratorType:
stack.append(to)
to = next(to)
continue
stack.pop()
if not stack: break
to = stack[-1].send(to)
return to
return wrapped_func
#### END ITERATE RECURSION ####
#===============================================================================================
#some shortcuts
def inp(): return sys.stdin.readline().rstrip("\r\n") #for fast input
def out(var): sys.stdout.write(str(var)) #for fast output, always take string
def lis(): return list(map(int, inp().split()))
def stringlis(): return list(map(str, inp().split()))
def sep(): return map(int, inp().split())
def strsep(): return map(str, inp().split())
# def graph(vertex): return [[] for i in range(0,vertex+1)]
def testcase(t):
for pp in range(t):
solve(pp)
def google(p):
print('Case #'+str(p)+': ',end='')
def lcm(a,b): return (a*b)//gcd(a,b)
def power(x, y, p) :
y%=(p-1) #not so sure about this. used when y>p-1. if p is prime.
res = 1 # Initialize result
x = x % p # Update x if it is more , than or equal to p
if (x == 0) :
return 0
while (y > 0) :
if ((y & 1) == 1) : # If y is odd, multiply, x with result
res = (res * x) % p
y = y >> 1 # y = y/2
x = (x * x) % p
return res
def ncr(n,r): return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))
def isPrime(n) :
if (n <= 1) : return False
if (n <= 3) : return True
if (n % 2 == 0 or n % 3 == 0) : return False
i = 5
while(i * i <= n) :
if (n % i == 0 or n % (i + 2) == 0) :
return False
i = i + 6
return True
inf = pow(10,20)
mod = 10**9+7
#===============================================================================================
# code here ;))
def solve(case):
x,y = sep()
if(x > y):
print("NO")
return
xbin = list(bin(x)[2:])
ybin = list(bin(y)[2:])
f = xbin.count('1')
s = ybin.count('1')
# if(y%2 and x%2 == 0):
# print('NO')
# return
find = [0]*(len(xbin) + 1)
sind = [0]*(len(ybin) + 1)
for i in range(len(xbin)-1,-1,-1):
if(xbin[i] == '1'):
find[i] = find[i + 1] + 1
else:
find[i] = find[i + 1]
for i in range(len(ybin)-1,-1,-1):
if(ybin[i] == '1'):
sind[i] = sind[i + 1] + 1
else:
sind[i] = sind[i + 1]
i = len(xbin) -1
j = len(ybin) -1
# print(find)
# print(sind)
while(i >= 0 or j >= 0):
if(sind[max(j,0)] > find[max(i,0)]):
print('NO')
return
i-=1;j-=1
print('YES')
# testcase(1)
testcase(int(inp()))
``` | instruction | 0 | 20,509 | 13 | 41,018 |
Yes | output | 1 | 20,509 | 13 | 41,019 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a new attraction in Singapore Zoo: The Infinite Zoo.
The Infinite Zoo can be represented by a graph with an infinite number of vertices labeled 1,2,3,…. There is a directed edge from vertex u to vertex u+v if and only if u\&v=v, where \& denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND). There are no other edges in the graph.
Zookeeper has q queries. In the i-th query she will ask you if she can travel from vertex u_i to vertex v_i by going through directed edges.
Input
The first line contains an integer q (1 ≤ q ≤ 10^5) — the number of queries.
The i-th of the next q lines will contain two integers u_i, v_i (1 ≤ u_i, v_i < 2^{30}) — a query made by Zookeeper.
Output
For the i-th of the q queries, output "YES" in a single line if Zookeeper can travel from vertex u_i to vertex v_i. Otherwise, output "NO".
You can print your answer in any case. For example, if the answer is "YES", then the output "Yes" or "yeS" will also be considered as correct answer.
Example
Input
5
1 4
3 6
1 6
6 2
5 5
Output
YES
YES
NO
NO
YES
Note
The subgraph on vertices 1,2,3,4,5,6 is shown below.
<image>
Submitted Solution:
```
import sys
input = sys.stdin.buffer.readline
def solve(u, v):
ulist = list(map(int, "{:031b}".format(u)))
vlist = list(map(int, "{:031b}".format(v)))
su = sum(ulist)
sv = sum(vlist)
if su < sv:
return False
diff = su - sv
cur = 0
for ub, vb in zip(ulist, vlist):
cur += (vb - ub)
if cur < 0:
return False
spare = cur
if spare > 0:
spare = min(spare, diff)
cur += spare
diff -= spare
return diff == 0
# """
q = int(input())
answers = []
for _ in range(q):
u, v = map(int, input().split())
answers.append("YES" if solve(u, v) else "NO")
print('\n'.join(answers))
"""
import random
for t in range(10000):
u = random.randrange(1, 2 ** random.randint(1, 30))
w = u
for _ in range(random.randint(1, 100)):
v = random.randint(1, w) & w
if w + v >= 2 ** 30:
break
w += v
if not solve(u, w):
print(u, w)
# """
``` | instruction | 0 | 20,510 | 13 | 41,020 |
Yes | output | 1 | 20,510 | 13 | 41,021 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a new attraction in Singapore Zoo: The Infinite Zoo.
The Infinite Zoo can be represented by a graph with an infinite number of vertices labeled 1,2,3,…. There is a directed edge from vertex u to vertex u+v if and only if u\&v=v, where \& denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND). There are no other edges in the graph.
Zookeeper has q queries. In the i-th query she will ask you if she can travel from vertex u_i to vertex v_i by going through directed edges.
Input
The first line contains an integer q (1 ≤ q ≤ 10^5) — the number of queries.
The i-th of the next q lines will contain two integers u_i, v_i (1 ≤ u_i, v_i < 2^{30}) — a query made by Zookeeper.
Output
For the i-th of the q queries, output "YES" in a single line if Zookeeper can travel from vertex u_i to vertex v_i. Otherwise, output "NO".
You can print your answer in any case. For example, if the answer is "YES", then the output "Yes" or "yeS" will also be considered as correct answer.
Example
Input
5
1 4
3 6
1 6
6 2
5 5
Output
YES
YES
NO
NO
YES
Note
The subgraph on vertices 1,2,3,4,5,6 is shown below.
<image>
Submitted Solution:
```
import io,os
input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
q = int(input())
for _ in range(q):
u, v = list(map(int, input().split()))
if u > v:
print("NO")
else:
ans = True
while v > 0:
if v & (-v) < u & (-u) or u == 0:
ans = False
v -= v & (-v)
u -= u & (-u)
if ans:
print("YES")
else:
print("NO")
``` | instruction | 0 | 20,511 | 13 | 41,022 |
Yes | output | 1 | 20,511 | 13 | 41,023 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a new attraction in Singapore Zoo: The Infinite Zoo.
The Infinite Zoo can be represented by a graph with an infinite number of vertices labeled 1,2,3,…. There is a directed edge from vertex u to vertex u+v if and only if u\&v=v, where \& denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND). There are no other edges in the graph.
Zookeeper has q queries. In the i-th query she will ask you if she can travel from vertex u_i to vertex v_i by going through directed edges.
Input
The first line contains an integer q (1 ≤ q ≤ 10^5) — the number of queries.
The i-th of the next q lines will contain two integers u_i, v_i (1 ≤ u_i, v_i < 2^{30}) — a query made by Zookeeper.
Output
For the i-th of the q queries, output "YES" in a single line if Zookeeper can travel from vertex u_i to vertex v_i. Otherwise, output "NO".
You can print your answer in any case. For example, if the answer is "YES", then the output "Yes" or "yeS" will also be considered as correct answer.
Example
Input
5
1 4
3 6
1 6
6 2
5 5
Output
YES
YES
NO
NO
YES
Note
The subgraph on vertices 1,2,3,4,5,6 is shown below.
<image>
Submitted Solution:
```
import sys
input = sys.stdin.readline
def parse(x):
out = []
for i in range(29,-1,-1):
if x >= (1 << i):
out.append(i)
x -= (1 << i)
return out + [31] * (30 - len(out))
res = []
n = int(input())
for _ in range(n):
a, b = map(int, input().split())
if b < a:
res.append('NO')
continue
p1 = parse(a)
p2 = parse(b)
for i in range(29,-1,-1):
if p1[i] > p2[i]:
res.append('NO')
break
else:
res.append('YES')
print('\n'.join(res))
``` | instruction | 0 | 20,512 | 13 | 41,024 |
No | output | 1 | 20,512 | 13 | 41,025 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a new attraction in Singapore Zoo: The Infinite Zoo.
The Infinite Zoo can be represented by a graph with an infinite number of vertices labeled 1,2,3,…. There is a directed edge from vertex u to vertex u+v if and only if u\&v=v, where \& denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND). There are no other edges in the graph.
Zookeeper has q queries. In the i-th query she will ask you if she can travel from vertex u_i to vertex v_i by going through directed edges.
Input
The first line contains an integer q (1 ≤ q ≤ 10^5) — the number of queries.
The i-th of the next q lines will contain two integers u_i, v_i (1 ≤ u_i, v_i < 2^{30}) — a query made by Zookeeper.
Output
For the i-th of the q queries, output "YES" in a single line if Zookeeper can travel from vertex u_i to vertex v_i. Otherwise, output "NO".
You can print your answer in any case. For example, if the answer is "YES", then the output "Yes" or "yeS" will also be considered as correct answer.
Example
Input
5
1 4
3 6
1 6
6 2
5 5
Output
YES
YES
NO
NO
YES
Note
The subgraph on vertices 1,2,3,4,5,6 is shown below.
<image>
Submitted Solution:
```
import math
from collections import deque
from sys import stdin, stdout, setrecursionlimit
from string import ascii_letters
from decimal import *
letters = ascii_letters[:26]
from collections import defaultdict
#from functools import reduce
input = stdin.readline
#print = stdout.write
for _ in range(int(input())):
a, b = map(lambda x : list(str(bin(int(x)))[2:])[::-1], input().split())
cnt = 0
can = True
la = len(a)
lb = len(b)
if a > b:
can = False
for i in range(max(lb, la)):
if i < la and a[i] == '1':
cnt += 1
if i >= lb:
can = False
if i < lb and b[i] == '1':
if cnt == 0:
can = False
else:
cnt -= 1
print('YES' if can else 'NO')
``` | instruction | 0 | 20,513 | 13 | 41,026 |
No | output | 1 | 20,513 | 13 | 41,027 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a new attraction in Singapore Zoo: The Infinite Zoo.
The Infinite Zoo can be represented by a graph with an infinite number of vertices labeled 1,2,3,…. There is a directed edge from vertex u to vertex u+v if and only if u\&v=v, where \& denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND). There are no other edges in the graph.
Zookeeper has q queries. In the i-th query she will ask you if she can travel from vertex u_i to vertex v_i by going through directed edges.
Input
The first line contains an integer q (1 ≤ q ≤ 10^5) — the number of queries.
The i-th of the next q lines will contain two integers u_i, v_i (1 ≤ u_i, v_i < 2^{30}) — a query made by Zookeeper.
Output
For the i-th of the q queries, output "YES" in a single line if Zookeeper can travel from vertex u_i to vertex v_i. Otherwise, output "NO".
You can print your answer in any case. For example, if the answer is "YES", then the output "Yes" or "yeS" will also be considered as correct answer.
Example
Input
5
1 4
3 6
1 6
6 2
5 5
Output
YES
YES
NO
NO
YES
Note
The subgraph on vertices 1,2,3,4,5,6 is shown below.
<image>
Submitted Solution:
```
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
n = int(input())
for i in range(n):
f,t = list(map(int,input().split()))
if f>t:
print('NO')
elif f==t:
print('YES')
else:
if f&1==0 and t&1:
print('NO')
else:
binf = bin(f)[2:]
bint = bin(t)[2:]
ans = 'YES'
n = len(bint)
binf = '0'*(n-len(binf))+ binf
last1 = 0
for i in range(n):
if bint[i] == '1':
last1 = i
currSum = 0
for i in range(last1):
if bint[i] == '1':
currSum += 1
if binf[i] == '1':
currSum -= 1
if currSum<0:
ans = 'NO'
break
if bint.count('1')>binf.count('1'):
print('NO')
else:
print(ans)
#"{} {} {}".format(maxele,minele,minele)
# yield " ".join([str(x) for x in ans])
``` | instruction | 0 | 20,514 | 13 | 41,028 |
No | output | 1 | 20,514 | 13 | 41,029 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a new attraction in Singapore Zoo: The Infinite Zoo.
The Infinite Zoo can be represented by a graph with an infinite number of vertices labeled 1,2,3,…. There is a directed edge from vertex u to vertex u+v if and only if u\&v=v, where \& denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND). There are no other edges in the graph.
Zookeeper has q queries. In the i-th query she will ask you if she can travel from vertex u_i to vertex v_i by going through directed edges.
Input
The first line contains an integer q (1 ≤ q ≤ 10^5) — the number of queries.
The i-th of the next q lines will contain two integers u_i, v_i (1 ≤ u_i, v_i < 2^{30}) — a query made by Zookeeper.
Output
For the i-th of the q queries, output "YES" in a single line if Zookeeper can travel from vertex u_i to vertex v_i. Otherwise, output "NO".
You can print your answer in any case. For example, if the answer is "YES", then the output "Yes" or "yeS" will also be considered as correct answer.
Example
Input
5
1 4
3 6
1 6
6 2
5 5
Output
YES
YES
NO
NO
YES
Note
The subgraph on vertices 1,2,3,4,5,6 is shown below.
<image>
Submitted Solution:
```
t = int(input())
for _ in range(t):
a, b = map(int, input().split())
if b == a:
print("YES")
continue
if b < a:
print("NO")
continue
works = True
aCount = 0
bCount = 0
i = 0
c = 1 << i
while c <= b:
if b & c:
bCount += 1
if a % c:
aCount += 1
if bCount > aCount:
works = False
break
i += 1
c = 1 << i
if not works:
print("NO")
else:
print("YES")
``` | instruction | 0 | 20,515 | 13 | 41,030 |
No | output | 1 | 20,515 | 13 | 41,031 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Recently, Pari and Arya did some research about NP-Hard problems and they found the minimum vertex cover problem very interesting.
Suppose the graph G is given. Subset A of its vertices is called a vertex cover of this graph, if for each edge uv there is at least one endpoint of it in this set, i.e. <image> or <image> (or both).
Pari and Arya have won a great undirected graph as an award in a team contest. Now they have to split it in two parts, but both of them want their parts of the graph to be a vertex cover.
They have agreed to give you their graph and you need to find two disjoint subsets of its vertices A and B, such that both A and B are vertex cover or claim it's impossible. Each vertex should be given to no more than one of the friends (or you can even keep it for yourself).
Input
The first line of the input contains two integers n and m (2 ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000) — the number of vertices and the number of edges in the prize graph, respectively.
Each of the next m lines contains a pair of integers ui and vi (1 ≤ ui, vi ≤ n), denoting an undirected edge between ui and vi. It's guaranteed the graph won't contain any self-loops or multiple edges.
Output
If it's impossible to split the graph between Pari and Arya as they expect, print "-1" (without quotes).
If there are two disjoint sets of vertices, such that both sets are vertex cover, print their descriptions. Each description must contain two lines. The first line contains a single integer k denoting the number of vertices in that vertex cover, and the second line contains k integers — the indices of vertices. Note that because of m ≥ 1, vertex cover cannot be empty.
Examples
Input
4 2
1 2
2 3
Output
1
2
2
1 3
Input
3 3
1 2
2 3
1 3
Output
-1
Note
In the first sample, you can give the vertex number 2 to Arya and vertices numbered 1 and 3 to Pari and keep vertex number 4 for yourself (or give it someone, if you wish).
In the second sample, there is no way to satisfy both Pari and Arya. | instruction | 0 | 20,753 | 13 | 41,506 |
Tags: dfs and similar, graphs
Correct Solution:
```
n,m=map(int,input().split())
flag=False
f=[0]*100001
E=[[] for i in range(n+1)]
e=[tuple(map(int,input().split())) for _ in range(m)]
for u,v in sorted(e): E[u]+=[v]; E[v]+=[u]
def bfs(nom,col):
ch=[(nom,col)]
while ch:
v,c=ch.pop()
if f[v]==0:
f[v]=c
for u in E[v]:
if f[u]==0: ch+=[(u,3-c)]
for x in range(1,n+1):
if f[x]==0: bfs(x,1)
for u,v in e:
if f[u]==f[v]: flag=True; break
if flag: print(-1)
else:
a=[i for i in range(n+1) if f[i]==1]
b=[i for i in range(n+1) if f[i]==2]
print(len(a)); print(*a)
print(len(b)); print(*b)
# Made By Mostafa_Khaled
``` | output | 1 | 20,753 | 13 | 41,507 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Recently, Pari and Arya did some research about NP-Hard problems and they found the minimum vertex cover problem very interesting.
Suppose the graph G is given. Subset A of its vertices is called a vertex cover of this graph, if for each edge uv there is at least one endpoint of it in this set, i.e. <image> or <image> (or both).
Pari and Arya have won a great undirected graph as an award in a team contest. Now they have to split it in two parts, but both of them want their parts of the graph to be a vertex cover.
They have agreed to give you their graph and you need to find two disjoint subsets of its vertices A and B, such that both A and B are vertex cover or claim it's impossible. Each vertex should be given to no more than one of the friends (or you can even keep it for yourself).
Input
The first line of the input contains two integers n and m (2 ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000) — the number of vertices and the number of edges in the prize graph, respectively.
Each of the next m lines contains a pair of integers ui and vi (1 ≤ ui, vi ≤ n), denoting an undirected edge between ui and vi. It's guaranteed the graph won't contain any self-loops or multiple edges.
Output
If it's impossible to split the graph between Pari and Arya as they expect, print "-1" (without quotes).
If there are two disjoint sets of vertices, such that both sets are vertex cover, print their descriptions. Each description must contain two lines. The first line contains a single integer k denoting the number of vertices in that vertex cover, and the second line contains k integers — the indices of vertices. Note that because of m ≥ 1, vertex cover cannot be empty.
Examples
Input
4 2
1 2
2 3
Output
1
2
2
1 3
Input
3 3
1 2
2 3
1 3
Output
-1
Note
In the first sample, you can give the vertex number 2 to Arya and vertices numbered 1 and 3 to Pari and keep vertex number 4 for yourself (or give it someone, if you wish).
In the second sample, there is no way to satisfy both Pari and Arya. | instruction | 0 | 20,754 | 13 | 41,508 |
Tags: dfs and similar, graphs
Correct Solution:
```
def a():
_, d = (int(x) for x in input().split())
curr_win_streak = 0
max_win_streak = 0
for _ in range(d):
if '0' in input():
curr_win_streak += 1
else:
curr_win_streak = 0
max_win_streak = max(max_win_streak, curr_win_streak)
print(max_win_streak)
def b():
x = input()
print(x + x[::-1])
def c():
nodes_nr, edges_nr = (int(x) for x in input().split())
node_idx___neigh_idxes = [[] for _ in range(nodes_nr + 1)]
for _ in range(edges_nr):
n1, n2 = (int(x) for x in input().split())
node_idx___neigh_idxes[n1].append(n2)
node_idx___neigh_idxes[n2].append(n1)
node_idx___color = [-1 for _ in range(nodes_nr + 1)]
for node_idx in range(1, nodes_nr + 1):
if node_idx___color[node_idx] != -1:
continue
stack = [node_idx]
node_idx___color[node_idx] = 0
while stack:
node = stack.pop()
curr_color = node_idx___color[node]
neigh_color = (node_idx___color[node] + 1) % 2
for neigh in node_idx___neigh_idxes[node]:
if node_idx___color[neigh] == curr_color:
print(-1)
return
elif node_idx___color[neigh] == -1:
node_idx___color[neigh] = neigh_color
stack.append(neigh)
print(node_idx___color.count(0))
print(*[idx for idx, color in enumerate(node_idx___color) if color == 0])
print(node_idx___color.count(1))
print(*[idx for idx, color in enumerate(node_idx___color) if color == 1])
if __name__ == '__main__':
c()
``` | output | 1 | 20,754 | 13 | 41,509 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Recently, Pari and Arya did some research about NP-Hard problems and they found the minimum vertex cover problem very interesting.
Suppose the graph G is given. Subset A of its vertices is called a vertex cover of this graph, if for each edge uv there is at least one endpoint of it in this set, i.e. <image> or <image> (or both).
Pari and Arya have won a great undirected graph as an award in a team contest. Now they have to split it in two parts, but both of them want their parts of the graph to be a vertex cover.
They have agreed to give you their graph and you need to find two disjoint subsets of its vertices A and B, such that both A and B are vertex cover or claim it's impossible. Each vertex should be given to no more than one of the friends (or you can even keep it for yourself).
Input
The first line of the input contains two integers n and m (2 ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000) — the number of vertices and the number of edges in the prize graph, respectively.
Each of the next m lines contains a pair of integers ui and vi (1 ≤ ui, vi ≤ n), denoting an undirected edge between ui and vi. It's guaranteed the graph won't contain any self-loops or multiple edges.
Output
If it's impossible to split the graph between Pari and Arya as they expect, print "-1" (without quotes).
If there are two disjoint sets of vertices, such that both sets are vertex cover, print their descriptions. Each description must contain two lines. The first line contains a single integer k denoting the number of vertices in that vertex cover, and the second line contains k integers — the indices of vertices. Note that because of m ≥ 1, vertex cover cannot be empty.
Examples
Input
4 2
1 2
2 3
Output
1
2
2
1 3
Input
3 3
1 2
2 3
1 3
Output
-1
Note
In the first sample, you can give the vertex number 2 to Arya and vertices numbered 1 and 3 to Pari and keep vertex number 4 for yourself (or give it someone, if you wish).
In the second sample, there is no way to satisfy both Pari and Arya. | instruction | 0 | 20,755 | 13 | 41,510 |
Tags: dfs and similar, graphs
Correct Solution:
```
n, m = map(int, input().split())
adj = [[] for i in range(n)]
edges = []
for i in range(m):
u, v = map(int, input().split())
adj[u - 1].append(v - 1)
adj[v - 1].append(u - 1)
edges.append((u - 1, v - 1))
col = [-1] * n
from collections import deque
def BFS(v, c):
q = deque([(v, c)])
while q:
v, c = q.pop()
col[v] = c
for nv in adj[v]:
if col[nv] == -1:
q.append((nv, 1 - c))
for i in range(n):
if col[i] == -1:
BFS(i, 0)
if all(col[u] != col[v] for u, v in edges):
z = [i + 1 for i in range(n) if col[i] == 0]
o = [i + 1 for i in range(n) if col[i] == 1]
print(len(z))
print(*z)
print(len(o))
print(*o)
else:
print(-1)
``` | output | 1 | 20,755 | 13 | 41,511 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Recently, Pari and Arya did some research about NP-Hard problems and they found the minimum vertex cover problem very interesting.
Suppose the graph G is given. Subset A of its vertices is called a vertex cover of this graph, if for each edge uv there is at least one endpoint of it in this set, i.e. <image> or <image> (or both).
Pari and Arya have won a great undirected graph as an award in a team contest. Now they have to split it in two parts, but both of them want their parts of the graph to be a vertex cover.
They have agreed to give you their graph and you need to find two disjoint subsets of its vertices A and B, such that both A and B are vertex cover or claim it's impossible. Each vertex should be given to no more than one of the friends (or you can even keep it for yourself).
Input
The first line of the input contains two integers n and m (2 ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000) — the number of vertices and the number of edges in the prize graph, respectively.
Each of the next m lines contains a pair of integers ui and vi (1 ≤ ui, vi ≤ n), denoting an undirected edge between ui and vi. It's guaranteed the graph won't contain any self-loops or multiple edges.
Output
If it's impossible to split the graph between Pari and Arya as they expect, print "-1" (without quotes).
If there are two disjoint sets of vertices, such that both sets are vertex cover, print their descriptions. Each description must contain two lines. The first line contains a single integer k denoting the number of vertices in that vertex cover, and the second line contains k integers — the indices of vertices. Note that because of m ≥ 1, vertex cover cannot be empty.
Examples
Input
4 2
1 2
2 3
Output
1
2
2
1 3
Input
3 3
1 2
2 3
1 3
Output
-1
Note
In the first sample, you can give the vertex number 2 to Arya and vertices numbered 1 and 3 to Pari and keep vertex number 4 for yourself (or give it someone, if you wish).
In the second sample, there is no way to satisfy both Pari and Arya. | instruction | 0 | 20,756 | 13 | 41,512 |
Tags: dfs and similar, graphs
Correct Solution:
```
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import random
sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def main():
n,m = LI()
aa = [LI() for _ in range(m)]
e = collections.defaultdict(set)
for a,b in aa:
e[a].add(b)
e[b].add(a)
d = collections.defaultdict(lambda: None)
def f(i, k):
q = [(i,k)]
while q:
nq = []
for i,k in q:
if d[i] is not None:
if d[i] == k:
continue
return False
d[i] = k
nk = 1 - k
for c in e[i]:
nq.append((c,nk))
q = nq
return True
for i in range(n):
if len(e[i]) > 0 and d[i] is None:
r = f(i, 0)
if not r:
return -1
a = []
b = []
for i in sorted(d.keys()):
if d[i] is None:
continue
if d[i] == 1:
a.append(i)
else:
b.append(i)
return '\n'.join(map(str, [len(a), ' '.join(map(str, a)), len(b), ' '.join(map(str, b))]))
print(main())
``` | output | 1 | 20,756 | 13 | 41,513 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Recently, Pari and Arya did some research about NP-Hard problems and they found the minimum vertex cover problem very interesting.
Suppose the graph G is given. Subset A of its vertices is called a vertex cover of this graph, if for each edge uv there is at least one endpoint of it in this set, i.e. <image> or <image> (or both).
Pari and Arya have won a great undirected graph as an award in a team contest. Now they have to split it in two parts, but both of them want their parts of the graph to be a vertex cover.
They have agreed to give you their graph and you need to find two disjoint subsets of its vertices A and B, such that both A and B are vertex cover or claim it's impossible. Each vertex should be given to no more than one of the friends (or you can even keep it for yourself).
Input
The first line of the input contains two integers n and m (2 ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000) — the number of vertices and the number of edges in the prize graph, respectively.
Each of the next m lines contains a pair of integers ui and vi (1 ≤ ui, vi ≤ n), denoting an undirected edge between ui and vi. It's guaranteed the graph won't contain any self-loops or multiple edges.
Output
If it's impossible to split the graph between Pari and Arya as they expect, print "-1" (without quotes).
If there are two disjoint sets of vertices, such that both sets are vertex cover, print their descriptions. Each description must contain two lines. The first line contains a single integer k denoting the number of vertices in that vertex cover, and the second line contains k integers — the indices of vertices. Note that because of m ≥ 1, vertex cover cannot be empty.
Examples
Input
4 2
1 2
2 3
Output
1
2
2
1 3
Input
3 3
1 2
2 3
1 3
Output
-1
Note
In the first sample, you can give the vertex number 2 to Arya and vertices numbered 1 and 3 to Pari and keep vertex number 4 for yourself (or give it someone, if you wish).
In the second sample, there is no way to satisfy both Pari and Arya. | instruction | 0 | 20,757 | 13 | 41,514 |
Tags: dfs and similar, graphs
Correct Solution:
```
from collections import deque
__author__ = 'aste'
def main():
n, m = [int(x) for x in input().split()]
color = [0]*n
graph = [[] for i in range(0, n)]
for i in range(0, m):
u, v = [int(x) - 1 for x in input().split()]
graph[u].append(v)
graph[v].append(u)
# bipartite
res = True
for i in range(0, n):
if color[i] != 0:
continue
q = deque()
color[i] = 1
q.append(i)
while q:
v = q.popleft()
for a in graph[v]:
if color[a] == 0:
color[a] = -color[v]
q.append(a)
elif color[a] != -color[v]:
res = False
break
if not res:
break
if not res:
print(-1)
else:
s1 = []
s2 = []
for i in range(0, n):
if color[i] == 1:
s1.append(i)
else:
s2.append(i)
print(len(s1))
print(" ".join(str(x + 1) for x in s1))
print(len(s2))
print(" ".join(str(x + 1) for x in s2))
main()
``` | output | 1 | 20,757 | 13 | 41,515 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Recently, Pari and Arya did some research about NP-Hard problems and they found the minimum vertex cover problem very interesting.
Suppose the graph G is given. Subset A of its vertices is called a vertex cover of this graph, if for each edge uv there is at least one endpoint of it in this set, i.e. <image> or <image> (or both).
Pari and Arya have won a great undirected graph as an award in a team contest. Now they have to split it in two parts, but both of them want their parts of the graph to be a vertex cover.
They have agreed to give you their graph and you need to find two disjoint subsets of its vertices A and B, such that both A and B are vertex cover or claim it's impossible. Each vertex should be given to no more than one of the friends (or you can even keep it for yourself).
Input
The first line of the input contains two integers n and m (2 ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000) — the number of vertices and the number of edges in the prize graph, respectively.
Each of the next m lines contains a pair of integers ui and vi (1 ≤ ui, vi ≤ n), denoting an undirected edge between ui and vi. It's guaranteed the graph won't contain any self-loops or multiple edges.
Output
If it's impossible to split the graph between Pari and Arya as they expect, print "-1" (without quotes).
If there are two disjoint sets of vertices, such that both sets are vertex cover, print their descriptions. Each description must contain two lines. The first line contains a single integer k denoting the number of vertices in that vertex cover, and the second line contains k integers — the indices of vertices. Note that because of m ≥ 1, vertex cover cannot be empty.
Examples
Input
4 2
1 2
2 3
Output
1
2
2
1 3
Input
3 3
1 2
2 3
1 3
Output
-1
Note
In the first sample, you can give the vertex number 2 to Arya and vertices numbered 1 and 3 to Pari and keep vertex number 4 for yourself (or give it someone, if you wish).
In the second sample, there is no way to satisfy both Pari and Arya. | instruction | 0 | 20,758 | 13 | 41,516 |
Tags: dfs and similar, graphs
Correct Solution:
```
n,m=map(int,input().split())
flag=False
f=[0]*100001
E=[[] for i in range(n+1)]
e=[tuple(map(int,input().split())) for _ in range(m)]
for u,v in sorted(e): E[u]+=[v]; E[v]+=[u]
def bfs(nom,col):
ch=[(nom,col)]
while ch:
v,c=ch.pop()
if f[v]==0:
f[v]=c
for u in E[v]:
if f[u]==0: ch+=[(u,3-c)]
for x in range(1,n+1):
if f[x]==0: bfs(x,1)
for u,v in e:
if f[u]==f[v]: flag=True; break
if flag: print(-1)
else:
a=[i for i in range(n+1) if f[i]==1]
b=[i for i in range(n+1) if f[i]==2]
print(len(a)); print(*a)
print(len(b)); print(*b)
``` | output | 1 | 20,758 | 13 | 41,517 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Recently, Pari and Arya did some research about NP-Hard problems and they found the minimum vertex cover problem very interesting.
Suppose the graph G is given. Subset A of its vertices is called a vertex cover of this graph, if for each edge uv there is at least one endpoint of it in this set, i.e. <image> or <image> (or both).
Pari and Arya have won a great undirected graph as an award in a team contest. Now they have to split it in two parts, but both of them want their parts of the graph to be a vertex cover.
They have agreed to give you their graph and you need to find two disjoint subsets of its vertices A and B, such that both A and B are vertex cover or claim it's impossible. Each vertex should be given to no more than one of the friends (or you can even keep it for yourself).
Input
The first line of the input contains two integers n and m (2 ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000) — the number of vertices and the number of edges in the prize graph, respectively.
Each of the next m lines contains a pair of integers ui and vi (1 ≤ ui, vi ≤ n), denoting an undirected edge between ui and vi. It's guaranteed the graph won't contain any self-loops or multiple edges.
Output
If it's impossible to split the graph between Pari and Arya as they expect, print "-1" (without quotes).
If there are two disjoint sets of vertices, such that both sets are vertex cover, print their descriptions. Each description must contain two lines. The first line contains a single integer k denoting the number of vertices in that vertex cover, and the second line contains k integers — the indices of vertices. Note that because of m ≥ 1, vertex cover cannot be empty.
Examples
Input
4 2
1 2
2 3
Output
1
2
2
1 3
Input
3 3
1 2
2 3
1 3
Output
-1
Note
In the first sample, you can give the vertex number 2 to Arya and vertices numbered 1 and 3 to Pari and keep vertex number 4 for yourself (or give it someone, if you wish).
In the second sample, there is no way to satisfy both Pari and Arya. | instruction | 0 | 20,759 | 13 | 41,518 |
Tags: dfs and similar, graphs
Correct Solution:
```
n,m = map(int,input().split())
g = [set() for i in range(n)]
for i in range(m):
a, b = map(int, input().split())
g[a-1].add(b-1)
g[b-1].add(a-1)
a = []
p = [-1]*n
for i in range(n):
if p[i] != -1: continue
p[i] = 0
a.append(i)
while a:
x = a.pop()
for elem in g[x]:
if p[elem] == p[x]:
print(-1)
exit(0)
if p[elem] == -1:
p[elem] = not p[x]
a.append(elem)
print(p.count(0))
for i in range(n):
if p[i] == 0:
print(i+1, end = ' ')
print()
print(p.count(1))
for i in range(n):
if p[i] == 1:
print(i+1, end = ' ')
``` | output | 1 | 20,759 | 13 | 41,519 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Recently, Pari and Arya did some research about NP-Hard problems and they found the minimum vertex cover problem very interesting.
Suppose the graph G is given. Subset A of its vertices is called a vertex cover of this graph, if for each edge uv there is at least one endpoint of it in this set, i.e. <image> or <image> (or both).
Pari and Arya have won a great undirected graph as an award in a team contest. Now they have to split it in two parts, but both of them want their parts of the graph to be a vertex cover.
They have agreed to give you their graph and you need to find two disjoint subsets of its vertices A and B, such that both A and B are vertex cover or claim it's impossible. Each vertex should be given to no more than one of the friends (or you can even keep it for yourself).
Input
The first line of the input contains two integers n and m (2 ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000) — the number of vertices and the number of edges in the prize graph, respectively.
Each of the next m lines contains a pair of integers ui and vi (1 ≤ ui, vi ≤ n), denoting an undirected edge between ui and vi. It's guaranteed the graph won't contain any self-loops or multiple edges.
Output
If it's impossible to split the graph between Pari and Arya as they expect, print "-1" (without quotes).
If there are two disjoint sets of vertices, such that both sets are vertex cover, print their descriptions. Each description must contain two lines. The first line contains a single integer k denoting the number of vertices in that vertex cover, and the second line contains k integers — the indices of vertices. Note that because of m ≥ 1, vertex cover cannot be empty.
Examples
Input
4 2
1 2
2 3
Output
1
2
2
1 3
Input
3 3
1 2
2 3
1 3
Output
-1
Note
In the first sample, you can give the vertex number 2 to Arya and vertices numbered 1 and 3 to Pari and keep vertex number 4 for yourself (or give it someone, if you wish).
In the second sample, there is no way to satisfy both Pari and Arya. | instruction | 0 | 20,760 | 13 | 41,520 |
Tags: dfs and similar, graphs
Correct Solution:
```
n,m = map(int, input().split())
g={x : [] for x in range(1, n + 1)}
for item in range(m):
u,v = map(int, input().split())
g[v].append(u)
g[u].append(v)
colors = [None]*(n+1)
def inversecolor(clr):
return [0,1][clr == 0]
def solve(g):
paths = [x for x in g if g[x] != []]
currentcolor = 0
colors[paths[-1]] = currentcolor
while paths:
current = paths.pop()
for targetvertex in g[current]:
if colors[targetvertex] is None:
colors[targetvertex] = inversecolor(colors[current])
paths.append(targetvertex)
else:
if colors[targetvertex] != inversecolor(colors[current]):
return "-1"
return [x for x,item in enumerate(colors) if item == 1],[x for x,item in enumerate(colors) if item == 0]
ans = solve(g)
if ans == "-1":
print(ans)
else:
a = ans[0]
b = ans[1]
print(len(a))
print(" ".join([str(x) for x in a]))
print(len(b))
print(" ".join([str(x) for x in b]))
``` | output | 1 | 20,760 | 13 | 41,521 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Recently, Pari and Arya did some research about NP-Hard problems and they found the minimum vertex cover problem very interesting.
Suppose the graph G is given. Subset A of its vertices is called a vertex cover of this graph, if for each edge uv there is at least one endpoint of it in this set, i.e. <image> or <image> (or both).
Pari and Arya have won a great undirected graph as an award in a team contest. Now they have to split it in two parts, but both of them want their parts of the graph to be a vertex cover.
They have agreed to give you their graph and you need to find two disjoint subsets of its vertices A and B, such that both A and B are vertex cover or claim it's impossible. Each vertex should be given to no more than one of the friends (or you can even keep it for yourself).
Input
The first line of the input contains two integers n and m (2 ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000) — the number of vertices and the number of edges in the prize graph, respectively.
Each of the next m lines contains a pair of integers ui and vi (1 ≤ ui, vi ≤ n), denoting an undirected edge between ui and vi. It's guaranteed the graph won't contain any self-loops or multiple edges.
Output
If it's impossible to split the graph between Pari and Arya as they expect, print "-1" (without quotes).
If there are two disjoint sets of vertices, such that both sets are vertex cover, print their descriptions. Each description must contain two lines. The first line contains a single integer k denoting the number of vertices in that vertex cover, and the second line contains k integers — the indices of vertices. Note that because of m ≥ 1, vertex cover cannot be empty.
Examples
Input
4 2
1 2
2 3
Output
1
2
2
1 3
Input
3 3
1 2
2 3
1 3
Output
-1
Note
In the first sample, you can give the vertex number 2 to Arya and vertices numbered 1 and 3 to Pari and keep vertex number 4 for yourself (or give it someone, if you wish).
In the second sample, there is no way to satisfy both Pari and Arya.
Submitted Solution:
```
import sys
def solve():
n, m = [int(x) for x in input().split()]
E = {i:[] for i in range(n)}
for i in range(m):
u, v = [int(x) - 1 for x in input().split()]
E[u].append(v)
E[v].append(u)
color = [None]*n
stack = []
for v in range(n):
if color[v] is None:
stack.append((v, True))
while stack:
u, col = stack.pop()
color[u] = col
for w in E[u]:
if color[w] is None:
stack.append((w, not col))
elif color[w] == col:
print(-1)
return None
A = list(filter(lambda v: color[v-1], range(1, n+1)))
B = list(filter(lambda v: not color[v-1], range(1, n+1)))
if n == 1:
print(-1)
elif m == 0:
print(1)
print(1)
print(n-1)
print(' '.join(map(str, range(2, n+1))))
else:
print(len(A))
print(' '.join(map(str, A)))
print(len(B))
print(' '.join(map(str, B)))
solve()
``` | instruction | 0 | 20,761 | 13 | 41,522 |
Yes | output | 1 | 20,761 | 13 | 41,523 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Recently, Pari and Arya did some research about NP-Hard problems and they found the minimum vertex cover problem very interesting.
Suppose the graph G is given. Subset A of its vertices is called a vertex cover of this graph, if for each edge uv there is at least one endpoint of it in this set, i.e. <image> or <image> (or both).
Pari and Arya have won a great undirected graph as an award in a team contest. Now they have to split it in two parts, but both of them want their parts of the graph to be a vertex cover.
They have agreed to give you their graph and you need to find two disjoint subsets of its vertices A and B, such that both A and B are vertex cover or claim it's impossible. Each vertex should be given to no more than one of the friends (or you can even keep it for yourself).
Input
The first line of the input contains two integers n and m (2 ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000) — the number of vertices and the number of edges in the prize graph, respectively.
Each of the next m lines contains a pair of integers ui and vi (1 ≤ ui, vi ≤ n), denoting an undirected edge between ui and vi. It's guaranteed the graph won't contain any self-loops or multiple edges.
Output
If it's impossible to split the graph between Pari and Arya as they expect, print "-1" (without quotes).
If there are two disjoint sets of vertices, such that both sets are vertex cover, print their descriptions. Each description must contain two lines. The first line contains a single integer k denoting the number of vertices in that vertex cover, and the second line contains k integers — the indices of vertices. Note that because of m ≥ 1, vertex cover cannot be empty.
Examples
Input
4 2
1 2
2 3
Output
1
2
2
1 3
Input
3 3
1 2
2 3
1 3
Output
-1
Note
In the first sample, you can give the vertex number 2 to Arya and vertices numbered 1 and 3 to Pari and keep vertex number 4 for yourself (or give it someone, if you wish).
In the second sample, there is no way to satisfy both Pari and Arya.
Submitted Solution:
```
n,m = map(int, input().split())
g={x : [] for x in range(1, n + 1)}
for item in range(m):
u,v = map(int, input().split())
g[v].append(u)
g[u].append(v)
colors = [None]*(n+1)
def inverseColor(clr):
return [0,1][clr == 0]
def solve(g):
for first in g:
if first != []:
break
path = [[first]]
currentColor = 0
colors[first] = currentColor
while path:
current = path.pop()
for targetVertex in g[current[-1]]:
if colors[targetVertex] == None:
colors[targetVertex] = inverseColor(colors[current[-1]])
else:
if colors[targetVertex] != inverseColor(colors[current[-1]]):
return "-1"
if targetVertex not in current:
path.append(current + [targetVertex])
return [x for x,item in enumerate(colors) if item == 1],[x for x,item in enumerate(colors) if item == 0]
Ans = solve(g)
# if Ans == "-1":
# print(Ans)
# else:
# A = Ans[0]
# B = Ans[1]
# print(len(A))
# print(" ".join([str(x) for x in A]))
# print(len(B))
# print(" ".join([str(x) for x in B]))
``` | instruction | 0 | 20,762 | 13 | 41,524 |
No | output | 1 | 20,762 | 13 | 41,525 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Recently, Pari and Arya did some research about NP-Hard problems and they found the minimum vertex cover problem very interesting.
Suppose the graph G is given. Subset A of its vertices is called a vertex cover of this graph, if for each edge uv there is at least one endpoint of it in this set, i.e. <image> or <image> (or both).
Pari and Arya have won a great undirected graph as an award in a team contest. Now they have to split it in two parts, but both of them want their parts of the graph to be a vertex cover.
They have agreed to give you their graph and you need to find two disjoint subsets of its vertices A and B, such that both A and B are vertex cover or claim it's impossible. Each vertex should be given to no more than one of the friends (or you can even keep it for yourself).
Input
The first line of the input contains two integers n and m (2 ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000) — the number of vertices and the number of edges in the prize graph, respectively.
Each of the next m lines contains a pair of integers ui and vi (1 ≤ ui, vi ≤ n), denoting an undirected edge between ui and vi. It's guaranteed the graph won't contain any self-loops or multiple edges.
Output
If it's impossible to split the graph between Pari and Arya as they expect, print "-1" (without quotes).
If there are two disjoint sets of vertices, such that both sets are vertex cover, print their descriptions. Each description must contain two lines. The first line contains a single integer k denoting the number of vertices in that vertex cover, and the second line contains k integers — the indices of vertices. Note that because of m ≥ 1, vertex cover cannot be empty.
Examples
Input
4 2
1 2
2 3
Output
1
2
2
1 3
Input
3 3
1 2
2 3
1 3
Output
-1
Note
In the first sample, you can give the vertex number 2 to Arya and vertices numbered 1 and 3 to Pari and keep vertex number 4 for yourself (or give it someone, if you wish).
In the second sample, there is no way to satisfy both Pari and Arya.
Submitted Solution:
```
n,m=map(int,input().split())
f=[0]*100001
k1=k2=0
for _ in range(m):
u,v=map(int,input().split())
if f[u]==f[v]==0: f[u]=1; f[v]=2
elif f[u]==f[v]: print("-1"); break
elif f[u]: f[v]=3-f[u]
else: f[u]=3-f[v]
else:
a=[i for i in range(n+1) if f[i]==1]
b=[i for i in range(n+1) if f[i]==2]
print(len(a)); print(*a)
print(len(b)); print(*b)
``` | instruction | 0 | 20,763 | 13 | 41,526 |
No | output | 1 | 20,763 | 13 | 41,527 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Recently, Pari and Arya did some research about NP-Hard problems and they found the minimum vertex cover problem very interesting.
Suppose the graph G is given. Subset A of its vertices is called a vertex cover of this graph, if for each edge uv there is at least one endpoint of it in this set, i.e. <image> or <image> (or both).
Pari and Arya have won a great undirected graph as an award in a team contest. Now they have to split it in two parts, but both of them want their parts of the graph to be a vertex cover.
They have agreed to give you their graph and you need to find two disjoint subsets of its vertices A and B, such that both A and B are vertex cover or claim it's impossible. Each vertex should be given to no more than one of the friends (or you can even keep it for yourself).
Input
The first line of the input contains two integers n and m (2 ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000) — the number of vertices and the number of edges in the prize graph, respectively.
Each of the next m lines contains a pair of integers ui and vi (1 ≤ ui, vi ≤ n), denoting an undirected edge between ui and vi. It's guaranteed the graph won't contain any self-loops or multiple edges.
Output
If it's impossible to split the graph between Pari and Arya as they expect, print "-1" (without quotes).
If there are two disjoint sets of vertices, such that both sets are vertex cover, print their descriptions. Each description must contain two lines. The first line contains a single integer k denoting the number of vertices in that vertex cover, and the second line contains k integers — the indices of vertices. Note that because of m ≥ 1, vertex cover cannot be empty.
Examples
Input
4 2
1 2
2 3
Output
1
2
2
1 3
Input
3 3
1 2
2 3
1 3
Output
-1
Note
In the first sample, you can give the vertex number 2 to Arya and vertices numbered 1 and 3 to Pari and keep vertex number 4 for yourself (or give it someone, if you wish).
In the second sample, there is no way to satisfy both Pari and Arya.
Submitted Solution:
```
n,m = map(int, input().split())
g={x : [] for x in range(1, n + 1)}
for item in range(m):
u,v = map(int, input().split())
g[v].append(u)
g[u].append(v)
colors = [None]*(n+1)
def inverseColor(clr):
return [0,1][clr == 0]
def solve(g):
currentColor = 0
colors[1] = currentColor
for item in g:
currentColor = (currentColor + 1) % 2
for i,edge in enumerate(g[item]):
if colors[edge] == None :
colors[edge] = currentColor
elif colors[edge] != inverseColor(colors[item]):
return "-1"
return [x for x,item in enumerate(colors) if item == 1],[x for x,item in enumerate(colors) if item == 0]
Ans = solve(g)
if Ans == "-1":
print(Ans)
else:
A = Ans[0]
B = Ans[1]
print(len(A))
print(" ".join([str(x) for x in A]))
print(len(B))
print(" ".join([str(x) for x in B]))
``` | instruction | 0 | 20,764 | 13 | 41,528 |
No | output | 1 | 20,764 | 13 | 41,529 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Recently, Pari and Arya did some research about NP-Hard problems and they found the minimum vertex cover problem very interesting.
Suppose the graph G is given. Subset A of its vertices is called a vertex cover of this graph, if for each edge uv there is at least one endpoint of it in this set, i.e. <image> or <image> (or both).
Pari and Arya have won a great undirected graph as an award in a team contest. Now they have to split it in two parts, but both of them want their parts of the graph to be a vertex cover.
They have agreed to give you their graph and you need to find two disjoint subsets of its vertices A and B, such that both A and B are vertex cover or claim it's impossible. Each vertex should be given to no more than one of the friends (or you can even keep it for yourself).
Input
The first line of the input contains two integers n and m (2 ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000) — the number of vertices and the number of edges in the prize graph, respectively.
Each of the next m lines contains a pair of integers ui and vi (1 ≤ ui, vi ≤ n), denoting an undirected edge between ui and vi. It's guaranteed the graph won't contain any self-loops or multiple edges.
Output
If it's impossible to split the graph between Pari and Arya as they expect, print "-1" (without quotes).
If there are two disjoint sets of vertices, such that both sets are vertex cover, print their descriptions. Each description must contain two lines. The first line contains a single integer k denoting the number of vertices in that vertex cover, and the second line contains k integers — the indices of vertices. Note that because of m ≥ 1, vertex cover cannot be empty.
Examples
Input
4 2
1 2
2 3
Output
1
2
2
1 3
Input
3 3
1 2
2 3
1 3
Output
-1
Note
In the first sample, you can give the vertex number 2 to Arya and vertices numbered 1 and 3 to Pari and keep vertex number 4 for yourself (or give it someone, if you wish).
In the second sample, there is no way to satisfy both Pari and Arya.
Submitted Solution:
```
def a():
_, d = (int(x) for x in input().split())
curr_win_streak = 0
max_win_streak = 0
for _ in range(d):
if '0' in input():
curr_win_streak += 1
else:
curr_win_streak = 0
max_win_streak = max(max_win_streak, curr_win_streak)
print(max_win_streak)
def b():
x = input()
print(x + x[::-1])
def c():
nodes_nr, edges_nr = (int(x) for x in input().split())
node_idx___neigh_idxes = [[] for _ in range(nodes_nr + 1)]
for _ in range(edges_nr):
n1, n2 = (int(x) for x in input().split())
node_idx___neigh_idxes[n1].append(n2)
node_idx___neigh_idxes[n2].append(n1)
node_idx___color = [-1 for _ in range(nodes_nr + 1)]
for node_idx in range(1, nodes_nr + 1):
if node_idx___color[node_idx] != -1:
continue
stack = [node_idx]
node_idx___color[node_idx] = 0
while stack:
node = stack.pop()
curr_color = node_idx___color[node]
neigh_color = (node_idx___color[node] + 1) % 2
for neigh in node_idx___neigh_idxes[node]:
if node_idx___color[neigh] == curr_color:
print(-1)
return
elif node_idx___color[neigh] == -1:
node_idx___color[neigh] = neigh_color
stack.append(neigh)
print(node_idx___color.count(0))
print(*[idx for idx, color in enumerate(node_idx___color) if color == 0])
print(node_idx___color.count(1))
print(*[idx for idx, color in enumerate(node_idx___color) if color == 1])
if __name__ == '__main__':
c()
``` | instruction | 0 | 20,765 | 13 | 41,530 |
No | output | 1 | 20,765 | 13 | 41,531 |
Provide a correct Python 3 solution for this coding contest problem.
We have a tree with N vertices and N-1 edges, respectively numbered 1, 2,\cdots, N and 1, 2, \cdots, N-1. Edge i connects Vertex u_i and v_i.
For integers L, R (1 \leq L \leq R \leq N), let us define a function f(L, R) as follows:
* Let S be the set of the vertices numbered L through R. f(L, R) represents the number of connected components in the subgraph formed only from the vertex set S and the edges whose endpoints both belong to S.
Compute \sum_{L=1}^{N} \sum_{R=L}^{N} f(L, R).
Constraints
* 1 \leq N \leq 2 \times 10^5
* 1 \leq u_i, v_i \leq N
* The given graph is a tree.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
u_1 v_1
u_2 v_2
:
u_{N-1} v_{N-1}
Output
Print \sum_{L=1}^{N} \sum_{R=L}^{N} f(L, R).
Examples
Input
3
1 3
2 3
Output
7
Input
2
1 2
Output
3
Input
10
5 3
5 7
8 9
1 9
9 10
8 4
7 4
6 10
7 2
Output
113 | instruction | 0 | 20,930 | 13 | 41,860 |
"Correct Solution:
```
N = int(input())
M = 0
for _ in range(N - 1):
u, v = sorted(map(int, input().split()))
M += u * (N - v + 1)
V = 0
for r in range(1, N + 1):
V += r * (N - r + 1)
print(V - M)
``` | output | 1 | 20,930 | 13 | 41,861 |
Provide a correct Python 3 solution for this coding contest problem.
We have a tree with N vertices and N-1 edges, respectively numbered 1, 2,\cdots, N and 1, 2, \cdots, N-1. Edge i connects Vertex u_i and v_i.
For integers L, R (1 \leq L \leq R \leq N), let us define a function f(L, R) as follows:
* Let S be the set of the vertices numbered L through R. f(L, R) represents the number of connected components in the subgraph formed only from the vertex set S and the edges whose endpoints both belong to S.
Compute \sum_{L=1}^{N} \sum_{R=L}^{N} f(L, R).
Constraints
* 1 \leq N \leq 2 \times 10^5
* 1 \leq u_i, v_i \leq N
* The given graph is a tree.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
u_1 v_1
u_2 v_2
:
u_{N-1} v_{N-1}
Output
Print \sum_{L=1}^{N} \sum_{R=L}^{N} f(L, R).
Examples
Input
3
1 3
2 3
Output
7
Input
2
1 2
Output
3
Input
10
5 3
5 7
8 9
1 9
9 10
8 4
7 4
6 10
7 2
Output
113 | instruction | 0 | 20,931 | 13 | 41,862 |
"Correct Solution:
```
n = int(input())
ans = 0
for i in range(1, n+1):
ans += i*(n-i+1)
for i in range(n-1):
u, v = sorted(list(map(int, input().split())))
ans -= u*(n-v+1)
print(ans)
``` | output | 1 | 20,931 | 13 | 41,863 |
Provide a correct Python 3 solution for this coding contest problem.
We have a tree with N vertices and N-1 edges, respectively numbered 1, 2,\cdots, N and 1, 2, \cdots, N-1. Edge i connects Vertex u_i and v_i.
For integers L, R (1 \leq L \leq R \leq N), let us define a function f(L, R) as follows:
* Let S be the set of the vertices numbered L through R. f(L, R) represents the number of connected components in the subgraph formed only from the vertex set S and the edges whose endpoints both belong to S.
Compute \sum_{L=1}^{N} \sum_{R=L}^{N} f(L, R).
Constraints
* 1 \leq N \leq 2 \times 10^5
* 1 \leq u_i, v_i \leq N
* The given graph is a tree.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
u_1 v_1
u_2 v_2
:
u_{N-1} v_{N-1}
Output
Print \sum_{L=1}^{N} \sum_{R=L}^{N} f(L, R).
Examples
Input
3
1 3
2 3
Output
7
Input
2
1 2
Output
3
Input
10
5 3
5 7
8 9
1 9
9 10
8 4
7 4
6 10
7 2
Output
113 | instruction | 0 | 20,932 | 13 | 41,864 |
"Correct Solution:
```
def main():
N = int(input())
h = 0
for _ in range(N - 1):
u, v = map(int, input().split())
if u > v:
u, v = v, u
h += u * (N + 1 - v)
return N * (N + 1) * (N + 2) // 6 - h
print(main())
``` | output | 1 | 20,932 | 13 | 41,865 |
Provide a correct Python 3 solution for this coding contest problem.
We have a tree with N vertices and N-1 edges, respectively numbered 1, 2,\cdots, N and 1, 2, \cdots, N-1. Edge i connects Vertex u_i and v_i.
For integers L, R (1 \leq L \leq R \leq N), let us define a function f(L, R) as follows:
* Let S be the set of the vertices numbered L through R. f(L, R) represents the number of connected components in the subgraph formed only from the vertex set S and the edges whose endpoints both belong to S.
Compute \sum_{L=1}^{N} \sum_{R=L}^{N} f(L, R).
Constraints
* 1 \leq N \leq 2 \times 10^5
* 1 \leq u_i, v_i \leq N
* The given graph is a tree.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
u_1 v_1
u_2 v_2
:
u_{N-1} v_{N-1}
Output
Print \sum_{L=1}^{N} \sum_{R=L}^{N} f(L, R).
Examples
Input
3
1 3
2 3
Output
7
Input
2
1 2
Output
3
Input
10
5 3
5 7
8 9
1 9
9 10
8 4
7 4
6 10
7 2
Output
113 | instruction | 0 | 20,933 | 13 | 41,866 |
"Correct Solution:
```
n = int(input())
uv = [sorted(map(int, input().split())) for _ in range(n - 1)]
# 解説AC
ans = sum(i * (n + 1 - i) for i in range(n + 1))
for u, v in uv:
ans -= u * (n - v + 1)
print(ans)
``` | output | 1 | 20,933 | 13 | 41,867 |
Provide a correct Python 3 solution for this coding contest problem.
We have a tree with N vertices and N-1 edges, respectively numbered 1, 2,\cdots, N and 1, 2, \cdots, N-1. Edge i connects Vertex u_i and v_i.
For integers L, R (1 \leq L \leq R \leq N), let us define a function f(L, R) as follows:
* Let S be the set of the vertices numbered L through R. f(L, R) represents the number of connected components in the subgraph formed only from the vertex set S and the edges whose endpoints both belong to S.
Compute \sum_{L=1}^{N} \sum_{R=L}^{N} f(L, R).
Constraints
* 1 \leq N \leq 2 \times 10^5
* 1 \leq u_i, v_i \leq N
* The given graph is a tree.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
u_1 v_1
u_2 v_2
:
u_{N-1} v_{N-1}
Output
Print \sum_{L=1}^{N} \sum_{R=L}^{N} f(L, R).
Examples
Input
3
1 3
2 3
Output
7
Input
2
1 2
Output
3
Input
10
5 3
5 7
8 9
1 9
9 10
8 4
7 4
6 10
7 2
Output
113 | instruction | 0 | 20,934 | 13 | 41,868 |
"Correct Solution:
```
import sys
input = sys.stdin.readline
N = int(input())
full = 0
for x in range(1, N+1):
full += x * (N-x+1)
for _ in range(N-1):
a, b = map(lambda s: int(s)-1, input().split())
if b < a: a, b = b, a
full -= (a+1) * (N-b)
print(full)
``` | output | 1 | 20,934 | 13 | 41,869 |
Provide a correct Python 3 solution for this coding contest problem.
We have a tree with N vertices and N-1 edges, respectively numbered 1, 2,\cdots, N and 1, 2, \cdots, N-1. Edge i connects Vertex u_i and v_i.
For integers L, R (1 \leq L \leq R \leq N), let us define a function f(L, R) as follows:
* Let S be the set of the vertices numbered L through R. f(L, R) represents the number of connected components in the subgraph formed only from the vertex set S and the edges whose endpoints both belong to S.
Compute \sum_{L=1}^{N} \sum_{R=L}^{N} f(L, R).
Constraints
* 1 \leq N \leq 2 \times 10^5
* 1 \leq u_i, v_i \leq N
* The given graph is a tree.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
u_1 v_1
u_2 v_2
:
u_{N-1} v_{N-1}
Output
Print \sum_{L=1}^{N} \sum_{R=L}^{N} f(L, R).
Examples
Input
3
1 3
2 3
Output
7
Input
2
1 2
Output
3
Input
10
5 3
5 7
8 9
1 9
9 10
8 4
7 4
6 10
7 2
Output
113 | instruction | 0 | 20,935 | 13 | 41,870 |
"Correct Solution:
```
n=int(input())
ans=0
#完全非連結の場合
for i in range(1,n+1):
ans+=i*(n-i+1)
#解説参照
#木
#辺が使われるたびに連結成分数が1減る
for i in range(n-1):
v,w=map(int,input().split())
if v>w:
v,w=w,v
ans-= v*(n-w+1)
print(ans)
``` | output | 1 | 20,935 | 13 | 41,871 |
Provide a correct Python 3 solution for this coding contest problem.
We have a tree with N vertices and N-1 edges, respectively numbered 1, 2,\cdots, N and 1, 2, \cdots, N-1. Edge i connects Vertex u_i and v_i.
For integers L, R (1 \leq L \leq R \leq N), let us define a function f(L, R) as follows:
* Let S be the set of the vertices numbered L through R. f(L, R) represents the number of connected components in the subgraph formed only from the vertex set S and the edges whose endpoints both belong to S.
Compute \sum_{L=1}^{N} \sum_{R=L}^{N} f(L, R).
Constraints
* 1 \leq N \leq 2 \times 10^5
* 1 \leq u_i, v_i \leq N
* The given graph is a tree.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
u_1 v_1
u_2 v_2
:
u_{N-1} v_{N-1}
Output
Print \sum_{L=1}^{N} \sum_{R=L}^{N} f(L, R).
Examples
Input
3
1 3
2 3
Output
7
Input
2
1 2
Output
3
Input
10
5 3
5 7
8 9
1 9
9 10
8 4
7 4
6 10
7 2
Output
113 | instruction | 0 | 20,936 | 13 | 41,872 |
"Correct Solution:
```
N = int(input())
uv = [[int(i) for i in input().split()] for _ in range(N - 1)]
e = 0
for u, v in uv :
e += min(u, v) * (N - max(u, v) + 1)
v = 0
for i in range(1, N + 1) :
v += i * (N + 1 - i)
print(v - e)
``` | output | 1 | 20,936 | 13 | 41,873 |
Provide a correct Python 3 solution for this coding contest problem.
We have a tree with N vertices and N-1 edges, respectively numbered 1, 2,\cdots, N and 1, 2, \cdots, N-1. Edge i connects Vertex u_i and v_i.
For integers L, R (1 \leq L \leq R \leq N), let us define a function f(L, R) as follows:
* Let S be the set of the vertices numbered L through R. f(L, R) represents the number of connected components in the subgraph formed only from the vertex set S and the edges whose endpoints both belong to S.
Compute \sum_{L=1}^{N} \sum_{R=L}^{N} f(L, R).
Constraints
* 1 \leq N \leq 2 \times 10^5
* 1 \leq u_i, v_i \leq N
* The given graph is a tree.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
u_1 v_1
u_2 v_2
:
u_{N-1} v_{N-1}
Output
Print \sum_{L=1}^{N} \sum_{R=L}^{N} f(L, R).
Examples
Input
3
1 3
2 3
Output
7
Input
2
1 2
Output
3
Input
10
5 3
5 7
8 9
1 9
9 10
8 4
7 4
6 10
7 2
Output
113 | instruction | 0 | 20,937 | 13 | 41,874 |
"Correct Solution:
```
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)
N = int(input())
edges = 0
nodes = 0
for i in range(1, N+1):
nodes += i*(N-i+1)
for _ in range(N-1):
u, v = mapint()
if u>v:
u, v = v, u
edges += u*(N-v+1)
print(nodes-edges)
``` | output | 1 | 20,937 | 13 | 41,875 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a tree with N vertices and N-1 edges, respectively numbered 1, 2,\cdots, N and 1, 2, \cdots, N-1. Edge i connects Vertex u_i and v_i.
For integers L, R (1 \leq L \leq R \leq N), let us define a function f(L, R) as follows:
* Let S be the set of the vertices numbered L through R. f(L, R) represents the number of connected components in the subgraph formed only from the vertex set S and the edges whose endpoints both belong to S.
Compute \sum_{L=1}^{N} \sum_{R=L}^{N} f(L, R).
Constraints
* 1 \leq N \leq 2 \times 10^5
* 1 \leq u_i, v_i \leq N
* The given graph is a tree.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
u_1 v_1
u_2 v_2
:
u_{N-1} v_{N-1}
Output
Print \sum_{L=1}^{N} \sum_{R=L}^{N} f(L, R).
Examples
Input
3
1 3
2 3
Output
7
Input
2
1 2
Output
3
Input
10
5 3
5 7
8 9
1 9
9 10
8 4
7 4
6 10
7 2
Output
113
Submitted Solution:
```
N = int(input())
edge = 0
for i in range(N-1):
u,v = map(int,input().split())
if u > v:
a = u
u = v
v = a
edge += u*(N-v+1)
vertice = 0
for i in range(1,N+1):
vertice += i*(N-i+1)
print(vertice-edge)
``` | instruction | 0 | 20,938 | 13 | 41,876 |
Yes | output | 1 | 20,938 | 13 | 41,877 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a tree with N vertices and N-1 edges, respectively numbered 1, 2,\cdots, N and 1, 2, \cdots, N-1. Edge i connects Vertex u_i and v_i.
For integers L, R (1 \leq L \leq R \leq N), let us define a function f(L, R) as follows:
* Let S be the set of the vertices numbered L through R. f(L, R) represents the number of connected components in the subgraph formed only from the vertex set S and the edges whose endpoints both belong to S.
Compute \sum_{L=1}^{N} \sum_{R=L}^{N} f(L, R).
Constraints
* 1 \leq N \leq 2 \times 10^5
* 1 \leq u_i, v_i \leq N
* The given graph is a tree.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
u_1 v_1
u_2 v_2
:
u_{N-1} v_{N-1}
Output
Print \sum_{L=1}^{N} \sum_{R=L}^{N} f(L, R).
Examples
Input
3
1 3
2 3
Output
7
Input
2
1 2
Output
3
Input
10
5 3
5 7
8 9
1 9
9 10
8 4
7 4
6 10
7 2
Output
113
Submitted Solution:
```
def main():
import sys
def input(): return sys.stdin.readline().rstrip()
n = int(input())
edges = 0
for i in range(n-1):
u, v = map(int, input().split())
edges += min(u, v)*(n-max(u, v)+1)
ans = n*(n+1)*(n+2)//6 - edges
print(ans)
if __name__ == '__main__':
main()
``` | instruction | 0 | 20,939 | 13 | 41,878 |
Yes | output | 1 | 20,939 | 13 | 41,879 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a tree with N vertices and N-1 edges, respectively numbered 1, 2,\cdots, N and 1, 2, \cdots, N-1. Edge i connects Vertex u_i and v_i.
For integers L, R (1 \leq L \leq R \leq N), let us define a function f(L, R) as follows:
* Let S be the set of the vertices numbered L through R. f(L, R) represents the number of connected components in the subgraph formed only from the vertex set S and the edges whose endpoints both belong to S.
Compute \sum_{L=1}^{N} \sum_{R=L}^{N} f(L, R).
Constraints
* 1 \leq N \leq 2 \times 10^5
* 1 \leq u_i, v_i \leq N
* The given graph is a tree.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
u_1 v_1
u_2 v_2
:
u_{N-1} v_{N-1}
Output
Print \sum_{L=1}^{N} \sum_{R=L}^{N} f(L, R).
Examples
Input
3
1 3
2 3
Output
7
Input
2
1 2
Output
3
Input
10
5 3
5 7
8 9
1 9
9 10
8 4
7 4
6 10
7 2
Output
113
Submitted Solution:
```
N = int(input())
V = 0
E = 0
for i in range(N+1):
V += i * (N-i+1)
for i in range(N-1):
a, b = map(int, input().split())
if a > b:
a, b = b, a
E += a * (N-b+1)
ans = V - E
print(ans)
``` | instruction | 0 | 20,940 | 13 | 41,880 |
Yes | output | 1 | 20,940 | 13 | 41,881 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a tree with N vertices and N-1 edges, respectively numbered 1, 2,\cdots, N and 1, 2, \cdots, N-1. Edge i connects Vertex u_i and v_i.
For integers L, R (1 \leq L \leq R \leq N), let us define a function f(L, R) as follows:
* Let S be the set of the vertices numbered L through R. f(L, R) represents the number of connected components in the subgraph formed only from the vertex set S and the edges whose endpoints both belong to S.
Compute \sum_{L=1}^{N} \sum_{R=L}^{N} f(L, R).
Constraints
* 1 \leq N \leq 2 \times 10^5
* 1 \leq u_i, v_i \leq N
* The given graph is a tree.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
u_1 v_1
u_2 v_2
:
u_{N-1} v_{N-1}
Output
Print \sum_{L=1}^{N} \sum_{R=L}^{N} f(L, R).
Examples
Input
3
1 3
2 3
Output
7
Input
2
1 2
Output
3
Input
10
5 3
5 7
8 9
1 9
9 10
8 4
7 4
6 10
7 2
Output
113
Submitted Solution:
```
ans = []
n = int(input())
for i in range(n):
x = i+1
y = n-i
ans.append(x*y)
pos = sum(ans)
cnt = 0
for i in range(n-1):
u,v = list(map(int,input().split()))
x,y = min(u,v),max(u,v)
cnt+=(x*(n-y+1))
print(pos-cnt)
``` | instruction | 0 | 20,941 | 13 | 41,882 |
Yes | output | 1 | 20,941 | 13 | 41,883 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a tree with N vertices and N-1 edges, respectively numbered 1, 2,\cdots, N and 1, 2, \cdots, N-1. Edge i connects Vertex u_i and v_i.
For integers L, R (1 \leq L \leq R \leq N), let us define a function f(L, R) as follows:
* Let S be the set of the vertices numbered L through R. f(L, R) represents the number of connected components in the subgraph formed only from the vertex set S and the edges whose endpoints both belong to S.
Compute \sum_{L=1}^{N} \sum_{R=L}^{N} f(L, R).
Constraints
* 1 \leq N \leq 2 \times 10^5
* 1 \leq u_i, v_i \leq N
* The given graph is a tree.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
u_1 v_1
u_2 v_2
:
u_{N-1} v_{N-1}
Output
Print \sum_{L=1}^{N} \sum_{R=L}^{N} f(L, R).
Examples
Input
3
1 3
2 3
Output
7
Input
2
1 2
Output
3
Input
10
5 3
5 7
8 9
1 9
9 10
8 4
7 4
6 10
7 2
Output
113
Submitted Solution:
```
N = int(input())
q = [sorted(list(map(int, input().split()))) for _ in range(N-1)]
ans = 0
for l in range(1, N+1):
ql = [q[i] for i in range(N-1) if l <= q[i][0]]
for r in range(l, N+1):
points = (r - l + 1)
edges = len([qli for qli in ql if qli[1] <= r])
res = points - edges
ans += res
print(ans)
``` | instruction | 0 | 20,942 | 13 | 41,884 |
No | output | 1 | 20,942 | 13 | 41,885 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a tree with N vertices and N-1 edges, respectively numbered 1, 2,\cdots, N and 1, 2, \cdots, N-1. Edge i connects Vertex u_i and v_i.
For integers L, R (1 \leq L \leq R \leq N), let us define a function f(L, R) as follows:
* Let S be the set of the vertices numbered L through R. f(L, R) represents the number of connected components in the subgraph formed only from the vertex set S and the edges whose endpoints both belong to S.
Compute \sum_{L=1}^{N} \sum_{R=L}^{N} f(L, R).
Constraints
* 1 \leq N \leq 2 \times 10^5
* 1 \leq u_i, v_i \leq N
* The given graph is a tree.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
u_1 v_1
u_2 v_2
:
u_{N-1} v_{N-1}
Output
Print \sum_{L=1}^{N} \sum_{R=L}^{N} f(L, R).
Examples
Input
3
1 3
2 3
Output
7
Input
2
1 2
Output
3
Input
10
5 3
5 7
8 9
1 9
9 10
8 4
7 4
6 10
7 2
Output
113
Submitted Solution:
```
n = int(input())
nodes = []
components = 0
for i in range(n - 1):
edge = list(map(int, input().split()))
node_max = max(edge)
node_min = min(edge)
if node_min not in nodes:
nodes.append(node_min)
components += node_min * (n - node_min + 1)
if node_max not in nodes:
nodes.append(node_max)
components += node_max * (n - node_max + 1)
num_edge = node_min * (n - node_max + 1)
components -= num_edge
if n == 1:
components = 1
print(components)
``` | instruction | 0 | 20,943 | 13 | 41,886 |
No | output | 1 | 20,943 | 13 | 41,887 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a tree with N vertices and N-1 edges, respectively numbered 1, 2,\cdots, N and 1, 2, \cdots, N-1. Edge i connects Vertex u_i and v_i.
For integers L, R (1 \leq L \leq R \leq N), let us define a function f(L, R) as follows:
* Let S be the set of the vertices numbered L through R. f(L, R) represents the number of connected components in the subgraph formed only from the vertex set S and the edges whose endpoints both belong to S.
Compute \sum_{L=1}^{N} \sum_{R=L}^{N} f(L, R).
Constraints
* 1 \leq N \leq 2 \times 10^5
* 1 \leq u_i, v_i \leq N
* The given graph is a tree.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
u_1 v_1
u_2 v_2
:
u_{N-1} v_{N-1}
Output
Print \sum_{L=1}^{N} \sum_{R=L}^{N} f(L, R).
Examples
Input
3
1 3
2 3
Output
7
Input
2
1 2
Output
3
Input
10
5 3
5 7
8 9
1 9
9 10
8 4
7 4
6 10
7 2
Output
113
Submitted Solution:
```
def main() {
n = int(input())
ans = n * (n + 1) * (n + 2) // 6;
for i in range(n):
x, y = map(int, input().split())
if (x > y) { y, x = x, y; }
ans -= x * (n - y + 1);
}
print(ans);
}
main()
``` | instruction | 0 | 20,944 | 13 | 41,888 |
No | output | 1 | 20,944 | 13 | 41,889 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a tree with N vertices and N-1 edges, respectively numbered 1, 2,\cdots, N and 1, 2, \cdots, N-1. Edge i connects Vertex u_i and v_i.
For integers L, R (1 \leq L \leq R \leq N), let us define a function f(L, R) as follows:
* Let S be the set of the vertices numbered L through R. f(L, R) represents the number of connected components in the subgraph formed only from the vertex set S and the edges whose endpoints both belong to S.
Compute \sum_{L=1}^{N} \sum_{R=L}^{N} f(L, R).
Constraints
* 1 \leq N \leq 2 \times 10^5
* 1 \leq u_i, v_i \leq N
* The given graph is a tree.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
u_1 v_1
u_2 v_2
:
u_{N-1} v_{N-1}
Output
Print \sum_{L=1}^{N} \sum_{R=L}^{N} f(L, R).
Examples
Input
3
1 3
2 3
Output
7
Input
2
1 2
Output
3
Input
10
5 3
5 7
8 9
1 9
9 10
8 4
7 4
6 10
7 2
Output
113
Submitted Solution:
```
import numpy as np
n = int(input())
edges = [list(map(int, input().split())) for _ in range(n - 1)]
grid = np.zeros([n, n])
for i in range(n - 1):
edge = sorted(edges[i])
grid[edge[0] - 1, edge[1] - 1] = 1
components = 0
for i in range(n):
for j in range(i, n):
loss = grid[i:j + 1, i:j + 1].sum()
components += j - i + 1 - loss
print(int(components))
``` | instruction | 0 | 20,945 | 13 | 41,890 |
No | output | 1 | 20,945 | 13 | 41,891 |
Provide a correct Python 3 solution for this coding contest problem.
AtCoDeer the deer wants a directed graph that satisfies the following conditions:
* The number of vertices, N, is at most 300.
* There must not be self-loops or multiple edges.
* The vertices are numbered from 1 through N.
* Each edge has either an integer weight between 0 and 100 (inclusive), or a label `X` or `Y`.
* For every pair of two integers (x,y) such that 1 ≤ x ≤ A, 1 ≤ y ≤ B, the shortest distance from Vertex S to Vertex T in the graph where the edges labeled `X` have the weight x and the edges labeled `Y` have the weight y, is d_{x,y}.
Construct such a graph (and a pair of S and T) for him, or report that it does not exist. Refer to Output section for output format.
Constraints
* 1 ≤ A,B ≤ 10
* 1 ≤ d_{x,y} ≤ 100 (1 ≤ x ≤ A, 1 ≤ y ≤ B)
* All input values are integers.
Input
Input is given from Standard Input in the following format:
A B
d_{1,1} d_{1,2} .. d_{1,B}
d_{2,1} d_{2,2} .. d_{2,B}
:
d_{A,1} d_{A,2} .. d_{A,B}
Output
If no graph satisfies the condition, print `Impossible`.
If there exists a graph that satisfies the condition, print `Possible` in the first line. Then, in the subsequent lines, print the constructed graph in the following format:
N M
u_1 v_1 c_1
u_2 v_2 c_2
:
u_M v_M c_M
S T
Here, M is the number of the edges, and u_i, v_i, c_i represent edges as follows: there is an edge from Vertex u_i to Vertex v_i whose weight or label is c_i.
Also refer to Sample Outputs.
Examples
Input
2 3
1 2 2
1 2 3
Output
Possible
3 4
1 2 X
2 3 1
3 2 Y
1 3 Y
1 3
Input
1 3
100 50 1
Output
Impossible | instruction | 0 | 21,014 | 13 | 42,028 |
"Correct Solution:
```
# seishin.py
A, B = map(int, input().split())
D = [list(map(int, input().split())) for i in range(A)]
N = 101
F = [[0]*N for i in range(N)]
for a in range(N):
for b in range(N):
res = 0
for x in range(A):
for y in range(B):
res = max(res, D[x][y] - (x+1)*a - (y+1)*b)
F[a][b] = res
for x in range(A):
for y in range(B):
v = 101
for a in range(N):
for b in range(N):
v = min(v, (x+1)*a + (y+1)*b + F[a][b])
if v != D[x][y]:
print("Impossible")
exit(0)
print("Possible")
print(2*N, N**2 + 2*(N-1))
for i in range(N-1):
print(i+1, i+2, "X")
print(103+i, 102+i, "Y")
for a in range(N):
for b in range(N):
print(a+1, 102+b, F[a][b])
print(1, 102)
``` | output | 1 | 21,014 | 13 | 42,029 |
Provide a correct Python 3 solution for this coding contest problem.
AtCoDeer the deer wants a directed graph that satisfies the following conditions:
* The number of vertices, N, is at most 300.
* There must not be self-loops or multiple edges.
* The vertices are numbered from 1 through N.
* Each edge has either an integer weight between 0 and 100 (inclusive), or a label `X` or `Y`.
* For every pair of two integers (x,y) such that 1 ≤ x ≤ A, 1 ≤ y ≤ B, the shortest distance from Vertex S to Vertex T in the graph where the edges labeled `X` have the weight x and the edges labeled `Y` have the weight y, is d_{x,y}.
Construct such a graph (and a pair of S and T) for him, or report that it does not exist. Refer to Output section for output format.
Constraints
* 1 ≤ A,B ≤ 10
* 1 ≤ d_{x,y} ≤ 100 (1 ≤ x ≤ A, 1 ≤ y ≤ B)
* All input values are integers.
Input
Input is given from Standard Input in the following format:
A B
d_{1,1} d_{1,2} .. d_{1,B}
d_{2,1} d_{2,2} .. d_{2,B}
:
d_{A,1} d_{A,2} .. d_{A,B}
Output
If no graph satisfies the condition, print `Impossible`.
If there exists a graph that satisfies the condition, print `Possible` in the first line. Then, in the subsequent lines, print the constructed graph in the following format:
N M
u_1 v_1 c_1
u_2 v_2 c_2
:
u_M v_M c_M
S T
Here, M is the number of the edges, and u_i, v_i, c_i represent edges as follows: there is an edge from Vertex u_i to Vertex v_i whose weight or label is c_i.
Also refer to Sample Outputs.
Examples
Input
2 3
1 2 2
1 2 3
Output
Possible
3 4
1 2 X
2 3 1
3 2 Y
1 3 Y
1 3
Input
1 3
100 50 1
Output
Impossible | instruction | 0 | 21,015 | 13 | 42,030 |
"Correct Solution:
```
a, b = map(int, input().split())
distances = [list(map(int, input().split())) for _ in range(a)]
n = 202
m = 0
g = [[] for _ in range(n)]
for i in range(100):
g[i].append((i + 1, 'X'))
m += 1
for i in range(100):
g[i + 101].append((i + 102, 'Y'))
m += 1
for p in range(0, 101):
for q in range(0, 101):
r = -10000000
for i in range(a):
for j in range(b):
r = max(r, distances[i][j] - p * (i + 1) - q * (j + 1))
if r >= 0:
g[p].append((n - q - 1, r))
m += 1
for i in range(a):
for j in range(b):
d = [float('inf')] * n
d[0] = 0
s = [0]
while s:
p = s.pop()
for to, dist in g[p]:
if dist == 'X':
dist = i + 1
elif dist == 'Y':
dist = j + 1
s.append(to)
d[to] = min(d[to], d[p] + dist)
if d[-1] != distances[i][j]:
print('Impossible')
exit(0)
print('Possible')
print(n, m)
for i in range(n):
for node, dist in g[i]:
print(i + 1, node + 1, dist)
print(1, n)
``` | output | 1 | 21,015 | 13 | 42,031 |
Provide a correct Python 3 solution for this coding contest problem.
AtCoDeer the deer wants a directed graph that satisfies the following conditions:
* The number of vertices, N, is at most 300.
* There must not be self-loops or multiple edges.
* The vertices are numbered from 1 through N.
* Each edge has either an integer weight between 0 and 100 (inclusive), or a label `X` or `Y`.
* For every pair of two integers (x,y) such that 1 ≤ x ≤ A, 1 ≤ y ≤ B, the shortest distance from Vertex S to Vertex T in the graph where the edges labeled `X` have the weight x and the edges labeled `Y` have the weight y, is d_{x,y}.
Construct such a graph (and a pair of S and T) for him, or report that it does not exist. Refer to Output section for output format.
Constraints
* 1 ≤ A,B ≤ 10
* 1 ≤ d_{x,y} ≤ 100 (1 ≤ x ≤ A, 1 ≤ y ≤ B)
* All input values are integers.
Input
Input is given from Standard Input in the following format:
A B
d_{1,1} d_{1,2} .. d_{1,B}
d_{2,1} d_{2,2} .. d_{2,B}
:
d_{A,1} d_{A,2} .. d_{A,B}
Output
If no graph satisfies the condition, print `Impossible`.
If there exists a graph that satisfies the condition, print `Possible` in the first line. Then, in the subsequent lines, print the constructed graph in the following format:
N M
u_1 v_1 c_1
u_2 v_2 c_2
:
u_M v_M c_M
S T
Here, M is the number of the edges, and u_i, v_i, c_i represent edges as follows: there is an edge from Vertex u_i to Vertex v_i whose weight or label is c_i.
Also refer to Sample Outputs.
Examples
Input
2 3
1 2 2
1 2 3
Output
Possible
3 4
1 2 X
2 3 1
3 2 Y
1 3 Y
1 3
Input
1 3
100 50 1
Output
Impossible | instruction | 0 | 21,016 | 13 | 42,032 |
"Correct Solution:
```
from heapq import *
import sys
sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def impossible():
print("Impossible")
exit()
def main():
h, w = map(int, input().split())
t = [list(map(int, input().split())) for _ in range(h)]
edge = [[0] * 101 for _ in range(101)]
# XとYをつなぐ辺の作成
for n_x in range(101):
for n_y in range(101):
cost = 0
for i in range(h):
for j in range(w):
x, y = i + 1, j + 1
cur_cost = t[i][j] - x * n_x - y * n_y
if cur_cost > cost: cost = cur_cost
edge[n_x][n_y] = cost
# p2D(edge)
# 条件を満たしているかのチェック
for i in range(h):
for j in range(w):
x, y = i + 1, j + 1
tij = t[i][j]
min_dist = 1000
for n_x in range(101):
for n_y in range(101):
dist = x * n_x + y * n_y + edge[n_x][n_y]
if dist < min_dist: min_dist = dist
if tij != min_dist: impossible()
print("Possible")
print(202, 101 * 101 + 200)
for u in range(1, 101):
print(u, u + 1, "X")
for u in range(102, 202):
print(u, u + 1, "Y")
for n_x in range(101):
for n_y in range(101):
print(n_x + 1, 202 - n_y, edge[n_x][n_y])
print(1, 202)
main()
``` | output | 1 | 21,016 | 13 | 42,033 |
Provide a correct Python 3 solution for this coding contest problem.
AtCoDeer the deer wants a directed graph that satisfies the following conditions:
* The number of vertices, N, is at most 300.
* There must not be self-loops or multiple edges.
* The vertices are numbered from 1 through N.
* Each edge has either an integer weight between 0 and 100 (inclusive), or a label `X` or `Y`.
* For every pair of two integers (x,y) such that 1 ≤ x ≤ A, 1 ≤ y ≤ B, the shortest distance from Vertex S to Vertex T in the graph where the edges labeled `X` have the weight x and the edges labeled `Y` have the weight y, is d_{x,y}.
Construct such a graph (and a pair of S and T) for him, or report that it does not exist. Refer to Output section for output format.
Constraints
* 1 ≤ A,B ≤ 10
* 1 ≤ d_{x,y} ≤ 100 (1 ≤ x ≤ A, 1 ≤ y ≤ B)
* All input values are integers.
Input
Input is given from Standard Input in the following format:
A B
d_{1,1} d_{1,2} .. d_{1,B}
d_{2,1} d_{2,2} .. d_{2,B}
:
d_{A,1} d_{A,2} .. d_{A,B}
Output
If no graph satisfies the condition, print `Impossible`.
If there exists a graph that satisfies the condition, print `Possible` in the first line. Then, in the subsequent lines, print the constructed graph in the following format:
N M
u_1 v_1 c_1
u_2 v_2 c_2
:
u_M v_M c_M
S T
Here, M is the number of the edges, and u_i, v_i, c_i represent edges as follows: there is an edge from Vertex u_i to Vertex v_i whose weight or label is c_i.
Also refer to Sample Outputs.
Examples
Input
2 3
1 2 2
1 2 3
Output
Possible
3 4
1 2 X
2 3 1
3 2 Y
1 3 Y
1 3
Input
1 3
100 50 1
Output
Impossible | instruction | 0 | 21,017 | 13 | 42,034 |
"Correct Solution:
```
#!/usr/bin/env python3
def solve(x_max, y_max, d):
c_table = [[-1] * 100 for _ in range(100)]
for a in range(100):
c_table_a = c_table[a]
for x in range(x_max):
ax = a * (x + 1)
dx = d[x]
for b in range(100):
c = -1
for y in range(y_max):
c = max(c, dx[y] - ax - b * (y + 1))
c_table_a[b] = max(c_table_a[b], c)
ab_max = 0
c_set = {}
for x in range(x_max):
dx = d[x]
for y in range(y_max):
dxy = d[x][y]
f = False
for a in range(100):
c_table_a = c_table[a]
ax = a * (x + 1)
for b in range(100):
if dxy == ax + b * (y + 1) + c_table_a[b]:
ab_max = max(ab_max, a, b)
c_set[(a, b)] = c_table_a[b]
f = True
break
if f: break
if not f:
print('Impossible')
return
print('Possible')
print('{} {}'.format((ab_max + 1) * 2, ab_max * 2 + len(c_set)))
for i in range(ab_max):
print('{} {} X'.format(i + 1, i + 2))
for i in range(ab_max):
print('{} {} Y'.format(ab_max + i + 3, ab_max + i + 2))
for k, c in c_set.items():
print('{} {} {}'.format(k[0] + 1, k[1] + ab_max + 2, c))
print('{} {}'.format(1, ab_max + 2))
def main():
A, B = input().split()
A = int(A)
B = int(B)
d = [list(map(int, input().split())) for _ in range(A)]
solve(A, B, d)
if __name__ == '__main__':
main()
``` | output | 1 | 21,017 | 13 | 42,035 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.