message
stringlengths
2
49.9k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
446
108k
cluster
float64
13
13
__index_level_0__
int64
892
217k
Provide a correct Python 3 solution for this coding contest problem. There are two standard ways to represent a graph $G = (V, E)$, where $V$ is a set of vertices and $E$ is a set of edges; Adjacency list representation and Adjacency matrix representation. An adjacency-list representation consists of an array $Adj[|V|]$ of $|V|$ lists, one for each vertex in $V$. For each $u \in V$, the adjacency list $Adj[u]$ contains all vertices $v$ such that there is an edge $(u, v) \in E$. That is, $Adj[u]$ consists of all vertices adjacent to $u$ in $G$. An adjacency-matrix representation consists of $|V| \times |V|$ matrix $A = a_{ij}$ such that $a_{ij} = 1$ if $(i, j) \in E$, $a_{ij} = 0$ otherwise. Write a program which reads a directed graph $G$ represented by the adjacency list, and prints its adjacency-matrix representation. $G$ consists of $n\; (=|V|)$ vertices identified by their IDs $1, 2,.., n$ respectively. Constraints * $1 \leq n \leq 100$ Input In the first line, an integer $n$ is given. In the next $n$ lines, an adjacency list $Adj[u]$ for vertex $u$ are given in the following format: $u$ $k$ $v_1$ $v_2$ ... $v_k$ $u$ is vertex ID and $k$ denotes its degree. $v_i$ are IDs of vertices adjacent to $u$. Output As shown in the following sample output, print the adjacent-matrix representation of $G$. Put a single space character between $a_{ij}$. Example Input 4 1 2 2 4 2 1 4 3 0 4 1 3 Output 0 1 0 1 0 0 0 1 0 0 0 0 0 0 1 0
instruction
0
106,994
13
213,988
"Correct Solution: ``` n = int(input()) M = [[0 for i in range(n)] for j in range(n)] for i in range(n): _ = list(map(int, input().split())) u = _[0] - 1 # 0オリジンに変換 degree = _[1] for j in range(degree): v = _[j+2] - 1 # 0オリジンに変換 M[u][v] = 1 for row in M: print(' '.join(map(str, row))) ```
output
1
106,994
13
213,989
Provide a correct Python 3 solution for this coding contest problem. There are two standard ways to represent a graph $G = (V, E)$, where $V$ is a set of vertices and $E$ is a set of edges; Adjacency list representation and Adjacency matrix representation. An adjacency-list representation consists of an array $Adj[|V|]$ of $|V|$ lists, one for each vertex in $V$. For each $u \in V$, the adjacency list $Adj[u]$ contains all vertices $v$ such that there is an edge $(u, v) \in E$. That is, $Adj[u]$ consists of all vertices adjacent to $u$ in $G$. An adjacency-matrix representation consists of $|V| \times |V|$ matrix $A = a_{ij}$ such that $a_{ij} = 1$ if $(i, j) \in E$, $a_{ij} = 0$ otherwise. Write a program which reads a directed graph $G$ represented by the adjacency list, and prints its adjacency-matrix representation. $G$ consists of $n\; (=|V|)$ vertices identified by their IDs $1, 2,.., n$ respectively. Constraints * $1 \leq n \leq 100$ Input In the first line, an integer $n$ is given. In the next $n$ lines, an adjacency list $Adj[u]$ for vertex $u$ are given in the following format: $u$ $k$ $v_1$ $v_2$ ... $v_k$ $u$ is vertex ID and $k$ denotes its degree. $v_i$ are IDs of vertices adjacent to $u$. Output As shown in the following sample output, print the adjacent-matrix representation of $G$. Put a single space character between $a_{ij}$. Example Input 4 1 2 2 4 2 1 4 3 0 4 1 3 Output 0 1 0 1 0 0 0 1 0 0 0 0 0 0 1 0
instruction
0
106,995
13
213,990
"Correct Solution: ``` n = int(input()) dic = {} for _ in range(n): datum = list(map(int, input().split())) status = ["0"]*n for i in datum[2:]: status[i-1] = "1" print(" ".join(status)) ```
output
1
106,995
13
213,991
Provide a correct Python 3 solution for this coding contest problem. There are two standard ways to represent a graph $G = (V, E)$, where $V$ is a set of vertices and $E$ is a set of edges; Adjacency list representation and Adjacency matrix representation. An adjacency-list representation consists of an array $Adj[|V|]$ of $|V|$ lists, one for each vertex in $V$. For each $u \in V$, the adjacency list $Adj[u]$ contains all vertices $v$ such that there is an edge $(u, v) \in E$. That is, $Adj[u]$ consists of all vertices adjacent to $u$ in $G$. An adjacency-matrix representation consists of $|V| \times |V|$ matrix $A = a_{ij}$ such that $a_{ij} = 1$ if $(i, j) \in E$, $a_{ij} = 0$ otherwise. Write a program which reads a directed graph $G$ represented by the adjacency list, and prints its adjacency-matrix representation. $G$ consists of $n\; (=|V|)$ vertices identified by their IDs $1, 2,.., n$ respectively. Constraints * $1 \leq n \leq 100$ Input In the first line, an integer $n$ is given. In the next $n$ lines, an adjacency list $Adj[u]$ for vertex $u$ are given in the following format: $u$ $k$ $v_1$ $v_2$ ... $v_k$ $u$ is vertex ID and $k$ denotes its degree. $v_i$ are IDs of vertices adjacent to $u$. Output As shown in the following sample output, print the adjacent-matrix representation of $G$. Put a single space character between $a_{ij}$. Example Input 4 1 2 2 4 2 1 4 3 0 4 1 3 Output 0 1 0 1 0 0 0 1 0 0 0 0 0 0 1 0
instruction
0
106,996
13
213,992
"Correct Solution: ``` def main(): n = int(input()) rows = [] for i in range(n): rows.append([int(x) for x in input().split()]) for row in rows: u,k = row[0],row[1] ul = [0]*n for j in range(k): ul[row[j+2]-1] = 1 print(' '.join([str(u) for u in ul])) if __name__ == '__main__': main() ```
output
1
106,996
13
213,993
Provide a correct Python 3 solution for this coding contest problem. There are two standard ways to represent a graph $G = (V, E)$, where $V$ is a set of vertices and $E$ is a set of edges; Adjacency list representation and Adjacency matrix representation. An adjacency-list representation consists of an array $Adj[|V|]$ of $|V|$ lists, one for each vertex in $V$. For each $u \in V$, the adjacency list $Adj[u]$ contains all vertices $v$ such that there is an edge $(u, v) \in E$. That is, $Adj[u]$ consists of all vertices adjacent to $u$ in $G$. An adjacency-matrix representation consists of $|V| \times |V|$ matrix $A = a_{ij}$ such that $a_{ij} = 1$ if $(i, j) \in E$, $a_{ij} = 0$ otherwise. Write a program which reads a directed graph $G$ represented by the adjacency list, and prints its adjacency-matrix representation. $G$ consists of $n\; (=|V|)$ vertices identified by their IDs $1, 2,.., n$ respectively. Constraints * $1 \leq n \leq 100$ Input In the first line, an integer $n$ is given. In the next $n$ lines, an adjacency list $Adj[u]$ for vertex $u$ are given in the following format: $u$ $k$ $v_1$ $v_2$ ... $v_k$ $u$ is vertex ID and $k$ denotes its degree. $v_i$ are IDs of vertices adjacent to $u$. Output As shown in the following sample output, print the adjacent-matrix representation of $G$. Put a single space character between $a_{ij}$. Example Input 4 1 2 2 4 2 1 4 3 0 4 1 3 Output 0 1 0 1 0 0 0 1 0 0 0 0 0 0 1 0
instruction
0
106,997
13
213,994
"Correct Solution: ``` # -*- coding: utf-8 -*- N = int(input()) adj = [["0"]*N for _ in range(N)] for _ in range(N): inp = list(map(int, input().split())) for i in inp[2:]: adj[inp[0]-1][i-1] = "1" for a in adj: print(" ".join(a)) ```
output
1
106,997
13
213,995
Provide a correct Python 3 solution for this coding contest problem. There are two standard ways to represent a graph $G = (V, E)$, where $V$ is a set of vertices and $E$ is a set of edges; Adjacency list representation and Adjacency matrix representation. An adjacency-list representation consists of an array $Adj[|V|]$ of $|V|$ lists, one for each vertex in $V$. For each $u \in V$, the adjacency list $Adj[u]$ contains all vertices $v$ such that there is an edge $(u, v) \in E$. That is, $Adj[u]$ consists of all vertices adjacent to $u$ in $G$. An adjacency-matrix representation consists of $|V| \times |V|$ matrix $A = a_{ij}$ such that $a_{ij} = 1$ if $(i, j) \in E$, $a_{ij} = 0$ otherwise. Write a program which reads a directed graph $G$ represented by the adjacency list, and prints its adjacency-matrix representation. $G$ consists of $n\; (=|V|)$ vertices identified by their IDs $1, 2,.., n$ respectively. Constraints * $1 \leq n \leq 100$ Input In the first line, an integer $n$ is given. In the next $n$ lines, an adjacency list $Adj[u]$ for vertex $u$ are given in the following format: $u$ $k$ $v_1$ $v_2$ ... $v_k$ $u$ is vertex ID and $k$ denotes its degree. $v_i$ are IDs of vertices adjacent to $u$. Output As shown in the following sample output, print the adjacent-matrix representation of $G$. Put a single space character between $a_{ij}$. Example Input 4 1 2 2 4 2 1 4 3 0 4 1 3 Output 0 1 0 1 0 0 0 1 0 0 0 0 0 0 1 0
instruction
0
106,998
13
213,996
"Correct Solution: ``` n=int(input()) da=[[0 for i in range(n)] for i in range(n)] for i in range(n): se=list(map(int,input().split())) for j in se[2:]: da[i][j-1]=1 for i in range(n): for j in range(n): if j==n-1: print(da[i][j]) else: print(da[i][j],end=' ') ```
output
1
106,998
13
213,997
Provide a correct Python 3 solution for this coding contest problem. There are two standard ways to represent a graph $G = (V, E)$, where $V$ is a set of vertices and $E$ is a set of edges; Adjacency list representation and Adjacency matrix representation. An adjacency-list representation consists of an array $Adj[|V|]$ of $|V|$ lists, one for each vertex in $V$. For each $u \in V$, the adjacency list $Adj[u]$ contains all vertices $v$ such that there is an edge $(u, v) \in E$. That is, $Adj[u]$ consists of all vertices adjacent to $u$ in $G$. An adjacency-matrix representation consists of $|V| \times |V|$ matrix $A = a_{ij}$ such that $a_{ij} = 1$ if $(i, j) \in E$, $a_{ij} = 0$ otherwise. Write a program which reads a directed graph $G$ represented by the adjacency list, and prints its adjacency-matrix representation. $G$ consists of $n\; (=|V|)$ vertices identified by their IDs $1, 2,.., n$ respectively. Constraints * $1 \leq n \leq 100$ Input In the first line, an integer $n$ is given. In the next $n$ lines, an adjacency list $Adj[u]$ for vertex $u$ are given in the following format: $u$ $k$ $v_1$ $v_2$ ... $v_k$ $u$ is vertex ID and $k$ denotes its degree. $v_i$ are IDs of vertices adjacent to $u$. Output As shown in the following sample output, print the adjacent-matrix representation of $G$. Put a single space character between $a_{ij}$. Example Input 4 1 2 2 4 2 1 4 3 0 4 1 3 Output 0 1 0 1 0 0 0 1 0 0 0 0 0 0 1 0
instruction
0
106,999
13
213,998
"Correct Solution: ``` n = int(input()) mat = [[0]*n for _ in range(n)] for _ in range(n): In = list(map(int, input().split())) u, k, vs = In[0], In[1], In[2:] for v in vs: mat[u - 1][v - 1] = 1 for i in range(n): print(*mat[i]) ```
output
1
106,999
13
213,999
Provide a correct Python 3 solution for this coding contest problem. There are two standard ways to represent a graph $G = (V, E)$, where $V$ is a set of vertices and $E$ is a set of edges; Adjacency list representation and Adjacency matrix representation. An adjacency-list representation consists of an array $Adj[|V|]$ of $|V|$ lists, one for each vertex in $V$. For each $u \in V$, the adjacency list $Adj[u]$ contains all vertices $v$ such that there is an edge $(u, v) \in E$. That is, $Adj[u]$ consists of all vertices adjacent to $u$ in $G$. An adjacency-matrix representation consists of $|V| \times |V|$ matrix $A = a_{ij}$ such that $a_{ij} = 1$ if $(i, j) \in E$, $a_{ij} = 0$ otherwise. Write a program which reads a directed graph $G$ represented by the adjacency list, and prints its adjacency-matrix representation. $G$ consists of $n\; (=|V|)$ vertices identified by their IDs $1, 2,.., n$ respectively. Constraints * $1 \leq n \leq 100$ Input In the first line, an integer $n$ is given. In the next $n$ lines, an adjacency list $Adj[u]$ for vertex $u$ are given in the following format: $u$ $k$ $v_1$ $v_2$ ... $v_k$ $u$ is vertex ID and $k$ denotes its degree. $v_i$ are IDs of vertices adjacent to $u$. Output As shown in the following sample output, print the adjacent-matrix representation of $G$. Put a single space character between $a_{ij}$. Example Input 4 1 2 2 4 2 1 4 3 0 4 1 3 Output 0 1 0 1 0 0 0 1 0 0 0 0 0 0 1 0
instruction
0
107,000
13
214,000
"Correct Solution: ``` n = int(input()) m = [[0 for _ in range(n)] for _ in range(n)] for i in range(n): u, k, *v = map(int, input().split()) for j in v: m[u-1][j-1] = 1 for i in range(n): print(" ".join(map(str,m[i]))) ```
output
1
107,000
13
214,001
Provide a correct Python 3 solution for this coding contest problem. There are two standard ways to represent a graph $G = (V, E)$, where $V$ is a set of vertices and $E$ is a set of edges; Adjacency list representation and Adjacency matrix representation. An adjacency-list representation consists of an array $Adj[|V|]$ of $|V|$ lists, one for each vertex in $V$. For each $u \in V$, the adjacency list $Adj[u]$ contains all vertices $v$ such that there is an edge $(u, v) \in E$. That is, $Adj[u]$ consists of all vertices adjacent to $u$ in $G$. An adjacency-matrix representation consists of $|V| \times |V|$ matrix $A = a_{ij}$ such that $a_{ij} = 1$ if $(i, j) \in E$, $a_{ij} = 0$ otherwise. Write a program which reads a directed graph $G$ represented by the adjacency list, and prints its adjacency-matrix representation. $G$ consists of $n\; (=|V|)$ vertices identified by their IDs $1, 2,.., n$ respectively. Constraints * $1 \leq n \leq 100$ Input In the first line, an integer $n$ is given. In the next $n$ lines, an adjacency list $Adj[u]$ for vertex $u$ are given in the following format: $u$ $k$ $v_1$ $v_2$ ... $v_k$ $u$ is vertex ID and $k$ denotes its degree. $v_i$ are IDs of vertices adjacent to $u$. Output As shown in the following sample output, print the adjacent-matrix representation of $G$. Put a single space character between $a_{ij}$. Example Input 4 1 2 2 4 2 1 4 3 0 4 1 3 Output 0 1 0 1 0 0 0 1 0 0 0 0 0 0 1 0
instruction
0
107,001
13
214,002
"Correct Solution: ``` def adjmatrices(): n = int(input()) for i in range(n): node_list = list(map(int, input().split())) matrix = [0]*n for j in node_list[2:]: matrix[j-1] = 1 print(*matrix) if __name__ == '__main__': adjmatrices() ```
output
1
107,001
13
214,003
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are two standard ways to represent a graph $G = (V, E)$, where $V$ is a set of vertices and $E$ is a set of edges; Adjacency list representation and Adjacency matrix representation. An adjacency-list representation consists of an array $Adj[|V|]$ of $|V|$ lists, one for each vertex in $V$. For each $u \in V$, the adjacency list $Adj[u]$ contains all vertices $v$ such that there is an edge $(u, v) \in E$. That is, $Adj[u]$ consists of all vertices adjacent to $u$ in $G$. An adjacency-matrix representation consists of $|V| \times |V|$ matrix $A = a_{ij}$ such that $a_{ij} = 1$ if $(i, j) \in E$, $a_{ij} = 0$ otherwise. Write a program which reads a directed graph $G$ represented by the adjacency list, and prints its adjacency-matrix representation. $G$ consists of $n\; (=|V|)$ vertices identified by their IDs $1, 2,.., n$ respectively. Constraints * $1 \leq n \leq 100$ Input In the first line, an integer $n$ is given. In the next $n$ lines, an adjacency list $Adj[u]$ for vertex $u$ are given in the following format: $u$ $k$ $v_1$ $v_2$ ... $v_k$ $u$ is vertex ID and $k$ denotes its degree. $v_i$ are IDs of vertices adjacent to $u$. Output As shown in the following sample output, print the adjacent-matrix representation of $G$. Put a single space character between $a_{ij}$. Example Input 4 1 2 2 4 2 1 4 3 0 4 1 3 Output 0 1 0 1 0 0 0 1 0 0 0 0 0 0 1 0 Submitted Solution: ``` n=int(input()) v=[] u=0 e=0 A=[[0 for j in range(n)]for i in range(n)] for i in range(n): S=input().split() if S[1]!=0: for j in range(int(S[1])): A[i][int(S[2+j])-1]=1 for i in range(n): for j in range(n): if j!=n-1: print(A[i][j],end=" ") else: print(A[i][j],end="\n") ```
instruction
0
107,002
13
214,004
Yes
output
1
107,002
13
214,005
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are two standard ways to represent a graph $G = (V, E)$, where $V$ is a set of vertices and $E$ is a set of edges; Adjacency list representation and Adjacency matrix representation. An adjacency-list representation consists of an array $Adj[|V|]$ of $|V|$ lists, one for each vertex in $V$. For each $u \in V$, the adjacency list $Adj[u]$ contains all vertices $v$ such that there is an edge $(u, v) \in E$. That is, $Adj[u]$ consists of all vertices adjacent to $u$ in $G$. An adjacency-matrix representation consists of $|V| \times |V|$ matrix $A = a_{ij}$ such that $a_{ij} = 1$ if $(i, j) \in E$, $a_{ij} = 0$ otherwise. Write a program which reads a directed graph $G$ represented by the adjacency list, and prints its adjacency-matrix representation. $G$ consists of $n\; (=|V|)$ vertices identified by their IDs $1, 2,.., n$ respectively. Constraints * $1 \leq n \leq 100$ Input In the first line, an integer $n$ is given. In the next $n$ lines, an adjacency list $Adj[u]$ for vertex $u$ are given in the following format: $u$ $k$ $v_1$ $v_2$ ... $v_k$ $u$ is vertex ID and $k$ denotes its degree. $v_i$ are IDs of vertices adjacent to $u$. Output As shown in the following sample output, print the adjacent-matrix representation of $G$. Put a single space character between $a_{ij}$. Example Input 4 1 2 2 4 2 1 4 3 0 4 1 3 Output 0 1 0 1 0 0 0 1 0 0 0 0 0 0 1 0 Submitted Solution: ``` import sys def input(): return sys.stdin.readline().rstrip() def main(): n = int(input()) G = [[0] * n for _ in range(n)] for i in range(n): u, k, *v = map(int, input().split()) for j in range(k): G[u-1][v[j]-1] = 1 for i in range(n): print(*G[i]) if __name__ == '__main__': main() ```
instruction
0
107,003
13
214,006
Yes
output
1
107,003
13
214,007
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are two standard ways to represent a graph $G = (V, E)$, where $V$ is a set of vertices and $E$ is a set of edges; Adjacency list representation and Adjacency matrix representation. An adjacency-list representation consists of an array $Adj[|V|]$ of $|V|$ lists, one for each vertex in $V$. For each $u \in V$, the adjacency list $Adj[u]$ contains all vertices $v$ such that there is an edge $(u, v) \in E$. That is, $Adj[u]$ consists of all vertices adjacent to $u$ in $G$. An adjacency-matrix representation consists of $|V| \times |V|$ matrix $A = a_{ij}$ such that $a_{ij} = 1$ if $(i, j) \in E$, $a_{ij} = 0$ otherwise. Write a program which reads a directed graph $G$ represented by the adjacency list, and prints its adjacency-matrix representation. $G$ consists of $n\; (=|V|)$ vertices identified by their IDs $1, 2,.., n$ respectively. Constraints * $1 \leq n \leq 100$ Input In the first line, an integer $n$ is given. In the next $n$ lines, an adjacency list $Adj[u]$ for vertex $u$ are given in the following format: $u$ $k$ $v_1$ $v_2$ ... $v_k$ $u$ is vertex ID and $k$ denotes its degree. $v_i$ are IDs of vertices adjacent to $u$. Output As shown in the following sample output, print the adjacent-matrix representation of $G$. Put a single space character between $a_{ij}$. Example Input 4 1 2 2 4 2 1 4 3 0 4 1 3 Output 0 1 0 1 0 0 0 1 0 0 0 0 0 0 1 0 Submitted Solution: ``` n = int(input()) A = [[0] * n for _ in range(n)] for _ in range(n): u, k, *v = list(map(int, input().split())) for j in v: A[u-1][j-1] = 1 for row in A: print(' '.join(list(map(str,row)))) ```
instruction
0
107,004
13
214,008
Yes
output
1
107,004
13
214,009
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are two standard ways to represent a graph $G = (V, E)$, where $V$ is a set of vertices and $E$ is a set of edges; Adjacency list representation and Adjacency matrix representation. An adjacency-list representation consists of an array $Adj[|V|]$ of $|V|$ lists, one for each vertex in $V$. For each $u \in V$, the adjacency list $Adj[u]$ contains all vertices $v$ such that there is an edge $(u, v) \in E$. That is, $Adj[u]$ consists of all vertices adjacent to $u$ in $G$. An adjacency-matrix representation consists of $|V| \times |V|$ matrix $A = a_{ij}$ such that $a_{ij} = 1$ if $(i, j) \in E$, $a_{ij} = 0$ otherwise. Write a program which reads a directed graph $G$ represented by the adjacency list, and prints its adjacency-matrix representation. $G$ consists of $n\; (=|V|)$ vertices identified by their IDs $1, 2,.., n$ respectively. Constraints * $1 \leq n \leq 100$ Input In the first line, an integer $n$ is given. In the next $n$ lines, an adjacency list $Adj[u]$ for vertex $u$ are given in the following format: $u$ $k$ $v_1$ $v_2$ ... $v_k$ $u$ is vertex ID and $k$ denotes its degree. $v_i$ are IDs of vertices adjacent to $u$. Output As shown in the following sample output, print the adjacent-matrix representation of $G$. Put a single space character between $a_{ij}$. Example Input 4 1 2 2 4 2 1 4 3 0 4 1 3 Output 0 1 0 1 0 0 0 1 0 0 0 0 0 0 1 0 Submitted Solution: ``` n = int(input()) G = [[0]*n for i in range(n)] for i in range(n): u,k,*vs = map(int,input().split()) for j in range(len(vs)): G[u-1][vs[j]-1] = 1 for i in range(n): print(" ".join(map(str,G[i]))) ```
instruction
0
107,005
13
214,010
Yes
output
1
107,005
13
214,011
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are two standard ways to represent a graph $G = (V, E)$, where $V$ is a set of vertices and $E$ is a set of edges; Adjacency list representation and Adjacency matrix representation. An adjacency-list representation consists of an array $Adj[|V|]$ of $|V|$ lists, one for each vertex in $V$. For each $u \in V$, the adjacency list $Adj[u]$ contains all vertices $v$ such that there is an edge $(u, v) \in E$. That is, $Adj[u]$ consists of all vertices adjacent to $u$ in $G$. An adjacency-matrix representation consists of $|V| \times |V|$ matrix $A = a_{ij}$ such that $a_{ij} = 1$ if $(i, j) \in E$, $a_{ij} = 0$ otherwise. Write a program which reads a directed graph $G$ represented by the adjacency list, and prints its adjacency-matrix representation. $G$ consists of $n\; (=|V|)$ vertices identified by their IDs $1, 2,.., n$ respectively. Constraints * $1 \leq n \leq 100$ Input In the first line, an integer $n$ is given. In the next $n$ lines, an adjacency list $Adj[u]$ for vertex $u$ are given in the following format: $u$ $k$ $v_1$ $v_2$ ... $v_k$ $u$ is vertex ID and $k$ denotes its degree. $v_i$ are IDs of vertices adjacent to $u$. Output As shown in the following sample output, print the adjacent-matrix representation of $G$. Put a single space character between $a_{ij}$. Example Input 4 1 2 2 4 2 1 4 3 0 4 1 3 Output 0 1 0 1 0 0 0 1 0 0 0 0 0 0 1 0 Submitted Solution: ``` import sys class Graph: def __init__(self, nodes={}): self.nodes = nodes n = int(input()) G = Graph() for line in sys.stdin: numList = line.split() G.nodes[int(numList[0])] = list(map(int, numList[1:])) for _ in range(n): out = '' for i in range(n): if i + 1 != n: out += '0 ' if i + 1 in G.nodes[1] else '1 ' else: out += '0' if i + 1 in G.nodes[1] else '1' print(out) ```
instruction
0
107,006
13
214,012
No
output
1
107,006
13
214,013
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are two standard ways to represent a graph $G = (V, E)$, where $V$ is a set of vertices and $E$ is a set of edges; Adjacency list representation and Adjacency matrix representation. An adjacency-list representation consists of an array $Adj[|V|]$ of $|V|$ lists, one for each vertex in $V$. For each $u \in V$, the adjacency list $Adj[u]$ contains all vertices $v$ such that there is an edge $(u, v) \in E$. That is, $Adj[u]$ consists of all vertices adjacent to $u$ in $G$. An adjacency-matrix representation consists of $|V| \times |V|$ matrix $A = a_{ij}$ such that $a_{ij} = 1$ if $(i, j) \in E$, $a_{ij} = 0$ otherwise. Write a program which reads a directed graph $G$ represented by the adjacency list, and prints its adjacency-matrix representation. $G$ consists of $n\; (=|V|)$ vertices identified by their IDs $1, 2,.., n$ respectively. Constraints * $1 \leq n \leq 100$ Input In the first line, an integer $n$ is given. In the next $n$ lines, an adjacency list $Adj[u]$ for vertex $u$ are given in the following format: $u$ $k$ $v_1$ $v_2$ ... $v_k$ $u$ is vertex ID and $k$ denotes its degree. $v_i$ are IDs of vertices adjacent to $u$. Output As shown in the following sample output, print the adjacent-matrix representation of $G$. Put a single space character between $a_{ij}$. Example Input 4 1 2 2 4 2 1 4 3 0 4 1 3 Output 0 1 0 1 0 0 0 1 0 0 0 0 0 0 1 0 Submitted Solution: ``` import numpy as np n=int(input()) g=np.zeros((n,n),dtype=np.int) for i in range(n): s=list(map(int,input().split())) for j in range(s[1]): g[s[0]][s[2+j]]+=1 print(g) ```
instruction
0
107,007
13
214,014
No
output
1
107,007
13
214,015
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are two standard ways to represent a graph $G = (V, E)$, where $V$ is a set of vertices and $E$ is a set of edges; Adjacency list representation and Adjacency matrix representation. An adjacency-list representation consists of an array $Adj[|V|]$ of $|V|$ lists, one for each vertex in $V$. For each $u \in V$, the adjacency list $Adj[u]$ contains all vertices $v$ such that there is an edge $(u, v) \in E$. That is, $Adj[u]$ consists of all vertices adjacent to $u$ in $G$. An adjacency-matrix representation consists of $|V| \times |V|$ matrix $A = a_{ij}$ such that $a_{ij} = 1$ if $(i, j) \in E$, $a_{ij} = 0$ otherwise. Write a program which reads a directed graph $G$ represented by the adjacency list, and prints its adjacency-matrix representation. $G$ consists of $n\; (=|V|)$ vertices identified by their IDs $1, 2,.., n$ respectively. Constraints * $1 \leq n \leq 100$ Input In the first line, an integer $n$ is given. In the next $n$ lines, an adjacency list $Adj[u]$ for vertex $u$ are given in the following format: $u$ $k$ $v_1$ $v_2$ ... $v_k$ $u$ is vertex ID and $k$ denotes its degree. $v_i$ are IDs of vertices adjacent to $u$. Output As shown in the following sample output, print the adjacent-matrix representation of $G$. Put a single space character between $a_{ij}$. Example Input 4 1 2 2 4 2 1 4 3 0 4 1 3 Output 0 1 0 1 0 0 0 1 0 0 0 0 0 0 1 0 Submitted Solution: ``` #16D8102008K Yagyu Hazuki n=int(input()) dp = [[0 for i in range(n)] for j in range(n)] for _ in range(n): inputs = list(map(int, input().split())) u = inputs[0] k = inputs[1] v = inputs[2:] for each in v: dp[u-1][each-1] = 1 print(dp) ```
instruction
0
107,008
13
214,016
No
output
1
107,008
13
214,017
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are two standard ways to represent a graph $G = (V, E)$, where $V$ is a set of vertices and $E$ is a set of edges; Adjacency list representation and Adjacency matrix representation. An adjacency-list representation consists of an array $Adj[|V|]$ of $|V|$ lists, one for each vertex in $V$. For each $u \in V$, the adjacency list $Adj[u]$ contains all vertices $v$ such that there is an edge $(u, v) \in E$. That is, $Adj[u]$ consists of all vertices adjacent to $u$ in $G$. An adjacency-matrix representation consists of $|V| \times |V|$ matrix $A = a_{ij}$ such that $a_{ij} = 1$ if $(i, j) \in E$, $a_{ij} = 0$ otherwise. Write a program which reads a directed graph $G$ represented by the adjacency list, and prints its adjacency-matrix representation. $G$ consists of $n\; (=|V|)$ vertices identified by their IDs $1, 2,.., n$ respectively. Constraints * $1 \leq n \leq 100$ Input In the first line, an integer $n$ is given. In the next $n$ lines, an adjacency list $Adj[u]$ for vertex $u$ are given in the following format: $u$ $k$ $v_1$ $v_2$ ... $v_k$ $u$ is vertex ID and $k$ denotes its degree. $v_i$ are IDs of vertices adjacent to $u$. Output As shown in the following sample output, print the adjacent-matrix representation of $G$. Put a single space character between $a_{ij}$. Example Input 4 1 2 2 4 2 1 4 3 0 4 1 3 Output 0 1 0 1 0 0 0 1 0 0 0 0 0 0 1 0 Submitted Solution: ``` n=int(input()) a=[[0 for j in range(n)]for i in range(n)] for i in range(n): b=list(map(int,input().split())) for i in range(b[1]): print(b[i+2]) a[b[0]-1][b[i+2]-1]=1 for i in range(n): print(a[i]) ```
instruction
0
107,009
13
214,018
No
output
1
107,009
13
214,019
Provide tags and a correct Python 3 solution for this coding contest problem. Recently Bob invented a new game with a tree (we should remind you, that a tree is a connected graph without cycles): he deletes any (possibly, zero) amount of edges of the tree, and counts the product of sizes of the connected components left after the deletion. Your task is to find out the maximum number that Bob can get in his new game for a given tree. Input The first input line contains integer number n (1 ≤ n ≤ 700) — amount of vertices in the tree. The following n - 1 lines contain the description of the edges. Each line contains the pair of vertices' indexes, joined by an edge, ai, bi (1 ≤ ai, bi ≤ n). It's guaranteed that the graph described in the input is a tree. Output Output the only number — the maximum product of sizes of the connected components, that Bob can get after deleting some of the tree's edges. Examples Input 5 1 2 2 3 3 4 4 5 Output 6 Input 8 1 2 1 3 2 4 2 5 3 6 3 7 6 8 Output 18 Input 3 1 2 1 3 Output 3
instruction
0
107,275
13
214,550
Tags: dp Correct Solution: ``` def dfs(v, pr = -1): dp[v][1] = 1 sz[v] = 1 for to in g[v]: if to == pr: continue dfs(to, v) for x in range(sz[v], 0, -1): for y in range(sz[to], -1, -1): #print(v, x, y, dp[v][x], dp[to][y]) dp[v][x + y] = max(dp[v][x + y], dp[v][x] * dp[to][y]) sz[v] += sz[to] for i in range(1, sz[v] + 1): dp[v][0] = max(dp[v][0], dp[v][i] * i) n = int(input()) sz = [0 for i in range(n)] g = [[] for i in range(n)] dp = [[0 for j in range(n + 1)] for i in range(n + 1)] for i in range(n - 1): x, y = map(int, input().split()) x -= 1 y -= 1 g[x].append(y) g[y].append(x) dfs(0) print(dp[0][0]) ```
output
1
107,275
13
214,551
Provide tags and a correct Python 3 solution for this coding contest problem. Recently Bob invented a new game with a tree (we should remind you, that a tree is a connected graph without cycles): he deletes any (possibly, zero) amount of edges of the tree, and counts the product of sizes of the connected components left after the deletion. Your task is to find out the maximum number that Bob can get in his new game for a given tree. Input The first input line contains integer number n (1 ≤ n ≤ 700) — amount of vertices in the tree. The following n - 1 lines contain the description of the edges. Each line contains the pair of vertices' indexes, joined by an edge, ai, bi (1 ≤ ai, bi ≤ n). It's guaranteed that the graph described in the input is a tree. Output Output the only number — the maximum product of sizes of the connected components, that Bob can get after deleting some of the tree's edges. Examples Input 5 1 2 2 3 3 4 4 5 Output 6 Input 8 1 2 1 3 2 4 2 5 3 6 3 7 6 8 Output 18 Input 3 1 2 1 3 Output 3
instruction
0
107,276
13
214,552
Tags: dp Correct Solution: ``` from fractions import Fraction n = int(input()) adj = [list() for x in range(n)] H = [0] * n F = [0] * n FoH = [list() for x in range(n)] sz = 0 order = [0] * n pi = [-1] * n def dfs(u, p = -1): global pi, order, sz pi[u] = p for v in adj[u]: if v != p: dfs(v, u) order[sz] = u sz += 1 T1 = [0] * n T2 = [0] * n T3 = [0] * n def solve(u, p = -1): global H, F, FoH F[u] = 1 for v in adj[u]: if v != p: F[u] *= H[v] FoH[u].append(Fraction(F[v], H[v])) ans = F[u] FoH[u].sort() FoH[u].reverse() pd = 1 s = 0 for x in FoH[u]: pd *= x s += 1 ans = max(ans, int(pd * F[u]) * (s+1)) for v in adj[u]: if v != p: pd = 1 s = 0 for x in FoH[v]: pd *= x s += 1 ans = max(ans, int(pd * F[u] * F[v]) // H[v] * (s+2)) #print(u+1, ':', FoH[u], ans) H[u] = ans for i in range(1, n): u, v = [int(x) for x in input().split()] u -= 1 v -= 1 adj[u].append(v) adj[v].append(u) dfs(0) for x in order: solve(x, pi[x]) print(H[0]) ```
output
1
107,276
13
214,553
Provide tags and a correct Python 3 solution for this coding contest problem. Recently Bob invented a new game with a tree (we should remind you, that a tree is a connected graph without cycles): he deletes any (possibly, zero) amount of edges of the tree, and counts the product of sizes of the connected components left after the deletion. Your task is to find out the maximum number that Bob can get in his new game for a given tree. Input The first input line contains integer number n (1 ≤ n ≤ 700) — amount of vertices in the tree. The following n - 1 lines contain the description of the edges. Each line contains the pair of vertices' indexes, joined by an edge, ai, bi (1 ≤ ai, bi ≤ n). It's guaranteed that the graph described in the input is a tree. Output Output the only number — the maximum product of sizes of the connected components, that Bob can get after deleting some of the tree's edges. Examples Input 5 1 2 2 3 3 4 4 5 Output 6 Input 8 1 2 1 3 2 4 2 5 3 6 3 7 6 8 Output 18 Input 3 1 2 1 3 Output 3
instruction
0
107,277
13
214,554
Tags: dp Correct Solution: ``` n=int(input()) w=[[] for i in range(n+1)] sz=[0]*(n+1) f=[[0]*(n+1) for i in range(n+1)] def dfs(u,p): f[u][1]=sz[u]=1 for v in w[u]: if v!=p: dfs(v,u) for j in range(sz[u],-1,-1): for k in range(sz[v],-1,-1):f[u][j+k]=max(f[u][j+k],f[u][j]*f[v][k]) sz[u]+=sz[v] for i in range(1,sz[u]+1):f[u][0]=max(f[u][0],f[u][i]*i) for i in range(n-1): x,y=map(int,input().split()) w[x]+=[y] w[y]+=[x] dfs(1,0) print(f[1][0]) ```
output
1
107,277
13
214,555
Provide tags and a correct Python 3 solution for this coding contest problem. Recently Bob invented a new game with a tree (we should remind you, that a tree is a connected graph without cycles): he deletes any (possibly, zero) amount of edges of the tree, and counts the product of sizes of the connected components left after the deletion. Your task is to find out the maximum number that Bob can get in his new game for a given tree. Input The first input line contains integer number n (1 ≤ n ≤ 700) — amount of vertices in the tree. The following n - 1 lines contain the description of the edges. Each line contains the pair of vertices' indexes, joined by an edge, ai, bi (1 ≤ ai, bi ≤ n). It's guaranteed that the graph described in the input is a tree. Output Output the only number — the maximum product of sizes of the connected components, that Bob can get after deleting some of the tree's edges. Examples Input 5 1 2 2 3 3 4 4 5 Output 6 Input 8 1 2 1 3 2 4 2 5 3 6 3 7 6 8 Output 18 Input 3 1 2 1 3 Output 3
instruction
0
107,278
13
214,556
Tags: dp Correct Solution: ``` maxn = 710 to = [[] for i in range(maxn)] dp = [[0 for i in range(maxn)] for i in range(maxn)] size = [0 for i in range(maxn)] n = int(input()) def dfs(now, f): size[now] = 1 dp[now][1] = 1 for i in to[now]: if i != f: dfs(i, now) size[now] += size[i] for j in reversed(range(0, size[now] + 1)): for k in range(max(0, j - size[now] + size[i]), min(j, size[i]) + 1): dp[now][j] = max(dp[now][j], dp[now][j - k] * dp[i][k]) for i in range(1, n + 1): dp[now][0] = max(dp[now][0], dp[now][i] * i) for i in range(1, n): u, v = map(int, input().split(' ')) to[u].append(v) to[v].append(u) dfs(1, 0) print(dp[1][0]) ```
output
1
107,278
13
214,557
Provide tags and a correct Python 3 solution for this coding contest problem. Recently Bob invented a new game with a tree (we should remind you, that a tree is a connected graph without cycles): he deletes any (possibly, zero) amount of edges of the tree, and counts the product of sizes of the connected components left after the deletion. Your task is to find out the maximum number that Bob can get in his new game for a given tree. Input The first input line contains integer number n (1 ≤ n ≤ 700) — amount of vertices in the tree. The following n - 1 lines contain the description of the edges. Each line contains the pair of vertices' indexes, joined by an edge, ai, bi (1 ≤ ai, bi ≤ n). It's guaranteed that the graph described in the input is a tree. Output Output the only number — the maximum product of sizes of the connected components, that Bob can get after deleting some of the tree's edges. Examples Input 5 1 2 2 3 3 4 4 5 Output 6 Input 8 1 2 1 3 2 4 2 5 3 6 3 7 6 8 Output 18 Input 3 1 2 1 3 Output 3
instruction
0
107,279
13
214,558
Tags: dp Correct Solution: ``` n = int(input()) edges = [[] for i in range(n)] ch = [[] for i in range(n)] for i in range(n - 1): a, b = map(int, input().split()) a -= 1 b -= 1 edges[a].append(b) edges[b].append(a) dp = [1 for i in range(n)] prod = [1 for i in range(n)] def dfs(v, pr): prod[v] = 1 for to in edges[v]: if to != pr: dfs(to, v) ch[v].append(to) prod[v] *= dp[to] ch[v].sort(key = lambda i : dp[i] / prod[i]) dp[v] = prod[v] now = prod[v] cnt = 1 for to in ch[v]: cnt += 1 now //= dp[to] now *= prod[to] dp[v] = max(dp[v], now * cnt) for to in ch[v]: now = prod[v] // dp[to] * prod[to] cnt = 2 for gr in ch[to]: cnt += 1 now //= dp[gr] now *= prod[gr] dp[v] = max(dp[v], now * cnt) dfs(0, -1) print(dp[0]) ```
output
1
107,279
13
214,559
Provide tags and a correct Python 3 solution for this coding contest problem. Recently Bob invented a new game with a tree (we should remind you, that a tree is a connected graph without cycles): he deletes any (possibly, zero) amount of edges of the tree, and counts the product of sizes of the connected components left after the deletion. Your task is to find out the maximum number that Bob can get in his new game for a given tree. Input The first input line contains integer number n (1 ≤ n ≤ 700) — amount of vertices in the tree. The following n - 1 lines contain the description of the edges. Each line contains the pair of vertices' indexes, joined by an edge, ai, bi (1 ≤ ai, bi ≤ n). It's guaranteed that the graph described in the input is a tree. Output Output the only number — the maximum product of sizes of the connected components, that Bob can get after deleting some of the tree's edges. Examples Input 5 1 2 2 3 3 4 4 5 Output 6 Input 8 1 2 1 3 2 4 2 5 3 6 3 7 6 8 Output 18 Input 3 1 2 1 3 Output 3
instruction
0
107,280
13
214,560
Tags: dp Correct Solution: ``` n=int(input()) sz=[0]*(n+1) g=[[] for _ in range(n+1)] dp=[[0]*(n+1) for _ in range(n+1)] def dfs(u,pr): sz[u]=dp[u][1]=1 for v in g[u]: if(v==pr):continue dfs(v,u) for i in range(sz[u],-1,-1): for j in range(sz[v],-1,-1): dp[u][i+j]=max(dp[u][i+j],dp[u][i]*dp[v][j]) sz[u]+=sz[v] for i in range(sz[u]+1): dp[u][0]=max(dp[u][0],dp[u][i]*i); for _ in range(n-1): a,b=map(int,input().split()) g[a].append(b) g[b].append(a) dfs(1,-1) print(dp[1][0]) ```
output
1
107,280
13
214,561
Provide tags and a correct Python 3 solution for this coding contest problem. Recently Bob invented a new game with a tree (we should remind you, that a tree is a connected graph without cycles): he deletes any (possibly, zero) amount of edges of the tree, and counts the product of sizes of the connected components left after the deletion. Your task is to find out the maximum number that Bob can get in his new game for a given tree. Input The first input line contains integer number n (1 ≤ n ≤ 700) — amount of vertices in the tree. The following n - 1 lines contain the description of the edges. Each line contains the pair of vertices' indexes, joined by an edge, ai, bi (1 ≤ ai, bi ≤ n). It's guaranteed that the graph described in the input is a tree. Output Output the only number — the maximum product of sizes of the connected components, that Bob can get after deleting some of the tree's edges. Examples Input 5 1 2 2 3 3 4 4 5 Output 6 Input 8 1 2 1 3 2 4 2 5 3 6 3 7 6 8 Output 18 Input 3 1 2 1 3 Output 3
instruction
0
107,281
13
214,562
Tags: dp Correct Solution: ``` from fractions import Fraction n = int(input()) G = [[] for i in range(n)] for i in range(n-1): a, b = (int(x)-1 for x in input().split()) G[a].append(b) G[b].append(a) f, g, h = [1]*n, [1]*n, [1]*n def dfs(v, p): if p != -1: G[v].remove(p) for e in G[v]: dfs(e, v) h[v] *= f[e] tmp = [] f[v], g[v] = h[v], h[v]*2 for e in G[v]: f[v] = max(f[v], h[v] // f[e] * g[e]) tmp.append((f[e], h[e])) fPart, hPart = h[v], 1 tmp.sort(key=lambda x: Fraction(*x)) for i in range(len(tmp)): fPart //= tmp[i][0] hPart *= tmp[i][1] f[v] = max(f[v], fPart*hPart*(i+2)) g[v] = max(g[v], fPart*hPart*(i+3)) dfs(0, -1) print(f[0]) ```
output
1
107,281
13
214,563
Provide tags and a correct Python 3 solution for this coding contest problem. Recently Bob invented a new game with a tree (we should remind you, that a tree is a connected graph without cycles): he deletes any (possibly, zero) amount of edges of the tree, and counts the product of sizes of the connected components left after the deletion. Your task is to find out the maximum number that Bob can get in his new game for a given tree. Input The first input line contains integer number n (1 ≤ n ≤ 700) — amount of vertices in the tree. The following n - 1 lines contain the description of the edges. Each line contains the pair of vertices' indexes, joined by an edge, ai, bi (1 ≤ ai, bi ≤ n). It's guaranteed that the graph described in the input is a tree. Output Output the only number — the maximum product of sizes of the connected components, that Bob can get after deleting some of the tree's edges. Examples Input 5 1 2 2 3 3 4 4 5 Output 6 Input 8 1 2 1 3 2 4 2 5 3 6 3 7 6 8 Output 18 Input 3 1 2 1 3 Output 3
instruction
0
107,282
13
214,564
Tags: dp Correct Solution: ``` def dfs(u,pa,gr): szz[u]=1 for i in gr[u]: if i ==pa: continue dfs(i,u,gr) szz[u]+=szz[i] first=True n=szz[u] temp=[0 for i in range(0,n)] cs=0 for it in gr[u]: if it==pa: continue if first: first=False for i in range(0,szz[it]+1): temp[i]=dp[it][i] if i!=0: temp[i]//=i cs+=szz[it] else: cs+=szz[it] for i in reversed(range(0,cs+1)): mx=0 for j in range(max(0,i-cs+szz[it]),min(i,szz[it])+1): mx1=temp[i-j]*dp[it][j] if j!=0: mx1//=j mx=max(mx,mx1) temp[i]=mx if first: dp[u][0]=1 dp[u][1]=1 else: for i in range(1,n+1): dp[u][i]=temp[i-1]*i dp[u][0]=max(dp[u][0],dp[u][i]) n1 = int(input()) gr=[[]for i in range(0,n1)] for i in range(0,n1-1): a,b=input().split() a=int(a) b=int(b) a-=1 b-=1 gr[a].append(b) gr[b].append(a) szz=[0 for i in range(0,n1)] dp= [[0 for i in range(0,n1+1)] for j in range(0,n1+1)] dfs(0,-1,gr) print(int(dp[0][0])) ```
output
1
107,282
13
214,565
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Recently Bob invented a new game with a tree (we should remind you, that a tree is a connected graph without cycles): he deletes any (possibly, zero) amount of edges of the tree, and counts the product of sizes of the connected components left after the deletion. Your task is to find out the maximum number that Bob can get in his new game for a given tree. Input The first input line contains integer number n (1 ≤ n ≤ 700) — amount of vertices in the tree. The following n - 1 lines contain the description of the edges. Each line contains the pair of vertices' indexes, joined by an edge, ai, bi (1 ≤ ai, bi ≤ n). It's guaranteed that the graph described in the input is a tree. Output Output the only number — the maximum product of sizes of the connected components, that Bob can get after deleting some of the tree's edges. Examples Input 5 1 2 2 3 3 4 4 5 Output 6 Input 8 1 2 1 3 2 4 2 5 3 6 3 7 6 8 Output 18 Input 3 1 2 1 3 Output 3 Submitted Solution: ``` n = int(input()) node = [[] for i in range(n)] for i in range(1,n): a,b = map(int,input().split()) node[a-1].append(b-1) node[b-1].append(a-1) dp = [[] for i in range(n)] def combine(a, b): c = [0]*(len(a)+len(b)-1) for i,va in enumerate(a): for j,vb in enumerate(b): c[i+j] = max(c[i+j],va*vb) return c def dfs(p,par=-1): dp[p] = [0,1] for i in node[p]: if i == par: continue dfs(i,p) dp[p] = combine(dp[i],dp[p]) ma = 0 for i,v in enumerate(dp[p]): ma = max(ma, i*v) dp[p][0] = ma dfs(0) print(dp[0][0]) ```
instruction
0
107,283
13
214,566
Yes
output
1
107,283
13
214,567
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Recently Bob invented a new game with a tree (we should remind you, that a tree is a connected graph without cycles): he deletes any (possibly, zero) amount of edges of the tree, and counts the product of sizes of the connected components left after the deletion. Your task is to find out the maximum number that Bob can get in his new game for a given tree. Input The first input line contains integer number n (1 ≤ n ≤ 700) — amount of vertices in the tree. The following n - 1 lines contain the description of the edges. Each line contains the pair of vertices' indexes, joined by an edge, ai, bi (1 ≤ ai, bi ≤ n). It's guaranteed that the graph described in the input is a tree. Output Output the only number — the maximum product of sizes of the connected components, that Bob can get after deleting some of the tree's edges. Examples Input 5 1 2 2 3 3 4 4 5 Output 6 Input 8 1 2 1 3 2 4 2 5 3 6 3 7 6 8 Output 18 Input 3 1 2 1 3 Output 3 Submitted Solution: ``` from fractions import Fraction import sys #print(sys.getrecursionlimit()) sys.setrecursionlimit(10000) c = int(input()) edges=dict((i,[]) for i in range(1,c+1)) for i in range(0,c-1): a,b=tuple(map(int,input().split())) edges[a].append(b) edges[b].append(a) #print(edges.get(1,[])) #exit() dp=[None for i in range(c+1)] def dfs(r,p): #print(r) if dp[r] is not None: return dp[r] children=filter(lambda x: x != p, edges[r]) cs=[dfs(i,r) for i in children] #print(r,cs) cs.sort(key=lambda x:Fraction(x[0],x[1]),reverse=True) f=1 for c in cs: f*=c[1] h=f k=1 m=f for c in cs: m=m//c[1]*c[0] k+=1 #if m*k==24: #print("aaa") h=max(h,m*k) m=f for c in cs: k=2 a=f//c[1]*c[0] h=max(h,a*k) for d in c[2]: a=a//d[1]*d[0] k+=1 #if a*k==24: #print("bbb",a,k,c,d) h=max(h,a*k) dp[r]=(f,h,cs) #print(r,dp[r]) return dp[r] print(dfs(1,0)[1]) ```
instruction
0
107,284
13
214,568
Yes
output
1
107,284
13
214,569
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Recently Bob invented a new game with a tree (we should remind you, that a tree is a connected graph without cycles): he deletes any (possibly, zero) amount of edges of the tree, and counts the product of sizes of the connected components left after the deletion. Your task is to find out the maximum number that Bob can get in his new game for a given tree. Input The first input line contains integer number n (1 ≤ n ≤ 700) — amount of vertices in the tree. The following n - 1 lines contain the description of the edges. Each line contains the pair of vertices' indexes, joined by an edge, ai, bi (1 ≤ ai, bi ≤ n). It's guaranteed that the graph described in the input is a tree. Output Output the only number — the maximum product of sizes of the connected components, that Bob can get after deleting some of the tree's edges. Examples Input 5 1 2 2 3 3 4 4 5 Output 6 Input 8 1 2 1 3 2 4 2 5 3 6 3 7 6 8 Output 18 Input 3 1 2 1 3 Output 3 Submitted Solution: ``` from fractions import Fraction import sys #print(sys.getrecursionlimit()) sys.setrecursionlimit(1000*100) c = int(input()) edges=dict((i,[]) for i in range(1,c+1)) for i in range(0,c-1): a,b=tuple(map(int,input().split())) edges[a].append(b) edges[b].append(a) #print(edges.get(1,[])) #exit() dp=[None for i in range(c+1)] def dfs(r,p): #print(r) if dp[r] is not None: return dp[r] children=filter(lambda x: x != p, edges[r]) cs=[dfs(i,r) for i in children] #print(r,cs) cs.sort(key=lambda x:Fraction(x[0],x[1]),reverse=True) f=1 for c in cs: f*=c[1] h=f k=1 m=f for c in cs: m=m//c[1]*c[0] k+=1 #if m*k==24: #print("aaa") h=max(h,m*k) m=f for c in cs: k=2 a=f//c[1]*c[0] h=max(h,a*k) for d in c[2]: a=a//d[1]*d[0] k+=1 #if a*k==24: #print("bbb",a,k,c,d) h=max(h,a*k) dp[r]=(f,h,cs) #print(r,dp[r]) return dp[r] print(dfs(1,0)[1]) ```
instruction
0
107,285
13
214,570
Yes
output
1
107,285
13
214,571
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Recently Bob invented a new game with a tree (we should remind you, that a tree is a connected graph without cycles): he deletes any (possibly, zero) amount of edges of the tree, and counts the product of sizes of the connected components left after the deletion. Your task is to find out the maximum number that Bob can get in his new game for a given tree. Input The first input line contains integer number n (1 ≤ n ≤ 700) — amount of vertices in the tree. The following n - 1 lines contain the description of the edges. Each line contains the pair of vertices' indexes, joined by an edge, ai, bi (1 ≤ ai, bi ≤ n). It's guaranteed that the graph described in the input is a tree. Output Output the only number — the maximum product of sizes of the connected components, that Bob can get after deleting some of the tree's edges. Examples Input 5 1 2 2 3 3 4 4 5 Output 6 Input 8 1 2 1 3 2 4 2 5 3 6 3 7 6 8 Output 18 Input 3 1 2 1 3 Output 3 Submitted Solution: ``` def run(E, F, G, curr, prev): for nex in E[curr]: if nex == prev: continue run(E, F, G, nex, curr) G[curr].append(F[nex]) G[curr].sort(key=lambda x: x[0] / x[1], reverse=True) F[curr][0] = 1 for i in range(len(G[curr])): F[curr][0] *= G[curr][i][1] x = F[curr][0] for i in range(len(G[curr])): x = x * G[curr][i][0] // G[curr][i][1] F[curr][1] = max(F[curr][1], x * (i + 2)) for i in range(len(E[curr])): p = E[curr][i] if p == prev: continue x = F[curr][0] * F[p][0] // F[p][1] for j in range(len(G[p])): x = x * G[p][j][0] // G[p][j][1] F[curr][1] = max(F[curr][1], x * (j + 3)) n = int(input()) E = [[] for i in range(n)] for i in range(n - 1): a, b = map(lambda x: int(x) - 1, input().split()) E[a].append(b) E[b].append(a) F = [[1] * 2 for i in range(n)] G = [[] for i in range(n)] run(E, F, G, 0, -1) print(max(F[0])) ```
instruction
0
107,286
13
214,572
Yes
output
1
107,286
13
214,573
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Recently Bob invented a new game with a tree (we should remind you, that a tree is a connected graph without cycles): he deletes any (possibly, zero) amount of edges of the tree, and counts the product of sizes of the connected components left after the deletion. Your task is to find out the maximum number that Bob can get in his new game for a given tree. Input The first input line contains integer number n (1 ≤ n ≤ 700) — amount of vertices in the tree. The following n - 1 lines contain the description of the edges. Each line contains the pair of vertices' indexes, joined by an edge, ai, bi (1 ≤ ai, bi ≤ n). It's guaranteed that the graph described in the input is a tree. Output Output the only number — the maximum product of sizes of the connected components, that Bob can get after deleting some of the tree's edges. Examples Input 5 1 2 2 3 3 4 4 5 Output 6 Input 8 1 2 1 3 2 4 2 5 3 6 3 7 6 8 Output 18 Input 3 1 2 1 3 Output 3 Submitted Solution: ``` #fuck python # 1:root estefade 2:!1 import sys def dfs(x,par): a=1 dp2[x]=1 dp1[x][1]=1 ans[x]=1 for i in (g[x]): if(i!=par): dfs(i,x) dp2[x]*=ans[i] for i in (g[x]): if(i!=par): for j in range(1,len(g[i])+2): ans[x]=max(int((dp2[x]/ans[i]*dp1[i][j]*(j+1))/j),ans[x]) for i in (g[x]): if(i!=par): for j in range(len(g[x])+2,0,-1): dp1[x][j]=dp1[x][j]*ans[i] dp1[x][j]=max(dp1[x][j],dp1[x][j-1]*dp2[i]) for i in range(len(g[x])+3): dp1[x][i]=dp1[x][i]*i ans[x]=max(ans[x],dp1[x][i]) N=709 g=[] ans=[] dp1=[] dp2=[] for i in range(0,N): g.append([]) dp1.append([]) dp2.append(0) ans.append(0) for i in range(0,N): for j in range(0,N): dp1[i].append(0) n=int(input()) for i in range(1,n): x,y=map(int,input().split()) g[x].append(y) g[y].append(x) dfs(1,0) print(ans[1]) ```
instruction
0
107,287
13
214,574
No
output
1
107,287
13
214,575
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Recently Bob invented a new game with a tree (we should remind you, that a tree is a connected graph without cycles): he deletes any (possibly, zero) amount of edges of the tree, and counts the product of sizes of the connected components left after the deletion. Your task is to find out the maximum number that Bob can get in his new game for a given tree. Input The first input line contains integer number n (1 ≤ n ≤ 700) — amount of vertices in the tree. The following n - 1 lines contain the description of the edges. Each line contains the pair of vertices' indexes, joined by an edge, ai, bi (1 ≤ ai, bi ≤ n). It's guaranteed that the graph described in the input is a tree. Output Output the only number — the maximum product of sizes of the connected components, that Bob can get after deleting some of the tree's edges. Examples Input 5 1 2 2 3 3 4 4 5 Output 6 Input 8 1 2 1 3 2 4 2 5 3 6 3 7 6 8 Output 18 Input 3 1 2 1 3 Output 3 Submitted Solution: ``` #fuck python # 1:root estefade 2:!1 import sys def dfs(x,par): a=1 dp2[x]=1 dp1[x][1]=1 ans[x]=1 for i in (g[x]): if(i!=par): dfs(i,x) dp2[x]*=ans[i] for i in (g[x]): if(i!=par): for j in range(1,len(g[i])+2): if(dp2[x]%ans[i]>0): print("ey vay") ans[x]=int(max(dp2[x]/ans[i]*c[i][j]*(j+1),ans[x])) for i in (g[x]): if(i!=par): for j in range(len(g[x])+2,0,-1): dp1[x][j]=dp1[x][j]*ans[i] dp1[x][j]=max(dp1[x][j],dp1[x][j-1]*dp2[i]) for i in range(len(g[x])+3): c[x][i]=dp1[x][i] dp1[x][i]=dp1[x][i]*i ans[x]=max(ans[x],dp1[x][i]) N=709 g=[] ans=[] dp1=[] dp2=[] c=[] for i in range(0,N): g.append([]) dp1.append([]) c.append([]) dp2.append(0) ans.append(0) for i in range(0,N): for j in range(0,N): dp1[i].append(0) c[i].append(0) n=int(input()) for i in range(1,n): x,y=map(int,input().split()) g[x].append(y) g[y].append(x) dfs(1,0) print(int(ans[1])) ```
instruction
0
107,288
13
214,576
No
output
1
107,288
13
214,577
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Recently Bob invented a new game with a tree (we should remind you, that a tree is a connected graph without cycles): he deletes any (possibly, zero) amount of edges of the tree, and counts the product of sizes of the connected components left after the deletion. Your task is to find out the maximum number that Bob can get in his new game for a given tree. Input The first input line contains integer number n (1 ≤ n ≤ 700) — amount of vertices in the tree. The following n - 1 lines contain the description of the edges. Each line contains the pair of vertices' indexes, joined by an edge, ai, bi (1 ≤ ai, bi ≤ n). It's guaranteed that the graph described in the input is a tree. Output Output the only number — the maximum product of sizes of the connected components, that Bob can get after deleting some of the tree's edges. Examples Input 5 1 2 2 3 3 4 4 5 Output 6 Input 8 1 2 1 3 2 4 2 5 3 6 3 7 6 8 Output 18 Input 3 1 2 1 3 Output 3 Submitted Solution: ``` def dfs(u,pa,gr): szz[u]=1 for i in gr[u]: if i ==pa: continue dfs(i,u,gr) szz[u]+=szz[i] first=True # bool first=true; n=szz[u] # int n=szz[u]; # temp=[0 for i in range(0,n)] temp=[0 for i in range(0,n)] # v temp(n,0); for it in gr[u]: if it==pa: continue if first: first=False for i in range(0,szz[it]+1): temp[i]=dp[it][i] if i!=0: temp[i]//=i else: for i in reversed(range(0,n)): mx=0 for j in range(min(i,szz[it])+1): mx1=temp[i-j]*dp[it][j] # print(type(j)) if j!=0: mx1//=j mx=max(mx,mx1) temp[i]=mx if first: dp[u][0]=1 dp[u][1]=1 else: for i in range(1,n+1): # print(type(i)) dp[u][i]=temp[i-1]*i dp[u][0]=max(dp[u][0],dp[u][i]) n1 = int(input()) gr=[[]for i in range(0,n1)] for i in range(0,n1-1): a,b=input().split() a=int(a) b=int(b) a-=1 b-=1 gr[a].append(b) gr[b].append(a) szz=[0 for i in range(0,n1)] dp= [[0 for i in range(0,n1+1)] for j in range(0,n1+1)] dfs(0,-1,gr) print(int(dp[0][0])) print(dp) ```
instruction
0
107,289
13
214,578
No
output
1
107,289
13
214,579
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Recently Bob invented a new game with a tree (we should remind you, that a tree is a connected graph without cycles): he deletes any (possibly, zero) amount of edges of the tree, and counts the product of sizes of the connected components left after the deletion. Your task is to find out the maximum number that Bob can get in his new game for a given tree. Input The first input line contains integer number n (1 ≤ n ≤ 700) — amount of vertices in the tree. The following n - 1 lines contain the description of the edges. Each line contains the pair of vertices' indexes, joined by an edge, ai, bi (1 ≤ ai, bi ≤ n). It's guaranteed that the graph described in the input is a tree. Output Output the only number — the maximum product of sizes of the connected components, that Bob can get after deleting some of the tree's edges. Examples Input 5 1 2 2 3 3 4 4 5 Output 6 Input 8 1 2 1 3 2 4 2 5 3 6 3 7 6 8 Output 18 Input 3 1 2 1 3 Output 3 Submitted Solution: ``` maxn = 710 to = [[] for i in range(maxn)] dp = [[0 for i in range(maxn)] for i in range(maxn)] size = [0 for i in range(maxn)] n = int(input()) def dfs(now, f): size[now] = 1 dp[now][1] = 1 for i in to[now]: if i != f: dfs(i, now) size[now] += size[i] for j in reversed(range(0, 12)): for k in range(0, min(j, size[i]) + 1): dp[now][j] = max(dp[now][j], dp[now][j - k] * dp[i][k]) for i in range(1, n + 1): dp[now][0] = max(dp[now][0], dp[now][i] * i) for i in range(1, n): u, v = map(int, input().split(' ')) to[u].append(v) to[v].append(u) dfs(1, 0) print(dp[1][0]) # print(dp) ```
instruction
0
107,290
13
214,580
No
output
1
107,290
13
214,581
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Ksenia has her winter exams. Today she is learning combinatorics. Here's one of the problems she needs to learn to solve. How many distinct trees are there consisting of n vertices, each with the following properties: * the tree is marked, that is, the vertices of the tree are numbered from 1 to n; * each vertex of the tree is connected with at most three other vertices, and at the same moment the vertex with number 1 is connected with at most two other vertices; * the size of the tree's maximum matching equals k. Two trees are considered distinct if there are such two vertices u and v, that in one tree they are connected by an edge and in the other tree they are not. Help Ksenia solve the problem for the given n and k. As the answer to the problem can be very huge you should output it modulo 1000000007 (109 + 7). Input The first line contains two integers n, k (1 ≤ n, k ≤ 50). Output Print a single integer — the answer to the problem modulo 1000000007 (109 + 7). Examples Input 1 1 Output 0 Input 2 1 Output 1 Input 3 1 Output 3 Input 4 2 Output 12 Note If you aren't familiar with matchings, please, read the following link: http://en.wikipedia.org/wiki/Matching_(graph_theory). Submitted Solution: ``` base = 1000000007 ans = {} C = {} def extgcd(n, m): if m == 0: return 1, 0, n x, y, d = extgcd(m, n % m) return y, x - n // m * y, d def modinv(a, base): return extgcd(a, base)[0] def init(maxn): global C C[0, 0] = 1 for j in range(1, maxn): C[0, j] = 0 for i in range(1, maxn): C[i, 0] = 1 for j in range(1, maxn): C[i, j] = (C[i - 1, j] + C[i - 1, j - 1]) % base def gao(n, k): #print(n,k) if (n, k) in ans: return ans[n, k] if n <= 1: return 1 if k == n else 0 ret = 0 for i in range(n): j = n - 1 - i for p in range(k): q = k - 1 - p ret += C[n - 1, i] * gao(i, i - p) * gao(j, j - q) ans[n, k] = ret * n * modinv(2, base) % base return ans[n, k] def main(n, k): """ >>> main(1,1) 0 >>> main(2,1) 1 >>> main(3,1) 3 >>> main(4,2) 12 """ if k > n - k: return 0 if k != n - k: return (gao(n, k) + gao(n, n - k)) * modinv(n, base) % base else: return gao(n, k) * modinv(n, base) % base init(51) import doctest doctest.testmod() n,k=tuple(map(int, input().split())) print(main(n,k)) ```
instruction
0
107,327
13
214,654
No
output
1
107,327
13
214,655
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Ksenia has her winter exams. Today she is learning combinatorics. Here's one of the problems she needs to learn to solve. How many distinct trees are there consisting of n vertices, each with the following properties: * the tree is marked, that is, the vertices of the tree are numbered from 1 to n; * each vertex of the tree is connected with at most three other vertices, and at the same moment the vertex with number 1 is connected with at most two other vertices; * the size of the tree's maximum matching equals k. Two trees are considered distinct if there are such two vertices u and v, that in one tree they are connected by an edge and in the other tree they are not. Help Ksenia solve the problem for the given n and k. As the answer to the problem can be very huge you should output it modulo 1000000007 (109 + 7). Input The first line contains two integers n, k (1 ≤ n, k ≤ 50). Output Print a single integer — the answer to the problem modulo 1000000007 (109 + 7). Examples Input 1 1 Output 0 Input 2 1 Output 1 Input 3 1 Output 3 Input 4 2 Output 12 Note If you aren't familiar with matchings, please, read the following link: http://en.wikipedia.org/wiki/Matching_(graph_theory). Submitted Solution: ``` #AKIOI print(1) ```
instruction
0
107,328
13
214,656
No
output
1
107,328
13
214,657
Provide tags and a correct Python 3 solution for this coding contest problem. A tree of size n is an undirected connected graph consisting of n vertices without cycles. Consider some tree with n vertices. We call a tree invariant relative to permutation p = p1p2... pn, if for any two vertices of the tree u and v the condition holds: "vertices u and v are connected by an edge if and only if vertices pu and pv are connected by an edge". You are given permutation p of size n. Find some tree size n, invariant relative to the given permutation. Input The first line contains number n (1 ≤ n ≤ 105) — the size of the permutation (also equal to the size of the sought tree). The second line contains permutation pi (1 ≤ pi ≤ n). Output If the sought tree does not exist, print "NO" (without the quotes). Otherwise, print "YES", and then print n - 1 lines, each of which contains two integers — the numbers of vertices connected by an edge of the tree you found. The vertices are numbered from 1, the order of the edges and the order of the vertices within the edges does not matter. If there are multiple solutions, output any of them. Examples Input 4 4 3 2 1 Output YES 4 1 4 2 1 3 Input 3 3 1 2 Output NO Note In the first sample test a permutation transforms edge (4, 1) into edge (1, 4), edge (4, 2) into edge (1, 3) and edge (1, 3) into edge (4, 2). These edges all appear in the resulting tree. It can be shown that in the second sample test no tree satisfies the given condition.
instruction
0
107,391
13
214,782
Tags: constructive algorithms, dfs and similar, greedy, trees Correct Solution: ``` def read_data(): n = int(input()) ps = list(map(int, input().split())) return n, ps def solve(n, ps): ps = [p - 1 for p in ps] cycles = [0] * n roots = [] has_odd_cycle = False for i in range(n): if cycles[i] > 0: continue q = ps[i] cycle = 1 while q != i: cycle += 1 q = ps[q] if cycle & 1: has_odd_cycle = True cycles[i] = cycle roots.append(i) q = ps[i] while q != i: cycles[q] = cycle q = ps[q] mincycle = min(cycles) if mincycle > 2: return False, [] if mincycle == 1: p = cycles.index(1) return True, [(p, i) for i in range(n) if i != p] if has_odd_cycle: return False, [] p = cycles.index(2) q = ps[p] edges = [(p, q)] for root in roots: if root == p or root == q: continue edges.append((p, root)) r = ps[root] edges.append((q, r)) r = ps[r] while r != root: edges.append((p, r)) r = ps[r] edges.append((q, r)) r = ps[r] return True, edges n, ps = read_data() is_possible, edges = solve(n, ps) if is_possible: print('YES') for a, b in edges: print(a + 1, b + 1) else: print('NO') ```
output
1
107,391
13
214,783
Provide tags and a correct Python 3 solution for this coding contest problem. A tree of size n is an undirected connected graph consisting of n vertices without cycles. Consider some tree with n vertices. We call a tree invariant relative to permutation p = p1p2... pn, if for any two vertices of the tree u and v the condition holds: "vertices u and v are connected by an edge if and only if vertices pu and pv are connected by an edge". You are given permutation p of size n. Find some tree size n, invariant relative to the given permutation. Input The first line contains number n (1 ≤ n ≤ 105) — the size of the permutation (also equal to the size of the sought tree). The second line contains permutation pi (1 ≤ pi ≤ n). Output If the sought tree does not exist, print "NO" (without the quotes). Otherwise, print "YES", and then print n - 1 lines, each of which contains two integers — the numbers of vertices connected by an edge of the tree you found. The vertices are numbered from 1, the order of the edges and the order of the vertices within the edges does not matter. If there are multiple solutions, output any of them. Examples Input 4 4 3 2 1 Output YES 4 1 4 2 1 3 Input 3 3 1 2 Output NO Note In the first sample test a permutation transforms edge (4, 1) into edge (1, 4), edge (4, 2) into edge (1, 3) and edge (1, 3) into edge (4, 2). These edges all appear in the resulting tree. It can be shown that in the second sample test no tree satisfies the given condition.
instruction
0
107,392
13
214,784
Tags: constructive algorithms, dfs and similar, greedy, trees Correct Solution: ``` n = int(input()) a = list(map(lambda x: int(x) - 1, input().split())) p = q = -1 for i in range(n): if a[a[i]] == i: p, q = i, a[i] if a[i] == i: print('YES') [print(i + 1, j + 1) for j in range(n) if i != j] exit() if p < 0 or q < 0: print('NO') exit() r = [(p, q)] v = [0] * n v[p] = v[q] = 1 for i in range(n): if not v[i]: r.append((p, i)) v[i] = 1 t = 0 x = a[i] while x != i: r.append((p if t else q, x)) v[x] = 1 t = 1 - t x = a[x] if not t: print('NO') exit() print('YES') [print(x + 1, y + 1) for x, y in r] ```
output
1
107,392
13
214,785
Provide tags and a correct Python 3 solution for this coding contest problem. A tree of size n is an undirected connected graph consisting of n vertices without cycles. Consider some tree with n vertices. We call a tree invariant relative to permutation p = p1p2... pn, if for any two vertices of the tree u and v the condition holds: "vertices u and v are connected by an edge if and only if vertices pu and pv are connected by an edge". You are given permutation p of size n. Find some tree size n, invariant relative to the given permutation. Input The first line contains number n (1 ≤ n ≤ 105) — the size of the permutation (also equal to the size of the sought tree). The second line contains permutation pi (1 ≤ pi ≤ n). Output If the sought tree does not exist, print "NO" (without the quotes). Otherwise, print "YES", and then print n - 1 lines, each of which contains two integers — the numbers of vertices connected by an edge of the tree you found. The vertices are numbered from 1, the order of the edges and the order of the vertices within the edges does not matter. If there are multiple solutions, output any of them. Examples Input 4 4 3 2 1 Output YES 4 1 4 2 1 3 Input 3 3 1 2 Output NO Note In the first sample test a permutation transforms edge (4, 1) into edge (1, 4), edge (4, 2) into edge (1, 3) and edge (1, 3) into edge (4, 2). These edges all appear in the resulting tree. It can be shown that in the second sample test no tree satisfies the given condition.
instruction
0
107,393
13
214,786
Tags: constructive algorithms, dfs and similar, greedy, trees Correct Solution: ``` def main(): n = int(input()) a = list(map(lambda x: int(x) - 1, input().split())) p = -1 q = -1 for i in range(n): if a[a[i]] == i: p, q = i, a[i] if a[i] == i: print('YES') [print(i + 1, j + 1) for j in range(n) if i != j] exit() if p < 0 or q < 0: print('NO'); exit() ans = [(p, q)] used = [False] * n used[p] = True used[q] = True for i in range(n): if used[i]: continue ans.append((p, i)) used[i] = True flag = False x = a[i] while x != i: ans.append((p if flag else q, x)) used[x] = True x = a[x] flag = not flag if not flag: print('NO'); exit() print('YES') [print(x + 1, y + 1) for x, y in ans] main() ```
output
1
107,393
13
214,787
Provide tags and a correct Python 3 solution for this coding contest problem. A tree of size n is an undirected connected graph consisting of n vertices without cycles. Consider some tree with n vertices. We call a tree invariant relative to permutation p = p1p2... pn, if for any two vertices of the tree u and v the condition holds: "vertices u and v are connected by an edge if and only if vertices pu and pv are connected by an edge". You are given permutation p of size n. Find some tree size n, invariant relative to the given permutation. Input The first line contains number n (1 ≤ n ≤ 105) — the size of the permutation (also equal to the size of the sought tree). The second line contains permutation pi (1 ≤ pi ≤ n). Output If the sought tree does not exist, print "NO" (without the quotes). Otherwise, print "YES", and then print n - 1 lines, each of which contains two integers — the numbers of vertices connected by an edge of the tree you found. The vertices are numbered from 1, the order of the edges and the order of the vertices within the edges does not matter. If there are multiple solutions, output any of them. Examples Input 4 4 3 2 1 Output YES 4 1 4 2 1 3 Input 3 3 1 2 Output NO Note In the first sample test a permutation transforms edge (4, 1) into edge (1, 4), edge (4, 2) into edge (1, 3) and edge (1, 3) into edge (4, 2). These edges all appear in the resulting tree. It can be shown that in the second sample test no tree satisfies the given condition.
instruction
0
107,394
13
214,788
Tags: constructive algorithms, dfs and similar, greedy, trees Correct Solution: ``` n = int(input()) a = [0] + list(map(int, input().split())) vis = [False] * (n + 1) lp = [] for i in range(1, n + 1): if vis[i]: continue cur = i q = [] while not vis[cur]: vis[cur] = True q.append(cur) cur = a[cur] lp.append(q) lp.sort(key=len) res = [] def solve2(): res.append(tuple(lp[0])) for q in lp[1:]: if len(q) % 2 == 1: print('NO') exit() for i in range(len(q)): res.append((lp[0][i % 2], q[i])) def solve1(): for i in range(1, n + 1): if i == lp[0][0]: continue res.append((lp[0][0], i)) if len(lp[0]) >= 3: print('NO') exit() if len(lp[0]) >= 2: solve2() else: solve1() print('YES') for u, v in res: print(u, v) ```
output
1
107,394
13
214,789
Provide tags and a correct Python 3 solution for this coding contest problem. A tree of size n is an undirected connected graph consisting of n vertices without cycles. Consider some tree with n vertices. We call a tree invariant relative to permutation p = p1p2... pn, if for any two vertices of the tree u and v the condition holds: "vertices u and v are connected by an edge if and only if vertices pu and pv are connected by an edge". You are given permutation p of size n. Find some tree size n, invariant relative to the given permutation. Input The first line contains number n (1 ≤ n ≤ 105) — the size of the permutation (also equal to the size of the sought tree). The second line contains permutation pi (1 ≤ pi ≤ n). Output If the sought tree does not exist, print "NO" (without the quotes). Otherwise, print "YES", and then print n - 1 lines, each of which contains two integers — the numbers of vertices connected by an edge of the tree you found. The vertices are numbered from 1, the order of the edges and the order of the vertices within the edges does not matter. If there are multiple solutions, output any of them. Examples Input 4 4 3 2 1 Output YES 4 1 4 2 1 3 Input 3 3 1 2 Output NO Note In the first sample test a permutation transforms edge (4, 1) into edge (1, 4), edge (4, 2) into edge (1, 3) and edge (1, 3) into edge (4, 2). These edges all appear in the resulting tree. It can be shown that in the second sample test no tree satisfies the given condition.
instruction
0
107,395
13
214,790
Tags: constructive algorithms, dfs and similar, greedy, trees Correct Solution: ``` m = int(input()) L = list(map(int, input().split())) for i in range(len(L)): L[i] -= 1 temp = [] d = set() flag1 = 0 flag2 = 0 flag3 = 0 for index, num in enumerate(L): if num not in d: t = 1 d.add(num) ts = [num] while num != index: num = L[num] d.add(num) ts += num, t += 1 if t == 1: flag1 = 1 c = num break elif t == 2: flag2 = 1 c = len(temp) a = ts[0] b = ts[1] elif t % 2 == 1: flag3 = 1 temp.append(ts) if flag1 == 1: print('YES') for i in L: if not i == c: print(i + 1, c + 1) elif flag2 == 0: print('NO') else: if flag3 == 1: print('NO') else: print('YES') for index, i in enumerate(temp): if index == c: print(i[0]+1, i[1] + 1) continue for index, j in enumerate(i): if index % 2 == 0: print(a + 1, j + 1) else: print(b + 1, j + 1) ```
output
1
107,395
13
214,791
Provide tags and a correct Python 3 solution for this coding contest problem. A tree of size n is an undirected connected graph consisting of n vertices without cycles. Consider some tree with n vertices. We call a tree invariant relative to permutation p = p1p2... pn, if for any two vertices of the tree u and v the condition holds: "vertices u and v are connected by an edge if and only if vertices pu and pv are connected by an edge". You are given permutation p of size n. Find some tree size n, invariant relative to the given permutation. Input The first line contains number n (1 ≤ n ≤ 105) — the size of the permutation (also equal to the size of the sought tree). The second line contains permutation pi (1 ≤ pi ≤ n). Output If the sought tree does not exist, print "NO" (without the quotes). Otherwise, print "YES", and then print n - 1 lines, each of which contains two integers — the numbers of vertices connected by an edge of the tree you found. The vertices are numbered from 1, the order of the edges and the order of the vertices within the edges does not matter. If there are multiple solutions, output any of them. Examples Input 4 4 3 2 1 Output YES 4 1 4 2 1 3 Input 3 3 1 2 Output NO Note In the first sample test a permutation transforms edge (4, 1) into edge (1, 4), edge (4, 2) into edge (1, 3) and edge (1, 3) into edge (4, 2). These edges all appear in the resulting tree. It can be shown that in the second sample test no tree satisfies the given condition.
instruction
0
107,396
13
214,792
Tags: constructive algorithms, dfs and similar, greedy, trees Correct Solution: ``` import sys readline = sys.stdin.readline N = int(readline()) P = list(map(lambda x: int(x)-1, readline().split())) PP = [] used = set() for i in range(N): if i not in used: res = [] while i not in used: used.add(i) res.append(i) i = P[i] PP.append(res) if any(len(p) == 1 for p in PP): print('YES') for p in PP: if len(p) == 1: break d = p[0] Ans = [] for i in range(N): if i != d: Ans.append('{} {}'.format(i+1, d+1)) print('\n'.join(Ans)) elif not any(len(p) == 2 for p in PP): print('NO') elif any(len(p) & 1 for p in PP): print('NO') else: print('YES') dd = None for p in PP: if len(p) == 2: dd = p PP.remove(p) break d1, d2 = dd Ans = ['{} {}'.format(d1+1, d2+1)] for p in PP: for i in range(len(p)): if i&1: Ans.append('{} {}'.format(d1+1, p[i]+1)) else: Ans.append('{} {}'.format(d2+1, p[i]+1)) print('\n'.join(Ans)) ```
output
1
107,396
13
214,793
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A tree of size n is an undirected connected graph consisting of n vertices without cycles. Consider some tree with n vertices. We call a tree invariant relative to permutation p = p1p2... pn, if for any two vertices of the tree u and v the condition holds: "vertices u and v are connected by an edge if and only if vertices pu and pv are connected by an edge". You are given permutation p of size n. Find some tree size n, invariant relative to the given permutation. Input The first line contains number n (1 ≤ n ≤ 105) — the size of the permutation (also equal to the size of the sought tree). The second line contains permutation pi (1 ≤ pi ≤ n). Output If the sought tree does not exist, print "NO" (without the quotes). Otherwise, print "YES", and then print n - 1 lines, each of which contains two integers — the numbers of vertices connected by an edge of the tree you found. The vertices are numbered from 1, the order of the edges and the order of the vertices within the edges does not matter. If there are multiple solutions, output any of them. Examples Input 4 4 3 2 1 Output YES 4 1 4 2 1 3 Input 3 3 1 2 Output NO Note In the first sample test a permutation transforms edge (4, 1) into edge (1, 4), edge (4, 2) into edge (1, 3) and edge (1, 3) into edge (4, 2). These edges all appear in the resulting tree. It can be shown that in the second sample test no tree satisfies the given condition. Submitted Solution: ``` #Type 1: One Center #Type 2: Two Center n = int(input()) permu = list(map(int,input().split())) Type = 0 det = 0 Ind = -1 for i in range(n): if(permu[i]-1==i): det+=1 Ind=i if(det==1): Type=1 if(Type==0): for i in range(n): if(permu[permu[i]-1]-1==i): Ind=i Type=2 if(det>2): Type=0 if(Type==0): print("NO") elif(Type==1): print("YES") for i in range(n): if(i!=Ind): print(str(i+1)+' '+str(Ind+1)) elif(n%2==1): print("NO") else: print("YES") a,b=Ind,permu[Ind]-1 if(a==b): for i in range(n): if(permu[i]-1==i and i!=a): b=i t=1 for i in range(n): if(i!=a and i!=b): if(t>0): print(str(i+1)+' '+str(a+1)) t=-t else: print(str(i+1)+' '+str(b+1)) t=-t print(str(a+1)+' '+str(b+1)) ```
instruction
0
107,397
13
214,794
No
output
1
107,397
13
214,795
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A tree of size n is an undirected connected graph consisting of n vertices without cycles. Consider some tree with n vertices. We call a tree invariant relative to permutation p = p1p2... pn, if for any two vertices of the tree u and v the condition holds: "vertices u and v are connected by an edge if and only if vertices pu and pv are connected by an edge". You are given permutation p of size n. Find some tree size n, invariant relative to the given permutation. Input The first line contains number n (1 ≤ n ≤ 105) — the size of the permutation (also equal to the size of the sought tree). The second line contains permutation pi (1 ≤ pi ≤ n). Output If the sought tree does not exist, print "NO" (without the quotes). Otherwise, print "YES", and then print n - 1 lines, each of which contains two integers — the numbers of vertices connected by an edge of the tree you found. The vertices are numbered from 1, the order of the edges and the order of the vertices within the edges does not matter. If there are multiple solutions, output any of them. Examples Input 4 4 3 2 1 Output YES 4 1 4 2 1 3 Input 3 3 1 2 Output NO Note In the first sample test a permutation transforms edge (4, 1) into edge (1, 4), edge (4, 2) into edge (1, 3) and edge (1, 3) into edge (4, 2). These edges all appear in the resulting tree. It can be shown that in the second sample test no tree satisfies the given condition. Submitted Solution: ``` n=int(input()) t=["#"]+input().split() b=False v=0 for i in range(1,len(t)): if i==int(t[i]): b=True v=i if b: print("YES") for i in range(1,len(t)): if i!=v:print(v,i) else: bb=False x=0 y=0 for i in range(1,len(t)): if int(t[int(t[i])])==i: bb=True x=i y=int(t[i]) if bb: print("YES") print(x,y) for i in range(1,len(t)): if i!=x and i!=y: print(i,x) print(i,y) else: print("NO") ```
instruction
0
107,398
13
214,796
No
output
1
107,398
13
214,797
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A tree of size n is an undirected connected graph consisting of n vertices without cycles. Consider some tree with n vertices. We call a tree invariant relative to permutation p = p1p2... pn, if for any two vertices of the tree u and v the condition holds: "vertices u and v are connected by an edge if and only if vertices pu and pv are connected by an edge". You are given permutation p of size n. Find some tree size n, invariant relative to the given permutation. Input The first line contains number n (1 ≤ n ≤ 105) — the size of the permutation (also equal to the size of the sought tree). The second line contains permutation pi (1 ≤ pi ≤ n). Output If the sought tree does not exist, print "NO" (without the quotes). Otherwise, print "YES", and then print n - 1 lines, each of which contains two integers — the numbers of vertices connected by an edge of the tree you found. The vertices are numbered from 1, the order of the edges and the order of the vertices within the edges does not matter. If there are multiple solutions, output any of them. Examples Input 4 4 3 2 1 Output YES 4 1 4 2 1 3 Input 3 3 1 2 Output NO Note In the first sample test a permutation transforms edge (4, 1) into edge (1, 4), edge (4, 2) into edge (1, 3) and edge (1, 3) into edge (4, 2). These edges all appear in the resulting tree. It can be shown that in the second sample test no tree satisfies the given condition. Submitted Solution: ``` import sys #sys.stdin = open("in.txt") try: while True: n = int(input()) val = [] pos = [0] + list(map(int, input().split(" "))) flag = True for i in range(1, n+1, 1): if i != pos[pos[i]] and pos[pos[i]] != -1: flag = False elif pos[i] != -1: val.append([i, pos[i]]) if not flag: print("NO") continue tmp1 = val[0][0] tmp2 = val[0][1] print("YES") print(str(val[0][0])+" "+str(val[0][1])) for i in range(1, n//2, 1): print(str(tmp1)+" "+str(val[i][0])) print(str(tmp2)+" "+str(val[i][1])) tmp1 = val[i][0] tmp2 = val[i][1] except EOFError: pass ```
instruction
0
107,399
13
214,798
No
output
1
107,399
13
214,799
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A tree of size n is an undirected connected graph consisting of n vertices without cycles. Consider some tree with n vertices. We call a tree invariant relative to permutation p = p1p2... pn, if for any two vertices of the tree u and v the condition holds: "vertices u and v are connected by an edge if and only if vertices pu and pv are connected by an edge". You are given permutation p of size n. Find some tree size n, invariant relative to the given permutation. Input The first line contains number n (1 ≤ n ≤ 105) — the size of the permutation (also equal to the size of the sought tree). The second line contains permutation pi (1 ≤ pi ≤ n). Output If the sought tree does not exist, print "NO" (without the quotes). Otherwise, print "YES", and then print n - 1 lines, each of which contains two integers — the numbers of vertices connected by an edge of the tree you found. The vertices are numbered from 1, the order of the edges and the order of the vertices within the edges does not matter. If there are multiple solutions, output any of them. Examples Input 4 4 3 2 1 Output YES 4 1 4 2 1 3 Input 3 3 1 2 Output NO Note In the first sample test a permutation transforms edge (4, 1) into edge (1, 4), edge (4, 2) into edge (1, 3) and edge (1, 3) into edge (4, 2). These edges all appear in the resulting tree. It can be shown that in the second sample test no tree satisfies the given condition. Submitted Solution: ``` n = int(input()) permu = list(map(int,input().split())) Type = 0 det = 0 Ind = -1 for i in range(n): if(permu[i]-1==i): det+=1 Ind=i if(det==1): Type=1 if(Type==0): for i in range(n): if(permu[permu[i]-1]-1==i): Ind=i Type=2 if(Type==0): print("NO") elif(Type==1): print("YES") for i in range(n): if(i!=Ind): print(str(i+1)+' '+str(Ind+1)) elif(n%2==1): print("NO") else: print("YES") a,b=Ind,permu[Ind]-1 t=1 for i in range(n): if(i!=a and i!=b): if(t>0): print(str(i+1)+' '+str(a+1)) t=-t else: print(str(i+1)+' '+str(b+1)) t=-t print(str(a+1)+' '+str(b+1)) ```
instruction
0
107,400
13
214,800
No
output
1
107,400
13
214,801
Provide a correct Python 3 solution for this coding contest problem. There is a tree with N vertices, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N - 1), the i-th edge connects Vertex x_i and y_i. Taro has decided to paint each vertex in white or black, so that any black vertex can be reached from any other black vertex by passing through only black vertices. You are given a positive integer M. For each v (1 \leq v \leq N), answer the following question: * Assuming that Vertex v has to be black, find the number of ways in which the vertices can be painted, modulo M. Constraints * All values in input are integers. * 1 \leq N \leq 10^5 * 2 \leq M \leq 10^9 * 1 \leq x_i, y_i \leq N * The given graph is a tree. Input Input is given from Standard Input in the following format: N M x_1 y_1 x_2 y_2 : x_{N - 1} y_{N - 1} Output Print N lines. The v-th (1 \leq v \leq N) line should contain the answer to the following question: * Assuming that Vertex v has to be black, find the number of ways in which the vertices can be painted, modulo M. Examples Input 3 100 1 2 2 3 Output 3 4 3 Input 4 100 1 2 1 3 1 4 Output 8 5 5 5 Input 1 100 Output 1 Input 10 2 8 5 10 8 6 5 1 5 4 8 2 10 3 6 9 2 1 7 Output 0 0 1 1 1 0 1 0 1 1
instruction
0
107,686
13
215,372
"Correct Solution: ``` def main(): import sys input = sys.stdin.readline N, mod = map(int, input().split()) adj = [[] for _ in range(N+1)] for _ in range(N-1): a, b = map(int, input().split()) adj[a].append(b) adj[b].append(a) """ここから変更""" # op: 普通に根を固定した木DPで子の情報を親に集めるときの演算 def op(a, b): return (a * (b + 1))%mod # cum_merge: 累積opどうしをマージするときの演算 def cum_merge(a, b): return (a * b)%mod """ここまで変更""" # root=1でまず普通に木DPをする # 並行して各頂点につき、子の値の累積opを左右から求めておく # その後根から順番に、親からの寄与を求めていく(from_par) def Rerooting(adj): N = len(adj) - 1 st = [1] seen = [0] * (N + 1) seen[1] = 1 par = [0] * (N + 1) child = [[] for _ in range(N + 1)] seq = [] while st: v = st.pop() seq.append(v) for u in adj[v]: if not seen[u]: seen[u] = 1 par[u] = v child[v].append(u) st.append(u) seq.reverse() dp = [1] * (N + 1) left = [1] * (N + 1) right = [1] * (N + 1) for v in seq: tmp = 1 for u in child[v]: left[u] = tmp tmp = op(tmp, dp[u]) tmp = 1 for u in reversed(child[v]): right[u] = tmp tmp = op(tmp, dp[u]) dp[v] = tmp seq.reverse() from_par = [0] * (N + 1) for v in seq: if v == 1: continue from_par[v] = op(cum_merge(left[v], right[v]), from_par[par[v]]) dp[v] = op(dp[v], from_par[v]) return dp dp = Rerooting(adj) for i in range(1, N+1): print(dp[i]) if __name__ == '__main__': main() ```
output
1
107,686
13
215,373
Provide a correct Python 3 solution for this coding contest problem. There is a tree with N vertices, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N - 1), the i-th edge connects Vertex x_i and y_i. Taro has decided to paint each vertex in white or black, so that any black vertex can be reached from any other black vertex by passing through only black vertices. You are given a positive integer M. For each v (1 \leq v \leq N), answer the following question: * Assuming that Vertex v has to be black, find the number of ways in which the vertices can be painted, modulo M. Constraints * All values in input are integers. * 1 \leq N \leq 10^5 * 2 \leq M \leq 10^9 * 1 \leq x_i, y_i \leq N * The given graph is a tree. Input Input is given from Standard Input in the following format: N M x_1 y_1 x_2 y_2 : x_{N - 1} y_{N - 1} Output Print N lines. The v-th (1 \leq v \leq N) line should contain the answer to the following question: * Assuming that Vertex v has to be black, find the number of ways in which the vertices can be painted, modulo M. Examples Input 3 100 1 2 2 3 Output 3 4 3 Input 4 100 1 2 1 3 1 4 Output 8 5 5 5 Input 1 100 Output 1 Input 10 2 8 5 10 8 6 5 1 5 4 8 2 10 3 6 9 2 1 7 Output 0 0 1 1 1 0 1 0 1 1
instruction
0
107,687
13
215,374
"Correct Solution: ``` from collections import defaultdict, deque, Counter from heapq import heappush, heappop, heapify from bisect import bisect_right, bisect_left import random from itertools import permutations, accumulate, combinations, product import sys import string from bisect import bisect_left, bisect_right from math import factorial, ceil, floor, gamma, log from operator import mul from functools import reduce from copy import deepcopy sys.setrecursionlimit(2147483647) INF = 10 ** 20 def LI(): return list(map(int, sys.stdin.readline().split())) def I(): return int(sys.stdin.readline()) def LS(): return sys.stdin.buffer.readline().rstrip().split() def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8') def IR(n): return [I() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): pass def LSR(n): return [LS() for i in range(n)] def SRL(n): return [list(S()) for i in range(n)] def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)] n, mod = LI() G = [[] for _ in range(n)] for _ in range(n - 1): a, b = LI() G[a - 1] += [b - 1] G[b - 1] += [a - 1] dp = [-1] * n par = [-1] * n def f(u): ret = 1 for v in G[u]: if v == par[u]: continue par[v] = u ret = ret * f(v) dp[u] = ret if u: return ret + 1 f(0) ans = [0] * n ans[0] = dp[0] par_acc = [1] * n dp2 = [1] * n q = deque([0]) while q: u = q.pop() ans[u] = dp[u] * dp2[u] % mod L1 = [1] L2 = [] D = {} cnt = 0 for v in G[u]: if par[u] == v: continue L1 += [dp[v] + 1] L2 += [dp[v] + 1] D[cnt] = v q += [v] cnt += 1 L2 += [1] if len(L1) > 2: for i in range(len(L1) - 2): L1[i + 2] = L1[i + 2] * L1[i + 1] % mod L2[-i - 3] = L2[-i - 3] * L2[-i - 2] % mod for j in D: dp2[D[j]] = (L1[j] * L2[j + 1] * dp2[u] + 1) % mod print(*ans, sep="\n") ```
output
1
107,687
13
215,375
Provide a correct Python 3 solution for this coding contest problem. There is a tree with N vertices, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N - 1), the i-th edge connects Vertex x_i and y_i. Taro has decided to paint each vertex in white or black, so that any black vertex can be reached from any other black vertex by passing through only black vertices. You are given a positive integer M. For each v (1 \leq v \leq N), answer the following question: * Assuming that Vertex v has to be black, find the number of ways in which the vertices can be painted, modulo M. Constraints * All values in input are integers. * 1 \leq N \leq 10^5 * 2 \leq M \leq 10^9 * 1 \leq x_i, y_i \leq N * The given graph is a tree. Input Input is given from Standard Input in the following format: N M x_1 y_1 x_2 y_2 : x_{N - 1} y_{N - 1} Output Print N lines. The v-th (1 \leq v \leq N) line should contain the answer to the following question: * Assuming that Vertex v has to be black, find the number of ways in which the vertices can be painted, modulo M. Examples Input 3 100 1 2 2 3 Output 3 4 3 Input 4 100 1 2 1 3 1 4 Output 8 5 5 5 Input 1 100 Output 1 Input 10 2 8 5 10 8 6 5 1 5 4 8 2 10 3 6 9 2 1 7 Output 0 0 1 1 1 0 1 0 1 1
instruction
0
107,688
13
215,376
"Correct Solution: ``` import sys sys.setrecursionlimit(2147483647) INF=float("inf") MOD=10**9+7 # 998244353 input=lambda:sys.stdin.readline().rstrip() def resolve(): n, MOD = map(int,input().split()) E = [[] for _ in range(n)] nv_to_idx = [{} for _ in range(n)] for _ in range(n - 1): u, v = map(int,input().split()) u -= 1; v -= 1 nv_to_idx[u][v] = len(E[u]) nv_to_idx[v][u] = len(E[v]) E[u].append(v) E[v].append(u) # tree DP dp = [1] * n deg = [len(E[v]) - 1 for v in range(n)] deg[0] += 1 stack = [(0, -1)] while stack: v, p = stack.pop() if v >= 0: for nv in E[v]: if nv == p: continue stack.append((~nv, v)) stack.append((nv, v)) else: if deg[~v] == 0: dp[p] = dp[p] * (dp[~v] + 1) % MOD deg[p] -= 1 # rerooting def rerooting(v, p): # p-rooted -> dp[p] を v-rooted に modify if p != -1: idx = nv_to_idx[p][v] dp[p] = cum_left[p][idx] * cum_right[p][idx + 1] % MOD # monoid に対する cumulative sum を計算 if cum_left[v] is None: k = len(E[v]) left = [1] * (k + 1) right = [1] * (k + 1) for i in range(k): left[i + 1] = left[i] * (dp[E[v][i]] + 1) % MOD for i in range(k - 1, -1, -1): right[i] = right[i + 1] * (dp[E[v][i]] + 1) % MOD cum_left[v] = left cum_right[v] = right # dp[v] を v-rooted に modify dp[v] = cum_left[v][-1] ans = [None] * n stack = [(0, -1)] cum_left = [None] * n cum_right = [None] * n while stack: v, p = stack.pop() if v >= 0: rerooting(v, p) ans[v] = dp[v] for nv in E[v]: if nv == p: continue stack.append((~nv, v)) stack.append((nv, v)) else: rerooting(p, ~v) print(*ans, sep = '\n') resolve() ```
output
1
107,688
13
215,377
Provide a correct Python 3 solution for this coding contest problem. There is a tree with N vertices, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N - 1), the i-th edge connects Vertex x_i and y_i. Taro has decided to paint each vertex in white or black, so that any black vertex can be reached from any other black vertex by passing through only black vertices. You are given a positive integer M. For each v (1 \leq v \leq N), answer the following question: * Assuming that Vertex v has to be black, find the number of ways in which the vertices can be painted, modulo M. Constraints * All values in input are integers. * 1 \leq N \leq 10^5 * 2 \leq M \leq 10^9 * 1 \leq x_i, y_i \leq N * The given graph is a tree. Input Input is given from Standard Input in the following format: N M x_1 y_1 x_2 y_2 : x_{N - 1} y_{N - 1} Output Print N lines. The v-th (1 \leq v \leq N) line should contain the answer to the following question: * Assuming that Vertex v has to be black, find the number of ways in which the vertices can be painted, modulo M. Examples Input 3 100 1 2 2 3 Output 3 4 3 Input 4 100 1 2 1 3 1 4 Output 8 5 5 5 Input 1 100 Output 1 Input 10 2 8 5 10 8 6 5 1 5 4 8 2 10 3 6 9 2 1 7 Output 0 0 1 1 1 0 1 0 1 1
instruction
0
107,689
13
215,378
"Correct Solution: ``` # -*- coding: utf-8 -*- ############# # Libraries # ############# import sys input = sys.stdin.readline import math #from math import gcd import bisect import heapq from collections import defaultdict from collections import deque from collections import Counter from functools import lru_cache ############# # Constants # ############# INF = float('inf') AZ = "abcdefghijklmnopqrstuvwxyz" ############# # Functions # ############# ######INPUT###### def I(): return int(input().strip()) def S(): return input().strip() def IL(): return list(map(int,input().split())) def SL(): return list(map(str,input().split())) def ILs(n): return list(int(input()) for _ in range(n)) def SLs(n): return list(input().strip() for _ in range(n)) def ILL(n): return [list(map(int, input().split())) for _ in range(n)] def SLL(n): return [list(map(str, input().split())) for _ in range(n)] N,MOD = IL() #####Shorten##### def DD(arg): return defaultdict(arg) #####Inverse##### def inv(n): return pow(n, MOD-2, MOD) ######Combination###### kaijo_memo = [] def kaijo(n): if(len(kaijo_memo) > n): return kaijo_memo[n] if(len(kaijo_memo) == 0): kaijo_memo.append(1) while(len(kaijo_memo) <= n): kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD) return kaijo_memo[n] gyaku_kaijo_memo = [] def gyaku_kaijo(n): if(len(gyaku_kaijo_memo) > n): return gyaku_kaijo_memo[n] if(len(gyaku_kaijo_memo) == 0): gyaku_kaijo_memo.append(1) while(len(gyaku_kaijo_memo) <= n): gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD) return gyaku_kaijo_memo[n] def nCr(n,r): if n == r: return 1 if n < r or r < 0: return 0 ret = 1 ret = ret * kaijo(n) % MOD ret = ret * gyaku_kaijo(r) % MOD ret = ret * gyaku_kaijo(n-r) % MOD return ret ######Factorization###### def factorization(n): arr = [] temp = n for i in range(2, int(-(-n**0.5//1))+1): if temp%i==0: cnt=0 while temp%i==0: cnt+=1 temp //= i arr.append([i, cnt]) if temp!=1: arr.append([temp, 1]) if arr==[]: arr.append([n, 1]) return arr #####MakeDivisors###### def make_divisors(n): divisors = [] for i in range(1, int(n**0.5)+1): if n % i == 0: divisors.append(i) if i != n // i: divisors.append(n//i) return divisors #####MakePrimes###### def make_primes(N): max = int(math.sqrt(N)) seachList = [i for i in range(2,N+1)] primeNum = [] while seachList[0] <= max: primeNum.append(seachList[0]) tmp = seachList[0] seachList = [i for i in seachList if i % tmp != 0] primeNum.extend(seachList) return primeNum #####GCD##### def gcd(a, b): while b: a, b = b, a % b return a #####LCM##### def lcm(a, b): return a * b // gcd (a, b) #####BitCount##### def count_bit(n): count = 0 while n: n &= n-1 count += 1 return count #####ChangeBase##### def base_10_to_n(X, n): if X//n: return base_10_to_n(X//n, n)+[X%n] return [X%n] def base_n_to_10(X, n): return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X)))) def base_10_to_n_without_0(X, n): X -= 1 if X//n: return base_10_to_n_without_0(X//n, n)+[X%n] return [X%n] #####IntLog##### def int_log(n, a): count = 0 while n>=a: n //= a count += 1 return count ############# # Main Code # ############# order = [] d = [INF] * N def bfs(graph, start): N = len(graph) d[start] = 0 q = deque([start]) while q: u = q.popleft() order.append(u) for v in graph[u]: if d[v] != INF: continue d[v] = d[u] + 1 q.append(v) graph = [[] for i in range(N)] for _ in range(N-1): a,b = IL() graph[a-1].append(b-1) graph[b-1].append(a-1) bfs(graph, 0) dp = [0 for i in range(N)] cum = [None for i in range(N)] for x in order[::-1]: cum1 = [1] for y in graph[x]: if d[x] < d[y]: cum1.append((cum1[-1]*(dp[y]+1))%MOD) cum2 = [1] for y in graph[x][::-1]: if d[x] < d[y]: cum2.append((cum2[-1]*(dp[y]+1))%MOD) dp[x] = cum1[-1] cum[x] = [cum1,cum2] factor = [1 for i in range(N)] for x in order: i = 0 for y in graph[x]: if d[x] < d[y]: dpinved = cum[x][0][i] * cum[x][1][len(cum[x][0])-2-i] factor[y] = (dpinved*factor[x]+1)%MOD i += 1 for i in range(N): print((dp[i]*factor[i])%MOD) ```
output
1
107,689
13
215,379
Provide a correct Python 3 solution for this coding contest problem. There is a tree with N vertices, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N - 1), the i-th edge connects Vertex x_i and y_i. Taro has decided to paint each vertex in white or black, so that any black vertex can be reached from any other black vertex by passing through only black vertices. You are given a positive integer M. For each v (1 \leq v \leq N), answer the following question: * Assuming that Vertex v has to be black, find the number of ways in which the vertices can be painted, modulo M. Constraints * All values in input are integers. * 1 \leq N \leq 10^5 * 2 \leq M \leq 10^9 * 1 \leq x_i, y_i \leq N * The given graph is a tree. Input Input is given from Standard Input in the following format: N M x_1 y_1 x_2 y_2 : x_{N - 1} y_{N - 1} Output Print N lines. The v-th (1 \leq v \leq N) line should contain the answer to the following question: * Assuming that Vertex v has to be black, find the number of ways in which the vertices can be painted, modulo M. Examples Input 3 100 1 2 2 3 Output 3 4 3 Input 4 100 1 2 1 3 1 4 Output 8 5 5 5 Input 1 100 Output 1 Input 10 2 8 5 10 8 6 5 1 5 4 8 2 10 3 6 9 2 1 7 Output 0 0 1 1 1 0 1 0 1 1
instruction
0
107,690
13
215,380
"Correct Solution: ``` import sys sys.setrecursionlimit(10**9) input = sys.stdin.readline n, mod = map(int, input().split()) graph = [[] for _ in range(n)] for _ in range(n - 1): a, b = map(int, input().split()) a -= 1 b -= 1 graph[a].append(b) graph[b].append(a) ele_id = 1 # dp[v][i] = (頂点vから出るi番目の有向辺に関する部分木のDPの値) # dp[v][i] = (c, m) # c = 頂点vから出るi番目の有向辺に関する部分木の塗り方の数 # m = 頂点vから出るi番目の有向辺に関する部分木の頂点数 dp = [[ele_id] * len(graph[i]) for i in range(n)] ans = [ele_id] * n add_func = lambda x: x + 1 merge_func = lambda a, b: a * b % mod def dfs1(v, v_p): dp_cum = ele_id for i, v_next in enumerate(graph[v]): if v_next == v_p: continue dp[v][i] = dfs1(v_next, v) dp_cum = merge_func(dp_cum, dp[v][i]) return add_func(dp_cum) def dfs2(v, v_p, dp_vp): for i, v_next in enumerate(graph[v]): if v_next == v_p: dp[v][i] = dp_vp dp_l = [ele_id] * (len(graph[v]) + 1) dp_r = [ele_id] * (len(graph[v]) + 1) for i in range(len(graph[v])): dp_l[i + 1] = merge_func(dp_l[i], dp[v][i]) for i in reversed(range(len(graph[v]))): dp_r[i] = merge_func(dp_r[i + 1], dp[v][i]) ans[v] = add_func(dp_l[-1]) for i, v_next in enumerate(graph[v]): if v_next == v_p: continue dfs2(v_next, v, add_func(merge_func(dp_l[i], dp_r[i + 1]))) dfs1(0, -1) # print(*dp, sep='\n') dfs2(0, -1, ele_id) for ans_i in ans: print(ans_i - 1) ```
output
1
107,690
13
215,381
Provide a correct Python 3 solution for this coding contest problem. There is a tree with N vertices, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N - 1), the i-th edge connects Vertex x_i and y_i. Taro has decided to paint each vertex in white or black, so that any black vertex can be reached from any other black vertex by passing through only black vertices. You are given a positive integer M. For each v (1 \leq v \leq N), answer the following question: * Assuming that Vertex v has to be black, find the number of ways in which the vertices can be painted, modulo M. Constraints * All values in input are integers. * 1 \leq N \leq 10^5 * 2 \leq M \leq 10^9 * 1 \leq x_i, y_i \leq N * The given graph is a tree. Input Input is given from Standard Input in the following format: N M x_1 y_1 x_2 y_2 : x_{N - 1} y_{N - 1} Output Print N lines. The v-th (1 \leq v \leq N) line should contain the answer to the following question: * Assuming that Vertex v has to be black, find the number of ways in which the vertices can be painted, modulo M. Examples Input 3 100 1 2 2 3 Output 3 4 3 Input 4 100 1 2 1 3 1 4 Output 8 5 5 5 Input 1 100 Output 1 Input 10 2 8 5 10 8 6 5 1 5 4 8 2 10 3 6 9 2 1 7 Output 0 0 1 1 1 0 1 0 1 1
instruction
0
107,691
13
215,382
"Correct Solution: ``` from collections import deque N, M = map(int, input().split()) d = [list() for _ in range(N+1)] for _ in range(N-1): a, b = map(int, input().split()) d[a].append(b) d[b].append(a) queue=deque([1]) vs = set([1]) vs_bfs = list() parents = [0] * (N+1) while queue: v = queue.popleft() vs_bfs.append(v) for u in d[v]: if u in vs: continue parents[u] = v vs.add(u) queue.append(u) dp = [0 for _ in range(N+1)] dpl = [1 for _ in range(N+1)] dpr = [1 for _ in range(N+1)] for v in vs_bfs[::-1]: t = 1 for u in d[v]: if u == parents[v]: continue dpl[u] = t t = (t * (dp[u]+1)) % M t = 1 for u in d[v][::-1]: if u == parents[v]: continue dpr[u] = t t = (t * (dp[u]+1)) % M dp[v] = t dp2 = [0]*(N+1) for v in vs_bfs: if v == 1: dp2[v] = 1 continue p = parents[v] dp2[v] = (dp2[p] * (dpl[v]*dpr[v]) +1)% M # print(dp2[p] * (dpl[v]*dpr[v])) # dp2[v] = dp2[p] %M r = [0]*N for i in range(1, N+1): r[i-1] = str(dp[i] * dp2[i] % M) print("\n".join(r)) ```
output
1
107,691
13
215,383