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.
In this problem you have to build tournament graph, consisting of n vertices, such, that for any oriented pair of vertices (v, u) (v ≠ u) there exists a path from vertex v to vertex u consisting of no more then two edges.
A directed graph without self-loops is a tournament, if there is exactly one edge between any two distinct vertices (in one out of two possible directions).
Input
The first line contains an integer n (3 ≤ n ≤ 1000), the number of the graph's vertices.
Output
Print -1 if there is no graph, satisfying the described conditions.
Otherwise, print n lines with n integers in each. The numbers should be separated with spaces. That is adjacency matrix a of the found tournament. Consider the graph vertices to be numbered with integers from 1 to n. Then av, u = 0, if there is no edge from v to u, and av, u = 1 if there is one.
As the output graph has to be a tournament, following equalities must be satisfied:
* av, u + au, v = 1 for each v, u (1 ≤ v, u ≤ n; v ≠ u);
* av, v = 0 for each v (1 ≤ v ≤ n).
Examples
Input
3
Output
0 1 0
0 0 1
1 0 0
Input
4
Output
-1
Submitted Solution:
```
#i'm sure this is stupid but i want to check it
k = int(input())
if k==1:print(0)
elif k == 3:
print('0 1 0\n0 0 1\n1 0 0')
else:
print(-1)
``` | instruction | 0 | 16,292 | 13 | 32,584 |
No | output | 1 | 16,292 | 13 | 32,585 |
Provide a correct Python 3 solution for this coding contest problem.
Takahashi is not good at problems about trees in programming contests, and Aoki is helping him practice.
First, Takahashi created a tree with N vertices numbered 1 through N, and wrote 0 at each edge.
Then, Aoki gave him M queries. The i-th of them is as follows:
* Increment the number written at each edge along the path connecting vertices a_i and b_i, by one.
After Takahashi executed all of the queries, he told Aoki that, for every edge, the written number became an even number. However, Aoki forgot to confirm that the graph Takahashi created was actually a tree, and it is possible that Takahashi made a mistake in creating a tree or executing queries.
Determine whether there exists a tree that has the property mentioned by Takahashi.
Constraints
* 2 ≤ N ≤ 10^5
* 1 ≤ M ≤ 10^5
* 1 ≤ a_i,b_i ≤ N
* a_i ≠ b_i
Input
Input is given from Standard Input in the following format:
N M
a_1 b_1
:
a_M b_M
Output
Print `YES` if there exists a tree that has the property mentioned by Takahashi; print `NO` otherwise.
Examples
Input
4 4
1 2
2 4
1 3
3 4
Output
YES
Input
5 5
1 2
3 5
5 1
3 4
2 3
Output
NO | instruction | 0 | 16,679 | 13 | 33,358 |
"Correct Solution:
```
import sys
imput = sys.stdin.readline
N,M = map(int,input().split())
V = [0 for _ in range(N)]
for _ in range(M):
a,b = map(int,input().split())
V[a-1] += 1
V[b-1] += 1
print('YES' if all([v%2==0 for v in V]) else 'NO')
``` | output | 1 | 16,679 | 13 | 33,359 |
Provide a correct Python 3 solution for this coding contest problem.
Takahashi is not good at problems about trees in programming contests, and Aoki is helping him practice.
First, Takahashi created a tree with N vertices numbered 1 through N, and wrote 0 at each edge.
Then, Aoki gave him M queries. The i-th of them is as follows:
* Increment the number written at each edge along the path connecting vertices a_i and b_i, by one.
After Takahashi executed all of the queries, he told Aoki that, for every edge, the written number became an even number. However, Aoki forgot to confirm that the graph Takahashi created was actually a tree, and it is possible that Takahashi made a mistake in creating a tree or executing queries.
Determine whether there exists a tree that has the property mentioned by Takahashi.
Constraints
* 2 ≤ N ≤ 10^5
* 1 ≤ M ≤ 10^5
* 1 ≤ a_i,b_i ≤ N
* a_i ≠ b_i
Input
Input is given from Standard Input in the following format:
N M
a_1 b_1
:
a_M b_M
Output
Print `YES` if there exists a tree that has the property mentioned by Takahashi; print `NO` otherwise.
Examples
Input
4 4
1 2
2 4
1 3
3 4
Output
YES
Input
5 5
1 2
3 5
5 1
3 4
2 3
Output
NO | instruction | 0 | 16,680 | 13 | 33,360 |
"Correct Solution:
```
n, m = map(int, input().split())
cnt = [0 for _ in range(n)]
for _ in range(m):
a, b = map(int, input().split())
a -= 1
b -= 1
cnt[a] = (cnt[a] + 1) % 2
cnt[b] = (cnt[b] + 1) % 2
if 1 in cnt:
print("NO")
else:
print("YES")
``` | output | 1 | 16,680 | 13 | 33,361 |
Provide a correct Python 3 solution for this coding contest problem.
Takahashi is not good at problems about trees in programming contests, and Aoki is helping him practice.
First, Takahashi created a tree with N vertices numbered 1 through N, and wrote 0 at each edge.
Then, Aoki gave him M queries. The i-th of them is as follows:
* Increment the number written at each edge along the path connecting vertices a_i and b_i, by one.
After Takahashi executed all of the queries, he told Aoki that, for every edge, the written number became an even number. However, Aoki forgot to confirm that the graph Takahashi created was actually a tree, and it is possible that Takahashi made a mistake in creating a tree or executing queries.
Determine whether there exists a tree that has the property mentioned by Takahashi.
Constraints
* 2 ≤ N ≤ 10^5
* 1 ≤ M ≤ 10^5
* 1 ≤ a_i,b_i ≤ N
* a_i ≠ b_i
Input
Input is given from Standard Input in the following format:
N M
a_1 b_1
:
a_M b_M
Output
Print `YES` if there exists a tree that has the property mentioned by Takahashi; print `NO` otherwise.
Examples
Input
4 4
1 2
2 4
1 3
3 4
Output
YES
Input
5 5
1 2
3 5
5 1
3 4
2 3
Output
NO | instruction | 0 | 16,681 | 13 | 33,362 |
"Correct Solution:
```
n,m = map(int,input().split())
s = [0]*(n)
for i in range(m):
a,b = map(int,input().split())
s[a-1] += 1
s[b-1] += 1
for i in s:
if i%2 != 0:
print("NO")
exit()
print("YES")
``` | output | 1 | 16,681 | 13 | 33,363 |
Provide a correct Python 3 solution for this coding contest problem.
Takahashi is not good at problems about trees in programming contests, and Aoki is helping him practice.
First, Takahashi created a tree with N vertices numbered 1 through N, and wrote 0 at each edge.
Then, Aoki gave him M queries. The i-th of them is as follows:
* Increment the number written at each edge along the path connecting vertices a_i and b_i, by one.
After Takahashi executed all of the queries, he told Aoki that, for every edge, the written number became an even number. However, Aoki forgot to confirm that the graph Takahashi created was actually a tree, and it is possible that Takahashi made a mistake in creating a tree or executing queries.
Determine whether there exists a tree that has the property mentioned by Takahashi.
Constraints
* 2 ≤ N ≤ 10^5
* 1 ≤ M ≤ 10^5
* 1 ≤ a_i,b_i ≤ N
* a_i ≠ b_i
Input
Input is given from Standard Input in the following format:
N M
a_1 b_1
:
a_M b_M
Output
Print `YES` if there exists a tree that has the property mentioned by Takahashi; print `NO` otherwise.
Examples
Input
4 4
1 2
2 4
1 3
3 4
Output
YES
Input
5 5
1 2
3 5
5 1
3 4
2 3
Output
NO | instruction | 0 | 16,682 | 13 | 33,364 |
"Correct Solution:
```
# 2019/10/05
n,m=map(int,input().split())
res=[0]*n
for _ in range(m):
a,b=(map(int,input().split()))
res[a-1]+=1
res[b-1]+=1
for e in res:
if e%2:
print('NO')
exit()
else:
print('YES')
``` | output | 1 | 16,682 | 13 | 33,365 |
Provide a correct Python 3 solution for this coding contest problem.
Takahashi is not good at problems about trees in programming contests, and Aoki is helping him practice.
First, Takahashi created a tree with N vertices numbered 1 through N, and wrote 0 at each edge.
Then, Aoki gave him M queries. The i-th of them is as follows:
* Increment the number written at each edge along the path connecting vertices a_i and b_i, by one.
After Takahashi executed all of the queries, he told Aoki that, for every edge, the written number became an even number. However, Aoki forgot to confirm that the graph Takahashi created was actually a tree, and it is possible that Takahashi made a mistake in creating a tree or executing queries.
Determine whether there exists a tree that has the property mentioned by Takahashi.
Constraints
* 2 ≤ N ≤ 10^5
* 1 ≤ M ≤ 10^5
* 1 ≤ a_i,b_i ≤ N
* a_i ≠ b_i
Input
Input is given from Standard Input in the following format:
N M
a_1 b_1
:
a_M b_M
Output
Print `YES` if there exists a tree that has the property mentioned by Takahashi; print `NO` otherwise.
Examples
Input
4 4
1 2
2 4
1 3
3 4
Output
YES
Input
5 5
1 2
3 5
5 1
3 4
2 3
Output
NO | instruction | 0 | 16,683 | 13 | 33,366 |
"Correct Solution:
```
N,M = map(int,input().split())
V = [0]*(N+1)
for i in range(M):
a,b = map(int,input().split())
V[a] +=1
V[b] +=1
bool = True
for i in range(N+1):
if V[i] % 2 ==1:
bool = False
break
if bool:
print("YES")
else:
print("NO")
``` | output | 1 | 16,683 | 13 | 33,367 |
Provide a correct Python 3 solution for this coding contest problem.
Takahashi is not good at problems about trees in programming contests, and Aoki is helping him practice.
First, Takahashi created a tree with N vertices numbered 1 through N, and wrote 0 at each edge.
Then, Aoki gave him M queries. The i-th of them is as follows:
* Increment the number written at each edge along the path connecting vertices a_i and b_i, by one.
After Takahashi executed all of the queries, he told Aoki that, for every edge, the written number became an even number. However, Aoki forgot to confirm that the graph Takahashi created was actually a tree, and it is possible that Takahashi made a mistake in creating a tree or executing queries.
Determine whether there exists a tree that has the property mentioned by Takahashi.
Constraints
* 2 ≤ N ≤ 10^5
* 1 ≤ M ≤ 10^5
* 1 ≤ a_i,b_i ≤ N
* a_i ≠ b_i
Input
Input is given from Standard Input in the following format:
N M
a_1 b_1
:
a_M b_M
Output
Print `YES` if there exists a tree that has the property mentioned by Takahashi; print `NO` otherwise.
Examples
Input
4 4
1 2
2 4
1 3
3 4
Output
YES
Input
5 5
1 2
3 5
5 1
3 4
2 3
Output
NO | instruction | 0 | 16,684 | 13 | 33,368 |
"Correct Solution:
```
n,m=map(int,input().split())
l=[0]*n
for i in range(m):
a,b=map(int,input().split())
l[a-1]+=1
l[b-1]+=1
k=0
while k<n:
if l[k]%2==0:
k+=1
continue
else:
print("NO")
exit()
print("YES")
``` | output | 1 | 16,684 | 13 | 33,369 |
Provide a correct Python 3 solution for this coding contest problem.
Takahashi is not good at problems about trees in programming contests, and Aoki is helping him practice.
First, Takahashi created a tree with N vertices numbered 1 through N, and wrote 0 at each edge.
Then, Aoki gave him M queries. The i-th of them is as follows:
* Increment the number written at each edge along the path connecting vertices a_i and b_i, by one.
After Takahashi executed all of the queries, he told Aoki that, for every edge, the written number became an even number. However, Aoki forgot to confirm that the graph Takahashi created was actually a tree, and it is possible that Takahashi made a mistake in creating a tree or executing queries.
Determine whether there exists a tree that has the property mentioned by Takahashi.
Constraints
* 2 ≤ N ≤ 10^5
* 1 ≤ M ≤ 10^5
* 1 ≤ a_i,b_i ≤ N
* a_i ≠ b_i
Input
Input is given from Standard Input in the following format:
N M
a_1 b_1
:
a_M b_M
Output
Print `YES` if there exists a tree that has the property mentioned by Takahashi; print `NO` otherwise.
Examples
Input
4 4
1 2
2 4
1 3
3 4
Output
YES
Input
5 5
1 2
3 5
5 1
3 4
2 3
Output
NO | instruction | 0 | 16,685 | 13 | 33,370 |
"Correct Solution:
```
n,m=map(int,input().split())
l=[0 for _ in range(n)]
for i in range(m):
for j in map(int,input().split()):
l[j-1]+=1
c=0
for i in range(n):
if l[i]%2==1:
c+=1
break
if c==0:
print("YES")
else:
print("NO")
``` | output | 1 | 16,685 | 13 | 33,371 |
Provide a correct Python 3 solution for this coding contest problem.
Takahashi is not good at problems about trees in programming contests, and Aoki is helping him practice.
First, Takahashi created a tree with N vertices numbered 1 through N, and wrote 0 at each edge.
Then, Aoki gave him M queries. The i-th of them is as follows:
* Increment the number written at each edge along the path connecting vertices a_i and b_i, by one.
After Takahashi executed all of the queries, he told Aoki that, for every edge, the written number became an even number. However, Aoki forgot to confirm that the graph Takahashi created was actually a tree, and it is possible that Takahashi made a mistake in creating a tree or executing queries.
Determine whether there exists a tree that has the property mentioned by Takahashi.
Constraints
* 2 ≤ N ≤ 10^5
* 1 ≤ M ≤ 10^5
* 1 ≤ a_i,b_i ≤ N
* a_i ≠ b_i
Input
Input is given from Standard Input in the following format:
N M
a_1 b_1
:
a_M b_M
Output
Print `YES` if there exists a tree that has the property mentioned by Takahashi; print `NO` otherwise.
Examples
Input
4 4
1 2
2 4
1 3
3 4
Output
YES
Input
5 5
1 2
3 5
5 1
3 4
2 3
Output
NO | instruction | 0 | 16,686 | 13 | 33,372 |
"Correct Solution:
```
n,m=map(int,input().split())
d=[0]*(n+1)
for i in range(m):
a,b=map(int,input().split())
d[a]=(d[a]+1)%2
d[b]=(d[b]+1)%2
print("NO" if any(d) else "YES")
``` | output | 1 | 16,686 | 13 | 33,373 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Takahashi is not good at problems about trees in programming contests, and Aoki is helping him practice.
First, Takahashi created a tree with N vertices numbered 1 through N, and wrote 0 at each edge.
Then, Aoki gave him M queries. The i-th of them is as follows:
* Increment the number written at each edge along the path connecting vertices a_i and b_i, by one.
After Takahashi executed all of the queries, he told Aoki that, for every edge, the written number became an even number. However, Aoki forgot to confirm that the graph Takahashi created was actually a tree, and it is possible that Takahashi made a mistake in creating a tree or executing queries.
Determine whether there exists a tree that has the property mentioned by Takahashi.
Constraints
* 2 ≤ N ≤ 10^5
* 1 ≤ M ≤ 10^5
* 1 ≤ a_i,b_i ≤ N
* a_i ≠ b_i
Input
Input is given from Standard Input in the following format:
N M
a_1 b_1
:
a_M b_M
Output
Print `YES` if there exists a tree that has the property mentioned by Takahashi; print `NO` otherwise.
Examples
Input
4 4
1 2
2 4
1 3
3 4
Output
YES
Input
5 5
1 2
3 5
5 1
3 4
2 3
Output
NO
Submitted Solution:
```
n,m = map(int, input().split( ))
cnt = [0]*n
#出現奇数回は不可
#出現偶数回なら勝手になる
for i in range(m):
ai,bi = map(int, input().split( ))
ai-=1;bi-=1
cnt[ai]+=1;cnt[bi]+=1
for i in range(n):
if cnt[i]%2:
print("NO")
exit()
print("YES")
``` | instruction | 0 | 16,687 | 13 | 33,374 |
Yes | output | 1 | 16,687 | 13 | 33,375 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Takahashi is not good at problems about trees in programming contests, and Aoki is helping him practice.
First, Takahashi created a tree with N vertices numbered 1 through N, and wrote 0 at each edge.
Then, Aoki gave him M queries. The i-th of them is as follows:
* Increment the number written at each edge along the path connecting vertices a_i and b_i, by one.
After Takahashi executed all of the queries, he told Aoki that, for every edge, the written number became an even number. However, Aoki forgot to confirm that the graph Takahashi created was actually a tree, and it is possible that Takahashi made a mistake in creating a tree or executing queries.
Determine whether there exists a tree that has the property mentioned by Takahashi.
Constraints
* 2 ≤ N ≤ 10^5
* 1 ≤ M ≤ 10^5
* 1 ≤ a_i,b_i ≤ N
* a_i ≠ b_i
Input
Input is given from Standard Input in the following format:
N M
a_1 b_1
:
a_M b_M
Output
Print `YES` if there exists a tree that has the property mentioned by Takahashi; print `NO` otherwise.
Examples
Input
4 4
1 2
2 4
1 3
3 4
Output
YES
Input
5 5
1 2
3 5
5 1
3 4
2 3
Output
NO
Submitted Solution:
```
n,m=map(int,input().split())
from collections import defaultdict
d=defaultdict(int)
for i in range(m):
a,b=map(int,input().split())
a,b=a-1,b-1
d[a]+=1
d[b]+=1
for i in range(n):
if d[i]%2==1:
print('NO')
exit()
print('YES')
``` | instruction | 0 | 16,688 | 13 | 33,376 |
Yes | output | 1 | 16,688 | 13 | 33,377 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Takahashi is not good at problems about trees in programming contests, and Aoki is helping him practice.
First, Takahashi created a tree with N vertices numbered 1 through N, and wrote 0 at each edge.
Then, Aoki gave him M queries. The i-th of them is as follows:
* Increment the number written at each edge along the path connecting vertices a_i and b_i, by one.
After Takahashi executed all of the queries, he told Aoki that, for every edge, the written number became an even number. However, Aoki forgot to confirm that the graph Takahashi created was actually a tree, and it is possible that Takahashi made a mistake in creating a tree or executing queries.
Determine whether there exists a tree that has the property mentioned by Takahashi.
Constraints
* 2 ≤ N ≤ 10^5
* 1 ≤ M ≤ 10^5
* 1 ≤ a_i,b_i ≤ N
* a_i ≠ b_i
Input
Input is given from Standard Input in the following format:
N M
a_1 b_1
:
a_M b_M
Output
Print `YES` if there exists a tree that has the property mentioned by Takahashi; print `NO` otherwise.
Examples
Input
4 4
1 2
2 4
1 3
3 4
Output
YES
Input
5 5
1 2
3 5
5 1
3 4
2 3
Output
NO
Submitted Solution:
```
import collections
N,M=map(int,input().split())
L=[]
for i in range(M):
X,Y=map(int,input().split())
L.append(X)
L.append(Y)
S=collections.Counter(L)
for i,j in S.items():
if j%2==1:
print("NO")
exit()
print("YES")
``` | instruction | 0 | 16,689 | 13 | 33,378 |
Yes | output | 1 | 16,689 | 13 | 33,379 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Takahashi is not good at problems about trees in programming contests, and Aoki is helping him practice.
First, Takahashi created a tree with N vertices numbered 1 through N, and wrote 0 at each edge.
Then, Aoki gave him M queries. The i-th of them is as follows:
* Increment the number written at each edge along the path connecting vertices a_i and b_i, by one.
After Takahashi executed all of the queries, he told Aoki that, for every edge, the written number became an even number. However, Aoki forgot to confirm that the graph Takahashi created was actually a tree, and it is possible that Takahashi made a mistake in creating a tree or executing queries.
Determine whether there exists a tree that has the property mentioned by Takahashi.
Constraints
* 2 ≤ N ≤ 10^5
* 1 ≤ M ≤ 10^5
* 1 ≤ a_i,b_i ≤ N
* a_i ≠ b_i
Input
Input is given from Standard Input in the following format:
N M
a_1 b_1
:
a_M b_M
Output
Print `YES` if there exists a tree that has the property mentioned by Takahashi; print `NO` otherwise.
Examples
Input
4 4
1 2
2 4
1 3
3 4
Output
YES
Input
5 5
1 2
3 5
5 1
3 4
2 3
Output
NO
Submitted Solution:
```
N,M = list(map(int,input().split()))
lis = [0 for i in range(N)]
for a_i,b_i in [ map(int,input().split()) for _ in range(M) ]:
lis[a_i-1] += 1
lis[b_i-1] += 1
for x in lis:
if x%2==1:
print("NO")
quit()
print("YES")
``` | instruction | 0 | 16,690 | 13 | 33,380 |
Yes | output | 1 | 16,690 | 13 | 33,381 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Takahashi is not good at problems about trees in programming contests, and Aoki is helping him practice.
First, Takahashi created a tree with N vertices numbered 1 through N, and wrote 0 at each edge.
Then, Aoki gave him M queries. The i-th of them is as follows:
* Increment the number written at each edge along the path connecting vertices a_i and b_i, by one.
After Takahashi executed all of the queries, he told Aoki that, for every edge, the written number became an even number. However, Aoki forgot to confirm that the graph Takahashi created was actually a tree, and it is possible that Takahashi made a mistake in creating a tree or executing queries.
Determine whether there exists a tree that has the property mentioned by Takahashi.
Constraints
* 2 ≤ N ≤ 10^5
* 1 ≤ M ≤ 10^5
* 1 ≤ a_i,b_i ≤ N
* a_i ≠ b_i
Input
Input is given from Standard Input in the following format:
N M
a_1 b_1
:
a_M b_M
Output
Print `YES` if there exists a tree that has the property mentioned by Takahashi; print `NO` otherwise.
Examples
Input
4 4
1 2
2 4
1 3
3 4
Output
YES
Input
5 5
1 2
3 5
5 1
3 4
2 3
Output
NO
Submitted Solution:
```
N, M = map(int, input().split())
path = [[]] * N
for _ in range(M):
a, b = map(int, input().split())
path[a - 1].append(b - 1)
if M % 2 == 0:
print('YES')
else:
print('NO')
``` | instruction | 0 | 16,691 | 13 | 33,382 |
No | output | 1 | 16,691 | 13 | 33,383 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Takahashi is not good at problems about trees in programming contests, and Aoki is helping him practice.
First, Takahashi created a tree with N vertices numbered 1 through N, and wrote 0 at each edge.
Then, Aoki gave him M queries. The i-th of them is as follows:
* Increment the number written at each edge along the path connecting vertices a_i and b_i, by one.
After Takahashi executed all of the queries, he told Aoki that, for every edge, the written number became an even number. However, Aoki forgot to confirm that the graph Takahashi created was actually a tree, and it is possible that Takahashi made a mistake in creating a tree or executing queries.
Determine whether there exists a tree that has the property mentioned by Takahashi.
Constraints
* 2 ≤ N ≤ 10^5
* 1 ≤ M ≤ 10^5
* 1 ≤ a_i,b_i ≤ N
* a_i ≠ b_i
Input
Input is given from Standard Input in the following format:
N M
a_1 b_1
:
a_M b_M
Output
Print `YES` if there exists a tree that has the property mentioned by Takahashi; print `NO` otherwise.
Examples
Input
4 4
1 2
2 4
1 3
3 4
Output
YES
Input
5 5
1 2
3 5
5 1
3 4
2 3
Output
NO
Submitted Solution:
```
import sys
# 整数の入力
N,M=list(map(int, input().split()))
p=[]
q=0
count=0
for i in range(0,M):
p.append(list(map(int, input().split())))
for i in range(0,M):
q+=p[i][0]-p[i][1]
if q%2==1:
print("NO")
else:
for j in range(1,M,int(M/10)):
for i in range(0,M):
if p[i][0]==j or p[i][1]==j:
count+=1
if count%2==1:
print("NO")
sys.exit()
else:
print("YES")
``` | instruction | 0 | 16,692 | 13 | 33,384 |
No | output | 1 | 16,692 | 13 | 33,385 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Takahashi is not good at problems about trees in programming contests, and Aoki is helping him practice.
First, Takahashi created a tree with N vertices numbered 1 through N, and wrote 0 at each edge.
Then, Aoki gave him M queries. The i-th of them is as follows:
* Increment the number written at each edge along the path connecting vertices a_i and b_i, by one.
After Takahashi executed all of the queries, he told Aoki that, for every edge, the written number became an even number. However, Aoki forgot to confirm that the graph Takahashi created was actually a tree, and it is possible that Takahashi made a mistake in creating a tree or executing queries.
Determine whether there exists a tree that has the property mentioned by Takahashi.
Constraints
* 2 ≤ N ≤ 10^5
* 1 ≤ M ≤ 10^5
* 1 ≤ a_i,b_i ≤ N
* a_i ≠ b_i
Input
Input is given from Standard Input in the following format:
N M
a_1 b_1
:
a_M b_M
Output
Print `YES` if there exists a tree that has the property mentioned by Takahashi; print `NO` otherwise.
Examples
Input
4 4
1 2
2 4
1 3
3 4
Output
YES
Input
5 5
1 2
3 5
5 1
3 4
2 3
Output
NO
Submitted Solution:
```
*c,=open(0).read().split()[2:]
print('YNEOS'[any(c.count(i)%2for i in set(c))::2])
``` | instruction | 0 | 16,693 | 13 | 33,386 |
No | output | 1 | 16,693 | 13 | 33,387 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Takahashi is not good at problems about trees in programming contests, and Aoki is helping him practice.
First, Takahashi created a tree with N vertices numbered 1 through N, and wrote 0 at each edge.
Then, Aoki gave him M queries. The i-th of them is as follows:
* Increment the number written at each edge along the path connecting vertices a_i and b_i, by one.
After Takahashi executed all of the queries, he told Aoki that, for every edge, the written number became an even number. However, Aoki forgot to confirm that the graph Takahashi created was actually a tree, and it is possible that Takahashi made a mistake in creating a tree or executing queries.
Determine whether there exists a tree that has the property mentioned by Takahashi.
Constraints
* 2 ≤ N ≤ 10^5
* 1 ≤ M ≤ 10^5
* 1 ≤ a_i,b_i ≤ N
* a_i ≠ b_i
Input
Input is given from Standard Input in the following format:
N M
a_1 b_1
:
a_M b_M
Output
Print `YES` if there exists a tree that has the property mentioned by Takahashi; print `NO` otherwise.
Examples
Input
4 4
1 2
2 4
1 3
3 4
Output
YES
Input
5 5
1 2
3 5
5 1
3 4
2 3
Output
NO
Submitted Solution:
```
def main():
N, M = map(int, input().split())
i = 0
AB = []
while i < M:
a, b = map(int, input().split())
AB += [a, b]
i += 1
i = 1
while i <= N:
if AB.count(i) % 2 > 0:
print('NO')
break
i += 1
else:
print('YES')
main()
``` | instruction | 0 | 16,694 | 13 | 33,388 |
No | output | 1 | 16,694 | 13 | 33,389 |
Provide a correct Python 3 solution for this coding contest problem.
Find the sum of weights of edges of the Minimum Spanning Tree for a given weighted undirected graph G = (V, E).
Constraints
* 1 ≤ |V| ≤ 10,000
* 0 ≤ |E| ≤ 100,000
* 0 ≤ wi ≤ 10,000
* The graph is connected
* There are no parallel edges
* There are no self-loops
Input
|V| |E|
s0 t0 w0
s1 t1 w1
:
s|E|-1 t|E|-1 w|E|-1
, where |V| is the number of vertices and |E| is the number of edges in the graph. The graph vertices are named with the numbers 0, 1,..., |V|-1 respectively.
si and ti represent source and target verticess of i-th edge (undirected) and wi represents the weight of the i-th edge.
Output
Print the sum of the weights of the Minimum Spanning Tree.
Examples
Input
4 6
0 1 2
1 2 1
2 3 1
3 0 1
0 2 3
1 3 5
Output
3
Input
6 9
0 1 1
0 2 3
1 2 1
1 3 7
2 4 1
1 4 3
3 4 1
3 5 1
4 5 6
Output
5 | instruction | 0 | 16,796 | 13 | 33,592 |
"Correct Solution:
```
import heapq
def prim_heap():
used = [True] * n #True:不使用
edgelist = []
for e in edge[0]:
heapq.heappush(edgelist,e)
used[0] = False
res = 0
while len(edgelist) != 0:
minedge = heapq.heappop(edgelist)
if not used[minedge[1]]:
continue
v = minedge[1]
used[v] = False
for e in edge[v]:
if used[e[1]]:
heapq.heappush(edgelist,e)
res += minedge[0]
return res
#########################
n,w = map(int,input().split())
edge = [[] for i in range(n)]
#隣接リスト edge[i]:[コスト,行先]
for i in range(w):
x,y,z = map(int,input().split())
#heapqを用いて、なるべく軽い辺から採用するため、zが先
edge[x].append([z,y])
edge[y].append([z,x])#無向のとき
print(prim_heap())
``` | output | 1 | 16,796 | 13 | 33,593 |
Provide a correct Python 3 solution for this coding contest problem.
Find the sum of weights of edges of the Minimum Spanning Tree for a given weighted undirected graph G = (V, E).
Constraints
* 1 ≤ |V| ≤ 10,000
* 0 ≤ |E| ≤ 100,000
* 0 ≤ wi ≤ 10,000
* The graph is connected
* There are no parallel edges
* There are no self-loops
Input
|V| |E|
s0 t0 w0
s1 t1 w1
:
s|E|-1 t|E|-1 w|E|-1
, where |V| is the number of vertices and |E| is the number of edges in the graph. The graph vertices are named with the numbers 0, 1,..., |V|-1 respectively.
si and ti represent source and target verticess of i-th edge (undirected) and wi represents the weight of the i-th edge.
Output
Print the sum of the weights of the Minimum Spanning Tree.
Examples
Input
4 6
0 1 2
1 2 1
2 3 1
3 0 1
0 2 3
1 3 5
Output
3
Input
6 9
0 1 1
0 2 3
1 2 1
1 3 7
2 4 1
1 4 3
3 4 1
3 5 1
4 5 6
Output
5 | instruction | 0 | 16,797 | 13 | 33,594 |
"Correct Solution:
```
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
input:
6 9
0 1 1
0 2 3
1 2 1
1 3 7
2 4 1
1 4 3
3 4 1
3 5 1
4 5 6
output:
5
"""
import sys
import heapq as hp
WHITE, GRAY, BLACK = 0, 1, 2
def generate_adj_table(v_table):
for each in v_table:
v_from, v_to, edge_weight = map(int, each)
init_adj_table[v_from][v_to] = edge_weight
init_adj_table[v_to][v_from] = edge_weight
return init_adj_table
def prim_span_tree():
distance[init_v] = 0
parent[init_v] = -1
path_heap = []
hp.heappush(path_heap, (distance[init_v], init_v))
while len(path_heap) >= 1:
current_vertex = hp.heappop(path_heap)[1]
color[current_vertex] = BLACK
for adj_vertex, adj_weight in adj_table[current_vertex].items():
if color[adj_vertex] is not BLACK:
if adj_weight < distance[adj_vertex]:
distance[adj_vertex] = adj_weight
parent[adj_vertex] = current_vertex
color[adj_vertex] = GRAY
hp.heappush(path_heap, (adj_weight, adj_vertex))
return distance
if __name__ == '__main__':
_input = sys.stdin.readlines()
vertices, edges = map(int, _input[0].split())
v_info = map(lambda x: x.split(), _input[1:])
parent = [-1] * vertices
distance = [float('inf')] * vertices
color = [WHITE] * vertices
init_v = 0
init_adj_table = tuple(dict() for _ in range(vertices))
adj_table = generate_adj_table(v_info)
print(sum(prim_span_tree()))
``` | output | 1 | 16,797 | 13 | 33,595 |
Provide a correct Python 3 solution for this coding contest problem.
Find the sum of weights of edges of the Minimum Spanning Tree for a given weighted undirected graph G = (V, E).
Constraints
* 1 ≤ |V| ≤ 10,000
* 0 ≤ |E| ≤ 100,000
* 0 ≤ wi ≤ 10,000
* The graph is connected
* There are no parallel edges
* There are no self-loops
Input
|V| |E|
s0 t0 w0
s1 t1 w1
:
s|E|-1 t|E|-1 w|E|-1
, where |V| is the number of vertices and |E| is the number of edges in the graph. The graph vertices are named with the numbers 0, 1,..., |V|-1 respectively.
si and ti represent source and target verticess of i-th edge (undirected) and wi represents the weight of the i-th edge.
Output
Print the sum of the weights of the Minimum Spanning Tree.
Examples
Input
4 6
0 1 2
1 2 1
2 3 1
3 0 1
0 2 3
1 3 5
Output
3
Input
6 9
0 1 1
0 2 3
1 2 1
1 3 7
2 4 1
1 4 3
3 4 1
3 5 1
4 5 6
Output
5 | instruction | 0 | 16,798 | 13 | 33,596 |
"Correct Solution:
```
v, e = map(int, input().split())
p = [i for i in range(v)]
E = [list(map(int, input().split())) for _ in range(e)]
E = sorted(E, key=lambda x: x[2])
def root(x):
if p[x]==x:
return x
p[x] = root(p[x])
return p[x]
ans = 0
for edg in E:
a, b, w = edg
if root(a)!=root(b):
ans += w
p[root(a)] = root(b)
print(ans)
``` | output | 1 | 16,798 | 13 | 33,597 |
Provide a correct Python 3 solution for this coding contest problem.
Find the sum of weights of edges of the Minimum Spanning Tree for a given weighted undirected graph G = (V, E).
Constraints
* 1 ≤ |V| ≤ 10,000
* 0 ≤ |E| ≤ 100,000
* 0 ≤ wi ≤ 10,000
* The graph is connected
* There are no parallel edges
* There are no self-loops
Input
|V| |E|
s0 t0 w0
s1 t1 w1
:
s|E|-1 t|E|-1 w|E|-1
, where |V| is the number of vertices and |E| is the number of edges in the graph. The graph vertices are named with the numbers 0, 1,..., |V|-1 respectively.
si and ti represent source and target verticess of i-th edge (undirected) and wi represents the weight of the i-th edge.
Output
Print the sum of the weights of the Minimum Spanning Tree.
Examples
Input
4 6
0 1 2
1 2 1
2 3 1
3 0 1
0 2 3
1 3 5
Output
3
Input
6 9
0 1 1
0 2 3
1 2 1
1 3 7
2 4 1
1 4 3
3 4 1
3 5 1
4 5 6
Output
5 | instruction | 0 | 16,799 | 13 | 33,598 |
"Correct Solution:
```
import sys
from heapq import heappush, heappop, heapify
V, E = map(int, sys.stdin.readline().strip().split())
graph = [[] for _ in range(V)]
for _ in range(E):
s, t, w = map(int, sys.stdin.readline().strip().split())
graph[s].append((w, t))
graph[t].append((w, s))
used = set()
q = []
for i in range(V):
if len(graph[i]):
for w, t in graph[i]:
q.append((w, t))
used.add(i)
break
heapify(q)
ans = 0
while q:
w, s = heappop(q)
if s in used:
continue
used.add(s)
ans += w
for w, t in graph[s]:
if t in used:
continue
heappush(q, (w, t))
print(ans)
``` | output | 1 | 16,799 | 13 | 33,599 |
Provide a correct Python 3 solution for this coding contest problem.
Find the sum of weights of edges of the Minimum Spanning Tree for a given weighted undirected graph G = (V, E).
Constraints
* 1 ≤ |V| ≤ 10,000
* 0 ≤ |E| ≤ 100,000
* 0 ≤ wi ≤ 10,000
* The graph is connected
* There are no parallel edges
* There are no self-loops
Input
|V| |E|
s0 t0 w0
s1 t1 w1
:
s|E|-1 t|E|-1 w|E|-1
, where |V| is the number of vertices and |E| is the number of edges in the graph. The graph vertices are named with the numbers 0, 1,..., |V|-1 respectively.
si and ti represent source and target verticess of i-th edge (undirected) and wi represents the weight of the i-th edge.
Output
Print the sum of the weights of the Minimum Spanning Tree.
Examples
Input
4 6
0 1 2
1 2 1
2 3 1
3 0 1
0 2 3
1 3 5
Output
3
Input
6 9
0 1 1
0 2 3
1 2 1
1 3 7
2 4 1
1 4 3
3 4 1
3 5 1
4 5 6
Output
5 | instruction | 0 | 16,800 | 13 | 33,600 |
"Correct Solution:
```
# -*- coding: utf-8 -*-
from operator import itemgetter
import sys
sys.setrecursionlimit(10000)
def inpl(): return tuple(map(int, input().split()))
V, E = inpl()
edges = []
graph = [[] for _ in range(V)] # [target, cost]
total_cost = 0
for _ in range(E):
edges.append(inpl())
gw = itemgetter(2)
edges = sorted(edges, key = gw)
tree = [[-1, 1] for _ in range(V)] # [next, rank]
def find(i):
if tree[i][0] == -1:
group = i
else:
group = find(tree[i][0])
return group
def unite(x, y):
px = find(x)
py = find(y)
if tree[px][1] == tree[py][1]: # rank is same
tree[py][0] = px
tree[px][1] += 1
else:
if tree[px][1] < tree[py][1]:
px, py = py, px
tree[py][0] = px
for s, t, w in edges:
if find(s) != find(t):
unite(s, t)
total_cost += w
graph[s].append([t, w])
graph[t].append([s, w])
print(total_cost)
``` | output | 1 | 16,800 | 13 | 33,601 |
Provide a correct Python 3 solution for this coding contest problem.
Find the sum of weights of edges of the Minimum Spanning Tree for a given weighted undirected graph G = (V, E).
Constraints
* 1 ≤ |V| ≤ 10,000
* 0 ≤ |E| ≤ 100,000
* 0 ≤ wi ≤ 10,000
* The graph is connected
* There are no parallel edges
* There are no self-loops
Input
|V| |E|
s0 t0 w0
s1 t1 w1
:
s|E|-1 t|E|-1 w|E|-1
, where |V| is the number of vertices and |E| is the number of edges in the graph. The graph vertices are named with the numbers 0, 1,..., |V|-1 respectively.
si and ti represent source and target verticess of i-th edge (undirected) and wi represents the weight of the i-th edge.
Output
Print the sum of the weights of the Minimum Spanning Tree.
Examples
Input
4 6
0 1 2
1 2 1
2 3 1
3 0 1
0 2 3
1 3 5
Output
3
Input
6 9
0 1 1
0 2 3
1 2 1
1 3 7
2 4 1
1 4 3
3 4 1
3 5 1
4 5 6
Output
5 | instruction | 0 | 16,801 | 13 | 33,602 |
"Correct Solution:
```
from heapq import heappush, heappop
line = input()
v, e = list(map(int, line.split()))
edge = {}
edge[0] = [[0, 0]]
for _ in range(0, e):
line = input()
s, t, w = list(map(int, line.split()))
if s not in edge:
edge[s] = [[t, w]]
else:
edge[s] += [[t, w]]
if t not in edge:
edge[t] = [[s, w]]
else:
edge[t] += [[s, w]]
def solve(v, edge):
q = []
vtx = set([i for i in range(0, v)])
for t, w in edge[0]:
heappush(q, (w, 0, t))
vn = set([0])
en = []
cost = 0
while len(vn) < v:
w = s = t = 0
qq = []
while q:
w, s, t = heappop(q)
if s in vn and t not in vn:
s, t = t, s
break
if s not in vn and t in vn:
break
vn.add(s)
en += [(s, t)]
cost += w
for t, w in edge[s]:
heappush(q, (w, s, t))
return cost
print(solve(v, edge))
``` | output | 1 | 16,801 | 13 | 33,603 |
Provide a correct Python 3 solution for this coding contest problem.
Find the sum of weights of edges of the Minimum Spanning Tree for a given weighted undirected graph G = (V, E).
Constraints
* 1 ≤ |V| ≤ 10,000
* 0 ≤ |E| ≤ 100,000
* 0 ≤ wi ≤ 10,000
* The graph is connected
* There are no parallel edges
* There are no self-loops
Input
|V| |E|
s0 t0 w0
s1 t1 w1
:
s|E|-1 t|E|-1 w|E|-1
, where |V| is the number of vertices and |E| is the number of edges in the graph. The graph vertices are named with the numbers 0, 1,..., |V|-1 respectively.
si and ti represent source and target verticess of i-th edge (undirected) and wi represents the weight of the i-th edge.
Output
Print the sum of the weights of the Minimum Spanning Tree.
Examples
Input
4 6
0 1 2
1 2 1
2 3 1
3 0 1
0 2 3
1 3 5
Output
3
Input
6 9
0 1 1
0 2 3
1 2 1
1 3 7
2 4 1
1 4 3
3 4 1
3 5 1
4 5 6
Output
5 | instruction | 0 | 16,802 | 13 | 33,604 |
"Correct Solution:
```
import sys
readline = sys.stdin.readline
from operator import itemgetter
class UnionFind:
__slots__ = ['p', 'rank']
def __init__(self, n):
self.p = list(range(n))
self.rank = [0] * n
def find_root(self, x):
if x != self.p[x]:
self.p[x] = self.find_root(self.p[x])
return self.p[x]
def same(self, x, y):
return self.find_root(x) == self.find_root(y)
def unite(self, x, y):
u = self.find_root(x)
v = self.find_root(y)
if u == v: return
if self.rank[u] < self.rank[v]:
self.p[u] = v
else:
self.p[v] = u
if self.rank[u] == self.rank[v]:
self.rank[u] += 1
n, m = map(int, readline().split())
edges = [tuple(map(int, readline().split())) for _ in [0] * m]
edges.sort(key=itemgetter(2), reverse=True)
uf = UnionFind(n)
ans = 0
for _ in [0] * (n - 1):
while edges:
s, t, w = edges.pop()
if not uf.same(s, t):
uf.unite(s, t)
break
ans += w
print(ans)
``` | output | 1 | 16,802 | 13 | 33,605 |
Provide a correct Python 3 solution for this coding contest problem.
Find the sum of weights of edges of the Minimum Spanning Tree for a given weighted undirected graph G = (V, E).
Constraints
* 1 ≤ |V| ≤ 10,000
* 0 ≤ |E| ≤ 100,000
* 0 ≤ wi ≤ 10,000
* The graph is connected
* There are no parallel edges
* There are no self-loops
Input
|V| |E|
s0 t0 w0
s1 t1 w1
:
s|E|-1 t|E|-1 w|E|-1
, where |V| is the number of vertices and |E| is the number of edges in the graph. The graph vertices are named with the numbers 0, 1,..., |V|-1 respectively.
si and ti represent source and target verticess of i-th edge (undirected) and wi represents the weight of the i-th edge.
Output
Print the sum of the weights of the Minimum Spanning Tree.
Examples
Input
4 6
0 1 2
1 2 1
2 3 1
3 0 1
0 2 3
1 3 5
Output
3
Input
6 9
0 1 1
0 2 3
1 2 1
1 3 7
2 4 1
1 4 3
3 4 1
3 5 1
4 5 6
Output
5 | instruction | 0 | 16,803 | 13 | 33,606 |
"Correct Solution:
```
import sys
def kruskal(v_count: int, edges: list) -> int:
tree = [-1]*v_count
def get_root(x):
if tree[x] < 0:
return x
tree[x] = get_root(tree[x])
return tree[x]
def unite(x, y):
x, y = get_root(x), get_root(y)
if x != y:
big, small = (x, y) if tree[x] < tree[y] else (y, x)
tree[big] += tree[small]
tree[small] = big
return x != y
cost = 0
for (w, _s, _t), _ in zip(filter(lambda p: unite(*p[1:]), sorted(edges)),
range(v_count-1)):
cost += w
return cost
V, E = map(int, input().split())
edges = [(w, s, t) for s, t, w in (map(int, l.split()) for l in sys.stdin)]
print(kruskal(V, edges))
``` | output | 1 | 16,803 | 13 | 33,607 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Find the sum of weights of edges of the Minimum Spanning Tree for a given weighted undirected graph G = (V, E).
Constraints
* 1 ≤ |V| ≤ 10,000
* 0 ≤ |E| ≤ 100,000
* 0 ≤ wi ≤ 10,000
* The graph is connected
* There are no parallel edges
* There are no self-loops
Input
|V| |E|
s0 t0 w0
s1 t1 w1
:
s|E|-1 t|E|-1 w|E|-1
, where |V| is the number of vertices and |E| is the number of edges in the graph. The graph vertices are named with the numbers 0, 1,..., |V|-1 respectively.
si and ti represent source and target verticess of i-th edge (undirected) and wi represents the weight of the i-th edge.
Output
Print the sum of the weights of the Minimum Spanning Tree.
Examples
Input
4 6
0 1 2
1 2 1
2 3 1
3 0 1
0 2 3
1 3 5
Output
3
Input
6 9
0 1 1
0 2 3
1 2 1
1 3 7
2 4 1
1 4 3
3 4 1
3 5 1
4 5 6
Output
5
Submitted Solution:
```
def root(r):
if par[r] == r:
return r
par[r] = root(par[r])
return par[r]
def same(x, y):
return root(x) == root(y)
def unite(x, y):
x = root(x)
y = root(y)
par[x] = y
v,e = map(int, input().split())
adj = [list(map(int, input().split())) for i in range(e)]
adj.sort(key = lambda x:x[2])
par = [i for i in range(v)]
sum = 0
for i,j,k in adj:
if not same(i, j):
sum += k
unite(i, j)
print(sum)
``` | instruction | 0 | 16,804 | 13 | 33,608 |
Yes | output | 1 | 16,804 | 13 | 33,609 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Find the sum of weights of edges of the Minimum Spanning Tree for a given weighted undirected graph G = (V, E).
Constraints
* 1 ≤ |V| ≤ 10,000
* 0 ≤ |E| ≤ 100,000
* 0 ≤ wi ≤ 10,000
* The graph is connected
* There are no parallel edges
* There are no self-loops
Input
|V| |E|
s0 t0 w0
s1 t1 w1
:
s|E|-1 t|E|-1 w|E|-1
, where |V| is the number of vertices and |E| is the number of edges in the graph. The graph vertices are named with the numbers 0, 1,..., |V|-1 respectively.
si and ti represent source and target verticess of i-th edge (undirected) and wi represents the weight of the i-th edge.
Output
Print the sum of the weights of the Minimum Spanning Tree.
Examples
Input
4 6
0 1 2
1 2 1
2 3 1
3 0 1
0 2 3
1 3 5
Output
3
Input
6 9
0 1 1
0 2 3
1 2 1
1 3 7
2 4 1
1 4 3
3 4 1
3 5 1
4 5 6
Output
5
Submitted Solution:
```
# Union-Find木(サイズ付き)
class UnionFind():
# 初めは全ての頂点が別々の木の根
def __init__(self, n): # n要素で初期化
self.parent = [None] * n # 親
self.rank = [None] * n # 木の深さ
self._size = [1] * n # 要素が属する集合の大きさ(根の要素のみ参照すること)
for i in range(n):
self.parent[i] = i
self.rank[i] = 0
def root_of(self, x):
children = [x]
while self.parent[x] != x:
# px = self.parent[x]
x = self.parent[x]
children.append(x)
for ch in children:
self.parent[ch] = x
return x
# xとyの属する集合を併合
def union(self, x, y):
rx = self.root_of(x)
ry = self.root_of(y)
if rx == ry:
return
if self.rank[rx] < self.rank[ry]: # ランクの小さい木から大きい木の根に辺を張る
self.parent[rx] = ry # rxをryの子とする
self._size[ry] += self._size[rx]
else:
self.parent[ry] = rx
self._size[rx] += self._size[ry]
if self.rank[rx] == self.rank[ry]:
self.rank[rx] += 1
# 同じ集合に属するかどうか
def same(self, x, y):
return self.root_of(x) == self.root_of(y)
def kruskal(E, n):
'''
E ... (辺のコスト, (始点, 終点))のリスト
n ... 頂点の数
'''
E.sort()
cost = 0
uf = UnionFind(n)
for c, (x, y) in E:
if not uf.same(x, y):
cost += c
uf.union(x, y)
return cost
n, m = [int(x) for x in input().split()]
E = []
for _ in range(m):
s, t, w = [int(x) for x in input().split()]
E.append((w, (s, t)))
print(kruskal(E, n))
``` | instruction | 0 | 16,805 | 13 | 33,610 |
Yes | output | 1 | 16,805 | 13 | 33,611 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Find the sum of weights of edges of the Minimum Spanning Tree for a given weighted undirected graph G = (V, E).
Constraints
* 1 ≤ |V| ≤ 10,000
* 0 ≤ |E| ≤ 100,000
* 0 ≤ wi ≤ 10,000
* The graph is connected
* There are no parallel edges
* There are no self-loops
Input
|V| |E|
s0 t0 w0
s1 t1 w1
:
s|E|-1 t|E|-1 w|E|-1
, where |V| is the number of vertices and |E| is the number of edges in the graph. The graph vertices are named with the numbers 0, 1,..., |V|-1 respectively.
si and ti represent source and target verticess of i-th edge (undirected) and wi represents the weight of the i-th edge.
Output
Print the sum of the weights of the Minimum Spanning Tree.
Examples
Input
4 6
0 1 2
1 2 1
2 3 1
3 0 1
0 2 3
1 3 5
Output
3
Input
6 9
0 1 1
0 2 3
1 2 1
1 3 7
2 4 1
1 4 3
3 4 1
3 5 1
4 5 6
Output
5
Submitted Solution:
```
class DisjointSet:
def __init__(self, size):
self.rank = [0 for i in range(size)]
self.p = [0 for i in range(size)]
for i in range(size):
self.makeSet(i)
def makeSet(self, x):
self.p[x] = x
self.rank[x] = 0
def same(self, x, y):
return self.findSet(x) == self.findSet(y)
def unite(self, x, y):
self.link(self.findSet(x), self.findSet(y))
def link(self, x, y):
if self.rank[x] > self.rank[y]:
self.p[y] = x
else:
self.p[x] = y
if self.rank[x] == self.rank[y]:
self.rank[y] += 1
def findSet(self, x):
if x != self.p[x]:
self.p[x] = self.findSet(self.p[x])
return self.p[x]
def kruskal(V, e):
# V=ノードの数,e=(s, t, w)のエッジのリスト
e = sorted(e, key=lambda x: x[2])
S = DisjointSet(V)
K = []
for s, t, w in e:
# if S.findSet(s) != S.findSet(t):
if not S.same(s, t):
S.unite(s, t)
K.append((s, t, w))
return K
if __name__ == '__main__':
V, E = map(int, input().split())
# s, t, w
e = [tuple(map(int, input().split())) for i in range(E)]
K = kruskal(V, e)
ans = 0
for s, t, w in K:
ans += w
print(ans)
``` | instruction | 0 | 16,806 | 13 | 33,612 |
Yes | output | 1 | 16,806 | 13 | 33,613 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Find the sum of weights of edges of the Minimum Spanning Tree for a given weighted undirected graph G = (V, E).
Constraints
* 1 ≤ |V| ≤ 10,000
* 0 ≤ |E| ≤ 100,000
* 0 ≤ wi ≤ 10,000
* The graph is connected
* There are no parallel edges
* There are no self-loops
Input
|V| |E|
s0 t0 w0
s1 t1 w1
:
s|E|-1 t|E|-1 w|E|-1
, where |V| is the number of vertices and |E| is the number of edges in the graph. The graph vertices are named with the numbers 0, 1,..., |V|-1 respectively.
si and ti represent source and target verticess of i-th edge (undirected) and wi represents the weight of the i-th edge.
Output
Print the sum of the weights of the Minimum Spanning Tree.
Examples
Input
4 6
0 1 2
1 2 1
2 3 1
3 0 1
0 2 3
1 3 5
Output
3
Input
6 9
0 1 1
0 2 3
1 2 1
1 3 7
2 4 1
1 4 3
3 4 1
3 5 1
4 5 6
Output
5
Submitted Solution:
```
#!/usr/bin/env python3
from collections import defaultdict
# クラスカル法を使う
V, E = map(int, input().split())
D = 0
tree = []
vertices = defaultdict(set)
for i in range(V):
tree.append(i) # 全ての頂点は自分の頂点 index と同じ index の木に属す
vertices[i].add(i)
edges = []
for i in range(E):
s, t, d = map(int, input().split())
edges.append((s, t, d))
edges.sort(key=lambda x: x[2])
while edges:
if V == 1:
break
s, t, d = edges.pop(0)
if tree[s] != tree[t]:
new = min(tree[s], tree[t])
old = max(tree[s], tree[t])
vertices[new] = vertices[new] | vertices[old]
for vertex in vertices[old]:
tree[vertex] = new
D += d
V -= 1
print(D)
``` | instruction | 0 | 16,807 | 13 | 33,614 |
Yes | output | 1 | 16,807 | 13 | 33,615 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Find the sum of weights of edges of the Minimum Spanning Tree for a given weighted undirected graph G = (V, E).
Constraints
* 1 ≤ |V| ≤ 10,000
* 0 ≤ |E| ≤ 100,000
* 0 ≤ wi ≤ 10,000
* The graph is connected
* There are no parallel edges
* There are no self-loops
Input
|V| |E|
s0 t0 w0
s1 t1 w1
:
s|E|-1 t|E|-1 w|E|-1
, where |V| is the number of vertices and |E| is the number of edges in the graph. The graph vertices are named with the numbers 0, 1,..., |V|-1 respectively.
si and ti represent source and target verticess of i-th edge (undirected) and wi represents the weight of the i-th edge.
Output
Print the sum of the weights of the Minimum Spanning Tree.
Examples
Input
4 6
0 1 2
1 2 1
2 3 1
3 0 1
0 2 3
1 3 5
Output
3
Input
6 9
0 1 1
0 2 3
1 2 1
1 3 7
2 4 1
1 4 3
3 4 1
3 5 1
4 5 6
Output
5
Submitted Solution:
```
import heapq
def prim(g, v):
for vi in range(v):
g[vi].sort()
cost = 0
heap = iter(g[0])
rest_tree = set(range(1, v))
while rest_tree:
d, u = next(heap)
if u in rest_tree:
cost += d
rest_tree -= {u}
heap = heapq.merge(heap, g[u])
return cost
from sys import stdin
readline = stdin.readline
v, e = map(int, readline().split())
from collections import defaultdict
g = defaultdict(list)
for _ in range(e):
s, t, d = map(int, readline().split())
g[s].append((d, t))
g[t].append((d, s))
print(prim(g, v))
``` | instruction | 0 | 16,808 | 13 | 33,616 |
No | output | 1 | 16,808 | 13 | 33,617 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Find the sum of weights of edges of the Minimum Spanning Tree for a given weighted undirected graph G = (V, E).
Constraints
* 1 ≤ |V| ≤ 10,000
* 0 ≤ |E| ≤ 100,000
* 0 ≤ wi ≤ 10,000
* The graph is connected
* There are no parallel edges
* There are no self-loops
Input
|V| |E|
s0 t0 w0
s1 t1 w1
:
s|E|-1 t|E|-1 w|E|-1
, where |V| is the number of vertices and |E| is the number of edges in the graph. The graph vertices are named with the numbers 0, 1,..., |V|-1 respectively.
si and ti represent source and target verticess of i-th edge (undirected) and wi represents the weight of the i-th edge.
Output
Print the sum of the weights of the Minimum Spanning Tree.
Examples
Input
4 6
0 1 2
1 2 1
2 3 1
3 0 1
0 2 3
1 3 5
Output
3
Input
6 9
0 1 1
0 2 3
1 2 1
1 3 7
2 4 1
1 4 3
3 4 1
3 5 1
4 5 6
Output
5
Submitted Solution:
```
import sys
from array import array
WHITE = 0
GRAY = 1
BLACK = 2
INF = 10001
line = sys.stdin.readline()
n, e = map(int, line.split())
color = array('i', [WHITE for _ in range(n)])
M = [[-1] * n for _ in range(n)]
d = array('i', [INF for _ in range(n)])
p = array('i', [-1 for _ in range(n)])
def prim():
d[0] = 0
p[0] = -1
res = 0
while 1:
mincost = INF
for i in range(n):
if color[i] != BLACK and d[i] < mincost:
mincost = d[i]
u = i
if mincost == INF:
break
res += d[u]
color[u] = BLACK
for v in range(n):
if color[v] != BLACK and M[u][v] != -1:
if M[u][v] < d[v]:
d[v] = M[u][v]
p[v] = u
return res
for i in range(e):
line = sys.stdin.readline()
s, t, w = map(int, line.split())
M[s][t] = w
M[t][s] = w
print(prim())
``` | instruction | 0 | 16,809 | 13 | 33,618 |
No | output | 1 | 16,809 | 13 | 33,619 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Find the sum of weights of edges of the Minimum Spanning Tree for a given weighted undirected graph G = (V, E).
Constraints
* 1 ≤ |V| ≤ 10,000
* 0 ≤ |E| ≤ 100,000
* 0 ≤ wi ≤ 10,000
* The graph is connected
* There are no parallel edges
* There are no self-loops
Input
|V| |E|
s0 t0 w0
s1 t1 w1
:
s|E|-1 t|E|-1 w|E|-1
, where |V| is the number of vertices and |E| is the number of edges in the graph. The graph vertices are named with the numbers 0, 1,..., |V|-1 respectively.
si and ti represent source and target verticess of i-th edge (undirected) and wi represents the weight of the i-th edge.
Output
Print the sum of the weights of the Minimum Spanning Tree.
Examples
Input
4 6
0 1 2
1 2 1
2 3 1
3 0 1
0 2 3
1 3 5
Output
3
Input
6 9
0 1 1
0 2 3
1 2 1
1 3 7
2 4 1
1 4 3
3 4 1
3 5 1
4 5 6
Output
5
Submitted Solution:
```
import sys
WHITE = 0
GRAY = 1
BLACK = 2
INF = 10001
line = sys.stdin.readline()
n, e = map(int, line.split())
color = [WHITE for _ in range(n)]
M = [[-1] * n for _ in range(n)]
d = [INF for _ in range(n)]
p = [-1 for _ in range(n)]
def prim():
d[0] = 0
p[0] = -1
res = 0
while 1:
mincost = INF
for i in range(n):
if color[i] != BLACK and d[i] < mincost:
mincost = d[i]
u = i
if mincost == INF:
break
res += d[u]
color[u] = BLACK
for v in range(n):
if color[v] != BLACK and M[u][v] != -1:
if M[u][v] < d[v]:
d[v] = M[u][v]
p[v] = u
color[v] = GRAY
return res
for i in range(e):
line = sys.stdin.readline()
s, t, w = map(int, line.split())
M[s][t] = w
M[t][s] = w
print(prim())
``` | instruction | 0 | 16,810 | 13 | 33,620 |
No | output | 1 | 16,810 | 13 | 33,621 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Find the sum of weights of edges of the Minimum Spanning Tree for a given weighted undirected graph G = (V, E).
Constraints
* 1 ≤ |V| ≤ 10,000
* 0 ≤ |E| ≤ 100,000
* 0 ≤ wi ≤ 10,000
* The graph is connected
* There are no parallel edges
* There are no self-loops
Input
|V| |E|
s0 t0 w0
s1 t1 w1
:
s|E|-1 t|E|-1 w|E|-1
, where |V| is the number of vertices and |E| is the number of edges in the graph. The graph vertices are named with the numbers 0, 1,..., |V|-1 respectively.
si and ti represent source and target verticess of i-th edge (undirected) and wi represents the weight of the i-th edge.
Output
Print the sum of the weights of the Minimum Spanning Tree.
Examples
Input
4 6
0 1 2
1 2 1
2 3 1
3 0 1
0 2 3
1 3 5
Output
3
Input
6 9
0 1 1
0 2 3
1 2 1
1 3 7
2 4 1
1 4 3
3 4 1
3 5 1
4 5 6
Output
5
Submitted Solution:
```
from collections import defaultdict
INF = 9999999999999999999999999999
def spanning_tree(G):
n = len(G)
T = {}
added = set()
min_c = INF
for i in G:
for j in G[i]:
if G[i][j] < min_c:
min_c = G[i][j]
min_e = (i, j)
T[min_e] = min_c
added.add(min_e[0])
added.add(min_e[1])
while len(added) < n:
min_c = INF
for u in added:
for v in G[u]:
if v in added:
continue
if G[u][v] < min_c:
min_c = G[u][v]
min_e = (u, v)
T[min_e] = min_c
added.add(min_e[1])
return T
G = {}
n, m = [int(v) for v in input().strip().split(' ')]
for i in range(n):
G[i] = {}
for _ in range(m):
s, t, c = [int(v) for v in input().strip().split(' ')]
G[s][t] = c
G[t][s] = c
T = spanning_tree(G)
s = 0
for e in T:
s += T[e]
print(s)
``` | instruction | 0 | 16,811 | 13 | 33,622 |
No | output | 1 | 16,811 | 13 | 33,623 |
Provide tags and a correct Python 3 solution for this coding contest problem.
It's the year 5555. You have a graph, and you want to find a long cycle and a huge independent set, just because you can. But for now, let's just stick with finding either.
Given a connected graph with n vertices, you can choose to either:
* find an independent set that has exactly ⌈√{n}⌉ vertices.
* find a simple cycle of length at least ⌈√{n}⌉.
An independent set is a set of vertices such that no two of them are connected by an edge. A simple cycle is a cycle that doesn't contain any vertex twice. I have a proof you can always solve one of these problems, but it's too long to fit this margin.
Input
The first line contains two integers n and m (5 ≤ n ≤ 10^5, n-1 ≤ m ≤ 2 ⋅ 10^5) — the number of vertices and edges in the graph.
Each of the next m lines contains two space-separated integers u and v (1 ≤ u,v ≤ n) that mean there's an edge between vertices u and v. It's guaranteed that the graph is connected and doesn't contain any self-loops or multiple edges.
Output
If you choose to solve the first problem, then on the first line print "1", followed by a line containing ⌈√{n}⌉ distinct integers not exceeding n, the vertices in the desired independent set.
If you, however, choose to solve the second problem, then on the first line print "2", followed by a line containing one integer, c, representing the length of the found cycle, followed by a line containing c distinct integers integers not exceeding n, the vertices in the desired cycle, in the order they appear in the cycle.
Examples
Input
6 6
1 3
3 4
4 2
2 6
5 6
5 1
Output
1
1 6 4
Input
6 8
1 3
3 4
4 2
2 6
5 6
5 1
1 4
2 5
Output
2
4
1 5 2 4
Input
5 4
1 2
1 3
2 4
2 5
Output
1
3 4 5
Note
In the first sample:
<image>
Notice that you can solve either problem, so printing the cycle 2-4-3-1-5-6 is also acceptable.
In the second sample:
<image>
Notice that if there are multiple answers you can print any, so printing the cycle 2-5-6, for example, is acceptable.
In the third sample:
<image> | instruction | 0 | 16,948 | 13 | 33,896 |
Tags: constructive algorithms, dfs and similar, graphs, greedy
Correct Solution:
```
import sys
input = sys.stdin.readline
n, m = map(int, input().split())
e = [tuple(map(int, input().split())) for _ in range(m)]
g = [[] for _ in range(n + 1)]
for u, v in e:
g[u].append(v)
g[v].append(u)
req = 1
while req * req < n:
req += 1
def dfs():
dep = [0] * (n + 1)
par = [0] * (n + 1)
mk = [0] * (n + 1)
st = [1]
st2 = []
while st:
u = st.pop()
if dep[u]:
continue
st2.append(u)
dep[u] = dep[par[u]] + 1
for v in g[u]:
if not dep[v]:
par[v] = u
st.append(v)
elif dep[u] - dep[v] + 1 >= req:
ans = []
while u != par[v]:
ans.append(u)
u = par[u]
return (None, ans)
while st2:
u = st2.pop()
if not mk[u]:
for v in g[u]:
if dep[v] < dep[u]:
mk[v] = 1
return ([u for u in range(1, n + 1) if not mk[u]][:req], None)
iset, cyc = dfs()
if iset:
print(1)
print(*iset)
else:
print(2)
print(len(cyc))
print(*cyc)
``` | output | 1 | 16,948 | 13 | 33,897 |
Provide tags and a correct Python 3 solution for this coding contest problem.
It's the year 5555. You have a graph, and you want to find a long cycle and a huge independent set, just because you can. But for now, let's just stick with finding either.
Given a connected graph with n vertices, you can choose to either:
* find an independent set that has exactly ⌈√{n}⌉ vertices.
* find a simple cycle of length at least ⌈√{n}⌉.
An independent set is a set of vertices such that no two of them are connected by an edge. A simple cycle is a cycle that doesn't contain any vertex twice. I have a proof you can always solve one of these problems, but it's too long to fit this margin.
Input
The first line contains two integers n and m (5 ≤ n ≤ 10^5, n-1 ≤ m ≤ 2 ⋅ 10^5) — the number of vertices and edges in the graph.
Each of the next m lines contains two space-separated integers u and v (1 ≤ u,v ≤ n) that mean there's an edge between vertices u and v. It's guaranteed that the graph is connected and doesn't contain any self-loops or multiple edges.
Output
If you choose to solve the first problem, then on the first line print "1", followed by a line containing ⌈√{n}⌉ distinct integers not exceeding n, the vertices in the desired independent set.
If you, however, choose to solve the second problem, then on the first line print "2", followed by a line containing one integer, c, representing the length of the found cycle, followed by a line containing c distinct integers integers not exceeding n, the vertices in the desired cycle, in the order they appear in the cycle.
Examples
Input
6 6
1 3
3 4
4 2
2 6
5 6
5 1
Output
1
1 6 4
Input
6 8
1 3
3 4
4 2
2 6
5 6
5 1
1 4
2 5
Output
2
4
1 5 2 4
Input
5 4
1 2
1 3
2 4
2 5
Output
1
3 4 5
Note
In the first sample:
<image>
Notice that you can solve either problem, so printing the cycle 2-4-3-1-5-6 is also acceptable.
In the second sample:
<image>
Notice that if there are multiple answers you can print any, so printing the cycle 2-5-6, for example, is acceptable.
In the third sample:
<image> | instruction | 0 | 16,949 | 13 | 33,898 |
Tags: constructive algorithms, dfs and similar, graphs, greedy
Correct Solution:
```
import math
import sys
input = sys.stdin.readline
from collections import *
def li():return [int(i) for i in input().rstrip('\n').split()]
def st():return input().rstrip('\n')
def val():return int(input().rstrip('\n'))
def li2():return [i for i in input().rstrip('\n')]
def li3():return [int(i) for i in input().rstrip('\n')]
def dfs():
global req
dep = [0]* (n + 1)
par = [0] * (n + 1)
st = [5]
st2 = []
while st:
u = st.pop()
if dep[u]:
continue
st2.append(u)
dep[u] = dep[par[u]] + 1
for v in g2[u]:
if not dep[v]:
par[v] = u
st.append(v)
elif dep[u] - dep[v] + 1>= req:
cyc = []
while u != par[v]:
cyc.append(u)
u = par[u]
return (None, cyc)
g = defaultdict(set)
g2 = defaultdict(set)
n,m = li()
for i in range(m):
a,b = li()
g[a].add(b)
g[b].add(a)
g2[a].add(b)
g2[b].add(a)
currset = set()
ma = math.ceil(n**0.5)
req = ma
for i in sorted(g,key = lambda x:len(g[x])):
if i in g:
currset.add(i)
for k in list(g[i]):
if k in g:g.pop(k)
g.pop(i)
if len(currset) == ma:break
if len(currset) >= ma:
print(1)
print(*list(currset)[:ma])
exit()
print(2)
_,cycles = dfs()
print(len(cycles))
print(*cycles)
``` | output | 1 | 16,949 | 13 | 33,899 |
Provide tags and a correct Python 3 solution for this coding contest problem.
It's the year 5555. You have a graph, and you want to find a long cycle and a huge independent set, just because you can. But for now, let's just stick with finding either.
Given a connected graph with n vertices, you can choose to either:
* find an independent set that has exactly ⌈√{n}⌉ vertices.
* find a simple cycle of length at least ⌈√{n}⌉.
An independent set is a set of vertices such that no two of them are connected by an edge. A simple cycle is a cycle that doesn't contain any vertex twice. I have a proof you can always solve one of these problems, but it's too long to fit this margin.
Input
The first line contains two integers n and m (5 ≤ n ≤ 10^5, n-1 ≤ m ≤ 2 ⋅ 10^5) — the number of vertices and edges in the graph.
Each of the next m lines contains two space-separated integers u and v (1 ≤ u,v ≤ n) that mean there's an edge between vertices u and v. It's guaranteed that the graph is connected and doesn't contain any self-loops or multiple edges.
Output
If you choose to solve the first problem, then on the first line print "1", followed by a line containing ⌈√{n}⌉ distinct integers not exceeding n, the vertices in the desired independent set.
If you, however, choose to solve the second problem, then on the first line print "2", followed by a line containing one integer, c, representing the length of the found cycle, followed by a line containing c distinct integers integers not exceeding n, the vertices in the desired cycle, in the order they appear in the cycle.
Examples
Input
6 6
1 3
3 4
4 2
2 6
5 6
5 1
Output
1
1 6 4
Input
6 8
1 3
3 4
4 2
2 6
5 6
5 1
1 4
2 5
Output
2
4
1 5 2 4
Input
5 4
1 2
1 3
2 4
2 5
Output
1
3 4 5
Note
In the first sample:
<image>
Notice that you can solve either problem, so printing the cycle 2-4-3-1-5-6 is also acceptable.
In the second sample:
<image>
Notice that if there are multiple answers you can print any, so printing the cycle 2-5-6, for example, is acceptable.
In the third sample:
<image> | instruction | 0 | 16,950 | 13 | 33,900 |
Tags: constructive algorithms, dfs and similar, graphs, greedy
Correct Solution:
```
import sys
input = sys.stdin.readline
n, m = map(int, input().split())
e = [tuple(map(int, input().split())) for _ in range(m)]
g = [[] for _ in range(n + 1)]
for u, v in e:
g[u].append(v)
g[v].append(u)
req = 1
while req * req < n:
req += 1
def dfs():
dep = [0] * (n + 1)
par = [0] * (n + 1)
st = [1]
st2 = []
while st:
u = st.pop()
if dep[u]:
continue
st2.append(u)
dep[u] = dep[par[u]] + 1
for v in g[u]:
if not dep[v]:
par[v] = u
st.append(v)
elif dep[u] - dep[v] + 1 >= req:
cyc = []
while u != par[v]:
cyc.append(u)
u = par[u]
return (None, cyc)
mk = [0] * (n + 1)
iset = []
while st2:
u = st2.pop()
if not mk[u]:
iset.append(u)
for v in g[u]:
mk[v] = 1
return (iset[:req], None)
iset, cyc = dfs()
if iset:
print(1)
print(*iset)
else:
print(2)
print(len(cyc))
print(*cyc)
``` | output | 1 | 16,950 | 13 | 33,901 |
Provide tags and a correct Python 3 solution for this coding contest problem.
It's the year 5555. You have a graph, and you want to find a long cycle and a huge independent set, just because you can. But for now, let's just stick with finding either.
Given a connected graph with n vertices, you can choose to either:
* find an independent set that has exactly ⌈√{n}⌉ vertices.
* find a simple cycle of length at least ⌈√{n}⌉.
An independent set is a set of vertices such that no two of them are connected by an edge. A simple cycle is a cycle that doesn't contain any vertex twice. I have a proof you can always solve one of these problems, but it's too long to fit this margin.
Input
The first line contains two integers n and m (5 ≤ n ≤ 10^5, n-1 ≤ m ≤ 2 ⋅ 10^5) — the number of vertices and edges in the graph.
Each of the next m lines contains two space-separated integers u and v (1 ≤ u,v ≤ n) that mean there's an edge between vertices u and v. It's guaranteed that the graph is connected and doesn't contain any self-loops or multiple edges.
Output
If you choose to solve the first problem, then on the first line print "1", followed by a line containing ⌈√{n}⌉ distinct integers not exceeding n, the vertices in the desired independent set.
If you, however, choose to solve the second problem, then on the first line print "2", followed by a line containing one integer, c, representing the length of the found cycle, followed by a line containing c distinct integers integers not exceeding n, the vertices in the desired cycle, in the order they appear in the cycle.
Examples
Input
6 6
1 3
3 4
4 2
2 6
5 6
5 1
Output
1
1 6 4
Input
6 8
1 3
3 4
4 2
2 6
5 6
5 1
1 4
2 5
Output
2
4
1 5 2 4
Input
5 4
1 2
1 3
2 4
2 5
Output
1
3 4 5
Note
In the first sample:
<image>
Notice that you can solve either problem, so printing the cycle 2-4-3-1-5-6 is also acceptable.
In the second sample:
<image>
Notice that if there are multiple answers you can print any, so printing the cycle 2-5-6, for example, is acceptable.
In the third sample:
<image> | instruction | 0 | 16,951 | 13 | 33,902 |
Tags: constructive algorithms, dfs and similar, graphs, greedy
Correct Solution:
```
import math
import sys
input = sys.stdin.readline
from collections import *
def li():return [int(i) for i in input().rstrip('\n').split()]
def st():return input().rstrip('\n')
def val():return int(input().rstrip('\n'))
def li2():return [i for i in input().rstrip('\n')]
def li3():return [int(i) for i in input().rstrip('\n')]
def dfs():
global req
dep = [0]* (n + 1)
par = [0] * (n + 1)
st = [5]
while st:
u = st.pop()
if dep[u]:
continue
dep[u] = dep[par[u]] + 1
for v in g2[u]:
if not dep[v]:
par[v] = u
st.append(v)
elif dep[u] - dep[v] + 1>= req:
cyc = []
while u != par[v]:
cyc.append(u)
u = par[u]
return (None, cyc)
g = defaultdict(set)
g2 = defaultdict(set)
n,m = li()
for i in range(m):
a,b = li()
g[a].add(b)
g[b].add(a)
g2[a].add(b)
g2[b].add(a)
for i in g2:
g2[i] = set(sorted(list(g2[i])))
currset = set()
ma = math.ceil(n**0.5)
req = ma
for i in sorted(g,key = lambda x:len(g[x])):
if i in g:
currset.add(i)
for k in list(g[i]):
if k in g:g.pop(k)
g.pop(i)
if len(currset) == ma:break
if len(currset) >= ma:
print(1)
print(*list(currset)[:ma])
exit()
print(2)
_,cycles = dfs()
print(len(cycles))
print(*cycles)
``` | output | 1 | 16,951 | 13 | 33,903 |
Provide tags and a correct Python 3 solution for this coding contest problem.
It's the year 5555. You have a graph, and you want to find a long cycle and a huge independent set, just because you can. But for now, let's just stick with finding either.
Given a connected graph with n vertices, you can choose to either:
* find an independent set that has exactly ⌈√{n}⌉ vertices.
* find a simple cycle of length at least ⌈√{n}⌉.
An independent set is a set of vertices such that no two of them are connected by an edge. A simple cycle is a cycle that doesn't contain any vertex twice. I have a proof you can always solve one of these problems, but it's too long to fit this margin.
Input
The first line contains two integers n and m (5 ≤ n ≤ 10^5, n-1 ≤ m ≤ 2 ⋅ 10^5) — the number of vertices and edges in the graph.
Each of the next m lines contains two space-separated integers u and v (1 ≤ u,v ≤ n) that mean there's an edge between vertices u and v. It's guaranteed that the graph is connected and doesn't contain any self-loops or multiple edges.
Output
If you choose to solve the first problem, then on the first line print "1", followed by a line containing ⌈√{n}⌉ distinct integers not exceeding n, the vertices in the desired independent set.
If you, however, choose to solve the second problem, then on the first line print "2", followed by a line containing one integer, c, representing the length of the found cycle, followed by a line containing c distinct integers integers not exceeding n, the vertices in the desired cycle, in the order they appear in the cycle.
Examples
Input
6 6
1 3
3 4
4 2
2 6
5 6
5 1
Output
1
1 6 4
Input
6 8
1 3
3 4
4 2
2 6
5 6
5 1
1 4
2 5
Output
2
4
1 5 2 4
Input
5 4
1 2
1 3
2 4
2 5
Output
1
3 4 5
Note
In the first sample:
<image>
Notice that you can solve either problem, so printing the cycle 2-4-3-1-5-6 is also acceptable.
In the second sample:
<image>
Notice that if there are multiple answers you can print any, so printing the cycle 2-5-6, for example, is acceptable.
In the third sample:
<image> | instruction | 0 | 16,952 | 13 | 33,904 |
Tags: constructive algorithms, dfs and similar, graphs, greedy
Correct Solution:
```
import math
import sys
input = sys.stdin.readline
from collections import *
def li():return [int(i) for i in input().rstrip('\n').split()]
def st():return input().rstrip('\n')
def val():return int(input().rstrip('\n'))
def li2():return [i for i in input().rstrip('\n')]
def li3():return [int(i) for i in input().rstrip('\n')]
def dfs():
global req
dep = [0]* (n + 1)
par = [0] * (n + 1)
st = [5]
st2 = []
while st:
u = st.pop()
if dep[u]:
continue
st2.append(u)
dep[u] = dep[par[u]] + 1
for v in g2[u]:
if not dep[v]:
par[v] = u
st.append(v)
elif dep[u] - dep[v] + 1>= req:
cyc = []
while u != par[v]:
cyc.append(u)
u = par[u]
return (None, cyc)
g = defaultdict(set)
g2 = defaultdict(set)
n,m = li()
for i in range(m):
a,b = li()
g[a].add(b)
g[b].add(a)
g2[a].add(b)
g2[b].add(a)
for i in g2:
g2[i] = set(sorted(list(g2[i])))
currset = set()
ma = math.ceil(n**0.5)
req = ma
for i in sorted(g,key = lambda x:len(g[x])):
if i in g:
currset.add(i)
for k in list(g[i]):
if k in g:g.pop(k)
g.pop(i)
if len(currset) == ma:break
if len(currset) >= ma:
print(1)
print(*list(currset)[:ma])
exit()
print(2)
_,cycles = dfs()
print(len(cycles))
print(*cycles)
``` | output | 1 | 16,952 | 13 | 33,905 |
Provide tags and a correct Python 3 solution for this coding contest problem.
It's the year 5555. You have a graph, and you want to find a long cycle and a huge independent set, just because you can. But for now, let's just stick with finding either.
Given a connected graph with n vertices, you can choose to either:
* find an independent set that has exactly ⌈√{n}⌉ vertices.
* find a simple cycle of length at least ⌈√{n}⌉.
An independent set is a set of vertices such that no two of them are connected by an edge. A simple cycle is a cycle that doesn't contain any vertex twice. I have a proof you can always solve one of these problems, but it's too long to fit this margin.
Input
The first line contains two integers n and m (5 ≤ n ≤ 10^5, n-1 ≤ m ≤ 2 ⋅ 10^5) — the number of vertices and edges in the graph.
Each of the next m lines contains two space-separated integers u and v (1 ≤ u,v ≤ n) that mean there's an edge between vertices u and v. It's guaranteed that the graph is connected and doesn't contain any self-loops or multiple edges.
Output
If you choose to solve the first problem, then on the first line print "1", followed by a line containing ⌈√{n}⌉ distinct integers not exceeding n, the vertices in the desired independent set.
If you, however, choose to solve the second problem, then on the first line print "2", followed by a line containing one integer, c, representing the length of the found cycle, followed by a line containing c distinct integers integers not exceeding n, the vertices in the desired cycle, in the order they appear in the cycle.
Examples
Input
6 6
1 3
3 4
4 2
2 6
5 6
5 1
Output
1
1 6 4
Input
6 8
1 3
3 4
4 2
2 6
5 6
5 1
1 4
2 5
Output
2
4
1 5 2 4
Input
5 4
1 2
1 3
2 4
2 5
Output
1
3 4 5
Note
In the first sample:
<image>
Notice that you can solve either problem, so printing the cycle 2-4-3-1-5-6 is also acceptable.
In the second sample:
<image>
Notice that if there are multiple answers you can print any, so printing the cycle 2-5-6, for example, is acceptable.
In the third sample:
<image> | instruction | 0 | 16,953 | 13 | 33,906 |
Tags: constructive algorithms, dfs and similar, graphs, greedy
Correct Solution:
```
import sys
input = sys.stdin.readline
from math import sqrt
n,m=map(int,input().split())
k=int(-(-sqrt(n)//1))
E=[[] for i in range(n+1)]
D=[0]*(n+1)
for i in range(m):
x,y=map(int,input().split())
E[x].append(y)
E[y].append(x)
D[x]+=1
D[y]+=1
Q=[(d,i+1) for i,d in enumerate(D[1:])]
import heapq
heapq.heapify(Q)
ANS=[]
USE=[0]*(n+1)
while Q:
d,i=heapq.heappop(Q)
if D[i]!=d or USE[i]==1:
continue
else:
if d>=k-1:
break
else:
ANS.append(i)
USE[i]=1
for to in E[i]:
if USE[to]==0:
USE[to]=1
for to2 in E[to]:
if USE[to2]==0:
D[to2]-=1
heapq.heappush(Q,(D[to2],to2))
#print(ANS)
if len(ANS)>=k:
print(1)
print(*ANS[:k])
sys.exit()
for i in range(1,n+1):
if USE[i]==0:
start=i
break
Q=[start]
ANS=[]
#print(USE)
while Q:
x=Q.pop()
USE[x]=1
ANS.append(x)
for to in E[x]:
if USE[to]==1:
continue
else:
USE[to]=1
Q.append(to)
break
for i in range(len(ANS)):
if ANS[-1] in E[ANS[i]]:
break
ANS=ANS[i:]
print(2)
print(len(ANS))
print(*ANS)
``` | output | 1 | 16,953 | 13 | 33,907 |
Provide tags and a correct Python 3 solution for this coding contest problem.
It's the year 5555. You have a graph, and you want to find a long cycle and a huge independent set, just because you can. But for now, let's just stick with finding either.
Given a connected graph with n vertices, you can choose to either:
* find an independent set that has exactly ⌈√{n}⌉ vertices.
* find a simple cycle of length at least ⌈√{n}⌉.
An independent set is a set of vertices such that no two of them are connected by an edge. A simple cycle is a cycle that doesn't contain any vertex twice. I have a proof you can always solve one of these problems, but it's too long to fit this margin.
Input
The first line contains two integers n and m (5 ≤ n ≤ 10^5, n-1 ≤ m ≤ 2 ⋅ 10^5) — the number of vertices and edges in the graph.
Each of the next m lines contains two space-separated integers u and v (1 ≤ u,v ≤ n) that mean there's an edge between vertices u and v. It's guaranteed that the graph is connected and doesn't contain any self-loops or multiple edges.
Output
If you choose to solve the first problem, then on the first line print "1", followed by a line containing ⌈√{n}⌉ distinct integers not exceeding n, the vertices in the desired independent set.
If you, however, choose to solve the second problem, then on the first line print "2", followed by a line containing one integer, c, representing the length of the found cycle, followed by a line containing c distinct integers integers not exceeding n, the vertices in the desired cycle, in the order they appear in the cycle.
Examples
Input
6 6
1 3
3 4
4 2
2 6
5 6
5 1
Output
1
1 6 4
Input
6 8
1 3
3 4
4 2
2 6
5 6
5 1
1 4
2 5
Output
2
4
1 5 2 4
Input
5 4
1 2
1 3
2 4
2 5
Output
1
3 4 5
Note
In the first sample:
<image>
Notice that you can solve either problem, so printing the cycle 2-4-3-1-5-6 is also acceptable.
In the second sample:
<image>
Notice that if there are multiple answers you can print any, so printing the cycle 2-5-6, for example, is acceptable.
In the third sample:
<image> | instruction | 0 | 16,954 | 13 | 33,908 |
Tags: constructive algorithms, dfs and similar, graphs, greedy
Correct Solution:
```
import math
import sys
input = sys.stdin.readline
def li():return [int(i) for i in input().rstrip('\n').split()]
def st():return input().rstrip('\n')
def val():return int(input().rstrip('\n'))
def li2():return [i for i in input().rstrip('\n')]
def li3():return [int(i) for i in input().rstrip('\n')]
def dfs():
global req
dep = [0]* (n + 1)
par = [0] * (n + 1)
st = [5]
st2 = []
while st:
u = st.pop()
if dep[u]:
continue
st2.append(u)
dep[u] = dep[par[u]] + 1
for v in g2[u]:
if not dep[v]:
par[v] = u
st.append(v)
elif dep[u] - dep[v] + 1>= req:
cyc = []
while u != par[v]:
cyc.append(u)
u = par[u]
return (None, cyc)
from collections import defaultdict,deque
g = defaultdict(set)
g2 = defaultdict(set)
n,m = li()
for i in range(m):
a,b = li()
g[a].add(b)
g[b].add(a)
g2[a].add(b)
g2[b].add(a)
currset = set()
ma = math.ceil(n**0.5)
req = ma
for i in sorted(g,key = lambda x:len(g[x])):
if i in g:
currset.add(i)
for k in list(g[i]):
if k in g:g.pop(k)
g.pop(i)
if len(currset) == ma:break
if len(currset) >= ma:
print(1)
print(*list(currset)[:ma])
exit()
print(2)
_,cycles = dfs()
print(len(cycles))
print(*cycles)
``` | output | 1 | 16,954 | 13 | 33,909 |
Provide tags and a correct Python 3 solution for this coding contest problem.
It's the year 5555. You have a graph, and you want to find a long cycle and a huge independent set, just because you can. But for now, let's just stick with finding either.
Given a connected graph with n vertices, you can choose to either:
* find an independent set that has exactly ⌈√{n}⌉ vertices.
* find a simple cycle of length at least ⌈√{n}⌉.
An independent set is a set of vertices such that no two of them are connected by an edge. A simple cycle is a cycle that doesn't contain any vertex twice. I have a proof you can always solve one of these problems, but it's too long to fit this margin.
Input
The first line contains two integers n and m (5 ≤ n ≤ 10^5, n-1 ≤ m ≤ 2 ⋅ 10^5) — the number of vertices and edges in the graph.
Each of the next m lines contains two space-separated integers u and v (1 ≤ u,v ≤ n) that mean there's an edge between vertices u and v. It's guaranteed that the graph is connected and doesn't contain any self-loops or multiple edges.
Output
If you choose to solve the first problem, then on the first line print "1", followed by a line containing ⌈√{n}⌉ distinct integers not exceeding n, the vertices in the desired independent set.
If you, however, choose to solve the second problem, then on the first line print "2", followed by a line containing one integer, c, representing the length of the found cycle, followed by a line containing c distinct integers integers not exceeding n, the vertices in the desired cycle, in the order they appear in the cycle.
Examples
Input
6 6
1 3
3 4
4 2
2 6
5 6
5 1
Output
1
1 6 4
Input
6 8
1 3
3 4
4 2
2 6
5 6
5 1
1 4
2 5
Output
2
4
1 5 2 4
Input
5 4
1 2
1 3
2 4
2 5
Output
1
3 4 5
Note
In the first sample:
<image>
Notice that you can solve either problem, so printing the cycle 2-4-3-1-5-6 is also acceptable.
In the second sample:
<image>
Notice that if there are multiple answers you can print any, so printing the cycle 2-5-6, for example, is acceptable.
In the third sample:
<image> | instruction | 0 | 16,955 | 13 | 33,910 |
Tags: constructive algorithms, dfs and similar, graphs, greedy
Correct Solution:
```
from collections import deque
from sys import stdin
import sys
from math import ceil
sys.setrecursionlimit(3*10**5)
n,m = map(int,stdin.readline().split())
rn = ceil(n**0.5)
lis = [ [] for i in range(n) ]
for i in range(m):
u,v = map(int,stdin.readline().split())
u -= 1
v -= 1
lis[u].append(v)
lis[v].append(u)
stk = [0]
slis = [0] * n
d = [float("inf")] * n
p = [i for i in range(n)]
d[0] = 0
while stk:
v = stk[-1]
if slis[v] < len(lis[v]):
nex = lis[v][slis[v]]
slis[v] += 1
if d[nex] == float("inf"):
d[nex] = d[v] + 1
p[nex] = v
stk.append(nex)
elif d[v] - d[nex] + 1 >= rn:
back = [v+1]
now = v
while now != nex:
now = p[now]
back.append(now+1)
#print (back)
print (2)
print (len(back))
print (*back)
sys.exit()
else:
del stk[-1]
dlis = [ [] for i in range(rn-1) ]
for i in range(n):
dlis[d[i] % (rn-1)].append(i+1)
maxind = 0
for i in range(rn-1):
if len(dlis[i]) > len(dlis[maxind]):
maxind = i
print (1)
print (*dlis[maxind][:rn])
``` | output | 1 | 16,955 | 13 | 33,911 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
It's the year 5555. You have a graph, and you want to find a long cycle and a huge independent set, just because you can. But for now, let's just stick with finding either.
Given a connected graph with n vertices, you can choose to either:
* find an independent set that has exactly ⌈√{n}⌉ vertices.
* find a simple cycle of length at least ⌈√{n}⌉.
An independent set is a set of vertices such that no two of them are connected by an edge. A simple cycle is a cycle that doesn't contain any vertex twice. I have a proof you can always solve one of these problems, but it's too long to fit this margin.
Input
The first line contains two integers n and m (5 ≤ n ≤ 10^5, n-1 ≤ m ≤ 2 ⋅ 10^5) — the number of vertices and edges in the graph.
Each of the next m lines contains two space-separated integers u and v (1 ≤ u,v ≤ n) that mean there's an edge between vertices u and v. It's guaranteed that the graph is connected and doesn't contain any self-loops or multiple edges.
Output
If you choose to solve the first problem, then on the first line print "1", followed by a line containing ⌈√{n}⌉ distinct integers not exceeding n, the vertices in the desired independent set.
If you, however, choose to solve the second problem, then on the first line print "2", followed by a line containing one integer, c, representing the length of the found cycle, followed by a line containing c distinct integers integers not exceeding n, the vertices in the desired cycle, in the order they appear in the cycle.
Examples
Input
6 6
1 3
3 4
4 2
2 6
5 6
5 1
Output
1
1 6 4
Input
6 8
1 3
3 4
4 2
2 6
5 6
5 1
1 4
2 5
Output
2
4
1 5 2 4
Input
5 4
1 2
1 3
2 4
2 5
Output
1
3 4 5
Note
In the first sample:
<image>
Notice that you can solve either problem, so printing the cycle 2-4-3-1-5-6 is also acceptable.
In the second sample:
<image>
Notice that if there are multiple answers you can print any, so printing the cycle 2-5-6, for example, is acceptable.
In the third sample:
<image>
Submitted Solution:
```
#!/usr/bin/env python3
import sys
from math import *
from collections import defaultdict
from queue import deque # Queues
from heapq import heappush, heappop # Priority Queues
# parse
lines = [line.strip() for line in sys.stdin.readlines()]
n, m = list(map(int, lines[0].split()))
edges = [set() for i in range(n)]
for i in range(1, m+1):
u, v = list(map(int, lines[i].split()))
u -= 1
v -= 1
edges[u].add(v)
edges[v].add(u)
nn = int(ceil(sqrt(n)))
def find_cycle(v, forbidden):
used = set([v])
forbidden = set(forbidden)
ret = [v]
while True:
v = ret[-1]
ss = edges[v] - used - forbidden
nxt = None
for s in ss:
nxt = s
break
if nxt is None:
break
ret += [nxt]
used.add(nxt)
i = 0
while ret[i] not in edges[ret[-1]]:
i += 1
return ret[i:]
q = []
for v in range(n):
heappush(q, (len(edges[v]), v))
# find indep set
ind = set()
covered = set()
while q:
d, v = heappop(q)
if v in covered:
continue
ind.add(v)
ss = set(edges[v])
ss.add(v)
if len(ind) == nn:
# found an indep set
print(1)
print(' '.join('%s' % (i+1) for i in ind))
break
if d >= nn - 1:
# found a cycle
ys = find_cycle(v, list(covered))
print(2)
print(len(ys))
print(' '.join('%s' % (i+1) for i in ys))
break
covered |= ss
ws = set()
for u in edges[v]:
for w in edges[u]:
ws.add(w)
ws -= ss
for w in ws:
edges[w] -= ss
heappush(q, (len(edges[w]), w))
``` | instruction | 0 | 16,956 | 13 | 33,912 |
Yes | output | 1 | 16,956 | 13 | 33,913 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
It's the year 5555. You have a graph, and you want to find a long cycle and a huge independent set, just because you can. But for now, let's just stick with finding either.
Given a connected graph with n vertices, you can choose to either:
* find an independent set that has exactly ⌈√{n}⌉ vertices.
* find a simple cycle of length at least ⌈√{n}⌉.
An independent set is a set of vertices such that no two of them are connected by an edge. A simple cycle is a cycle that doesn't contain any vertex twice. I have a proof you can always solve one of these problems, but it's too long to fit this margin.
Input
The first line contains two integers n and m (5 ≤ n ≤ 10^5, n-1 ≤ m ≤ 2 ⋅ 10^5) — the number of vertices and edges in the graph.
Each of the next m lines contains two space-separated integers u and v (1 ≤ u,v ≤ n) that mean there's an edge between vertices u and v. It's guaranteed that the graph is connected and doesn't contain any self-loops or multiple edges.
Output
If you choose to solve the first problem, then on the first line print "1", followed by a line containing ⌈√{n}⌉ distinct integers not exceeding n, the vertices in the desired independent set.
If you, however, choose to solve the second problem, then on the first line print "2", followed by a line containing one integer, c, representing the length of the found cycle, followed by a line containing c distinct integers integers not exceeding n, the vertices in the desired cycle, in the order they appear in the cycle.
Examples
Input
6 6
1 3
3 4
4 2
2 6
5 6
5 1
Output
1
1 6 4
Input
6 8
1 3
3 4
4 2
2 6
5 6
5 1
1 4
2 5
Output
2
4
1 5 2 4
Input
5 4
1 2
1 3
2 4
2 5
Output
1
3 4 5
Note
In the first sample:
<image>
Notice that you can solve either problem, so printing the cycle 2-4-3-1-5-6 is also acceptable.
In the second sample:
<image>
Notice that if there are multiple answers you can print any, so printing the cycle 2-5-6, for example, is acceptable.
In the third sample:
<image>
Submitted Solution:
```
from collections import Counter
from collections import defaultdict
import math
import random
import heapq as hq
from math import sqrt
import sys
from functools import reduce
# sys.setrecursionlimit(10000000)
def input():
return sys.stdin.readline().strip()
def iinput():
return int(input())
def tinput():
return input().split()
def rinput():
return map(int, tinput())
def rlinput():
return list(rinput())
mod = int(1e9)+7
def factors(n):
return set(reduce(list.__add__,
([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))
# ----------------------------------------------------
def dfs():
parent = [0]*(n+1) # for cycles
stk = [4]
depth = [0]*(n+1)
while len(stk) > 0:
node = stk.pop()
if depth[node] != 0:
continue
depth[node] = depth[parent[node]] + 1
for nd in gdup[node]:
if depth[nd] == 0:
stk.append(nd)
parent[nd] = node
elif depth[node] - depth[nd] + 1 >= want:
cycle = [nd]
curr = node
while curr != nd:
cycle.append(curr)
curr = parent[curr]
return cycle
if __name__ == "__main__":
n, m = rinput()
gdup = defaultdict(list)
want = int(math.ceil(math.sqrt(n)))
for _ in range(m):
u, v = rinput()
gdup[u].append(v)
gdup[v].append(u)
# part 1
flag = False
independent = set()
setlen = 0
temp = sorted(gdup, key=lambda x: len(gdup[x]))
visited = [False]*(n+1)
for i in temp:
if not visited[i]:
independent.add(i)
setlen += 1
if setlen == want:
flag = True
break
visited[i] = True
for nd in gdup[i]:
visited[nd] = True
if flag:
print(1)
print(*independent)
# part 2
if not flag:
cycle = dfs()
print(2)
print(len(cycle))
print(*cycle)
``` | instruction | 0 | 16,957 | 13 | 33,914 |
Yes | output | 1 | 16,957 | 13 | 33,915 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
It's the year 5555. You have a graph, and you want to find a long cycle and a huge independent set, just because you can. But for now, let's just stick with finding either.
Given a connected graph with n vertices, you can choose to either:
* find an independent set that has exactly ⌈√{n}⌉ vertices.
* find a simple cycle of length at least ⌈√{n}⌉.
An independent set is a set of vertices such that no two of them are connected by an edge. A simple cycle is a cycle that doesn't contain any vertex twice. I have a proof you can always solve one of these problems, but it's too long to fit this margin.
Input
The first line contains two integers n and m (5 ≤ n ≤ 10^5, n-1 ≤ m ≤ 2 ⋅ 10^5) — the number of vertices and edges in the graph.
Each of the next m lines contains two space-separated integers u and v (1 ≤ u,v ≤ n) that mean there's an edge between vertices u and v. It's guaranteed that the graph is connected and doesn't contain any self-loops or multiple edges.
Output
If you choose to solve the first problem, then on the first line print "1", followed by a line containing ⌈√{n}⌉ distinct integers not exceeding n, the vertices in the desired independent set.
If you, however, choose to solve the second problem, then on the first line print "2", followed by a line containing one integer, c, representing the length of the found cycle, followed by a line containing c distinct integers integers not exceeding n, the vertices in the desired cycle, in the order they appear in the cycle.
Examples
Input
6 6
1 3
3 4
4 2
2 6
5 6
5 1
Output
1
1 6 4
Input
6 8
1 3
3 4
4 2
2 6
5 6
5 1
1 4
2 5
Output
2
4
1 5 2 4
Input
5 4
1 2
1 3
2 4
2 5
Output
1
3 4 5
Note
In the first sample:
<image>
Notice that you can solve either problem, so printing the cycle 2-4-3-1-5-6 is also acceptable.
In the second sample:
<image>
Notice that if there are multiple answers you can print any, so printing the cycle 2-5-6, for example, is acceptable.
In the third sample:
<image>
Submitted Solution:
```
import sys
input = sys.stdin.readline
from math import sqrt
n,m=map(int,input().split())
k=int(-(-sqrt(n)//1))
E=[[] for i in range(n+1)]
D=[0]*(n+1)
for i in range(m):
x,y=map(int,input().split())
E[x].append(y)
E[y].append(x)
D[x]+=1
D[y]+=1
Q=[(d,i+1) for i,d in enumerate(D[1:])]
import heapq
heapq.heapify(Q)
ANS=[]
USE=[0]*(n+1)
while Q:
d,i=heapq.heappop(Q)
if D[i]!=d or USE[i]==1:
continue
else:
if d>=k-1:
break
else:
ANS.append(i)
USE[i]=1
for to in E[i]:
if USE[to]==0:
D[to]-=1
USE[to]=1
heapq.heappush(Q,(D[to],to))
if len(ANS)>=k:
print(1)
print(*ANS[:k])
sys.exit()
for i in range(1,n+1):
if USE[i]==0:
start=i
break
from collections import deque
Q=deque([start])
DEPTH=[-1]*(n+1)
FROM=[-1]*(n+1)
DEPTH[start]=0
while Q:
x=Q.popleft()
for to in E[x]:
if DEPTH[to]==-1:
DEPTH[to]=DEPTH[x]+1
FROM[to]=x
Q.append(to)
elif to!=FROM[x] and DEPTH[to]+DEPTH[x]+1>=k:
ANS1=[]
ANS2=[]
now=to
while now!=start:
ANS1.append(now)
now=FROM[now]
now=x
while now!=start:
ANS2.append(now)
now=FROM[now]
ANS=[start]+ANS2[::-1]+ANS1
print(2)
print(len(ANS))
print(*ANS)
sys.exit()
``` | instruction | 0 | 16,958 | 13 | 33,916 |
No | output | 1 | 16,958 | 13 | 33,917 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
It's the year 5555. You have a graph, and you want to find a long cycle and a huge independent set, just because you can. But for now, let's just stick with finding either.
Given a connected graph with n vertices, you can choose to either:
* find an independent set that has exactly ⌈√{n}⌉ vertices.
* find a simple cycle of length at least ⌈√{n}⌉.
An independent set is a set of vertices such that no two of them are connected by an edge. A simple cycle is a cycle that doesn't contain any vertex twice. I have a proof you can always solve one of these problems, but it's too long to fit this margin.
Input
The first line contains two integers n and m (5 ≤ n ≤ 10^5, n-1 ≤ m ≤ 2 ⋅ 10^5) — the number of vertices and edges in the graph.
Each of the next m lines contains two space-separated integers u and v (1 ≤ u,v ≤ n) that mean there's an edge between vertices u and v. It's guaranteed that the graph is connected and doesn't contain any self-loops or multiple edges.
Output
If you choose to solve the first problem, then on the first line print "1", followed by a line containing ⌈√{n}⌉ distinct integers not exceeding n, the vertices in the desired independent set.
If you, however, choose to solve the second problem, then on the first line print "2", followed by a line containing one integer, c, representing the length of the found cycle, followed by a line containing c distinct integers integers not exceeding n, the vertices in the desired cycle, in the order they appear in the cycle.
Examples
Input
6 6
1 3
3 4
4 2
2 6
5 6
5 1
Output
1
1 6 4
Input
6 8
1 3
3 4
4 2
2 6
5 6
5 1
1 4
2 5
Output
2
4
1 5 2 4
Input
5 4
1 2
1 3
2 4
2 5
Output
1
3 4 5
Note
In the first sample:
<image>
Notice that you can solve either problem, so printing the cycle 2-4-3-1-5-6 is also acceptable.
In the second sample:
<image>
Notice that if there are multiple answers you can print any, so printing the cycle 2-5-6, for example, is acceptable.
In the third sample:
<image>
Submitted Solution:
```
import math
import sys
input = sys.stdin.readline
def li():return [int(i) for i in input().rstrip('\n').split()]
def st():return input().rstrip('\n')
def val():return int(input().rstrip('\n'))
def li2():return [i for i in input().rstrip('\n')]
def li3():return [int(i) for i in input().rstrip('\n')]
def dfs_cycle(u, p, color,mark, par):
global cyclenumber
if color[u] == 2:
return
if color[u] == 1:
cyclenumber += 1
cur = p
mark[cur] = cyclenumber
while cur != u:
cur = par[cur]
mark[cur] = cyclenumber
return
par[u] = p
color[u] = 1
for v in g2[u]:
if v == par[u]:
continue
dfs_cycle(v, u, color, mark, par)
color[u] = 2
maxcycles = []
def printCycles(edges, mark):
global maxcycles
for i in range(len(mark)):
if mark[i] != 0:
cycles[mark[i]].append(i)
for i in range(len(cycles)):
if len(cycles[i]) > len(maxcycles):
maxcycles = cycles[i]
from collections import defaultdict,deque
g = defaultdict(set)
g2 = defaultdict(set)
n,m = li()
cycles = [[] for i in range(m)]
cyclenumber = 0
for i in range(m):
a,b = li()
g[a].add(b)
g[b].add(a)
g2[a].add(b)
g2[b].add(a)
currset = set()
ma = math.ceil(n**0.5)
for i in sorted(g,key = lambda x:len(g[x])):
if i in g:
currset.add(i)
for k in list(g[i]):
if k in g:g.pop(k)
g.pop(i)
if len(currset) == ma:break
if len(currset) >= ma:
print(1)
print(*list(currset)[:ma])
exit()
print(2)
mark = [0] * (m + 1)
par = [0]*(m + 1)
color = [0]*(m + 1)
edges = m
dfs_cycle(2,1,color,mark,par)
printCycles(edges,mark)
print(len(maxcycles))
print(*maxcycles)
``` | instruction | 0 | 16,959 | 13 | 33,918 |
No | output | 1 | 16,959 | 13 | 33,919 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
It's the year 5555. You have a graph, and you want to find a long cycle and a huge independent set, just because you can. But for now, let's just stick with finding either.
Given a connected graph with n vertices, you can choose to either:
* find an independent set that has exactly ⌈√{n}⌉ vertices.
* find a simple cycle of length at least ⌈√{n}⌉.
An independent set is a set of vertices such that no two of them are connected by an edge. A simple cycle is a cycle that doesn't contain any vertex twice. I have a proof you can always solve one of these problems, but it's too long to fit this margin.
Input
The first line contains two integers n and m (5 ≤ n ≤ 10^5, n-1 ≤ m ≤ 2 ⋅ 10^5) — the number of vertices and edges in the graph.
Each of the next m lines contains two space-separated integers u and v (1 ≤ u,v ≤ n) that mean there's an edge between vertices u and v. It's guaranteed that the graph is connected and doesn't contain any self-loops or multiple edges.
Output
If you choose to solve the first problem, then on the first line print "1", followed by a line containing ⌈√{n}⌉ distinct integers not exceeding n, the vertices in the desired independent set.
If you, however, choose to solve the second problem, then on the first line print "2", followed by a line containing one integer, c, representing the length of the found cycle, followed by a line containing c distinct integers integers not exceeding n, the vertices in the desired cycle, in the order they appear in the cycle.
Examples
Input
6 6
1 3
3 4
4 2
2 6
5 6
5 1
Output
1
1 6 4
Input
6 8
1 3
3 4
4 2
2 6
5 6
5 1
1 4
2 5
Output
2
4
1 5 2 4
Input
5 4
1 2
1 3
2 4
2 5
Output
1
3 4 5
Note
In the first sample:
<image>
Notice that you can solve either problem, so printing the cycle 2-4-3-1-5-6 is also acceptable.
In the second sample:
<image>
Notice that if there are multiple answers you can print any, so printing the cycle 2-5-6, for example, is acceptable.
In the third sample:
<image>
Submitted Solution:
```
import math
import sys
input = sys.stdin.readline
def li():return [int(i) for i in input().rstrip('\n').split()]
def st():return input().rstrip('\n')
def val():return int(input().rstrip('\n'))
def li2():return [i for i in input().rstrip('\n')]
def li3():return [int(i) for i in input().rstrip('\n')]
def dfs(start):
q = deque()
q.append([start,1,[start],set([start])])
visited = {}
while q:
node,steps,currlist,currset = q.popleft()
if node == start and steps >= ma:return currlist[:-1]
visited[node] = steps
for k in g2[node]:
if k in currset:
temp = currlist.index(k)
if len(currlist) - temp >= ma:
return currlist[temp:]
else:
q.append([k,steps + 1,currlist + [k],currset|set([k])])
from collections import *
g = defaultdict(set)
g2 = defaultdict(set)
n,m = li()
for i in range(m):
a,b = li()
g[a].add(b)
g[b].add(a)
g2[a].add(b)
g2[b].add(a)
currset = set()
ma = math.ceil(n**0.5)
for i in sorted(g,key = lambda x:len(g[x])):
if i in g:
currset.add(i)
for k in list(g[i]):
if k in g:g.pop(k)
g.pop(i)
if len(currset) >= ma:
print(1)
print(*list(currset))
exit()
print(2)
anslist = dfs(1)
print(len(anslist))
print(*anslist)
``` | instruction | 0 | 16,960 | 13 | 33,920 |
No | output | 1 | 16,960 | 13 | 33,921 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
It's the year 5555. You have a graph, and you want to find a long cycle and a huge independent set, just because you can. But for now, let's just stick with finding either.
Given a connected graph with n vertices, you can choose to either:
* find an independent set that has exactly ⌈√{n}⌉ vertices.
* find a simple cycle of length at least ⌈√{n}⌉.
An independent set is a set of vertices such that no two of them are connected by an edge. A simple cycle is a cycle that doesn't contain any vertex twice. I have a proof you can always solve one of these problems, but it's too long to fit this margin.
Input
The first line contains two integers n and m (5 ≤ n ≤ 10^5, n-1 ≤ m ≤ 2 ⋅ 10^5) — the number of vertices and edges in the graph.
Each of the next m lines contains two space-separated integers u and v (1 ≤ u,v ≤ n) that mean there's an edge between vertices u and v. It's guaranteed that the graph is connected and doesn't contain any self-loops or multiple edges.
Output
If you choose to solve the first problem, then on the first line print "1", followed by a line containing ⌈√{n}⌉ distinct integers not exceeding n, the vertices in the desired independent set.
If you, however, choose to solve the second problem, then on the first line print "2", followed by a line containing one integer, c, representing the length of the found cycle, followed by a line containing c distinct integers integers not exceeding n, the vertices in the desired cycle, in the order they appear in the cycle.
Examples
Input
6 6
1 3
3 4
4 2
2 6
5 6
5 1
Output
1
1 6 4
Input
6 8
1 3
3 4
4 2
2 6
5 6
5 1
1 4
2 5
Output
2
4
1 5 2 4
Input
5 4
1 2
1 3
2 4
2 5
Output
1
3 4 5
Note
In the first sample:
<image>
Notice that you can solve either problem, so printing the cycle 2-4-3-1-5-6 is also acceptable.
In the second sample:
<image>
Notice that if there are multiple answers you can print any, so printing the cycle 2-5-6, for example, is acceptable.
In the third sample:
<image>
Submitted Solution:
```
import sys
input = sys.stdin.readline
from math import sqrt
n,m=map(int,input().split())
k=int(-(-sqrt(n)//1))
E=[[] for i in range(n+1)]
D=[0]*(n+1)
for i in range(m):
x,y=map(int,input().split())
E[x].append(y)
E[y].append(x)
D[x]+=1
D[y]+=1
Q=[(d,i+1) for i,d in enumerate(D[1:])]
import heapq
heapq.heapify(Q)
ANS=[]
USE=[0]*(n+1)
while Q:
d,i=heapq.heappop(Q)
if D[i]!=d or USE[i]==1:
continue
else:
if d>=k-1:
break
else:
ANS.append(i)
USE[i]=1
for to in E[i]:
if USE[to]==0:
USE[to]=1
for to2 in E[to]:
D[to2]-=1
heapq.heappush(Q,(D[to2],to2))
if len(ANS)>=k:
print(1)
print(*ANS[:k])
sys.exit()
for i in range(1,n+1):
if USE[i]==0:
start=i
break
from collections import deque
Q=deque([start])
DEPTH=[-1]*(n+1)
FROM=[-1]*(n+1)
DEPTH[start]=0
while Q:
x=Q.popleft()
for to in E[x]:
if DEPTH[to]==-1:
DEPTH[to]=DEPTH[x]+1
FROM[to]=x
Q.append(to)
elif to!=FROM[x] and DEPTH[to]+DEPTH[x]+1>=k:
ANS1=[]
ANS2=[]
now=to
while now!=start:
ANS1.append(now)
now=FROM[now]
now=x
while now!=start:
ANS2.append(now)
now=FROM[now]
ANS=[start]+ANS2[::-1]+ANS1
print(2)
print(len(ANS))
print(*ANS)
sys.exit()
``` | instruction | 0 | 16,961 | 13 | 33,922 |
No | output | 1 | 16,961 | 13 | 33,923 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given m sets of integers A_1, A_2, …, A_m; elements of these sets are integers between 1 and n, inclusive.
There are two arrays of positive integers a_1, a_2, …, a_m and b_1, b_2, …, b_n.
In one operation you can delete an element j from the set A_i and pay a_i + b_j coins for that.
You can make several (maybe none) operations (some sets can become empty).
After that, you will make an edge-colored undirected graph consisting of n vertices. For each set A_i you will add an edge (x, y) with color i for all x, y ∈ A_i and x < y. Some pairs of vertices can be connected with more than one edge, but such edges have different colors.
You call a cycle i_1 → e_1 → i_2 → e_2 → … → i_k → e_k → i_1 (e_j is some edge connecting vertices i_j and i_{j+1} in this graph) rainbow if all edges on it have different colors.
Find the minimum number of coins you should pay to get a graph without rainbow cycles.
Input
The first line contains two integers m and n (1 ≤ m, n ≤ 10^5), the number of sets and the number of vertices in the graph.
The second line contains m integers a_1, a_2, …, a_m (1 ≤ a_i ≤ 10^9).
The third line contains n integers b_1, b_2, …, b_n (1 ≤ b_i ≤ 10^9).
In the each of the next of m lines there are descriptions of sets. In the i-th line the first integer s_i (1 ≤ s_i ≤ n) is equal to the size of A_i. Then s_i integers follow: the elements of the set A_i. These integers are from 1 to n and distinct.
It is guaranteed that the sum of s_i for all 1 ≤ i ≤ m does not exceed 2 ⋅ 10^5.
Output
Print one integer: the minimum number of coins you should pay for operations to avoid rainbow cycles in the obtained graph.
Examples
Input
3 2
1 2 3
4 5
2 1 2
2 1 2
2 1 2
Output
11
Input
7 8
3 6 7 9 10 7 239
8 1 9 7 10 2 6 239
3 2 1 3
2 4 1
3 1 3 7
2 4 3
5 3 4 5 6 7
2 5 7
1 8
Output
66
Note
In the first test, you can make such operations:
* Delete element 1 from set 1. You should pay a_1 + b_1 = 5 coins for that.
* Delete element 1 from set 2. You should pay a_2 + b_1 = 6 coins for that.
You pay 11 coins in total. After these operations, the first and the second sets will be equal to \{2\} and the third set will be equal to \{1, 2\}.
So, the graph will consist of one edge (1, 2) of color 3.
In the second test, you can make such operations:
* Delete element 1 from set 1. You should pay a_1 + b_1 = 11 coins for that.
* Delete element 4 from set 2. You should pay a_2 + b_4 = 13 coins for that.
* Delete element 7 from set 3. You should pay a_3 + b_7 = 13 coins for that.
* Delete element 4 from set 4. You should pay a_4 + b_4 = 16 coins for that.
* Delete element 7 from set 6. You should pay a_6 + b_7 = 13 coins for that.
You pay 66 coins in total.
After these operations, the sets will be:
* \{2, 3\};
* \{1\};
* \{1, 3\};
* \{3\};
* \{3, 4, 5, 6, 7\};
* \{5\};
* \{8\}.
We will get the graph:
<image>
There are no rainbow cycles in it. | instruction | 0 | 16,979 | 13 | 33,958 |
Tags: data structures, dsu, graphs, greedy, sortings, trees
Correct Solution:
```
import sys
#from bisect import *
#from collections import deque
pl=1
#from math import *
#from copy import *
if pl:
input=sys.stdin.readline
else:
sys.stdin=open('input.txt', 'r')
sys.stdout=open('outpt.txt','w')
def li():
return [int(xxx) for xxx in input().split()]
def fi():
return int(input())
def si():
return list(input().rstrip())
def mi():
return map(int,input().split())
t=1
def find(i):
if a[i]==i:
return i
a[i]=find(a[i])
return a[i]
def union(x,y):
xs=find(x)
ys=find(y)
if xs==ys:
return False
if rank[xs]<rank[ys]:
xs,ys=ys,xs
rank[xs]+=1
a[ys]=xs
return True
def prim():
ans=0
edge.sort(key=lambda x:-x[0])
for w,i,j in edge:
if not union(i,j):
ans+=w
return ans
while t>0:
t-=1
m,n=mi()
c=li()
d=li()
edge=[]
for i in range(m):
p=li()
for j in range(1,p[0]+1):
edge.append([c[i]+d[p[j]-1],i+1,p[j]+m])
a=[i for i in range(n+m+5)]
rank=[0 for i in range(n+m+5)]
print(prim())
``` | output | 1 | 16,979 | 13 | 33,959 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given m sets of integers A_1, A_2, …, A_m; elements of these sets are integers between 1 and n, inclusive.
There are two arrays of positive integers a_1, a_2, …, a_m and b_1, b_2, …, b_n.
In one operation you can delete an element j from the set A_i and pay a_i + b_j coins for that.
You can make several (maybe none) operations (some sets can become empty).
After that, you will make an edge-colored undirected graph consisting of n vertices. For each set A_i you will add an edge (x, y) with color i for all x, y ∈ A_i and x < y. Some pairs of vertices can be connected with more than one edge, but such edges have different colors.
You call a cycle i_1 → e_1 → i_2 → e_2 → … → i_k → e_k → i_1 (e_j is some edge connecting vertices i_j and i_{j+1} in this graph) rainbow if all edges on it have different colors.
Find the minimum number of coins you should pay to get a graph without rainbow cycles.
Input
The first line contains two integers m and n (1 ≤ m, n ≤ 10^5), the number of sets and the number of vertices in the graph.
The second line contains m integers a_1, a_2, …, a_m (1 ≤ a_i ≤ 10^9).
The third line contains n integers b_1, b_2, …, b_n (1 ≤ b_i ≤ 10^9).
In the each of the next of m lines there are descriptions of sets. In the i-th line the first integer s_i (1 ≤ s_i ≤ n) is equal to the size of A_i. Then s_i integers follow: the elements of the set A_i. These integers are from 1 to n and distinct.
It is guaranteed that the sum of s_i for all 1 ≤ i ≤ m does not exceed 2 ⋅ 10^5.
Output
Print one integer: the minimum number of coins you should pay for operations to avoid rainbow cycles in the obtained graph.
Examples
Input
3 2
1 2 3
4 5
2 1 2
2 1 2
2 1 2
Output
11
Input
7 8
3 6 7 9 10 7 239
8 1 9 7 10 2 6 239
3 2 1 3
2 4 1
3 1 3 7
2 4 3
5 3 4 5 6 7
2 5 7
1 8
Output
66
Note
In the first test, you can make such operations:
* Delete element 1 from set 1. You should pay a_1 + b_1 = 5 coins for that.
* Delete element 1 from set 2. You should pay a_2 + b_1 = 6 coins for that.
You pay 11 coins in total. After these operations, the first and the second sets will be equal to \{2\} and the third set will be equal to \{1, 2\}.
So, the graph will consist of one edge (1, 2) of color 3.
In the second test, you can make such operations:
* Delete element 1 from set 1. You should pay a_1 + b_1 = 11 coins for that.
* Delete element 4 from set 2. You should pay a_2 + b_4 = 13 coins for that.
* Delete element 7 from set 3. You should pay a_3 + b_7 = 13 coins for that.
* Delete element 4 from set 4. You should pay a_4 + b_4 = 16 coins for that.
* Delete element 7 from set 6. You should pay a_6 + b_7 = 13 coins for that.
You pay 66 coins in total.
After these operations, the sets will be:
* \{2, 3\};
* \{1\};
* \{1, 3\};
* \{3\};
* \{3, 4, 5, 6, 7\};
* \{5\};
* \{8\}.
We will get the graph:
<image>
There are no rainbow cycles in it. | instruction | 0 | 16,980 | 13 | 33,960 |
Tags: data structures, dsu, graphs, greedy, sortings, trees
Correct Solution:
```
import sys;input=sys.stdin.readline
def root(x):
if x == p[x]:
return x
p[x] = y = root(p[x])
return y
def unite(x, y):
px = root(x); py = root(y)
if px == py:
return 0
rx = rank[px]; ry = rank[py]
if ry < rx:
p[py] = px
elif rx < ry:
p[px] = py
else:
p[py] = px
rank[px] += 1
return
if __name__ == "__main__":
M, N = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
E = []
R = 0
for i in range(M):
X = list(map(int, input().split()))
for j in X[1:]:
E.append((N+i, j-1, -A[i]-B[j-1]))
R += A[i]+B[j-1]
E.sort(key=lambda x:x[2])
*p, = range(N+M)
rank = [1]*(N+M)
for v, u, c in E:
if root(v) != root(u):
unite(v, u)
R += c
print(R)
``` | output | 1 | 16,980 | 13 | 33,961 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given m sets of integers A_1, A_2, …, A_m; elements of these sets are integers between 1 and n, inclusive.
There are two arrays of positive integers a_1, a_2, …, a_m and b_1, b_2, …, b_n.
In one operation you can delete an element j from the set A_i and pay a_i + b_j coins for that.
You can make several (maybe none) operations (some sets can become empty).
After that, you will make an edge-colored undirected graph consisting of n vertices. For each set A_i you will add an edge (x, y) with color i for all x, y ∈ A_i and x < y. Some pairs of vertices can be connected with more than one edge, but such edges have different colors.
You call a cycle i_1 → e_1 → i_2 → e_2 → … → i_k → e_k → i_1 (e_j is some edge connecting vertices i_j and i_{j+1} in this graph) rainbow if all edges on it have different colors.
Find the minimum number of coins you should pay to get a graph without rainbow cycles.
Input
The first line contains two integers m and n (1 ≤ m, n ≤ 10^5), the number of sets and the number of vertices in the graph.
The second line contains m integers a_1, a_2, …, a_m (1 ≤ a_i ≤ 10^9).
The third line contains n integers b_1, b_2, …, b_n (1 ≤ b_i ≤ 10^9).
In the each of the next of m lines there are descriptions of sets. In the i-th line the first integer s_i (1 ≤ s_i ≤ n) is equal to the size of A_i. Then s_i integers follow: the elements of the set A_i. These integers are from 1 to n and distinct.
It is guaranteed that the sum of s_i for all 1 ≤ i ≤ m does not exceed 2 ⋅ 10^5.
Output
Print one integer: the minimum number of coins you should pay for operations to avoid rainbow cycles in the obtained graph.
Examples
Input
3 2
1 2 3
4 5
2 1 2
2 1 2
2 1 2
Output
11
Input
7 8
3 6 7 9 10 7 239
8 1 9 7 10 2 6 239
3 2 1 3
2 4 1
3 1 3 7
2 4 3
5 3 4 5 6 7
2 5 7
1 8
Output
66
Note
In the first test, you can make such operations:
* Delete element 1 from set 1. You should pay a_1 + b_1 = 5 coins for that.
* Delete element 1 from set 2. You should pay a_2 + b_1 = 6 coins for that.
You pay 11 coins in total. After these operations, the first and the second sets will be equal to \{2\} and the third set will be equal to \{1, 2\}.
So, the graph will consist of one edge (1, 2) of color 3.
In the second test, you can make such operations:
* Delete element 1 from set 1. You should pay a_1 + b_1 = 11 coins for that.
* Delete element 4 from set 2. You should pay a_2 + b_4 = 13 coins for that.
* Delete element 7 from set 3. You should pay a_3 + b_7 = 13 coins for that.
* Delete element 4 from set 4. You should pay a_4 + b_4 = 16 coins for that.
* Delete element 7 from set 6. You should pay a_6 + b_7 = 13 coins for that.
You pay 66 coins in total.
After these operations, the sets will be:
* \{2, 3\};
* \{1\};
* \{1, 3\};
* \{3\};
* \{3, 4, 5, 6, 7\};
* \{5\};
* \{8\}.
We will get the graph:
<image>
There are no rainbow cycles in it. | instruction | 0 | 16,981 | 13 | 33,962 |
Tags: data structures, dsu, graphs, greedy, sortings, trees
Correct Solution:
```
import sys; input = sys.stdin.buffer.readline
# sys.setrecursionlimit(10**7)
def getlist():
return list(map(int, input().split()))
class UnionFind:
def __init__(self, N):
self.par = [i for i in range(N)]
self.rank = [0] * N
def find(self, x):
if self.par[x] == x:
return x
else:
self.par[x] = self.find(self.par[x])
return self.par[x]
def same_check(self, x, y):
return self.find(x) == self.find(y)
def union(self, x, y):
x = self.find(x); y = self.find(y)
if self.rank[x] < self.rank[y]:
self.par[x] = y
else:
self.par[y] = x
if self.rank[x] == self.rank[y]:
self.rank[x] += 1
class Kruskal:
def __init__(self, E, N):
self.uf = UnionFind(N)
self.cost = 0
for i in range(len(E)):
w, u, v = E[i]
if self.uf.same_check(u, v) != True:
self.uf.union(u, v)
self.cost += w
def maxcost(self):
return self.cost
def main():
M, N = getlist()
A = getlist()
B = getlist()
E = []
MAX = 0
for i in range(M):
num = getlist()
s = num[0]
for j in range(1, s + 1):
bj = num[j] - 1
w = A[i] + B[bj]
E.append((w, i, M + bj))
MAX += w
E.sort(key=lambda x: x[0], reverse=True)
K = Kruskal(E, N + M)
res = K.maxcost()
# print(res)
# print(E)
ans = MAX - res
print(ans)
if __name__ == '__main__':
main()
``` | output | 1 | 16,981 | 13 | 33,963 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.