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.
For a given weighted graph G(V, E) and a source r, find the source shortest path to each vertex from the source (SSSP: Single Source Shortest Path).
Constraints
* 1 ≤ |V| ≤ 100000
* 0 ≤ di ≤ 10000
* 0 ≤ |E| ≤ 500000
* There are no parallel edges
* There are no self-loops
Input
An edge-weighted graph G (V, E) and the source r.
|V| |E| r
s0 t0 d0
s1 t1 d1
:
s|E|-1 t|E|-1 d|E|-1
|V| is the number of vertices and |E| is the number of edges in G. The graph vertices are named with the numbers 0, 1,..., |V|-1 respectively. r is the source of the graph.
si and ti represent source and target vertices of i-th edge (directed) and di represents the cost of the i-th edge.
Output
Print the costs of SSSP in the following format.
c0
c1
:
c|V|-1
The output consists of |V| lines. Print the cost of the shortest path from the source r to each vertex 0, 1, ... |V|-1 in order. If there is no path from the source to a vertex, print INF.
Examples
Input
4 5 0
0 1 1
0 2 4
1 2 2
2 3 1
1 3 5
Output
0
1
3
4
Input
4 6 1
0 1 1
0 2 4
2 0 1
1 2 2
3 1 1
3 2 5
Output
3
0
2
INF
Submitted Solution:
```
# ダイクストラ法の練習に.
from heapq import heapify, heappop, heappush
def main():
vertex, edge, root = map(int, input().split())
max_val = 10 ** 10
graph = [[] for _ in range(vertex)]
for _ in range(edge):
s, t, d = map(int, input().split())
graph[s].append((t, d))
dist = [max_val for _ in range(vertex)]
dist[root] = 0
q = []
heapify(q)
heappush(q, (0, root))
while q:
now_d, now_v = heappop(q)
for next_v, length in graph[now_v]:
now_dist = dist[now_v] + length
if dist[next_v] > now_dist:
dist[next_v] = now_dist
heappush(q, (dist[next_v], next_v))
for d in dist:
print("INF" if d == max_val else d)
if __name__ == '__main__':
main()
``` | instruction | 0 | 72,474 | 13 | 144,948 |
Yes | output | 1 | 72,474 | 13 | 144,949 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For a given weighted graph G(V, E) and a source r, find the source shortest path to each vertex from the source (SSSP: Single Source Shortest Path).
Constraints
* 1 ≤ |V| ≤ 100000
* 0 ≤ di ≤ 10000
* 0 ≤ |E| ≤ 500000
* There are no parallel edges
* There are no self-loops
Input
An edge-weighted graph G (V, E) and the source r.
|V| |E| r
s0 t0 d0
s1 t1 d1
:
s|E|-1 t|E|-1 d|E|-1
|V| is the number of vertices and |E| is the number of edges in G. The graph vertices are named with the numbers 0, 1,..., |V|-1 respectively. r is the source of the graph.
si and ti represent source and target vertices of i-th edge (directed) and di represents the cost of the i-th edge.
Output
Print the costs of SSSP in the following format.
c0
c1
:
c|V|-1
The output consists of |V| lines. Print the cost of the shortest path from the source r to each vertex 0, 1, ... |V|-1 in order. If there is no path from the source to a vertex, print INF.
Examples
Input
4 5 0
0 1 1
0 2 4
1 2 2
2 3 1
1 3 5
Output
0
1
3
4
Input
4 6 1
0 1 1
0 2 4
2 0 1
1 2 2
3 1 1
3 2 5
Output
3
0
2
INF
Submitted Solution:
```
'''
????????£????????????????????????
'''
import queue
v,e,s=map(int,input().split())
edge=[]
inf =1000000100
for i in range(v):
edge.append([])
for i in range(e):
vs,vt,d=map(int,input().split())
edge[vs].append((vt,d))
dis=[]
for i in range(v):
dis.append(inf)
dis[s]=0
q = queue.PriorityQueue()
q.put((0,s))
'''
while(not q.empty()):
now=q.get()
nowv=now[1]
for (vt,d) in edge[nowv]:
if dis[vt]>dis[nowv]+d:
dis[vt]=dis[nowv]+d
q.put((dis[vt],vt))
'''
finished=0
while(finished<v and not q.empty()):
now=q.get()
nowv=now[1]
if now[0]>dis[nowv] continue;
for (vt,d) in edge[nowv]:
if dis[vt]>dis[nowv]+d:
dis[vt]=dis[nowv]+d
q.put((dis[vt],vt))
finished+=1
for i in range(v):
if dis[i]>=inf:
print("INF")
else:
print(dis[i])
``` | instruction | 0 | 72,475 | 13 | 144,950 |
No | output | 1 | 72,475 | 13 | 144,951 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For a given weighted graph G(V, E) and a source r, find the source shortest path to each vertex from the source (SSSP: Single Source Shortest Path).
Constraints
* 1 ≤ |V| ≤ 100000
* 0 ≤ di ≤ 10000
* 0 ≤ |E| ≤ 500000
* There are no parallel edges
* There are no self-loops
Input
An edge-weighted graph G (V, E) and the source r.
|V| |E| r
s0 t0 d0
s1 t1 d1
:
s|E|-1 t|E|-1 d|E|-1
|V| is the number of vertices and |E| is the number of edges in G. The graph vertices are named with the numbers 0, 1,..., |V|-1 respectively. r is the source of the graph.
si and ti represent source and target vertices of i-th edge (directed) and di represents the cost of the i-th edge.
Output
Print the costs of SSSP in the following format.
c0
c1
:
c|V|-1
The output consists of |V| lines. Print the cost of the shortest path from the source r to each vertex 0, 1, ... |V|-1 in order. If there is no path from the source to a vertex, print INF.
Examples
Input
4 5 0
0 1 1
0 2 4
1 2 2
2 3 1
1 3 5
Output
0
1
3
4
Input
4 6 1
0 1 1
0 2 4
2 0 1
1 2 2
3 1 1
3 2 5
Output
3
0
2
INF
Submitted Solution:
```
from collections import defaultdict
import heapq
with open("input.txt","r") as f:
vertice,edge,source = map(int,f.readline().split(" "))
link = defaultdict(dict)
weight = defaultdict(dict)
for e in range(edge):
i,j,w = map(int,f.readline().split(" "))
link[i] = link.get(i,set()) | {j}
weight[i].update({j:w})
#print(weight,link)
for v in range(vertice):
goal = v
queue = []
heapq.heappush(queue,(0,source))
heapq.heapify(queue)
went = set()
while queue != []:
#print(queue)
now_cost,here = heapq.heappop(queue)
#print(now_cost,here,queue)
if here == goal:
print(now_cost)
break
can_go = link[here]
not_went = set(can_go) - went
#print(v,here,now_cost,not_went,queue)
for nw in not_went:
next_cost = now_cost + weight[here][nw]
heapq.heappush(queue,(next_cost,nw))
went = went | {here}
else:
print("INF")
``` | instruction | 0 | 72,476 | 13 | 144,952 |
No | output | 1 | 72,476 | 13 | 144,953 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For a given weighted graph G(V, E) and a source r, find the source shortest path to each vertex from the source (SSSP: Single Source Shortest Path).
Constraints
* 1 ≤ |V| ≤ 100000
* 0 ≤ di ≤ 10000
* 0 ≤ |E| ≤ 500000
* There are no parallel edges
* There are no self-loops
Input
An edge-weighted graph G (V, E) and the source r.
|V| |E| r
s0 t0 d0
s1 t1 d1
:
s|E|-1 t|E|-1 d|E|-1
|V| is the number of vertices and |E| is the number of edges in G. The graph vertices are named with the numbers 0, 1,..., |V|-1 respectively. r is the source of the graph.
si and ti represent source and target vertices of i-th edge (directed) and di represents the cost of the i-th edge.
Output
Print the costs of SSSP in the following format.
c0
c1
:
c|V|-1
The output consists of |V| lines. Print the cost of the shortest path from the source r to each vertex 0, 1, ... |V|-1 in order. If there is no path from the source to a vertex, print INF.
Examples
Input
4 5 0
0 1 1
0 2 4
1 2 2
2 3 1
1 3 5
Output
0
1
3
4
Input
4 6 1
0 1 1
0 2 4
2 0 1
1 2 2
3 1 1
3 2 5
Output
3
0
2
INF
Submitted Solution:
```
# Single Source Shortest Path
class node():
def __init__(self, v, cost):
self.v = v
self.cost = cost
infty = 999999999
dict_c = {}
[vertex, edge, r] = list(map(int, input("").split()))
root_c = [node(r, 0)]
final_c = [infty for i in range(vertex)]
final_c[r] = 0
for i in range(edge):
[e1, e2, c] = list(map(int, input("").split()))
if e1 == r:
root_c.append(node(e2, c))
else:
dict_c[(e1, e2)] = c
def min_heap(i):
global root_c
l = 2*i
r = 2*i + 1
if l <= len(root_c) - 1 and root_c[l].cost < root_c[i].cost:
min = l
else:
min = i
if r <= len(root_c) - 1 and root_c[r].cost < root_c[min].cost:
min = r
if min != i:
root_c[i], root_c[min] = root_c[min], root_c[i]
min_heap(min)
def extract_min():
global root_c
min = root_c[1]
root_c[1] = root_c[len(root_c) - 1]
del root_c[len(root_c) - 1]
min_heap(1)
return min
def decrease_key(v, c):
global root_c
root_c[v].cost = c
while v > 1 and root_c[int(v/2)].cost > root_c[v].cost:
root_c[int(v/2)], root_c[v] = root_c[v], root_c[int(v/2)]
v = int(v/2)
def min_insert(v, c):
global infty, root_c
for i in range(len(root_c)):
if root_c[i].v == v:
decrease_key(i,c)
return
root_c.append(node(v, infty))
decrease_key(len(root_c) - 1, c)
def Dijkstra(root, n):
global infty, dict_c, root_c, final_c
label = [i for i in range(n)]
del label[root]
while len(label) != 0:
if len(root_c) > 1:
min_node = extract_min()
else:
break
final_c[min_node.v] = min_node.cost
for i in range(len(label)):
if min_node.v == label[i]:
delete_label = i
continue
if ((min_node.v, label[i]) in dict_c.keys()) == True:
if final_c[label[i]] > final_c[min_node.v] + dict_c[(min_node.v, label[i])]:
final_c[label[i]] = final_c[min_node.v] + dict_c[(min_node.v, label[i])]
min_insert(label[i], final_c[label[i]])
del label[delete_label]
min_heap(1)
Dijkstra(r, vertex)
for i in range(vertex):
if final_c[i] == infty:
print("INF")
else:
print(final_c[i])
``` | instruction | 0 | 72,477 | 13 | 144,954 |
No | output | 1 | 72,477 | 13 | 144,955 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For a given weighted graph G(V, E) and a source r, find the source shortest path to each vertex from the source (SSSP: Single Source Shortest Path).
Constraints
* 1 ≤ |V| ≤ 100000
* 0 ≤ di ≤ 10000
* 0 ≤ |E| ≤ 500000
* There are no parallel edges
* There are no self-loops
Input
An edge-weighted graph G (V, E) and the source r.
|V| |E| r
s0 t0 d0
s1 t1 d1
:
s|E|-1 t|E|-1 d|E|-1
|V| is the number of vertices and |E| is the number of edges in G. The graph vertices are named with the numbers 0, 1,..., |V|-1 respectively. r is the source of the graph.
si and ti represent source and target vertices of i-th edge (directed) and di represents the cost of the i-th edge.
Output
Print the costs of SSSP in the following format.
c0
c1
:
c|V|-1
The output consists of |V| lines. Print the cost of the shortest path from the source r to each vertex 0, 1, ... |V|-1 in order. If there is no path from the source to a vertex, print INF.
Examples
Input
4 5 0
0 1 1
0 2 4
1 2 2
2 3 1
1 3 5
Output
0
1
3
4
Input
4 6 1
0 1 1
0 2 4
2 0 1
1 2 2
3 1 1
3 2 5
Output
3
0
2
INF
Submitted Solution:
```
# AOJ GRL_1_A
# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_1_A
from heapq import heappush, heappop
INF = 100000000
def solve(r, v, dic):
dp = []
q = []
dp = [INF] * v
dp[r] = 0
heappush(q, (0, r))
while q:
cost, vtx = heappop(q)
if dp[vtx] < cost:
continue
if vtx not in dic:
continue
for vtx1, cost1 in dic[vtx]:
if cost + cost1 < dp[vtx1]:
dp[vtx1] = cost + cost1
heappush(q, (cost + cost1, vtx1))
return dp
dic = {}
dp = []
line = input()
v, e, r = list(map(int, line.split()))
for _ in range(0, e):
line = input()
s, t, d = list(map(int, line.split()))
if s not in dic:
dic[s] = [[t, d]]
else:
dic[s] += [[t, d]]
dp = solve(r, v, dic)
for t in range(0, v):
print(dp[t] if dp[t] != INF else "INF")
``` | instruction | 0 | 72,478 | 13 | 144,956 |
No | output | 1 | 72,478 | 13 | 144,957 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Chouti was tired of the tedious homework, so he opened up an old programming problem he created years ago.
You are given a connected undirected graph with n vertices and m weighted edges. There are k special vertices: x_1, x_2, …, x_k.
Let's define the cost of the path as the maximum weight of the edges in it. And the distance between two vertexes as the minimum cost of the paths connecting them.
For each special vertex, find another special vertex which is farthest from it (in terms of the previous paragraph, i.e. the corresponding distance is maximum possible) and output the distance between them.
The original constraints are really small so he thought the problem was boring. Now, he raises the constraints and hopes you can solve it for him.
Input
The first line contains three integers n, m and k (2 ≤ k ≤ n ≤ 10^5, n-1 ≤ m ≤ 10^5) — the number of vertices, the number of edges and the number of special vertices.
The second line contains k distinct integers x_1, x_2, …, x_k (1 ≤ x_i ≤ n).
Each of the following m lines contains three integers u, v and w (1 ≤ u,v ≤ n, 1 ≤ w ≤ 10^9), denoting there is an edge between u and v of weight w. The given graph is undirected, so an edge (u, v) can be used in the both directions.
The graph may have multiple edges and self-loops.
It is guaranteed, that the graph is connected.
Output
The first and only line should contain k integers. The i-th integer is the distance between x_i and the farthest special vertex from it.
Examples
Input
2 3 2
2 1
1 2 3
1 2 2
2 2 1
Output
2 2
Input
4 5 3
1 2 3
1 2 5
4 2 1
2 3 2
1 4 4
1 3 3
Output
3 3 3
Note
In the first example, the distance between vertex 1 and 2 equals to 2 because one can walk through the edge of weight 2 connecting them. So the distance to the farthest node for both 1 and 2 equals to 2.
In the second example, one can find that distance between 1 and 2, distance between 1 and 3 are both 3 and the distance between 2 and 3 is 2.
The graph may have multiple edges between and self-loops, as in the first example. | instruction | 0 | 72,515 | 13 | 145,030 |
Tags: dsu, graphs, shortest paths, sortings
Correct Solution:
```
import sys
def int_reader():
yield from (int(d) for d in sys.stdin.read().split())
ints = int_reader()
n, m, k = [next(ints) for i in range(3)]
special = [next(ints) for i in range(k)]
is_special = [0]*(n+1)
for x in special:
is_special[x] = 1
edges = []
for i in range(m):
u, v, w = [next(ints) for j in range(3)]
edges.append((w, u, v))
edges.sort()
fu = [i for i in range(n+1)]
def find(x):
S = []
while fu[x] != x:
S.append(x)
x = fu[x]
for y in S:
fu[y] = x
return x
def union(e):
a, b = find(e[1]), find(e[2])
if is_special[b]:
a, b = b, a
fu[b] = a
ans = 0
for e in edges:
a, b = find(e[1]), find(e[2])
if a == b: continue
if is_special[a] and is_special[b]:
ans = e[0]
union(e)
print((str(ans) + ' ') * k)
``` | output | 1 | 72,515 | 13 | 145,031 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Chouti was tired of the tedious homework, so he opened up an old programming problem he created years ago.
You are given a connected undirected graph with n vertices and m weighted edges. There are k special vertices: x_1, x_2, …, x_k.
Let's define the cost of the path as the maximum weight of the edges in it. And the distance between two vertexes as the minimum cost of the paths connecting them.
For each special vertex, find another special vertex which is farthest from it (in terms of the previous paragraph, i.e. the corresponding distance is maximum possible) and output the distance between them.
The original constraints are really small so he thought the problem was boring. Now, he raises the constraints and hopes you can solve it for him.
Input
The first line contains three integers n, m and k (2 ≤ k ≤ n ≤ 10^5, n-1 ≤ m ≤ 10^5) — the number of vertices, the number of edges and the number of special vertices.
The second line contains k distinct integers x_1, x_2, …, x_k (1 ≤ x_i ≤ n).
Each of the following m lines contains three integers u, v and w (1 ≤ u,v ≤ n, 1 ≤ w ≤ 10^9), denoting there is an edge between u and v of weight w. The given graph is undirected, so an edge (u, v) can be used in the both directions.
The graph may have multiple edges and self-loops.
It is guaranteed, that the graph is connected.
Output
The first and only line should contain k integers. The i-th integer is the distance between x_i and the farthest special vertex from it.
Examples
Input
2 3 2
2 1
1 2 3
1 2 2
2 2 1
Output
2 2
Input
4 5 3
1 2 3
1 2 5
4 2 1
2 3 2
1 4 4
1 3 3
Output
3 3 3
Note
In the first example, the distance between vertex 1 and 2 equals to 2 because one can walk through the edge of weight 2 connecting them. So the distance to the farthest node for both 1 and 2 equals to 2.
In the second example, one can find that distance between 1 and 2, distance between 1 and 3 are both 3 and the distance between 2 and 3 is 2.
The graph may have multiple edges between and self-loops, as in the first example. | instruction | 0 | 72,516 | 13 | 145,032 |
Tags: dsu, graphs, shortest paths, sortings
Correct Solution:
```
import heapq
import os,io
input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
class UnionFind():
def __init__(self, n):
self.n = n
self.parents = [-1] * n
def find(self, x):
if self.parents[x] < 0:
return x
else:
self.parents[x] = self.find(self.parents[x])
return self.parents[x]
def union(self, x, y):
x = self.find(x)
y = self.find(y)
if x == y:
return
if self.parents[x] > self.parents[y]:
x, y = y, x
self.parents[x] += self.parents[y]
self.parents[y] = x
def size(self, x):
return -self.parents[self.find(x)]
def same(self, x, y):
return self.find(x) == self.find(y)
def members(self, x):
root = self.find(x)
return [i for i in range(self.n) if self.find(i) == root]
def roots(self):
return [i for i, x in enumerate(self.parents) if x < 0]
def group_count(self):
return len(self.roots())
def all_group_members(self):
return {r: self.members(r) for r in self.roots()}
def __str__(self):
return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())
n,m,k=map(int,input().split())
x=list(map(int,input().split()))
graph=[]
for i in range(m):
u,v,w=map(int,input().split())
graph.append(u)
graph.append(v)
graph.append(w)
l=0
r=10**9+1
def is_ok(arg):
uf=UnionFind(n)
for i in range(m):
if graph[i*3+2]>arg:
continue
uf.union(graph[i*3+0]-1,graph[i*3+1]-1)
flag=1
for i in range(k-1):
if not uf.same(x[i]-1,x[i+1]-1):
flag=0
break
return flag
def meguru_bisect(ng, ok):
'''
初期値のng,okを受け取り,is_okを満たす最小(最大)のokを返す
まずis_okを定義すべし
ng ok は とり得る最小の値-1 とり得る最大の値+1
最大最小が逆の場合はよしなにひっくり返す
'''
while (abs(ok - ng) > 1):
mid = (ok + ng) // 2
if is_ok(mid):
ok = mid
else:
ng = mid
return ok
p=str(meguru_bisect(0,10**9+1))
print(' '.join([p]*k))
``` | output | 1 | 72,516 | 13 | 145,033 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Chouti was tired of the tedious homework, so he opened up an old programming problem he created years ago.
You are given a connected undirected graph with n vertices and m weighted edges. There are k special vertices: x_1, x_2, …, x_k.
Let's define the cost of the path as the maximum weight of the edges in it. And the distance between two vertexes as the minimum cost of the paths connecting them.
For each special vertex, find another special vertex which is farthest from it (in terms of the previous paragraph, i.e. the corresponding distance is maximum possible) and output the distance between them.
The original constraints are really small so he thought the problem was boring. Now, he raises the constraints and hopes you can solve it for him.
Input
The first line contains three integers n, m and k (2 ≤ k ≤ n ≤ 10^5, n-1 ≤ m ≤ 10^5) — the number of vertices, the number of edges and the number of special vertices.
The second line contains k distinct integers x_1, x_2, …, x_k (1 ≤ x_i ≤ n).
Each of the following m lines contains three integers u, v and w (1 ≤ u,v ≤ n, 1 ≤ w ≤ 10^9), denoting there is an edge between u and v of weight w. The given graph is undirected, so an edge (u, v) can be used in the both directions.
The graph may have multiple edges and self-loops.
It is guaranteed, that the graph is connected.
Output
The first and only line should contain k integers. The i-th integer is the distance between x_i and the farthest special vertex from it.
Examples
Input
2 3 2
2 1
1 2 3
1 2 2
2 2 1
Output
2 2
Input
4 5 3
1 2 3
1 2 5
4 2 1
2 3 2
1 4 4
1 3 3
Output
3 3 3
Note
In the first example, the distance between vertex 1 and 2 equals to 2 because one can walk through the edge of weight 2 connecting them. So the distance to the farthest node for both 1 and 2 equals to 2.
In the second example, one can find that distance between 1 and 2, distance between 1 and 3 are both 3 and the distance between 2 and 3 is 2.
The graph may have multiple edges between and self-loops, as in the first example. | instruction | 0 | 72,517 | 13 | 145,034 |
Tags: dsu, graphs, shortest paths, sortings
Correct Solution:
```
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
g = []
f = list(range(n+1))
s = [0] * (n+1)
def search(n):
while f[n] != n:
# print(f[n])
f[n] = f[f[n]]
n = f[n]
return n
def can_merge(u, v):
u = search(u)
v = search(v)
f[u] = v
if u == v:
return False
r = s[u] > 0 and s[v] > 0
s[v] += s[u]
return r
for _ in range(m):
u,v,w = map(int, input().split())
g.append((u,v,w))
g.sort(key = lambda tup: tup[2])
for i in a:
s[i] += 1
ans = 0
for t in g:
if can_merge(t[0],t[1]):
ans = t[2]
print(' '.join([str(ans)] * k))
``` | output | 1 | 72,517 | 13 | 145,035 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Chouti was tired of the tedious homework, so he opened up an old programming problem he created years ago.
You are given a connected undirected graph with n vertices and m weighted edges. There are k special vertices: x_1, x_2, …, x_k.
Let's define the cost of the path as the maximum weight of the edges in it. And the distance between two vertexes as the minimum cost of the paths connecting them.
For each special vertex, find another special vertex which is farthest from it (in terms of the previous paragraph, i.e. the corresponding distance is maximum possible) and output the distance between them.
The original constraints are really small so he thought the problem was boring. Now, he raises the constraints and hopes you can solve it for him.
Input
The first line contains three integers n, m and k (2 ≤ k ≤ n ≤ 10^5, n-1 ≤ m ≤ 10^5) — the number of vertices, the number of edges and the number of special vertices.
The second line contains k distinct integers x_1, x_2, …, x_k (1 ≤ x_i ≤ n).
Each of the following m lines contains three integers u, v and w (1 ≤ u,v ≤ n, 1 ≤ w ≤ 10^9), denoting there is an edge between u and v of weight w. The given graph is undirected, so an edge (u, v) can be used in the both directions.
The graph may have multiple edges and self-loops.
It is guaranteed, that the graph is connected.
Output
The first and only line should contain k integers. The i-th integer is the distance between x_i and the farthest special vertex from it.
Examples
Input
2 3 2
2 1
1 2 3
1 2 2
2 2 1
Output
2 2
Input
4 5 3
1 2 3
1 2 5
4 2 1
2 3 2
1 4 4
1 3 3
Output
3 3 3
Note
In the first example, the distance between vertex 1 and 2 equals to 2 because one can walk through the edge of weight 2 connecting them. So the distance to the farthest node for both 1 and 2 equals to 2.
In the second example, one can find that distance between 1 and 2, distance between 1 and 3 are both 3 and the distance between 2 and 3 is 2.
The graph may have multiple edges between and self-loops, as in the first example. | instruction | 0 | 72,518 | 13 | 145,036 |
Tags: dsu, graphs, shortest paths, sortings
Correct Solution:
```
def main():
import sys
input = sys.stdin.readline
# def find(a):
# if par[a] == a:
# return a
# par[a] = find(par[a])
# return par[a]
def find(a):
upd = []
cur = a
while par[cur] != cur:
upd.append(cur)
cur = par[cur]
for x in upd:
par[x] = cur
return cur
def union(a, b):
a = find(a)
b = find(b)
if a == b:
return
par[b] = a
def mst():
ret = []
for edge in edges:
u, v, w = edge
u = find(u)
v = find(v)
if u != v:
union(u, v)
ret.append(edge)
return ret
def dfs(u, par):
for v, w in adj[u]:
if v != par:
dist[v] = max(dist[u], w)
dfs(v, u)
def bfs(u):
visit = [False] * (n+1)
from collections import deque
dq = deque()
dq.append(u)
visit[u] = True
while dq:
u = dq.popleft()
for v, w in adj[u]:
if not visit[v]:
dist[v] = max(dist[u], w)
dq.append(v)
visit[v] = True
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
# n = 50000
# m = 2 * n
# k = n
# a = [i for i in range(1, n+1)]
# import random
par = [0] * (n+1)
for i in range(1, n+1):
par[i] = i
edges = []
# for i in range(1, n+1):
# edge = (i, 1 if i+1 > n else i+1, random.randint(1, 1000000000))
# edge = (i, 1 if i+2 > n else i+2, random.randint(1, 1000000000))
# edges.append(edge)
for i in range(m):
edge = tuple(map(int, input().split()))
edges.append(edge)
edges.sort(key=lambda x: x[2])
edges = mst()
adj = [list() for i in range(n+1)]
for edge in edges:
u, v, w = edge
adj[u].append((v, w))
adj[v].append((u, w))
dist = [0] * (n+1)
# dfs(a[0], -1)
bfs(a[0])
ans = 0
for x in a:
ans = max(ans, dist[x])
ans = [ans] * k
print(*ans)
main()
``` | output | 1 | 72,518 | 13 | 145,037 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Chouti was tired of the tedious homework, so he opened up an old programming problem he created years ago.
You are given a connected undirected graph with n vertices and m weighted edges. There are k special vertices: x_1, x_2, …, x_k.
Let's define the cost of the path as the maximum weight of the edges in it. And the distance between two vertexes as the minimum cost of the paths connecting them.
For each special vertex, find another special vertex which is farthest from it (in terms of the previous paragraph, i.e. the corresponding distance is maximum possible) and output the distance between them.
The original constraints are really small so he thought the problem was boring. Now, he raises the constraints and hopes you can solve it for him.
Input
The first line contains three integers n, m and k (2 ≤ k ≤ n ≤ 10^5, n-1 ≤ m ≤ 10^5) — the number of vertices, the number of edges and the number of special vertices.
The second line contains k distinct integers x_1, x_2, …, x_k (1 ≤ x_i ≤ n).
Each of the following m lines contains three integers u, v and w (1 ≤ u,v ≤ n, 1 ≤ w ≤ 10^9), denoting there is an edge between u and v of weight w. The given graph is undirected, so an edge (u, v) can be used in the both directions.
The graph may have multiple edges and self-loops.
It is guaranteed, that the graph is connected.
Output
The first and only line should contain k integers. The i-th integer is the distance between x_i and the farthest special vertex from it.
Examples
Input
2 3 2
2 1
1 2 3
1 2 2
2 2 1
Output
2 2
Input
4 5 3
1 2 3
1 2 5
4 2 1
2 3 2
1 4 4
1 3 3
Output
3 3 3
Note
In the first example, the distance between vertex 1 and 2 equals to 2 because one can walk through the edge of weight 2 connecting them. So the distance to the farthest node for both 1 and 2 equals to 2.
In the second example, one can find that distance between 1 and 2, distance between 1 and 3 are both 3 and the distance between 2 and 3 is 2.
The graph may have multiple edges between and self-loops, as in the first example. | instruction | 0 | 72,519 | 13 | 145,038 |
Tags: dsu, graphs, shortest paths, sortings
Correct Solution:
```
import sys
input = sys.stdin.readline
# def find(a):
# if par[a] == a:
# return a
# par[a] = find(par[a])
# return par[a]
def find(a):
upd = []
cur = a
while par[cur] != cur:
upd.append(cur)
cur = par[cur]
for x in upd:
par[x] = cur
return cur
def union(a, b):
a = find(a)
b = find(b)
if a == b:
return
par[a] = b
def mst():
ret = []
for edge in edges:
u, v, w = edge
u = find(u)
v = find(v)
if u != v:
union(u, v)
ret.append(edge)
return ret
def dfs(u, par):
for v, w in adj[u]:
if v != par:
dist[v] = max(dist[u], w)
dfs(v, u)
def bfs(u):
visit = [False] * (n+1)
from collections import deque
dq = deque()
dq.append(u)
visit[u] = True
while dq:
u = dq.popleft()
for v, w in adj[u]:
if not visit[v]:
dist[v] = max(dist[u], w)
dq.append(v)
visit[v] = True
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
# n = 50000
# m = 2 * n
# k = n
# a = [i for i in range(1, n+1)]
# import random
par = [0] * (n+1)
for i in range(1, n+1):
par[i] = i
edges = []
# for i in range(1, n+1):
# edge = (i, 1 if i+1 > n else i+1, random.randint(1, 1000000000))
# edge = (i, 1 if i+2 > n else i+2, random.randint(1, 1000000000))
# edges.append(edge)
for i in range(m):
edge = tuple(map(int, input().split()))
edges.append(edge)
edges.sort(key=lambda x: x[2])
edges = mst()
adj = [list() for i in range(n+1)]
for edge in edges:
u, v, w = edge
adj[u].append((v, w))
adj[v].append((u, w))
dist = [0] * (n+1)
# dfs(a[0], -1)
bfs(a[0])
ans = 0
for x in a:
ans = max(ans, dist[x])
ans = [ans] * k
print(*ans)
``` | output | 1 | 72,519 | 13 | 145,039 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Chouti was tired of the tedious homework, so he opened up an old programming problem he created years ago.
You are given a connected undirected graph with n vertices and m weighted edges. There are k special vertices: x_1, x_2, …, x_k.
Let's define the cost of the path as the maximum weight of the edges in it. And the distance between two vertexes as the minimum cost of the paths connecting them.
For each special vertex, find another special vertex which is farthest from it (in terms of the previous paragraph, i.e. the corresponding distance is maximum possible) and output the distance between them.
The original constraints are really small so he thought the problem was boring. Now, he raises the constraints and hopes you can solve it for him.
Input
The first line contains three integers n, m and k (2 ≤ k ≤ n ≤ 10^5, n-1 ≤ m ≤ 10^5) — the number of vertices, the number of edges and the number of special vertices.
The second line contains k distinct integers x_1, x_2, …, x_k (1 ≤ x_i ≤ n).
Each of the following m lines contains three integers u, v and w (1 ≤ u,v ≤ n, 1 ≤ w ≤ 10^9), denoting there is an edge between u and v of weight w. The given graph is undirected, so an edge (u, v) can be used in the both directions.
The graph may have multiple edges and self-loops.
It is guaranteed, that the graph is connected.
Output
The first and only line should contain k integers. The i-th integer is the distance between x_i and the farthest special vertex from it.
Examples
Input
2 3 2
2 1
1 2 3
1 2 2
2 2 1
Output
2 2
Input
4 5 3
1 2 3
1 2 5
4 2 1
2 3 2
1 4 4
1 3 3
Output
3 3 3
Note
In the first example, the distance between vertex 1 and 2 equals to 2 because one can walk through the edge of weight 2 connecting them. So the distance to the farthest node for both 1 and 2 equals to 2.
In the second example, one can find that distance between 1 and 2, distance between 1 and 3 are both 3 and the distance between 2 and 3 is 2.
The graph may have multiple edges between and self-loops, as in the first example. | instruction | 0 | 72,520 | 13 | 145,040 |
Tags: dsu, graphs, shortest paths, sortings
Correct Solution:
```
import sys
import heapq
from collections import deque
n,m,k=map(int,sys.stdin.readline().split())
X=list(map(int,sys.stdin.readline().split()))
start=X[0]
EDGE=[list(map(int,sys.stdin.readline().split())) for i in range(m)]
COST_vertex=[[] for i in range(n+1)]
for i,j,w in EDGE:
COST_vertex[i].append((j,w))
COST_vertex[j].append((i,w))
MINCOST=[float("inf")]*(n+1)#求めたいリスト:startから頂点iへの最短距離
MINCOST[start]=0
checking=[(0,start)]#start時点のcostは0.最初はこれをチェックする.
while checking:
cost,checktown=heapq.heappop(checking)
#cost,checktownからの行き先をcheck.
#cost最小なものを確定し,確定したものはcheckingから抜く.
if MINCOST[checktown]<cost:#確定したものを再度チェックしなくて良い.
continue
for to,co in COST_vertex[checktown]:
if MINCOST[to]>max(MINCOST[checktown],co):
MINCOST[to]=max(MINCOST[checktown],co)
#MINCOST候補に追加
heapq.heappush(checking,(max(MINCOST[checktown],co),to))
ANSLIST=[MINCOST[X[i]] for i in range(1,k)]
ANS=(str(max(ANSLIST))+" ")*k
sys.stdout.write(str(ANS)+"\n")
``` | output | 1 | 72,520 | 13 | 145,041 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Chouti was tired of the tedious homework, so he opened up an old programming problem he created years ago.
You are given a connected undirected graph with n vertices and m weighted edges. There are k special vertices: x_1, x_2, …, x_k.
Let's define the cost of the path as the maximum weight of the edges in it. And the distance between two vertexes as the minimum cost of the paths connecting them.
For each special vertex, find another special vertex which is farthest from it (in terms of the previous paragraph, i.e. the corresponding distance is maximum possible) and output the distance between them.
The original constraints are really small so he thought the problem was boring. Now, he raises the constraints and hopes you can solve it for him.
Input
The first line contains three integers n, m and k (2 ≤ k ≤ n ≤ 10^5, n-1 ≤ m ≤ 10^5) — the number of vertices, the number of edges and the number of special vertices.
The second line contains k distinct integers x_1, x_2, …, x_k (1 ≤ x_i ≤ n).
Each of the following m lines contains three integers u, v and w (1 ≤ u,v ≤ n, 1 ≤ w ≤ 10^9), denoting there is an edge between u and v of weight w. The given graph is undirected, so an edge (u, v) can be used in the both directions.
The graph may have multiple edges and self-loops.
It is guaranteed, that the graph is connected.
Output
The first and only line should contain k integers. The i-th integer is the distance between x_i and the farthest special vertex from it.
Examples
Input
2 3 2
2 1
1 2 3
1 2 2
2 2 1
Output
2 2
Input
4 5 3
1 2 3
1 2 5
4 2 1
2 3 2
1 4 4
1 3 3
Output
3 3 3
Note
In the first example, the distance between vertex 1 and 2 equals to 2 because one can walk through the edge of weight 2 connecting them. So the distance to the farthest node for both 1 and 2 equals to 2.
In the second example, one can find that distance between 1 and 2, distance between 1 and 3 are both 3 and the distance between 2 and 3 is 2.
The graph may have multiple edges between and self-loops, as in the first example. | instruction | 0 | 72,521 | 13 | 145,042 |
Tags: dsu, graphs, shortest paths, sortings
Correct Solution:
```
class Union:
def __init__(self, n, list_k):
self.p = {i:i for i in range(n+1)}
self.rank = {i:0 for i in range(n+1)}
for k in list_k:
self.rank[k] = 1
def find(self, x):
if x < 0: return x
if self.p[x] != x:
self.p[x] = self.find(self.p[x])
return self.p[x]
def union(self, x, y):
if x < 0 or y < 0:return
x = self.find(x)
y = self.find(y)
if x != y:
if self.rank[x] < self.rank[y]:
self.p[x] = y
self.rank[y] += self.rank[x]
else:
self.p[y] = x
self.rank[x] += self.rank[y]
n, m, k = map(int, input().split())
list_k = list(map(int, input().split()))
edge = []
for _ in range(m):
u, v, w = map(int, input().split())
edge.append((u,v,w))
edge = sorted(edge, key=lambda x:x[2])
U = Union(n, list_k)
val = 0
for u, v, w in edge:
if u == v: continue
par_1 = U.find(u)
par_2 = U.find(v)
if par_1 == par_2:
continue
if U.rank[par_1] + U.rank[par_2] == k:
val = w
break
U.union(u, v)
s = ''
for _ in range(len(list_k)):
s += str(val) + ' '
print(s)
#2 3 2
#2 1
#1 2 3
#1 2 2
#2 2 1
``` | output | 1 | 72,521 | 13 | 145,043 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Chouti was tired of the tedious homework, so he opened up an old programming problem he created years ago.
You are given a connected undirected graph with n vertices and m weighted edges. There are k special vertices: x_1, x_2, …, x_k.
Let's define the cost of the path as the maximum weight of the edges in it. And the distance between two vertexes as the minimum cost of the paths connecting them.
For each special vertex, find another special vertex which is farthest from it (in terms of the previous paragraph, i.e. the corresponding distance is maximum possible) and output the distance between them.
The original constraints are really small so he thought the problem was boring. Now, he raises the constraints and hopes you can solve it for him.
Input
The first line contains three integers n, m and k (2 ≤ k ≤ n ≤ 10^5, n-1 ≤ m ≤ 10^5) — the number of vertices, the number of edges and the number of special vertices.
The second line contains k distinct integers x_1, x_2, …, x_k (1 ≤ x_i ≤ n).
Each of the following m lines contains three integers u, v and w (1 ≤ u,v ≤ n, 1 ≤ w ≤ 10^9), denoting there is an edge between u and v of weight w. The given graph is undirected, so an edge (u, v) can be used in the both directions.
The graph may have multiple edges and self-loops.
It is guaranteed, that the graph is connected.
Output
The first and only line should contain k integers. The i-th integer is the distance between x_i and the farthest special vertex from it.
Examples
Input
2 3 2
2 1
1 2 3
1 2 2
2 2 1
Output
2 2
Input
4 5 3
1 2 3
1 2 5
4 2 1
2 3 2
1 4 4
1 3 3
Output
3 3 3
Note
In the first example, the distance between vertex 1 and 2 equals to 2 because one can walk through the edge of weight 2 connecting them. So the distance to the farthest node for both 1 and 2 equals to 2.
In the second example, one can find that distance between 1 and 2, distance between 1 and 3 are both 3 and the distance between 2 and 3 is 2.
The graph may have multiple edges between and self-loops, as in the first example. | instruction | 0 | 72,522 | 13 | 145,044 |
Tags: dsu, graphs, shortest paths, sortings
Correct Solution:
```
import sys
input = sys.stdin.readline
n, m, k = list(map(int, input().split()))
x = list(map(int, input().split()))
uf = [-1 for _ in range(n+1)]
def find(p, uf):
if uf[p] < 0:
return p
uf[p] = find(uf[p], uf)
return uf[p]
def union(p, q, uf, specials):
proot = find(p, uf)
qroot = find(q, uf)
if proot == qroot:
return
elif uf[proot] > uf[qroot]:
uf[qroot] += uf[proot]
uf[proot] = qroot
specials[qroot] = specials[qroot] or specials[proot]
else:
uf[proot] += uf[qroot]
uf[qroot] = proot
specials[proot] = specials[qroot] or specials[proot]
edges = []
for _ in range(m):
u, v, w = list(map(int, input().split()))
edges.append((w, u, v))
edges = sorted(edges, key=lambda item: item[0])
specials = [0] * (n + 1)
for item in x:
specials[item] = 1
#special_edges = []
ans = -1
for w, u, v in edges:
ufather, vfather = find(u, uf), find(v, uf)
if ufather != vfather:
if specials[ufather] == 1 and specials[vfather] == 1:
#special_edges.append((w, u, v))
ans = max(ans, w)
union(u, v, uf, specials)
#special_edges = sorted(special_edges, key=lambda item:item[0])
res = [ans] * k
print(' '.join(map(str, res)))
``` | output | 1 | 72,522 | 13 | 145,045 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Chouti was tired of the tedious homework, so he opened up an old programming problem he created years ago.
You are given a connected undirected graph with n vertices and m weighted edges. There are k special vertices: x_1, x_2, …, x_k.
Let's define the cost of the path as the maximum weight of the edges in it. And the distance between two vertexes as the minimum cost of the paths connecting them.
For each special vertex, find another special vertex which is farthest from it (in terms of the previous paragraph, i.e. the corresponding distance is maximum possible) and output the distance between them.
The original constraints are really small so he thought the problem was boring. Now, he raises the constraints and hopes you can solve it for him.
Input
The first line contains three integers n, m and k (2 ≤ k ≤ n ≤ 10^5, n-1 ≤ m ≤ 10^5) — the number of vertices, the number of edges and the number of special vertices.
The second line contains k distinct integers x_1, x_2, …, x_k (1 ≤ x_i ≤ n).
Each of the following m lines contains three integers u, v and w (1 ≤ u,v ≤ n, 1 ≤ w ≤ 10^9), denoting there is an edge between u and v of weight w. The given graph is undirected, so an edge (u, v) can be used in the both directions.
The graph may have multiple edges and self-loops.
It is guaranteed, that the graph is connected.
Output
The first and only line should contain k integers. The i-th integer is the distance between x_i and the farthest special vertex from it.
Examples
Input
2 3 2
2 1
1 2 3
1 2 2
2 2 1
Output
2 2
Input
4 5 3
1 2 3
1 2 5
4 2 1
2 3 2
1 4 4
1 3 3
Output
3 3 3
Note
In the first example, the distance between vertex 1 and 2 equals to 2 because one can walk through the edge of weight 2 connecting them. So the distance to the farthest node for both 1 and 2 equals to 2.
In the second example, one can find that distance between 1 and 2, distance between 1 and 3 are both 3 and the distance between 2 and 3 is 2.
The graph may have multiple edges between and self-loops, as in the first example.
Submitted Solution:
```
# SHRi GANESHA author: Kunal Verma #
import os
import sys
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict
from functools import reduce
from io import BytesIO, IOBase
from itertools import combinations
from math import gcd, inf, sqrt, ceil, floor
#sys.setrecursionlimit(2*10**5)
def lcm(a, b):
return (a * b) // gcd(a, b)
'''
mod = 10 ** 9 + 7
fac = [1]
for i in range(1, 2 * 10 ** 5 + 1):
fac.append((fac[-1] * i) % mod)
fac_in = [pow(fac[-1], mod - 2, mod)]
for i in range(2 * 10 ** 5, 0, -1):
fac_in.append((fac_in[-1] * i) % mod)
fac_in.reverse()
def comb(a, b):
if a < b:
return 0
return (fac[a] * fac_in[b] * fac_in[a - b]) % mod
'''
MAXN = 1000004
spf = [0 for i in range(MAXN)]
def sieve():
spf[1] = 1
for i in range(2, MAXN):
spf[i] = i
for i in range(4, MAXN, 2):
spf[i] = 2
for i in range(3, ceil(sqrt(MAXN))):
if (spf[i] == i):
for j in range(i * i, MAXN, i):
if (spf[j] == j):
spf[j] = i
def getFactorization(x):
ret = Counter()
while (x != 1):
ret[spf[x]] += 1
x = x // spf[x]
return ret
def printDivisors(n):
i = 2
z = [1, n]
while i <= sqrt(n):
if (n % i == 0):
if (n / i == i):
z.append(i)
else:
z.append(i)
z.append(n // i)
i = i + 1
return z
def create(n, x, f):
pq = len(bin(n)[2:])
if f == 0:
tt = min
else:
tt = max
dp = [[inf] * n for _ in range(pq)]
dp[0] = x
for i in range(1, pq):
for j in range(n - (1 << i) + 1):
dp[i][j] = tt(dp[i - 1][j], dp[i - 1][j + (1 << (i - 1))])
return dp
def enquiry(l, r, dp, f):
if l > r:
return inf if not f else -inf
if f == 1:
tt = max
else:
tt = min
pq1 = len(bin(r - l + 1)[2:]) - 1
return tt(dp[pq1][l], dp[pq1][r - (1 << pq1) + 1])
def SieveOfEratosthenes(n):
prime = [True for i in range(n + 1)]
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * p, n + 1, p):
prime[i] = False
p += 1
x = []
for i in range(2, n + 1):
if prime[i]:
x.append(i)
return x
def main():
class dsu:
def __init__(self, n):
self.parent = [0] * (n + 1)
self.size = [0] * (n + 1)
self.sp = [0] * (n + 1)
def make_set(self, v):
self.parent[v] = v
self.size[v] = 1
def union_set(self, a, b):
a = self.find_set(a)
b = self.find_set(b)
if (a != b):
if self.size[a] < self.size[b]:
a, b = b, a
self.parent[b] = a
self.size[a] += self.size[b]
self.sp[a] += self.sp[b]
def find_set(self, v):
if (v == self.parent[v]):
return v
self.parent[v] = self.find_set(self.parent[v])
return self.parent[v]
n, m, k = map(int, input().split())
x = [int(X) for X in input().split()]
spec = [0] * (n + 1)
for i in x:
spec[i] = 1
ed = []
for i in range(m):
u, v, w = map(int, input().split())
ed.append((w, u, v))
ed.sort()
xx = dsu(n)
for i in x:
xx.sp[i] = 1
for i in range(1, n + 1):
xx.make_set(i)
adj = [[] for i in range(n + 1)]
for i in ed:
if xx.find_set(i[1]) != xx.find_set(i[2]):
if xx.sp[xx.find_set(i[1])] + xx.sp[xx.find_set(i[2])] == k:
an = i[0]
break
else:
xx.union_set(i[1], i[2])
u, v = i[1:3]
adj[u].append(v)
adj[v].append(u)
print(*[an] * k)
# Fast IO 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")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
if __name__ == '__main__':
main()
``` | instruction | 0 | 72,523 | 13 | 145,046 |
Yes | output | 1 | 72,523 | 13 | 145,047 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Chouti was tired of the tedious homework, so he opened up an old programming problem he created years ago.
You are given a connected undirected graph with n vertices and m weighted edges. There are k special vertices: x_1, x_2, …, x_k.
Let's define the cost of the path as the maximum weight of the edges in it. And the distance between two vertexes as the minimum cost of the paths connecting them.
For each special vertex, find another special vertex which is farthest from it (in terms of the previous paragraph, i.e. the corresponding distance is maximum possible) and output the distance between them.
The original constraints are really small so he thought the problem was boring. Now, he raises the constraints and hopes you can solve it for him.
Input
The first line contains three integers n, m and k (2 ≤ k ≤ n ≤ 10^5, n-1 ≤ m ≤ 10^5) — the number of vertices, the number of edges and the number of special vertices.
The second line contains k distinct integers x_1, x_2, …, x_k (1 ≤ x_i ≤ n).
Each of the following m lines contains three integers u, v and w (1 ≤ u,v ≤ n, 1 ≤ w ≤ 10^9), denoting there is an edge between u and v of weight w. The given graph is undirected, so an edge (u, v) can be used in the both directions.
The graph may have multiple edges and self-loops.
It is guaranteed, that the graph is connected.
Output
The first and only line should contain k integers. The i-th integer is the distance between x_i and the farthest special vertex from it.
Examples
Input
2 3 2
2 1
1 2 3
1 2 2
2 2 1
Output
2 2
Input
4 5 3
1 2 3
1 2 5
4 2 1
2 3 2
1 4 4
1 3 3
Output
3 3 3
Note
In the first example, the distance between vertex 1 and 2 equals to 2 because one can walk through the edge of weight 2 connecting them. So the distance to the farthest node for both 1 and 2 equals to 2.
In the second example, one can find that distance between 1 and 2, distance between 1 and 3 are both 3 and the distance between 2 and 3 is 2.
The graph may have multiple edges between and self-loops, as in the first example.
Submitted Solution:
```
import sys
def int_reader():
yield from (int(d) for d in sys.stdin.read().split())
ints = int_reader()
n, m, k = [next(ints) for i in range(3)]
sp = {next(ints) for i in range(k)}
edges = []
for i in range(m):
u, v, w = [next(ints) for j in range(3)]
edges.append((w, u, v))
edges.sort()
fu = [i for i in range(n+1)]
def find(x):
S = []
while fu[x] != x:
S.append(x)
x = fu[x]
for y in S:
fu[y] = x
return x
def union(a, b):
if b in sp:
a, b = b, a
fu[b] = a
ans = 0
for e in edges:
a, b = find(e[1]), find(e[2])
if a == b: continue
if a in sp and b in sp:
ans = e[0]
union(a, b)
print((str(ans) + ' ') * k)
``` | instruction | 0 | 72,524 | 13 | 145,048 |
Yes | output | 1 | 72,524 | 13 | 145,049 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Chouti was tired of the tedious homework, so he opened up an old programming problem he created years ago.
You are given a connected undirected graph with n vertices and m weighted edges. There are k special vertices: x_1, x_2, …, x_k.
Let's define the cost of the path as the maximum weight of the edges in it. And the distance between two vertexes as the minimum cost of the paths connecting them.
For each special vertex, find another special vertex which is farthest from it (in terms of the previous paragraph, i.e. the corresponding distance is maximum possible) and output the distance between them.
The original constraints are really small so he thought the problem was boring. Now, he raises the constraints and hopes you can solve it for him.
Input
The first line contains three integers n, m and k (2 ≤ k ≤ n ≤ 10^5, n-1 ≤ m ≤ 10^5) — the number of vertices, the number of edges and the number of special vertices.
The second line contains k distinct integers x_1, x_2, …, x_k (1 ≤ x_i ≤ n).
Each of the following m lines contains three integers u, v and w (1 ≤ u,v ≤ n, 1 ≤ w ≤ 10^9), denoting there is an edge between u and v of weight w. The given graph is undirected, so an edge (u, v) can be used in the both directions.
The graph may have multiple edges and self-loops.
It is guaranteed, that the graph is connected.
Output
The first and only line should contain k integers. The i-th integer is the distance between x_i and the farthest special vertex from it.
Examples
Input
2 3 2
2 1
1 2 3
1 2 2
2 2 1
Output
2 2
Input
4 5 3
1 2 3
1 2 5
4 2 1
2 3 2
1 4 4
1 3 3
Output
3 3 3
Note
In the first example, the distance between vertex 1 and 2 equals to 2 because one can walk through the edge of weight 2 connecting them. So the distance to the farthest node for both 1 and 2 equals to 2.
In the second example, one can find that distance between 1 and 2, distance between 1 and 3 are both 3 and the distance between 2 and 3 is 2.
The graph may have multiple edges between and self-loops, as in the first example.
Submitted Solution:
```
def g():
return map(int,input().split())
n,m,k=g()
p=list(range(n+1))
z=[0]*(n+1)
for x in g():
z[x]=1
e=[]
for i in range(m):
u,v,w=g()
e+=[(w,u,v)]
e=sorted(e)
def q(x):
if x!=p[x]:
p[x]=q(p[x])
return p[x]
for w,u,v in e:
u=q(u);v=q(v)
if u!=v:
if u%5==3:
u,v=v,u
p[u]=v;z[v]+=z[u]
if z[v]==k:
print((str(w)+' ')*k);exit(0)
``` | instruction | 0 | 72,525 | 13 | 145,050 |
Yes | output | 1 | 72,525 | 13 | 145,051 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Chouti was tired of the tedious homework, so he opened up an old programming problem he created years ago.
You are given a connected undirected graph with n vertices and m weighted edges. There are k special vertices: x_1, x_2, …, x_k.
Let's define the cost of the path as the maximum weight of the edges in it. And the distance between two vertexes as the minimum cost of the paths connecting them.
For each special vertex, find another special vertex which is farthest from it (in terms of the previous paragraph, i.e. the corresponding distance is maximum possible) and output the distance between them.
The original constraints are really small so he thought the problem was boring. Now, he raises the constraints and hopes you can solve it for him.
Input
The first line contains three integers n, m and k (2 ≤ k ≤ n ≤ 10^5, n-1 ≤ m ≤ 10^5) — the number of vertices, the number of edges and the number of special vertices.
The second line contains k distinct integers x_1, x_2, …, x_k (1 ≤ x_i ≤ n).
Each of the following m lines contains three integers u, v and w (1 ≤ u,v ≤ n, 1 ≤ w ≤ 10^9), denoting there is an edge between u and v of weight w. The given graph is undirected, so an edge (u, v) can be used in the both directions.
The graph may have multiple edges and self-loops.
It is guaranteed, that the graph is connected.
Output
The first and only line should contain k integers. The i-th integer is the distance between x_i and the farthest special vertex from it.
Examples
Input
2 3 2
2 1
1 2 3
1 2 2
2 2 1
Output
2 2
Input
4 5 3
1 2 3
1 2 5
4 2 1
2 3 2
1 4 4
1 3 3
Output
3 3 3
Note
In the first example, the distance between vertex 1 and 2 equals to 2 because one can walk through the edge of weight 2 connecting them. So the distance to the farthest node for both 1 and 2 equals to 2.
In the second example, one can find that distance between 1 and 2, distance between 1 and 3 are both 3 and the distance between 2 and 3 is 2.
The graph may have multiple edges between and self-loops, as in the first example.
Submitted Solution:
```
def main():
import sys
input = sys.stdin.readline
# def find(a):
# if par[a] == a:
# return a
# par[a] = find(par[a])
# return par[a]
def find(a):
upd = []
cur = a
while par[cur] != cur:
upd.append(cur)
cur = par[cur]
for x in upd:
par[x] = cur
return cur
def union(a, b):
a = find(a)
b = find(b)
if a == b:
return
if height[b] > height[a]:
a, b = b, a
par[b] = a
if height[a] == height[b]:
height[a] += 1
def mst():
ret = []
for edge in edges:
u, v, w = edge
u = find(u)
v = find(v)
if u != v:
union(u, v)
ret.append(edge)
return ret
def dfs(u, par):
for v, w in adj[u]:
if v != par:
dist[v] = max(dist[u], w)
dfs(v, u)
def bfs(u):
visit = [False] * (n+1)
from collections import deque
dq = deque()
dq.append(u)
visit[u] = True
while dq:
u = dq.popleft()
for v, w in adj[u]:
if not visit[v]:
dist[v] = max(dist[u], w)
dq.append(v)
visit[v] = True
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
# n = 50000
# m = 2 * n
# k = n
# a = [i for i in range(1, n+1)]
# import random
par = [0] * (n+1)
height = [1] * (n+1)
for i in range(1, n+1):
par[i] = i
height[i] = 1
edges = []
# for i in range(1, n+1):
# edge = (i, 1 if i+1 > n else i+1, random.randint(1, 1000000000))
# edge = (i, 1 if i+2 > n else i+2, random.randint(1, 1000000000))
# edges.append(edge)
for i in range(m):
edge = tuple(map(int, input().split()))
edges.append(edge)
edges.sort(key=lambda x: x[2])
edges = mst()
adj = [list() for i in range(n+1)]
for edge in edges:
u, v, w = edge
adj[u].append((v, w))
adj[v].append((u, w))
dist = [0] * (n+1)
# dfs(a[0], -1)
bfs(a[0])
ans = 0
for x in a:
ans = max(ans, dist[x])
ans = [ans] * k
print(*ans)
main()
``` | instruction | 0 | 72,526 | 13 | 145,052 |
Yes | output | 1 | 72,526 | 13 | 145,053 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Chouti was tired of the tedious homework, so he opened up an old programming problem he created years ago.
You are given a connected undirected graph with n vertices and m weighted edges. There are k special vertices: x_1, x_2, …, x_k.
Let's define the cost of the path as the maximum weight of the edges in it. And the distance between two vertexes as the minimum cost of the paths connecting them.
For each special vertex, find another special vertex which is farthest from it (in terms of the previous paragraph, i.e. the corresponding distance is maximum possible) and output the distance between them.
The original constraints are really small so he thought the problem was boring. Now, he raises the constraints and hopes you can solve it for him.
Input
The first line contains three integers n, m and k (2 ≤ k ≤ n ≤ 10^5, n-1 ≤ m ≤ 10^5) — the number of vertices, the number of edges and the number of special vertices.
The second line contains k distinct integers x_1, x_2, …, x_k (1 ≤ x_i ≤ n).
Each of the following m lines contains three integers u, v and w (1 ≤ u,v ≤ n, 1 ≤ w ≤ 10^9), denoting there is an edge between u and v of weight w. The given graph is undirected, so an edge (u, v) can be used in the both directions.
The graph may have multiple edges and self-loops.
It is guaranteed, that the graph is connected.
Output
The first and only line should contain k integers. The i-th integer is the distance between x_i and the farthest special vertex from it.
Examples
Input
2 3 2
2 1
1 2 3
1 2 2
2 2 1
Output
2 2
Input
4 5 3
1 2 3
1 2 5
4 2 1
2 3 2
1 4 4
1 3 3
Output
3 3 3
Note
In the first example, the distance between vertex 1 and 2 equals to 2 because one can walk through the edge of weight 2 connecting them. So the distance to the farthest node for both 1 and 2 equals to 2.
In the second example, one can find that distance between 1 and 2, distance between 1 and 3 are both 3 and the distance between 2 and 3 is 2.
The graph may have multiple edges between and self-loops, as in the first example.
Submitted Solution:
```
from sys import stdin,stdout
n,m,k=map(int,input().split())
x=set(list(map(int,input().split())))
c=[]
vis=[0]*(n+1)
for i in range(m):
a,b,w=(int(r) for r in stdin.readline().split())
if a!=b:
c.append([w,a,b])
c.sort()
i=0
while x:
if vis[c[i][1]]==0 or vis[c[i][2]]==0:
vis[c[i][1]]=1
vis[c[i][2]]=1
ans=c[i][0]
if c[i][1] in x:
x.remove(c[i][1])
if c[i][2] in x:
x.remove(c[i][2])
i+=1
stdout.write((str(ans)+' ')*k)
``` | instruction | 0 | 72,527 | 13 | 145,054 |
No | output | 1 | 72,527 | 13 | 145,055 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Chouti was tired of the tedious homework, so he opened up an old programming problem he created years ago.
You are given a connected undirected graph with n vertices and m weighted edges. There are k special vertices: x_1, x_2, …, x_k.
Let's define the cost of the path as the maximum weight of the edges in it. And the distance between two vertexes as the minimum cost of the paths connecting them.
For each special vertex, find another special vertex which is farthest from it (in terms of the previous paragraph, i.e. the corresponding distance is maximum possible) and output the distance between them.
The original constraints are really small so he thought the problem was boring. Now, he raises the constraints and hopes you can solve it for him.
Input
The first line contains three integers n, m and k (2 ≤ k ≤ n ≤ 10^5, n-1 ≤ m ≤ 10^5) — the number of vertices, the number of edges and the number of special vertices.
The second line contains k distinct integers x_1, x_2, …, x_k (1 ≤ x_i ≤ n).
Each of the following m lines contains three integers u, v and w (1 ≤ u,v ≤ n, 1 ≤ w ≤ 10^9), denoting there is an edge between u and v of weight w. The given graph is undirected, so an edge (u, v) can be used in the both directions.
The graph may have multiple edges and self-loops.
It is guaranteed, that the graph is connected.
Output
The first and only line should contain k integers. The i-th integer is the distance between x_i and the farthest special vertex from it.
Examples
Input
2 3 2
2 1
1 2 3
1 2 2
2 2 1
Output
2 2
Input
4 5 3
1 2 3
1 2 5
4 2 1
2 3 2
1 4 4
1 3 3
Output
3 3 3
Note
In the first example, the distance between vertex 1 and 2 equals to 2 because one can walk through the edge of weight 2 connecting them. So the distance to the farthest node for both 1 and 2 equals to 2.
In the second example, one can find that distance between 1 and 2, distance between 1 and 3 are both 3 and the distance between 2 and 3 is 2.
The graph may have multiple edges between and self-loops, as in the first example.
Submitted Solution:
```
from sys import stdin,stdout
n,m,k=map(int,input().split())
x=set(list(map(int,input().split())))
c=[]
vis=[0]*(n+1)
for i in range(m):
a,b,w=(int(r) for r in stdin.readline().split())
if a!=b:
c.append([w,a,b])
c.sort()
i=0
p=list(range(n+1))
def q(x):
if x!=p[x]:
p[x]=q(p[x])
return p[x]
while x:
fl=0
u=q(c[i][1]);v=q(c[i][2])
if u!=v:
if c[i][1] in x:
x.remove(c[i][1])
if c[i][2] in x:
x.remove(c[i][2])
p[min(c[i][1],c[i][2])]=max(c[i][1],c[i][2])
ans=c[i][0]
i+=1
stdout.write((str(ans)+' ')*k)
``` | instruction | 0 | 72,528 | 13 | 145,056 |
No | output | 1 | 72,528 | 13 | 145,057 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Chouti was tired of the tedious homework, so he opened up an old programming problem he created years ago.
You are given a connected undirected graph with n vertices and m weighted edges. There are k special vertices: x_1, x_2, …, x_k.
Let's define the cost of the path as the maximum weight of the edges in it. And the distance between two vertexes as the minimum cost of the paths connecting them.
For each special vertex, find another special vertex which is farthest from it (in terms of the previous paragraph, i.e. the corresponding distance is maximum possible) and output the distance between them.
The original constraints are really small so he thought the problem was boring. Now, he raises the constraints and hopes you can solve it for him.
Input
The first line contains three integers n, m and k (2 ≤ k ≤ n ≤ 10^5, n-1 ≤ m ≤ 10^5) — the number of vertices, the number of edges and the number of special vertices.
The second line contains k distinct integers x_1, x_2, …, x_k (1 ≤ x_i ≤ n).
Each of the following m lines contains three integers u, v and w (1 ≤ u,v ≤ n, 1 ≤ w ≤ 10^9), denoting there is an edge between u and v of weight w. The given graph is undirected, so an edge (u, v) can be used in the both directions.
The graph may have multiple edges and self-loops.
It is guaranteed, that the graph is connected.
Output
The first and only line should contain k integers. The i-th integer is the distance between x_i and the farthest special vertex from it.
Examples
Input
2 3 2
2 1
1 2 3
1 2 2
2 2 1
Output
2 2
Input
4 5 3
1 2 3
1 2 5
4 2 1
2 3 2
1 4 4
1 3 3
Output
3 3 3
Note
In the first example, the distance between vertex 1 and 2 equals to 2 because one can walk through the edge of weight 2 connecting them. So the distance to the farthest node for both 1 and 2 equals to 2.
In the second example, one can find that distance between 1 and 2, distance between 1 and 3 are both 3 and the distance between 2 and 3 is 2.
The graph may have multiple edges between and self-loops, as in the first example.
Submitted Solution:
```
import time
import sys
class Edge(object):
def __init__(self,x,y,w):
self.x=x
self.y=y
self.w=w
def __lt__(self,other):
return self.w<other.w
def find(x):
if fa[x]!=x:
fa[x]=find(fa[x])
return fa[x]
t_0=time.perf_counter()
n,m,sp=map(int,input().split())
issp=[False]*(n+1)
vert=list(map(int,input().split()))
if n>5000:
print(time.perf_counter()-t_0)
for x in vert:
issp[x]=True
if n>5000:
print(time.perf_counter()-t_0)
e=[]
for i in range(m):
x,y,w=map(int,input().split())
if issp[x]==False:
x,y=y,x
e.append([x,y,w])
if n>5000:
print(time.perf_counter()-t_0)
sys.exit(0)
e.sort(key=lambda x:x[2])
rem=sp-1
fa=[i for i in range(n+1)]
for ed in e:
x=find(ed[0])
y=find(ed[1])
if x!=y:
if issp[x] and issp[y]:
rem-=1
if rem==0:
print((str(ed[2])+' ')*sp)
break
fa[y]=x
``` | instruction | 0 | 72,529 | 13 | 145,058 |
No | output | 1 | 72,529 | 13 | 145,059 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Chouti was tired of the tedious homework, so he opened up an old programming problem he created years ago.
You are given a connected undirected graph with n vertices and m weighted edges. There are k special vertices: x_1, x_2, …, x_k.
Let's define the cost of the path as the maximum weight of the edges in it. And the distance between two vertexes as the minimum cost of the paths connecting them.
For each special vertex, find another special vertex which is farthest from it (in terms of the previous paragraph, i.e. the corresponding distance is maximum possible) and output the distance between them.
The original constraints are really small so he thought the problem was boring. Now, he raises the constraints and hopes you can solve it for him.
Input
The first line contains three integers n, m and k (2 ≤ k ≤ n ≤ 10^5, n-1 ≤ m ≤ 10^5) — the number of vertices, the number of edges and the number of special vertices.
The second line contains k distinct integers x_1, x_2, …, x_k (1 ≤ x_i ≤ n).
Each of the following m lines contains three integers u, v and w (1 ≤ u,v ≤ n, 1 ≤ w ≤ 10^9), denoting there is an edge between u and v of weight w. The given graph is undirected, so an edge (u, v) can be used in the both directions.
The graph may have multiple edges and self-loops.
It is guaranteed, that the graph is connected.
Output
The first and only line should contain k integers. The i-th integer is the distance between x_i and the farthest special vertex from it.
Examples
Input
2 3 2
2 1
1 2 3
1 2 2
2 2 1
Output
2 2
Input
4 5 3
1 2 3
1 2 5
4 2 1
2 3 2
1 4 4
1 3 3
Output
3 3 3
Note
In the first example, the distance between vertex 1 and 2 equals to 2 because one can walk through the edge of weight 2 connecting them. So the distance to the farthest node for both 1 and 2 equals to 2.
In the second example, one can find that distance between 1 and 2, distance between 1 and 3 are both 3 and the distance between 2 and 3 is 2.
The graph may have multiple edges between and self-loops, as in the first example.
Submitted Solution:
```
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
g = []
f = list(range(n+1))
s = [0] * (n+1)
def search(n):
while f[n] != n:
f[n] = f[f[n]]
n = f[n]
return n
def can_merge(u, v):
u = search(u)
v = search(v)
f[u] = v
if u == v:
return False
s[v] += s[u]
if s[u] > 0 and s[v] > 0:
return True
return False
for _ in range(m):
u,v,w = map(int, input().split())
g.append((u,v,w))
g.sort(key = lambda tup: tup[2])
for i in a:
s[i] += 1
ans = 0
for t in g:
if can_merge(t[0],t[1]):
ans = t[2]
print(' '.join([str(ans)] * k))
``` | instruction | 0 | 72,530 | 13 | 145,060 |
No | output | 1 | 72,530 | 13 | 145,061 |
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 | 72,936 | 13 | 145,872 |
Tags: dfs and similar, graphs
Correct Solution:
```
(n, m) = map(int, input().split())
G = [None] * (n + 1)
for i in range(1, n + 1):
G[i] = []
for i in range(m):
(u, v) = map(int, input().split())
G[u].append(v)
G[v].append(u)
div = [None] * (n + 1)
def bfs():
global n, m, G, div
S = set(x for x in range(1, n + 1) if G[x])
now = []
next = []
while True:
if len(now) == 0:
if len(S) == 0:
return True
x = S.pop()
now = [x]
div[x] = 1
for x in now:
state = div[x]
for v in G[x]:
if not div[v]:
div[v] = 3 - state
S.remove(v)
next.append(v)
elif div[v] == state:
return False
now = next
next = []
if not bfs():
print('-1')
else:
A = [str(i) for i in range(1, n + 1) if div[i] == 1]
B = [str(i) for i in range(1, n + 1) if div[i] == 2]
print(len(A))
print(' '.join(A))
print(len(B))
print(' '.join(B))
``` | output | 1 | 72,936 | 13 | 145,873 |
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 | 72,937 | 13 | 145,874 |
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 | 72,937 | 13 | 145,875 |
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 | 72,938 | 13 | 145,876 |
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 | 72,938 | 13 | 145,877 |
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 | 72,939 | 13 | 145,878 |
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 | 72,939 | 13 | 145,879 |
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 | 72,940 | 13 | 145,880 |
Tags: dfs and similar, graphs
Correct Solution:
```
from collections import deque
lin = input().split()
n, m = int(lin[0]), int(lin[1])
graph = [[] for i in range(n)]
# graph2 = n * [[]] equivalent
for i in range(m):
curr = input().split()
u, v = int(curr[0]) - 1, int(curr[1]) - 1
graph[u].append(v)
graph[v].append(u)
## Graph built, do bfs to check if its bipartite.
color = [-1 for i in range(n)]
def bipartiteBFS(source: int) -> bool:
dq = deque()
dq.appendleft(source)
color[source] = 0 # start coloring w 0.
colorable = True
while len(dq) > 0 and colorable:
curr = dq.popleft() ## curr is like a parent
for neighbor in graph[curr]:
if color[neighbor] == - 1:
dq.append(neighbor)
color[neighbor] = 1 - color[curr]
elif (color[neighbor] == color[curr]): # not bipartite
colorable = False
return colorable
ans = True
for node_value in range(n):
if color[node_value] == -1:
ans &= bipartiteBFS(node_value)
if (ans):
set1 = [index for index in range(len(color)) if color[index]]
set2 = [index for index in range(len(color)) if not color[index]]
print (len(set1))
for num in set1:
print (num+1, end = ' ')
print()
print (len(set2))
for num in set2:
print (num+1, end = ' ')
print()
else:
print(-1)
``` | output | 1 | 72,940 | 13 | 145,881 |
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 | 72,941 | 13 | 145,882 |
Tags: dfs and similar, graphs
Correct Solution:
```
def main():
n, m = map(int, input().split())
l = [[] for _ in range(n + 1)]
for _ in range(m):
u, v = map(int, input().split())
l[u].append(v)
l[v].append(u)
res = [0] * (n + 1)
for u, x in enumerate(res):
if not x:
x, nxt = -1, [u]
while nxt:
x, cur, nxt = -x, nxt, []
for u in cur:
if l[u]:
res[u] = x
for v in l[u]:
if not res[v]:
nxt.append(v)
elif res[v] == x:
print(-1)
return
for x in -1, 1:
l = [u for u in range(1, n + 1) if res[u] == x]
print(len(l))
print(' '.join(map(str, l)))
if __name__ == "__main__":
main()
``` | output | 1 | 72,941 | 13 | 145,883 |
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 | 72,942 | 13 | 145,884 |
Tags: dfs and similar, graphs
Correct Solution:
```
M=lambda:map(int,input().split())
n,m=M()
graph=[set() for i in range(n)]
for _ in range(m):
a,b=M()
graph[a-1].add(b-1)
graph[b-1].add(a-1)
visited=[-1 for i in range(n)]
stack=[]
for i in range(n):
if visited[i]==-1 and len(graph[i])>0:
visited[i]=True
stack+=[i]
while stack:
x=stack.pop()
for j in graph[x]:
if visited[j]==visited[x]:
print(-1)
exit(0)
if visited[j]==-1:
visited[j]=not visited[x]
stack+=[j]
A=[]
B=[]
for i in range(len(visited)):
if visited[i]==True:
A.append(i+1)
elif visited[i]==False:
B.append(i+1)
print(len(A))
print(*A)
print(len(B))
print(*B)
``` | output | 1 | 72,942 | 13 | 145,885 |
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 | 72,943 | 13 | 145,886 |
Tags: dfs and similar, graphs
Correct Solution:
```
inp = lambda : map(int, input().split())
n, m = inp()
lines = [[] for i in range(n + 1)]
for i in range(m):
x, y = inp()
lines[x].append(y)
lines[y].append(x)
color = [-1] * (n + 1)
for j in range(1, n + 1):
if color[j] != -1:
continue
head = 0
color[j] = 0
q = [j]
while head < len(q):
for i in lines[q[head]]:
if color[i] == color[q[head]]:
print(-1)
quit()
if color[i] == -1:
q.append(i)
color[i] = (color[q[head]] + 1) % 2
head += 1
a = []
b = []
for i in range(len(color)):
if color[i] == 0:
a.append(str(i))
if color[i] == 1:
b.append(str(i))
print(len(a))
print(" ".join(a))
print(len(b))
print(" ".join(b))
``` | output | 1 | 72,943 | 13 | 145,887 |
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:
```
from collections import deque
n,m=map(int,input().split())
v=[[]for i in range(n+2)]
ans=[[] for i in range(3)]
vs=[0 for i in range(n+2)]
while m:
x,y=map(int,input().split())
v[x].append(y)
v[y].append(x)
m-=1
def bfs(nod,colr):
q=deque([nod])
vs[nod]=1
while q:
node=q.popleft()
colr=vs[node]
ans[colr].append(node)
for ch in v[node]:
if(not vs[ch]):
q.append(ch)
vs[ch]=3-vs[node]
elif(vs[ch]==vs[node]):
print(-1)
exit()
for i in range(1,n+1):
if not vs[i]:
bfs(i,1)
print(len(ans[1]))
print(*ans[1])
print(len(ans[2]))
print(*ans[2])
``` | instruction | 0 | 72,944 | 13 | 145,888 |
Yes | output | 1 | 72,944 | 13 | 145,889 |
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:
```
from sys import stdin
from collections import *
class graph:
# initialize graph
def __init__(self, gdict=None):
if gdict is None:
gdict = defaultdict(list)
self.gdict, self.edges, self.arr1, self.arr2, self.visit = gdict, [], defaultdict(int), defaultdict(
int), defaultdict(int)
# find edges
def find_edges(self):
return self.edges
# Get verticies
def get_vertices(self):
return list(self.gdict.keys())
# add vertix
def add_vertix(self, node):
self.gdict[node] = []
# add edge
def add_edge(self, node1, node2):
self.gdict[node1].append(node2)
self.gdict[node2].append(node1)
self.edges.append([node1, node2])
def bfs(self, i):
queue = deque([[i, 1]])
self.visit[i], self.arr1[i] = 1, 1
while queue:
# dequeue parent vertix
s, level = queue.popleft()
# enqueue child vertices
for i in self.gdict[s]:
if self.visit[i] == 0:
queue.append([i, level + 1])
self.visit[i] = 1
if level % 2:
self.arr2[i] = 1
else:
self.arr1[i] = 1
n, m = map(int, input().split())
graph1, e1, e2 = graph(), 0, 0
for i in range(m):
u, v = map(int, input().split())
graph1.add_edge(u, v)
for i in range(1, n + 1):
if not graph1.visit[i]:
graph1.bfs(i)
out1, out2 = list(graph1.arr1.keys()), list(graph1.arr2.keys())
# print(out1, out2)
for i, j in graph1.edges:
if graph1.arr1[i] and graph1.arr2[j] or graph1.arr1[j] and graph1.arr2[i]:
continue
else:
exit(print(-1))
print(len(out1))
print(*out1)
print(len(out2))
print(*out2)
``` | instruction | 0 | 72,945 | 13 | 145,890 |
Yes | output | 1 | 72,945 | 13 | 145,891 |
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
input = sys.stdin.buffer.readline
from collections import deque
def prog():
n,m = map(int,input().split())
adj_list = [[] for i in range(n+1)]
for i in range(m):
a,b = map(int,input().split())
adj_list[a].append(b)
adj_list[b].append(a)
locations = [-1 for i in range(n+1)]
visited = set()
impossible = False
for start in range(1,n+1):
if impossible == True:
break
if start in visited or len(adj_list[start]) == 0:
continue
s = deque([0,start])
while len(s) > 1 and not impossible:
curr = s[-1]
if curr not in visited:
locations[curr] = (locations[s[-2]] + 1) % 2
visited.add(curr)
had_neighbor = False
for i in range(len(adj_list[curr])):
neighbor = adj_list[curr].pop()
if neighbor not in visited:
had_neighbor = True
s.append(neighbor)
break
else:
if locations[curr] != (locations[neighbor] + 1) % 2:
impossible = True
break
if not had_neighbor:
s.pop()
if impossible == True:
print(-1)
else:
first_cover = []
second_cover = []
for i in range(1,n+1):
if locations[i] == 0:
first_cover.append(str(i))
elif locations[i] == 1:
second_cover.append(str(i))
print(len(first_cover))
print(" ".join(first_cover))
print(len(second_cover))
print(" ".join(second_cover))
prog()
``` | instruction | 0 | 72,946 | 13 | 145,892 |
Yes | output | 1 | 72,946 | 13 | 145,893 |
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:
```
from sys import stdin
from collections import *
class graph:
# initialize graph
def __init__(self, gdict=None):
if gdict is None:
gdict = defaultdict(list)
self.gdict, self.edges, self.colors = gdict, [], defaultdict(int)
# find edges
def find_edges(self):
return self.edges
# Get verticies
def get_vertices(self):
return list(self.gdict.keys())
# add vertix
def add_vertix(self, node):
self.gdict[node] = []
# add edge
def add_edge(self, node1, node2):
self.gdict[node1].append(node2)
self.gdict[node2].append(node1)
self.edges.append([node1, node2])
def bfs(self, i):
queue = deque([[i, 1]])
while queue:
# dequeue parent vertix
s, color = queue.popleft()
if self.colors[s] == 0:
self.colors[s] = color
# enqueue child vertices
for i in self.gdict[s]:
if self.colors[i] == 0:
queue.append([i, 3 - color])
n, m = map(int, input().split())
graph1 = graph()
for i in range(m):
u, v = map(int, input().split())
graph1.add_edge(u, v)
for i in range(1, n + 1):
if graph1.colors[i] == 0:
graph1.bfs(i)
for i, j in graph1.edges:
if graph1.colors[i] == graph1.colors[j]:
exit(print(-1))
a, b = [i for i in range(n + 1) if graph1.colors[i] == 1], [i for i in range(n + 1) if graph1.colors[i] == 2]
print(len(a))
print(*a)
print(len(b))
print(*b)
``` | instruction | 0 | 72,947 | 13 | 145,894 |
Yes | output | 1 | 72,947 | 13 | 145,895 |
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 IsPossible(graph,n):
isVisited = [False]*n
color = [0]*n
keys = list(graph.keys())
stack = [keys[0]]
while stack != []:
#print (color)
curr = stack.pop()
if not isVisited[curr-1]:
isVisited[curr-1] = True
if color[curr-1] == 0:
if color[graph[curr][0]-1] == 0 or color[graph[curr][0]-1] == 2:
color[curr-1] = 1
else:
color[curr-1] = 2
for kid in graph[curr]:
if color[curr-1] == color[kid-1]:
return []
else:
color[kid-1] = 3-color[curr-1]
stack.append(kid)
elif color[curr-1] == 1:
for kid in graph[curr]:
if color[kid-1] == 0 or color[kid-1] == 2:
color[kid-1] = 2
stack.append(kid)
else:
return []
else:
for kid in graph[curr]:
if color[kid-1] == 0 or color[kid-1] == 1:
color[kid-1] = 1
stack.append(kid)
else:
return []
return color
def Solve(color):
first = []
second = []
for i in range(len(color)):
if color[i] == 1:
first.append(i+1)
elif color[i] == 2:
second.append(i+1)
fstr = ''
sstr = ''
for i in first:
fstr += str(i)+' '
for i in second:
sstr += str(i) + ' '
print (len(first))
print (fstr)
print (len(second))
print (sstr)
def main():
n,m = map(int,input().split())
graph = {}
for i in range(m):
start,end = map(int,input().split())
if start not in graph.keys():
graph[start] = [end]
else:
graph[start].append(end)
if end not in graph.keys():
graph[end] = [start]
else:
graph[end].append(start)
color = IsPossible(graph,n)
if color == []:
print (-1)
else:
Solve(color)
main()
``` | instruction | 0 | 72,948 | 13 | 145,896 |
No | output | 1 | 72,948 | 13 | 145,897 |
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:
```
# in the name of god
from collections import defaultdict
n, m = map(int, input().strip().split())
g = defaultdict(list)
for _ in range(m):
u, v = map(int, input().strip().split())
g[u].append(v)
v1 = []
v2 = []
mark = [-1] * (n + 1)
def dfs(start, color):
mark[start] = color
if color == 0:
v1.append(start)
else:
v2.append(start)
for u in g[start]:
if mark[u] == -1:
if dfs(u, 1 - color):
return 1
if mark[u] != 1 - color:
return 1
return 0
sw = False
for i in range(1, n):
if mark[u] == -1 and len(g[i]) != 0:
res = dfs(i, 0)
try:
if res:
sw = True
break
except:
continue
if sw:
print(-1)
else:
print(len(v1))
print(' '.join(str(x) for x in v1))
print(len(v2))
print(' '.join(str(x) for x in v2))
``` | instruction | 0 | 72,949 | 13 | 145,898 |
No | output | 1 | 72,949 | 13 | 145,899 |
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
import math
import operator as op
from functools import reduce
from bisect import *
from collections import deque
## I/O ##
def read_line():
return sys.stdin.readline()[:-1]
def read_int():
return int(sys.stdin.readline())
def read_int_line():
return [int(v) for v in sys.stdin.readline().split()]
def read_float_line():
return [float(v) for v in sys.stdin.readline().split()]
## ##
## ncr ##
def ncr(n, r):
r = min(r, n-r)
numer = reduce(op.mul, range(n, n-r, -1), 1)
denom = reduce(op.mul, range(1, r+1), 1)
return numer / denom
## ##
## Fenwick Tree ##
N = int(1e6)+10
fenwick = [0]*N
def add(i,x,n):
while i<n:
fenwick[i]+=x
i = i|(i+1)
def fsum(i,n):
res = 0
while i>=0:
res += fenwick[i]
i = (i&(i+1))-1
return res
def fsum_range(i,j,n):
return fsum(j,n)-fsum(i-1,n)
## ##
## Main ##
mod = int(1e9)+7
mod2 = 998244353
# t = read_int()
t = 1
for i in range(t):
n, m = read_int_line()
adj = [-1]*(n+1)
for i in range(m):
u,v = read_int_line()
if adj[u]==-1:
adj[u] = []
adj[u].append(v)
else:
adj[u].append(v)
if adj[v]==-1:
adj[v] = []
adj.append(u)
else:
adj[v].append(u)
color = [-1]*(n+1)
f = True
q = deque()
for i in range(1,n+1):
if color[i]==-1:
q.append(i)
color[i] = 0
while len(q)!=0:
s = q.popleft()
if adj[s] !=-1:
for i in adj[s]:
if color[i] == -1:
color[i] = color[s]^1
q.append(i)
else:
if color[i] == color[s]:
f = False
if f:
s1 = []
s2 = []
for i in range(1,n+1):
if color[i]==1:
s1.append(i)
else:
s2.append(i)
print(len(s1))
print(*s1)
print(len(s2))
print(*s2)
else:
print(-1)
``` | instruction | 0 | 72,950 | 13 | 145,900 |
No | output | 1 | 72,950 | 13 | 145,901 |
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:
```
from sys import stdin, exit, setrecursionlimit
from math import *
from bisect import bisect_left
from collections import deque
input = stdin.readline
lmi = lambda: list(map(int, input().split()))
mi = lambda: map(int, input().split())
si = lambda: input().strip('\n')
ssi = lambda: input().strip('\n').split()
def dfs(node, bo, prev):
if not vis[node]:
arr[bo].append(node)
vis[node] = True
for i in graph[node]:
if i != prev:
dfs(i, bo ^ 1, node)
else:
print(-1)
exit()
n, m = mi()
graph = [[] for j in range(n+1)]
for i in range(m):
a, b = mi()
graph[a].append(b)
graph[b].append(a)
vis = [False for i in range(n+1)]
arr = [[], []]
for i in range(1, n+1):
if not vis[i]:
dfs(i, 0, -1)
print(len(arr[0]))
print(*arr[0])
print(len(arr[1]))
print(*arr[1])
``` | instruction | 0 | 72,951 | 13 | 145,902 |
No | output | 1 | 72,951 | 13 | 145,903 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an undirected graph, consisting of n vertices and m edges. The graph does not necessarily connected. Guaranteed, that the graph does not contain multiple edges (more than one edges between a pair of vertices) or loops (edges from a vertex to itself).
A cycle in a graph is called a simple, if it contains each own vertex exactly once. So simple cycle doesn't allow to visit a vertex more than once in a cycle.
Determine the edges, which belong to exactly on one simple cycle.
Input
The first line contain two integers n and m (1 ≤ n ≤ 100 000, 0 ≤ m ≤ min(n ⋅ (n - 1) / 2, 100 000)) — the number of vertices and the number of edges.
Each of the following m lines contain two integers u and v (1 ≤ u, v ≤ n, u ≠ v) — the description of the edges.
Output
In the first line print the number of edges, which belong to exactly one simple cycle.
In the second line print the indices of edges, which belong to exactly one simple cycle, in increasing order. The edges are numbered from one in the same order as they are given in the input.
Examples
Input
3 3
1 2
2 3
3 1
Output
3
1 2 3
Input
6 7
2 3
3 4
4 2
1 2
1 5
5 6
6 1
Output
6
1 2 3 5 6 7
Input
5 6
1 2
2 3
2 4
4 3
2 5
5 3
Output
0
Submitted Solution:
```
class Graph:
def __init__(self, n, edges):
self.vertex = [[] for i in range(n)]
for i in range(len(edges)):
self.vertex[edges[i][0] - 1].append(edges[i][1] - 1)
self.vertex[edges[i][1] - 1].append(edges[i][0] - 1)
self.count = len(self.vertex)
def __repr__(self):
return str(self.vertex)
class ResultCase:
def __init__(self, edges = []):
self.edges = edges
self.count = lambda : len(self.edges)
def add(self, x: int):
self.edges.append(x)
def __repr__(self):
sedges = ""
if self.count() != 0:
for i in enumerate(sorted(self.edges)):
sedges += "{} ".format(i[1])
return "{}\n{}".format(self.count(), sedges)
class TestCase:
def __init__(self, n, m, edges = []):
self.n = n
self.m = m
self.edges = dict([(tuple(edges[i]), i) for i in range(len(edges))])
self.G = None
def done(self):
self.G = Graph(self.n, list(self.edges.keys()))
def __repr__(self):
return "~~~ CASE ~~~\nn={}\nm={}\nedges={}\n~~~~~~~~~~~~".format(self.n, self.m, self.edges)
def read_input():
line = input()
spl = line.split()
case = TestCase(int(spl[0]), int(spl[1]))
for i in range(case.m):
line = input()
spl = line.split()
case.edges[tuple([int(spl[0]), int(spl[1])])] = i
case.done()
return case
class DisjointSet:
def __init__(self, elements: list):
self.elements = elements
self.P = [-1 for i in range(len(self.elements))]
def SetOf(self, x: int):
if self.P[x] < 0:
return x
return self.SetOf(self.P[x])
def Merge(self, x: int, y: int):
c_x = self.SetOf(x)
c_y = self.SetOf(y)
if c_x == c_y:
return
if abs(self.P[c_x]) < abs(self.P[c_y]):
self.P[c_y] += self.P[c_x]
self.P[c_x] = c_y
else:
self.P[c_x] += self.P[c_y]
self.P[c_y] = c_x
class DFS:
def __init__(self, g: Graph):
self.dep = []
self.color = []
self.p = []
#to store reverse edges
self.be = []
self.G = g
def run(self):
self.dep = [0 for i in range(self.G.count)]
self.color = [0 for i in range(self.G.count)]
self.p = [-1 for i in range(self.G.count)]
for i in range(self.G.count):
if self.color[i] == 0:
self.dfs(i, -1, 0)
def dfs(self, u: int, pi_u: int, deep: int):
self.dep[u] = deep
self.color[u] = 1
self.p[u] = pi_u
for i in range(len(self.G.vertex[u])):
v = self.G.vertex[u][i]
if v == pi_u:
continue
elif self.p[v] == -1:
self.dfs(v, u, deep + 1)
elif self.color[v] == 1:
self.be.append([u,v])
self.color[u] = 2
def __repr__(self):
return "dep = {}\ncolor = {}\np = {}\nbe = {}".format(self.dep, self.color, self.p, self.be)
def check_key(dictionary: dict, dkey: tuple):
if dictionary.keys().__contains__(dkey):
return dkey
elif dictionary.keys().__contains__((dkey[1], dkey[0])):
return (dkey[1], dkey[0])
return None
if __name__ == "__main__":
#case = TestCase(3, 2, [[0, 1], [1,2], [2,0]])
#case.done()
case = read_input()
dfs_iter = DFS(case.G)
dfs_iter.run()
pp = dfs_iter.p.copy()
index = [-1 for i in range(case.G.count)]
dsu = DisjointSet(dfs_iter.be)
for i in range(len(dfs_iter.be)):
x = dfs_iter.be[i][0]
path = []
while dfs_iter.dep[x] > dfs_iter.dep[dfs_iter.be[i][1]]:
path.append(x)
if index[x] == -1:
index[x] = i
else:
dsu.Merge(i, index[x])
x = dfs_iter.p[x]
for j in range(len(path)):
dfs_iter.p[j] = dfs_iter.be[i][1]
result = ResultCase()
for i in range(len(dfs_iter.be)):
if dsu.P[i] < 0 and abs(dsu.P[i]) == 1:
t = check_key(case.edges, tuple([dfs_iter.be[i][0]+ 1, dfs_iter.be[i][1] + 1]))
if t != None:
result.add(1 + case.edges[t])
else:
raise Exception("This key isn't in dict")
y = dfs_iter.be[i][0]
while y != dfs_iter.be[i][1]:
te = check_key(case.edges, tuple([y + 1, pp[y] + 1]))
if te != None:
result.add(1 + case.edges[te])
else:
raise Exception("This key isn't in dict")
y = pp[y]
print(result)
``` | instruction | 0 | 73,051 | 13 | 146,102 |
No | output | 1 | 73,051 | 13 | 146,103 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an undirected graph, consisting of n vertices and m edges. The graph does not necessarily connected. Guaranteed, that the graph does not contain multiple edges (more than one edges between a pair of vertices) or loops (edges from a vertex to itself).
A cycle in a graph is called a simple, if it contains each own vertex exactly once. So simple cycle doesn't allow to visit a vertex more than once in a cycle.
Determine the edges, which belong to exactly on one simple cycle.
Input
The first line contain two integers n and m (1 ≤ n ≤ 100 000, 0 ≤ m ≤ min(n ⋅ (n - 1) / 2, 100 000)) — the number of vertices and the number of edges.
Each of the following m lines contain two integers u and v (1 ≤ u, v ≤ n, u ≠ v) — the description of the edges.
Output
In the first line print the number of edges, which belong to exactly one simple cycle.
In the second line print the indices of edges, which belong to exactly one simple cycle, in increasing order. The edges are numbered from one in the same order as they are given in the input.
Examples
Input
3 3
1 2
2 3
3 1
Output
3
1 2 3
Input
6 7
2 3
3 4
4 2
1 2
1 5
5 6
6 1
Output
6
1 2 3 5 6 7
Input
5 6
1 2
2 3
2 4
4 3
2 5
5 3
Output
0
Submitted Solution:
```
import sys
#sys.setrecursionlimit(1000000)
class DisjointSet:
def __init__(self, n):
self.P = [-1] * n
def SetOf(self, x: int):
if self.P[x] < 0:
return x
self.P[x] = self.SetOf(self.P[x])
return self.P[x]
def Merge(self, x: int, y: int):
c_x = self.SetOf(x)
c_y = self.SetOf(y)
if c_x == c_y:
return
if abs(self.P[c_x]) < abs(self.P[c_y]):
self.P[c_y] += self.P[c_x]
self.P[c_x] = c_y
else:
self.P[c_x] += self.P[c_y]
self.P[c_y] = c_x
class E:
def __init__(self, u, v, ind):
self.u = u
self.v = v
self.ind = ind
line = sys.stdin.readline().split()
N, M = int(line[0]), int(line[1])
g = [[] for _ in range(N)]
mp = dict()
for i in range(1, M + 1):
line = sys.stdin.readline().split()
u, v = int(line[0]) - 1, int(line[1]) -1
mp[(u, v)] = mp[(v, u)] = i
g[u].append((v, i))
g[v].append((u, i))
dep = [0] * N
visited = [0] * N
pi = [-1] * N
edges = []
def dfs(c, p):
visited[c] = -1
pi[c] = p
for tuple_I in g[c]:
if p != tuple_I[0]:
if pi[tuple_I[0]] == -1:
dep[tuple_I[0]] = dep[c] + 1
dfs(tuple_I[0], c)
elif visited[tuple_I[0]] == -1:
edges.append(E(c, tuple_I[0], tuple_I[1]))
visited[c] = 1
for i in range(N):
if not visited[i]:
dfs(i, -1)
n = len(edges)
dsu = DisjointSet(n)
pp = pi.copy()
index = [-1] * N
for i in range(n):
u, v, ind = edges[i].u, edges[i].v, edges[i].ind
path = []
while dep[u] > dep[v]:
path.append(u)
if index[u] == -1:
index[u] = i
else:
dsu.Merge(i, index[u])
u = pi[u]
for x in range(len(path)):
pi[x] = v
res = set()
for i in range(n):
if dsu.P[i] == -1:
res.add(edges[i].ind)
u, v = edges[i].u, edges[i].v
while u != v:
res.add(mp[(u, pp[u])])
u = pp[u]
tp = []
def puts(s):
tp.append(s)
res = sorted(list(res))
sys.stdout.write(str(len(res)) + '\n')
for i in res:
puts(str(i))
puts("\n")
sys.stdout.write(" ".join(tp))
``` | instruction | 0 | 73,052 | 13 | 146,104 |
No | output | 1 | 73,052 | 13 | 146,105 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an undirected graph, consisting of n vertices and m edges. The graph does not necessarily connected. Guaranteed, that the graph does not contain multiple edges (more than one edges between a pair of vertices) or loops (edges from a vertex to itself).
A cycle in a graph is called a simple, if it contains each own vertex exactly once. So simple cycle doesn't allow to visit a vertex more than once in a cycle.
Determine the edges, which belong to exactly on one simple cycle.
Input
The first line contain two integers n and m (1 ≤ n ≤ 100 000, 0 ≤ m ≤ min(n ⋅ (n - 1) / 2, 100 000)) — the number of vertices and the number of edges.
Each of the following m lines contain two integers u and v (1 ≤ u, v ≤ n, u ≠ v) — the description of the edges.
Output
In the first line print the number of edges, which belong to exactly one simple cycle.
In the second line print the indices of edges, which belong to exactly one simple cycle, in increasing order. The edges are numbered from one in the same order as they are given in the input.
Examples
Input
3 3
1 2
2 3
3 1
Output
3
1 2 3
Input
6 7
2 3
3 4
4 2
1 2
1 5
5 6
6 1
Output
6
1 2 3 5 6 7
Input
5 6
1 2
2 3
2 4
4 3
2 5
5 3
Output
0
Submitted Solution:
```
import sys
#sys.setrecursionlimit(1000000)
class DisjointSet:
def __init__(self, n):
self.P = [-1] * n
def SetOf(self, x: int):
if self.P[x] < 0:
return x
self.P[x] = self.SetOf(self.P[x])
return self.P[x]
def Merge(self, x: int, y: int):
c_x = self.SetOf(x)
c_y = self.SetOf(y)
if c_x == c_y:
return
if abs(self.P[c_x]) < abs(self.P[c_y]):
self.P[c_y] += self.P[c_x]
self.P[c_x] = c_y
else:
self.P[c_x] += self.P[c_y]
self.P[c_y] = c_x
class E:
def __init__(self, u, v, ind):
self.u = u
self.v = v
self.ind = ind
line = sys.stdin.readline().split()
N, M = int(line[0]), int(line[1])
g = [[] for _ in range(N)]
mp = dict()
for i in range(1, M + 1):
line = sys.stdin.readline().split()
u, v = int(line[0]) - 1, int(line[1]) -1
mp[(u, v)] = mp[(v, u)] = i
g[u].append((v, i))
g[v].append((u, i))
dep = [0] * N
visited = [0] * N
pi = [-1] * N
edges = []
def dfs(c, p):
visited[c] = -1
pi[c] = p
for tuple_I in g[c]:
if p != tuple_I[0]:
if pi[tuple_I[0]] == -1:
dep[tuple_I[0]] = dep[c] + 1
dfs(tuple_I[0], c)
elif visited[tuple_I[0]] == -1:
edges.append(E(c, tuple_I[0], tuple_I[1]))
visited[c] = 1
for i in range(N):
if not visited[i]:
dfs(i, -1)
n = len(edges)
dsu = DisjointSet(n)
pp = pi.copy()
index = [-1] * N
for i in range(n):
u, v, ind = edges[i].u, edges[i].v, edges[i].ind
path = []
while dep[u] > dep[v]:
path.append(u)
if index[u] == -1:
index[u] = i
else:
dsu.Merge(i, index[u])
u = pi[u]
for x in range(len(path)):
pi[x] = v
res = set()
for i in range(n):
if dsu.P[i] == -1:
res.add(edges[i].ind)
u, v = edges[i].u, edges[i].v
while u != v:
res.add(mp[(u, pp[u])])
u = pp[u]
tp = []
def puts(s):
tp.append(s)
sys.stdout.write(str(len(res)) + '\n')
for i in res:
puts(str(i))
puts("\n")
sys.stdout.write(" ".join(tp))
``` | instruction | 0 | 73,053 | 13 | 146,106 |
No | output | 1 | 73,053 | 13 | 146,107 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Anadi has a set of dominoes. Every domino has two parts, and each part contains some dots. For every a and b such that 1 ≤ a ≤ b ≤ 6, there is exactly one domino with a dots on one half and b dots on the other half. The set contains exactly 21 dominoes. Here is an exact illustration of his set:
<image>
Also, Anadi has an undirected graph without self-loops and multiple edges. He wants to choose some dominoes and place them on the edges of this graph. He can use at most one domino of each type. Each edge can fit at most one domino. It's not necessary to place a domino on each edge of the graph.
When placing a domino on an edge, he also chooses its direction. In other words, one half of any placed domino must be directed toward one of the endpoints of the edge and the other half must be directed toward the other endpoint. There's a catch: if there are multiple halves of dominoes directed toward the same vertex, each of these halves must contain the same number of dots.
How many dominoes at most can Anadi place on the edges of his graph?
Input
The first line contains two integers n and m (1 ≤ n ≤ 7, 0 ≤ m ≤ (n⋅(n-1))/(2)) — the number of vertices and the number of edges in the graph.
The next m lines contain two integers each. Integers in the i-th line are a_i and b_i (1 ≤ a, b ≤ n, a ≠ b) and denote that there is an edge which connects vertices a_i and b_i.
The graph might be disconnected. It's however guaranteed that the graph doesn't contain any self-loops, and that there is at most one edge between any pair of vertices.
Output
Output one integer which denotes the maximum number of dominoes which Anadi can place on the edges of the graph.
Examples
Input
4 4
1 2
2 3
3 4
4 1
Output
4
Input
7 0
Output
0
Input
3 1
1 3
Output
1
Input
7 21
1 2
1 3
1 4
1 5
1 6
1 7
2 3
2 4
2 5
2 6
2 7
3 4
3 5
3 6
3 7
4 5
4 6
4 7
5 6
5 7
6 7
Output
16
Note
Here is an illustration of Anadi's graph from the first sample test:
<image>
And here is one of the ways to place a domino on each of its edges:
<image>
Note that each vertex is faced by the halves of dominoes with the same number of dots. For instance, all halves directed toward vertex 1 have three dots. | instruction | 0 | 73,406 | 13 | 146,812 |
Tags: brute force, graphs
Correct Solution:
```
def dominoes(n,m,edges):
if n<=6:
return m
if m==0:
return 0
mscore=7
for a in range(1,7):
for b in range(a):
score=codeg(a,b,edges)
if score<mscore:
mscore=score
return m-mscore
def codeg(a,b,edges):
c=0
for x in range(7):
if x==a:
continue
if x==b:
continue
if ([a,x] in edges) or ([x,a] in edges):
if ([b,x] in edges) or ([x,b] in edges):
c+=1
return c
n,m=map(int,input().split())
ed=[]
for x in range(m):
a,b=map(int,input().split())
a-=1
b-=1
ed.append([a,b])
print(dominoes(n,m,ed))
``` | output | 1 | 73,406 | 13 | 146,813 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Anadi has a set of dominoes. Every domino has two parts, and each part contains some dots. For every a and b such that 1 ≤ a ≤ b ≤ 6, there is exactly one domino with a dots on one half and b dots on the other half. The set contains exactly 21 dominoes. Here is an exact illustration of his set:
<image>
Also, Anadi has an undirected graph without self-loops and multiple edges. He wants to choose some dominoes and place them on the edges of this graph. He can use at most one domino of each type. Each edge can fit at most one domino. It's not necessary to place a domino on each edge of the graph.
When placing a domino on an edge, he also chooses its direction. In other words, one half of any placed domino must be directed toward one of the endpoints of the edge and the other half must be directed toward the other endpoint. There's a catch: if there are multiple halves of dominoes directed toward the same vertex, each of these halves must contain the same number of dots.
How many dominoes at most can Anadi place on the edges of his graph?
Input
The first line contains two integers n and m (1 ≤ n ≤ 7, 0 ≤ m ≤ (n⋅(n-1))/(2)) — the number of vertices and the number of edges in the graph.
The next m lines contain two integers each. Integers in the i-th line are a_i and b_i (1 ≤ a, b ≤ n, a ≠ b) and denote that there is an edge which connects vertices a_i and b_i.
The graph might be disconnected. It's however guaranteed that the graph doesn't contain any self-loops, and that there is at most one edge between any pair of vertices.
Output
Output one integer which denotes the maximum number of dominoes which Anadi can place on the edges of the graph.
Examples
Input
4 4
1 2
2 3
3 4
4 1
Output
4
Input
7 0
Output
0
Input
3 1
1 3
Output
1
Input
7 21
1 2
1 3
1 4
1 5
1 6
1 7
2 3
2 4
2 5
2 6
2 7
3 4
3 5
3 6
3 7
4 5
4 6
4 7
5 6
5 7
6 7
Output
16
Note
Here is an illustration of Anadi's graph from the first sample test:
<image>
And here is one of the ways to place a domino on each of its edges:
<image>
Note that each vertex is faced by the halves of dominoes with the same number of dots. For instance, all halves directed toward vertex 1 have three dots. | instruction | 0 | 73,407 | 13 | 146,814 |
Tags: brute force, graphs
Correct Solution:
```
n,m=map(int,input().split())
edges=[list(map(int,input().split())) for i in range(m)]
for i in range(m):
edges[i][0]-=1
edges[i][1]-=1
from itertools import permutations
l=list(permutations(range(6)))
mx=0
for ij in l:
for node in range(7):
for seven in range(6):
cnt=[None]*6
for xi in range(6):
cnt[xi]=[0]*6
for xj in range(xi,6):
cnt[xi][xj]=1
numbering=[0]*10
c=0
for i in range(6):
if node==i:
c=1
numbering[i+c]=ij[i]
numbering[node]=seven
curr=0
for e in edges:
u,v=numbering[e[0]],numbering[e[1]]
u,v=min(u,v),max(u,v)
if cnt[u][v]:
cnt[u][v]=0
curr+=1
mx=max(mx,curr)
print(mx)
``` | output | 1 | 73,407 | 13 | 146,815 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Anadi has a set of dominoes. Every domino has two parts, and each part contains some dots. For every a and b such that 1 ≤ a ≤ b ≤ 6, there is exactly one domino with a dots on one half and b dots on the other half. The set contains exactly 21 dominoes. Here is an exact illustration of his set:
<image>
Also, Anadi has an undirected graph without self-loops and multiple edges. He wants to choose some dominoes and place them on the edges of this graph. He can use at most one domino of each type. Each edge can fit at most one domino. It's not necessary to place a domino on each edge of the graph.
When placing a domino on an edge, he also chooses its direction. In other words, one half of any placed domino must be directed toward one of the endpoints of the edge and the other half must be directed toward the other endpoint. There's a catch: if there are multiple halves of dominoes directed toward the same vertex, each of these halves must contain the same number of dots.
How many dominoes at most can Anadi place on the edges of his graph?
Input
The first line contains two integers n and m (1 ≤ n ≤ 7, 0 ≤ m ≤ (n⋅(n-1))/(2)) — the number of vertices and the number of edges in the graph.
The next m lines contain two integers each. Integers in the i-th line are a_i and b_i (1 ≤ a, b ≤ n, a ≠ b) and denote that there is an edge which connects vertices a_i and b_i.
The graph might be disconnected. It's however guaranteed that the graph doesn't contain any self-loops, and that there is at most one edge between any pair of vertices.
Output
Output one integer which denotes the maximum number of dominoes which Anadi can place on the edges of the graph.
Examples
Input
4 4
1 2
2 3
3 4
4 1
Output
4
Input
7 0
Output
0
Input
3 1
1 3
Output
1
Input
7 21
1 2
1 3
1 4
1 5
1 6
1 7
2 3
2 4
2 5
2 6
2 7
3 4
3 5
3 6
3 7
4 5
4 6
4 7
5 6
5 7
6 7
Output
16
Note
Here is an illustration of Anadi's graph from the first sample test:
<image>
And here is one of the ways to place a domino on each of its edges:
<image>
Note that each vertex is faced by the halves of dominoes with the same number of dots. For instance, all halves directed toward vertex 1 have three dots. | instruction | 0 | 73,408 | 13 | 146,816 |
Tags: brute force, graphs
Correct Solution:
```
n, m = map(int, input().split())
edges = [[*map(int, input().split())] for _ in range(m)]
print(max(len({tuple(sorted((mask // (6 ** (x - 1))) % 6 for x in e)) for e in edges}) for mask in range(6 ** n)))
``` | output | 1 | 73,408 | 13 | 146,817 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Anadi has a set of dominoes. Every domino has two parts, and each part contains some dots. For every a and b such that 1 ≤ a ≤ b ≤ 6, there is exactly one domino with a dots on one half and b dots on the other half. The set contains exactly 21 dominoes. Here is an exact illustration of his set:
<image>
Also, Anadi has an undirected graph without self-loops and multiple edges. He wants to choose some dominoes and place them on the edges of this graph. He can use at most one domino of each type. Each edge can fit at most one domino. It's not necessary to place a domino on each edge of the graph.
When placing a domino on an edge, he also chooses its direction. In other words, one half of any placed domino must be directed toward one of the endpoints of the edge and the other half must be directed toward the other endpoint. There's a catch: if there are multiple halves of dominoes directed toward the same vertex, each of these halves must contain the same number of dots.
How many dominoes at most can Anadi place on the edges of his graph?
Input
The first line contains two integers n and m (1 ≤ n ≤ 7, 0 ≤ m ≤ (n⋅(n-1))/(2)) — the number of vertices and the number of edges in the graph.
The next m lines contain two integers each. Integers in the i-th line are a_i and b_i (1 ≤ a, b ≤ n, a ≠ b) and denote that there is an edge which connects vertices a_i and b_i.
The graph might be disconnected. It's however guaranteed that the graph doesn't contain any self-loops, and that there is at most one edge between any pair of vertices.
Output
Output one integer which denotes the maximum number of dominoes which Anadi can place on the edges of the graph.
Examples
Input
4 4
1 2
2 3
3 4
4 1
Output
4
Input
7 0
Output
0
Input
3 1
1 3
Output
1
Input
7 21
1 2
1 3
1 4
1 5
1 6
1 7
2 3
2 4
2 5
2 6
2 7
3 4
3 5
3 6
3 7
4 5
4 6
4 7
5 6
5 7
6 7
Output
16
Note
Here is an illustration of Anadi's graph from the first sample test:
<image>
And here is one of the ways to place a domino on each of its edges:
<image>
Note that each vertex is faced by the halves of dominoes with the same number of dots. For instance, all halves directed toward vertex 1 have three dots. | instruction | 0 | 73,409 | 13 | 146,818 |
Tags: brute force, graphs
Correct Solution:
```
import itertools
n, m = map(int, input().split())
edge = [[0 for _ in range(n)] for _ in range(n)]
for i in range(m):
a, b = map(int, input().split())
a -= 1
b -= 1
edge[a][b] = 1
edge[b][a] = 1
ans = 0
d = [0] * n
for d in list(itertools.product(range(6), repeat=n)):
count = set()
for i, v1 in enumerate(d):
for j, v2 in enumerate(d):
if edge[i][j] == 1:
count.add(str(v1) + str(v2))
ans = max(len(count), ans)
print((ans+1)//2)
``` | output | 1 | 73,409 | 13 | 146,819 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Anadi has a set of dominoes. Every domino has two parts, and each part contains some dots. For every a and b such that 1 ≤ a ≤ b ≤ 6, there is exactly one domino with a dots on one half and b dots on the other half. The set contains exactly 21 dominoes. Here is an exact illustration of his set:
<image>
Also, Anadi has an undirected graph without self-loops and multiple edges. He wants to choose some dominoes and place them on the edges of this graph. He can use at most one domino of each type. Each edge can fit at most one domino. It's not necessary to place a domino on each edge of the graph.
When placing a domino on an edge, he also chooses its direction. In other words, one half of any placed domino must be directed toward one of the endpoints of the edge and the other half must be directed toward the other endpoint. There's a catch: if there are multiple halves of dominoes directed toward the same vertex, each of these halves must contain the same number of dots.
How many dominoes at most can Anadi place on the edges of his graph?
Input
The first line contains two integers n and m (1 ≤ n ≤ 7, 0 ≤ m ≤ (n⋅(n-1))/(2)) — the number of vertices and the number of edges in the graph.
The next m lines contain two integers each. Integers in the i-th line are a_i and b_i (1 ≤ a, b ≤ n, a ≠ b) and denote that there is an edge which connects vertices a_i and b_i.
The graph might be disconnected. It's however guaranteed that the graph doesn't contain any self-loops, and that there is at most one edge between any pair of vertices.
Output
Output one integer which denotes the maximum number of dominoes which Anadi can place on the edges of the graph.
Examples
Input
4 4
1 2
2 3
3 4
4 1
Output
4
Input
7 0
Output
0
Input
3 1
1 3
Output
1
Input
7 21
1 2
1 3
1 4
1 5
1 6
1 7
2 3
2 4
2 5
2 6
2 7
3 4
3 5
3 6
3 7
4 5
4 6
4 7
5 6
5 7
6 7
Output
16
Note
Here is an illustration of Anadi's graph from the first sample test:
<image>
And here is one of the ways to place a domino on each of its edges:
<image>
Note that each vertex is faced by the halves of dominoes with the same number of dots. For instance, all halves directed toward vertex 1 have three dots. | instruction | 0 | 73,410 | 13 | 146,820 |
Tags: brute force, graphs
Correct Solution:
```
n,m=map(int,input().split())
d=[]
if n<7:
print(m)
exit(0)
for i in range(8):
d.append(set())
for i in range(m):
a,b=map(int,input().split())
d[a-1].add(b-1)
d[b-1].add(a-1)
minn=100
for i in range(0,7):
for j in range(0,7):
ss=d[i]&d[j]
if len(ss)<minn:
minn=len(ss)
print(m-minn)
``` | output | 1 | 73,410 | 13 | 146,821 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Anadi has a set of dominoes. Every domino has two parts, and each part contains some dots. For every a and b such that 1 ≤ a ≤ b ≤ 6, there is exactly one domino with a dots on one half and b dots on the other half. The set contains exactly 21 dominoes. Here is an exact illustration of his set:
<image>
Also, Anadi has an undirected graph without self-loops and multiple edges. He wants to choose some dominoes and place them on the edges of this graph. He can use at most one domino of each type. Each edge can fit at most one domino. It's not necessary to place a domino on each edge of the graph.
When placing a domino on an edge, he also chooses its direction. In other words, one half of any placed domino must be directed toward one of the endpoints of the edge and the other half must be directed toward the other endpoint. There's a catch: if there are multiple halves of dominoes directed toward the same vertex, each of these halves must contain the same number of dots.
How many dominoes at most can Anadi place on the edges of his graph?
Input
The first line contains two integers n and m (1 ≤ n ≤ 7, 0 ≤ m ≤ (n⋅(n-1))/(2)) — the number of vertices and the number of edges in the graph.
The next m lines contain two integers each. Integers in the i-th line are a_i and b_i (1 ≤ a, b ≤ n, a ≠ b) and denote that there is an edge which connects vertices a_i and b_i.
The graph might be disconnected. It's however guaranteed that the graph doesn't contain any self-loops, and that there is at most one edge between any pair of vertices.
Output
Output one integer which denotes the maximum number of dominoes which Anadi can place on the edges of the graph.
Examples
Input
4 4
1 2
2 3
3 4
4 1
Output
4
Input
7 0
Output
0
Input
3 1
1 3
Output
1
Input
7 21
1 2
1 3
1 4
1 5
1 6
1 7
2 3
2 4
2 5
2 6
2 7
3 4
3 5
3 6
3 7
4 5
4 6
4 7
5 6
5 7
6 7
Output
16
Note
Here is an illustration of Anadi's graph from the first sample test:
<image>
And here is one of the ways to place a domino on each of its edges:
<image>
Note that each vertex is faced by the halves of dominoes with the same number of dots. For instance, all halves directed toward vertex 1 have three dots. | instruction | 0 | 73,411 | 13 | 146,822 |
Tags: brute force, graphs
Correct Solution:
```
n, m = map(int, input().split())
g = [[]]*(n+1)
for i in range(m):
a, b = map(int, input().split())
g[a] = g[a][:] + [b]
g[b] = g[b][:] + [a]
if n==7:
M = 0
for i in range(1, n+1):
for j in range(1, n+1):
if i == j:
continue
N = []
for k in g[i]+g[j]:
N.append(k)
N = set(N)
num_edges = m - len(g[i]) - len(g[j]) + len(N)
M = max(M, num_edges)
print(min(M, 21))
else:
print(m)
``` | output | 1 | 73,411 | 13 | 146,823 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Anadi has a set of dominoes. Every domino has two parts, and each part contains some dots. For every a and b such that 1 ≤ a ≤ b ≤ 6, there is exactly one domino with a dots on one half and b dots on the other half. The set contains exactly 21 dominoes. Here is an exact illustration of his set:
<image>
Also, Anadi has an undirected graph without self-loops and multiple edges. He wants to choose some dominoes and place them on the edges of this graph. He can use at most one domino of each type. Each edge can fit at most one domino. It's not necessary to place a domino on each edge of the graph.
When placing a domino on an edge, he also chooses its direction. In other words, one half of any placed domino must be directed toward one of the endpoints of the edge and the other half must be directed toward the other endpoint. There's a catch: if there are multiple halves of dominoes directed toward the same vertex, each of these halves must contain the same number of dots.
How many dominoes at most can Anadi place on the edges of his graph?
Input
The first line contains two integers n and m (1 ≤ n ≤ 7, 0 ≤ m ≤ (n⋅(n-1))/(2)) — the number of vertices and the number of edges in the graph.
The next m lines contain two integers each. Integers in the i-th line are a_i and b_i (1 ≤ a, b ≤ n, a ≠ b) and denote that there is an edge which connects vertices a_i and b_i.
The graph might be disconnected. It's however guaranteed that the graph doesn't contain any self-loops, and that there is at most one edge between any pair of vertices.
Output
Output one integer which denotes the maximum number of dominoes which Anadi can place on the edges of the graph.
Examples
Input
4 4
1 2
2 3
3 4
4 1
Output
4
Input
7 0
Output
0
Input
3 1
1 3
Output
1
Input
7 21
1 2
1 3
1 4
1 5
1 6
1 7
2 3
2 4
2 5
2 6
2 7
3 4
3 5
3 6
3 7
4 5
4 6
4 7
5 6
5 7
6 7
Output
16
Note
Here is an illustration of Anadi's graph from the first sample test:
<image>
And here is one of the ways to place a domino on each of its edges:
<image>
Note that each vertex is faced by the halves of dominoes with the same number of dots. For instance, all halves directed toward vertex 1 have three dots. | instruction | 0 | 73,412 | 13 | 146,824 |
Tags: brute force, graphs
Correct Solution:
```
n,m=map(int,input().split())
L=[]
for i in range(n+1):
h=[]
L.append(h)
arr=[]
for i in range(m):
u,v=map(int,input().split())
L[u].append(v)
L[v].append(u)
arr.append((u,v))
if(n<7):
print(m)
else:
ans=0
for i in range(1,8):
ed=[]
for j in range(0,len(L[i])):
if(L[i][j]>i):
ed.append(L[i][j]-1)
else:
ed.append(L[i][j])
pre=dict()
for j in range(0,len(arr)):
x=arr[j][0]
y=arr[j][1]
if(x!=i and y!=i):
if(x>i):
x-=1
if(y>i):
y-=1
pre[(x,y)]=1
pre[(y,x)]=1
ct=0
for j in range(1,7):
c=0
for k in range(0,len(ed)):
if(pre.get((j,ed[k]))==None):
c+=1
ct=max(c,ct)
fnl=m-len(L[i])+ct
ans=max(ans,fnl)
print(ans)
``` | output | 1 | 73,412 | 13 | 146,825 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Anadi has a set of dominoes. Every domino has two parts, and each part contains some dots. For every a and b such that 1 ≤ a ≤ b ≤ 6, there is exactly one domino with a dots on one half and b dots on the other half. The set contains exactly 21 dominoes. Here is an exact illustration of his set:
<image>
Also, Anadi has an undirected graph without self-loops and multiple edges. He wants to choose some dominoes and place them on the edges of this graph. He can use at most one domino of each type. Each edge can fit at most one domino. It's not necessary to place a domino on each edge of the graph.
When placing a domino on an edge, he also chooses its direction. In other words, one half of any placed domino must be directed toward one of the endpoints of the edge and the other half must be directed toward the other endpoint. There's a catch: if there are multiple halves of dominoes directed toward the same vertex, each of these halves must contain the same number of dots.
How many dominoes at most can Anadi place on the edges of his graph?
Input
The first line contains two integers n and m (1 ≤ n ≤ 7, 0 ≤ m ≤ (n⋅(n-1))/(2)) — the number of vertices and the number of edges in the graph.
The next m lines contain two integers each. Integers in the i-th line are a_i and b_i (1 ≤ a, b ≤ n, a ≠ b) and denote that there is an edge which connects vertices a_i and b_i.
The graph might be disconnected. It's however guaranteed that the graph doesn't contain any self-loops, and that there is at most one edge between any pair of vertices.
Output
Output one integer which denotes the maximum number of dominoes which Anadi can place on the edges of the graph.
Examples
Input
4 4
1 2
2 3
3 4
4 1
Output
4
Input
7 0
Output
0
Input
3 1
1 3
Output
1
Input
7 21
1 2
1 3
1 4
1 5
1 6
1 7
2 3
2 4
2 5
2 6
2 7
3 4
3 5
3 6
3 7
4 5
4 6
4 7
5 6
5 7
6 7
Output
16
Note
Here is an illustration of Anadi's graph from the first sample test:
<image>
And here is one of the ways to place a domino on each of its edges:
<image>
Note that each vertex is faced by the halves of dominoes with the same number of dots. For instance, all halves directed toward vertex 1 have three dots. | instruction | 0 | 73,413 | 13 | 146,826 |
Tags: brute force, graphs
Correct Solution:
```
n , m = map(int,input().split())
lis=[]
for i in range(7):
lis.append(set())
for i in range(m):
a , b = map(int,input().split())
lis[a-1].add(b)
lis[b-1].add(a)
if n<=6:
print(m)
else:
ma=1000000
for i in range(7):
for j in range(i+1,7):
s=lis[i] & lis[j]
if len(s)<ma:
ma =len(s)
print(m-ma)
``` | output | 1 | 73,413 | 13 | 146,827 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Anadi has a set of dominoes. Every domino has two parts, and each part contains some dots. For every a and b such that 1 ≤ a ≤ b ≤ 6, there is exactly one domino with a dots on one half and b dots on the other half. The set contains exactly 21 dominoes. Here is an exact illustration of his set:
<image>
Also, Anadi has an undirected graph without self-loops and multiple edges. He wants to choose some dominoes and place them on the edges of this graph. He can use at most one domino of each type. Each edge can fit at most one domino. It's not necessary to place a domino on each edge of the graph.
When placing a domino on an edge, he also chooses its direction. In other words, one half of any placed domino must be directed toward one of the endpoints of the edge and the other half must be directed toward the other endpoint. There's a catch: if there are multiple halves of dominoes directed toward the same vertex, each of these halves must contain the same number of dots.
How many dominoes at most can Anadi place on the edges of his graph?
Input
The first line contains two integers n and m (1 ≤ n ≤ 7, 0 ≤ m ≤ (n⋅(n-1))/(2)) — the number of vertices and the number of edges in the graph.
The next m lines contain two integers each. Integers in the i-th line are a_i and b_i (1 ≤ a, b ≤ n, a ≠ b) and denote that there is an edge which connects vertices a_i and b_i.
The graph might be disconnected. It's however guaranteed that the graph doesn't contain any self-loops, and that there is at most one edge between any pair of vertices.
Output
Output one integer which denotes the maximum number of dominoes which Anadi can place on the edges of the graph.
Examples
Input
4 4
1 2
2 3
3 4
4 1
Output
4
Input
7 0
Output
0
Input
3 1
1 3
Output
1
Input
7 21
1 2
1 3
1 4
1 5
1 6
1 7
2 3
2 4
2 5
2 6
2 7
3 4
3 5
3 6
3 7
4 5
4 6
4 7
5 6
5 7
6 7
Output
16
Note
Here is an illustration of Anadi's graph from the first sample test:
<image>
And here is one of the ways to place a domino on each of its edges:
<image>
Note that each vertex is faced by the halves of dominoes with the same number of dots. For instance, all halves directed toward vertex 1 have three dots.
Submitted Solution:
```
from math import *
from collections import *
from bisect import *
import sys
input=sys.stdin.readline
t=1
while(t):
t-=1
n,k=map(int,input().split())
deg=[set() for i in range(n)]
for i in range(k):
u,v=map(int,input().split())
deg[u-1].add(v-1)
deg[v-1].add(u-1)
if(n<=6):
print(k)
else:
ma=0
for i in range(7):
for j in range(i+1,7):
ma=max(ma,k-len(deg[i]°[j]))
print(ma)
``` | instruction | 0 | 73,414 | 13 | 146,828 |
Yes | output | 1 | 73,414 | 13 | 146,829 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Anadi has a set of dominoes. Every domino has two parts, and each part contains some dots. For every a and b such that 1 ≤ a ≤ b ≤ 6, there is exactly one domino with a dots on one half and b dots on the other half. The set contains exactly 21 dominoes. Here is an exact illustration of his set:
<image>
Also, Anadi has an undirected graph without self-loops and multiple edges. He wants to choose some dominoes and place them on the edges of this graph. He can use at most one domino of each type. Each edge can fit at most one domino. It's not necessary to place a domino on each edge of the graph.
When placing a domino on an edge, he also chooses its direction. In other words, one half of any placed domino must be directed toward one of the endpoints of the edge and the other half must be directed toward the other endpoint. There's a catch: if there are multiple halves of dominoes directed toward the same vertex, each of these halves must contain the same number of dots.
How many dominoes at most can Anadi place on the edges of his graph?
Input
The first line contains two integers n and m (1 ≤ n ≤ 7, 0 ≤ m ≤ (n⋅(n-1))/(2)) — the number of vertices and the number of edges in the graph.
The next m lines contain two integers each. Integers in the i-th line are a_i and b_i (1 ≤ a, b ≤ n, a ≠ b) and denote that there is an edge which connects vertices a_i and b_i.
The graph might be disconnected. It's however guaranteed that the graph doesn't contain any self-loops, and that there is at most one edge between any pair of vertices.
Output
Output one integer which denotes the maximum number of dominoes which Anadi can place on the edges of the graph.
Examples
Input
4 4
1 2
2 3
3 4
4 1
Output
4
Input
7 0
Output
0
Input
3 1
1 3
Output
1
Input
7 21
1 2
1 3
1 4
1 5
1 6
1 7
2 3
2 4
2 5
2 6
2 7
3 4
3 5
3 6
3 7
4 5
4 6
4 7
5 6
5 7
6 7
Output
16
Note
Here is an illustration of Anadi's graph from the first sample test:
<image>
And here is one of the ways to place a domino on each of its edges:
<image>
Note that each vertex is faced by the halves of dominoes with the same number of dots. For instance, all halves directed toward vertex 1 have three dots.
Submitted Solution:
```
n,m=map(int,input().split())
edges=[list(map(int,input().split())) for i in range(m)]
for i in range(m):
edges[i][0]-=1
edges[i][1]-=1
from itertools import permutations
l=list(permutations(range(6)))
mx=0
for ij in l:
for node in range(7):
for seven in range(6):
cnt=[None]*10
for xi in range(6):
cnt[xi]=[0]*10
for xj in range(xi,6):
cnt[xi][xj]=1
numbering=[0]*10
c=0
for i in range(6):
if node==i:
c=1
numbering[i+c]=ij[i]
numbering[node]=seven
curr=0
for e in edges:
u,v=numbering[e[0]],numbering[e[1]]
u,v=min(u,v),max(u,v)
if cnt[u][v]:
cnt[u][v]=0
curr+=1
mx=max(mx,curr)
print(mx)
``` | instruction | 0 | 73,415 | 13 | 146,830 |
Yes | output | 1 | 73,415 | 13 | 146,831 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.