message
stringlengths
2
65.1k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
0
108k
cluster
float64
14
14
__index_level_0__
int64
0
217k
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Kolya loves putting gnomes at the circle table and giving them coins, and Tanya loves studying triplets of gnomes, sitting in the vertexes of an equilateral triangle. More formally, there are 3n gnomes sitting in a circle. Each gnome can have from 1 to 3 coins. Let's number the places in the order they occur in the circle by numbers from 0 to 3n - 1, let the gnome sitting on the i-th place have ai coins. If there is an integer i (0 ≀ i < n) such that ai + ai + n + ai + 2n β‰  6, then Tanya is satisfied. Count the number of ways to choose ai so that Tanya is satisfied. As there can be many ways of distributing coins, print the remainder of this number modulo 109 + 7. Two ways, a and b, are considered distinct if there is index i (0 ≀ i < 3n), such that ai β‰  bi (that is, some gnome got different number of coins in these two ways). Input A single line contains number n (1 ≀ n ≀ 105) β€” the number of the gnomes divided by three. Output Print a single number β€” the remainder of the number of variants of distributing coins that satisfy Tanya modulo 109 + 7. Examples Input 1 Output 20 Input 2 Output 680 Note 20 ways for n = 1 (gnome with index 0 sits on the top of the triangle, gnome 1 on the right vertex, gnome 2 on the left vertex): <image> Submitted Solution: ``` from math import factorial n = int(input()) if n==1: print(20) quit() print(int(factorial(3*3*n-1)/(factorial(3)*factorial(3*3*n-4)))) ```
instruction
0
99,842
14
199,684
No
output
1
99,842
14
199,685
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Kolya loves putting gnomes at the circle table and giving them coins, and Tanya loves studying triplets of gnomes, sitting in the vertexes of an equilateral triangle. More formally, there are 3n gnomes sitting in a circle. Each gnome can have from 1 to 3 coins. Let's number the places in the order they occur in the circle by numbers from 0 to 3n - 1, let the gnome sitting on the i-th place have ai coins. If there is an integer i (0 ≀ i < n) such that ai + ai + n + ai + 2n β‰  6, then Tanya is satisfied. Count the number of ways to choose ai so that Tanya is satisfied. As there can be many ways of distributing coins, print the remainder of this number modulo 109 + 7. Two ways, a and b, are considered distinct if there is index i (0 ≀ i < 3n), such that ai β‰  bi (that is, some gnome got different number of coins in these two ways). Input A single line contains number n (1 ≀ n ≀ 105) β€” the number of the gnomes divided by three. Output Print a single number β€” the remainder of the number of variants of distributing coins that satisfy Tanya modulo 109 + 7. Examples Input 1 Output 20 Input 2 Output 680 Note 20 ways for n = 1 (gnome with index 0 sits on the top of the triangle, gnome 1 on the right vertex, gnome 2 on the left vertex): <image> Submitted Solution: ``` n = int(input()) res = (3**(3*n) - 7**(n))%(10**9-7) print (res) ```
instruction
0
99,843
14
199,686
No
output
1
99,843
14
199,687
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Kolya loves putting gnomes at the circle table and giving them coins, and Tanya loves studying triplets of gnomes, sitting in the vertexes of an equilateral triangle. More formally, there are 3n gnomes sitting in a circle. Each gnome can have from 1 to 3 coins. Let's number the places in the order they occur in the circle by numbers from 0 to 3n - 1, let the gnome sitting on the i-th place have ai coins. If there is an integer i (0 ≀ i < n) such that ai + ai + n + ai + 2n β‰  6, then Tanya is satisfied. Count the number of ways to choose ai so that Tanya is satisfied. As there can be many ways of distributing coins, print the remainder of this number modulo 109 + 7. Two ways, a and b, are considered distinct if there is index i (0 ≀ i < 3n), such that ai β‰  bi (that is, some gnome got different number of coins in these two ways). Input A single line contains number n (1 ≀ n ≀ 105) β€” the number of the gnomes divided by three. Output Print a single number β€” the remainder of the number of variants of distributing coins that satisfy Tanya modulo 109 + 7. Examples Input 1 Output 20 Input 2 Output 680 Note 20 ways for n = 1 (gnome with index 0 sits on the top of the triangle, gnome 1 on the right vertex, gnome 2 on the left vertex): <image> Submitted Solution: ``` mod = 10000007 def modpow(a,b): if(b == 0): return(1) elif(b % 2 == 1): return((a * modpow(a,b-1) % mod)) else: tmp = modpow(a,b/2) return ((tmp*tmp) % mod) n = int(input()) a = modpow(27,n) b = modpow(7,n) print(a-b) ```
instruction
0
99,844
14
199,688
No
output
1
99,844
14
199,689
Provide tags and a correct Python 3 solution for this coding contest problem. Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures). There are n members, numbered 1 through n. m pairs of members are friends. Of course, a member can't be a friend with themselves. Let A-B denote that members A and B are friends. Limak thinks that a network is reasonable if and only if the following condition is satisfied: For every three distinct members (X, Y, Z), if X-Y and Y-Z then also X-Z. For example: if Alan and Bob are friends, and Bob and Ciri are friends, then Alan and Ciri should be friends as well. Can you help Limak and check if the network is reasonable? Print "YES" or "NO" accordingly, without the quotes. Input The first line of the input contain two integers n and m (3 ≀ n ≀ 150 000, <image>) β€” the number of members and the number of pairs of members that are friends. The i-th of the next m lines contains two distinct integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi). Members ai and bi are friends with each other. No pair of members will appear more than once in the input. Output If the given network is reasonable, print "YES" in a single line (without the quotes). Otherwise, print "NO" in a single line (without the quotes). Examples Input 4 3 1 3 3 4 1 4 Output YES Input 4 4 3 1 2 3 3 4 1 2 Output NO Input 10 4 4 3 5 10 8 9 1 2 Output YES Input 3 2 1 2 2 3 Output NO Note The drawings below show the situation in the first sample (on the left) and in the second sample (on the right). Each edge represents two members that are friends. The answer is "NO" in the second sample because members (2, 3) are friends and members (3, 4) are friends, while members (2, 4) are not. <image>
instruction
0
99,961
14
199,922
Tags: dfs and similar, dsu, graphs Correct Solution: ``` def sum_num(num): num = num-1 sum = 0 while num>0: sum+=num num = num-1 return sum class UnionFindSet(object): def __init__(self, data_list): self.father_dict = {} self.size_dict = {} self.m_num = {} for node in data_list: self.father_dict[node] = node self.size_dict[node] = 1 self.m_num[node] = 0 def find_head(self, node): father = self.father_dict[node] if(node != father): father = self.find_head(father) self.father_dict[node] = father return father def is_same_set(self, node_a, node_b): return self.find_head(node_a) == self.find_head(node_b) def union(self, node_a, node_b): if node_a is None or node_b is None: return a_head = self.find_head(node_a) b_head = self.find_head(node_b) if(a_head != b_head): a_set_size = self.size_dict[a_head] b_set_size = self.size_dict[b_head] a_m = self.m_num[a_head] b_m = self.m_num[b_head] if(a_set_size >= b_set_size): self.father_dict[b_head] = a_head self.size_dict[a_head] = a_set_size + b_set_size self.m_num[a_head] = a_m+b_m+1 self.m_num.pop(b_head) self.size_dict.pop(b_head) else: self.father_dict[a_head] = b_head self.size_dict[b_head] = a_set_size + b_set_size self.m_num[b_head] =a_m+b_m+1 self.m_num.pop(a_head) self.size_dict.pop(a_head) else: self.m_num[a_head]+=1 first_line = input() first_line = first_line.split(" ") first_line = [int(i) for i in first_line] m_1 = first_line[1] list_1 = [] list_item = [] for i in range(m_1): line = input() line = line.split(" ") line = [int(i) for i in line] list_1 += line list_item.append(line) flag = False d_set = set(list_1) union_find_set = UnionFindSet(list(d_set)) for item in list_item: union_find_set.union(item[0], item[1]) m_d=union_find_set.m_num s_d=union_find_set.size_dict for key in m_d: sum = sum_num(s_d[key]) if m_d[key] != sum: flag=True break if flag: print("NO") else: print("YES") ```
output
1
99,961
14
199,923
Provide tags and a correct Python 3 solution for this coding contest problem. Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures). There are n members, numbered 1 through n. m pairs of members are friends. Of course, a member can't be a friend with themselves. Let A-B denote that members A and B are friends. Limak thinks that a network is reasonable if and only if the following condition is satisfied: For every three distinct members (X, Y, Z), if X-Y and Y-Z then also X-Z. For example: if Alan and Bob are friends, and Bob and Ciri are friends, then Alan and Ciri should be friends as well. Can you help Limak and check if the network is reasonable? Print "YES" or "NO" accordingly, without the quotes. Input The first line of the input contain two integers n and m (3 ≀ n ≀ 150 000, <image>) β€” the number of members and the number of pairs of members that are friends. The i-th of the next m lines contains two distinct integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi). Members ai and bi are friends with each other. No pair of members will appear more than once in the input. Output If the given network is reasonable, print "YES" in a single line (without the quotes). Otherwise, print "NO" in a single line (without the quotes). Examples Input 4 3 1 3 3 4 1 4 Output YES Input 4 4 3 1 2 3 3 4 1 2 Output NO Input 10 4 4 3 5 10 8 9 1 2 Output YES Input 3 2 1 2 2 3 Output NO Note The drawings below show the situation in the first sample (on the left) and in the second sample (on the right). Each edge represents two members that are friends. The answer is "NO" in the second sample because members (2, 3) are friends and members (3, 4) are friends, while members (2, 4) are not. <image>
instruction
0
99,962
14
199,924
Tags: dfs and similar, dsu, graphs Correct Solution: ``` from collections import deque def bfs(start): res = [] queue = deque([start]) while queue: vertex = queue.pop() if not vis[vertex]: vis[vertex] = 1 res.append(vertex) for i in s[vertex]: if not vis[i]: queue.append(i) return res n, m = [int(i) for i in input().split()] s = [[] for i in range(n)] for i in range(m): a, b = [int(i) for i in input().split()] s[a-1].append(b-1) s[b-1].append(a-1) vis = [0 for i in range(n)] r = 0 for i in range(n): if not vis[i]: d = bfs(i) for j in d: if len(s[j]) != len(d)-1: r = 1 print("NO") break if r: break else: print("YES") ```
output
1
99,962
14
199,925
Provide tags and a correct Python 3 solution for this coding contest problem. Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures). There are n members, numbered 1 through n. m pairs of members are friends. Of course, a member can't be a friend with themselves. Let A-B denote that members A and B are friends. Limak thinks that a network is reasonable if and only if the following condition is satisfied: For every three distinct members (X, Y, Z), if X-Y and Y-Z then also X-Z. For example: if Alan and Bob are friends, and Bob and Ciri are friends, then Alan and Ciri should be friends as well. Can you help Limak and check if the network is reasonable? Print "YES" or "NO" accordingly, without the quotes. Input The first line of the input contain two integers n and m (3 ≀ n ≀ 150 000, <image>) β€” the number of members and the number of pairs of members that are friends. The i-th of the next m lines contains two distinct integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi). Members ai and bi are friends with each other. No pair of members will appear more than once in the input. Output If the given network is reasonable, print "YES" in a single line (without the quotes). Otherwise, print "NO" in a single line (without the quotes). Examples Input 4 3 1 3 3 4 1 4 Output YES Input 4 4 3 1 2 3 3 4 1 2 Output NO Input 10 4 4 3 5 10 8 9 1 2 Output YES Input 3 2 1 2 2 3 Output NO Note The drawings below show the situation in the first sample (on the left) and in the second sample (on the right). Each edge represents two members that are friends. The answer is "NO" in the second sample because members (2, 3) are friends and members (3, 4) are friends, while members (2, 4) are not. <image>
instruction
0
99,963
14
199,926
Tags: dfs and similar, dsu, graphs Correct Solution: ``` import sys,threading sys.setrecursionlimit(10**6) threading.stack_size(10**8) def main(): global al,va,ca,ml,ec,v,z n,m=map(int,input().split()) ml=[-1]*(n+1) al=[[]for i in range(n+1)] for i in range(m): a,b=map(int,input().split()) al[a].append(b) al[b].append(a) ml[a]=1 ml[b]=1 va=[0]*(n+1) ca=[-1]*(n+1) #print(ans,al) z=True for e in range(1,n+1): if(va[e]==0): ec=0 v=0 ec,v=dfs(e) ec=ec//2 #print(v,ec) if(ec==(v*(v-1))//2): pass else: z=False break #print(ca,z) if(z==False): print("NO") else: print("YES") def dfs(n): global al,va,ca,ec,v va[n]=1 v=v+1 ec=ec+len(al[n]) for e in al[n]: if(va[e]==0): dfs(e) return ec,v t=threading.Thread(target=main) t.start() t.join() ```
output
1
99,963
14
199,927
Provide tags and a correct Python 3 solution for this coding contest problem. Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures). There are n members, numbered 1 through n. m pairs of members are friends. Of course, a member can't be a friend with themselves. Let A-B denote that members A and B are friends. Limak thinks that a network is reasonable if and only if the following condition is satisfied: For every three distinct members (X, Y, Z), if X-Y and Y-Z then also X-Z. For example: if Alan and Bob are friends, and Bob and Ciri are friends, then Alan and Ciri should be friends as well. Can you help Limak and check if the network is reasonable? Print "YES" or "NO" accordingly, without the quotes. Input The first line of the input contain two integers n and m (3 ≀ n ≀ 150 000, <image>) β€” the number of members and the number of pairs of members that are friends. The i-th of the next m lines contains two distinct integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi). Members ai and bi are friends with each other. No pair of members will appear more than once in the input. Output If the given network is reasonable, print "YES" in a single line (without the quotes). Otherwise, print "NO" in a single line (without the quotes). Examples Input 4 3 1 3 3 4 1 4 Output YES Input 4 4 3 1 2 3 3 4 1 2 Output NO Input 10 4 4 3 5 10 8 9 1 2 Output YES Input 3 2 1 2 2 3 Output NO Note The drawings below show the situation in the first sample (on the left) and in the second sample (on the right). Each edge represents two members that are friends. The answer is "NO" in the second sample because members (2, 3) are friends and members (3, 4) are friends, while members (2, 4) are not. <image>
instruction
0
99,964
14
199,928
Tags: dfs and similar, dsu, graphs Correct Solution: ``` import sys n,m = map(int,input().split()) store = [set([i]) for i in range(n+1)] for i in range(m): a,b = map(int,input().split()) store[a].update([a,b]) store[b].update([a,b]) # print(store) no = 0 """cnt is a array of track which item are checked or not""" cnt = [0]*(n+1) for i in range(1,n+1): if cnt[i] == 1: # already checked this item continue for j in store[i]: # j = single item of set # print(j) cnt[j] = 1 if store[j] != store[i]: # print('store[i]: store[j]:',store[i],store[j]) no = 1 if no == 0: print('YES') else: print('NO') ```
output
1
99,964
14
199,929
Provide tags and a correct Python 3 solution for this coding contest problem. Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures). There are n members, numbered 1 through n. m pairs of members are friends. Of course, a member can't be a friend with themselves. Let A-B denote that members A and B are friends. Limak thinks that a network is reasonable if and only if the following condition is satisfied: For every three distinct members (X, Y, Z), if X-Y and Y-Z then also X-Z. For example: if Alan and Bob are friends, and Bob and Ciri are friends, then Alan and Ciri should be friends as well. Can you help Limak and check if the network is reasonable? Print "YES" or "NO" accordingly, without the quotes. Input The first line of the input contain two integers n and m (3 ≀ n ≀ 150 000, <image>) β€” the number of members and the number of pairs of members that are friends. The i-th of the next m lines contains two distinct integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi). Members ai and bi are friends with each other. No pair of members will appear more than once in the input. Output If the given network is reasonable, print "YES" in a single line (without the quotes). Otherwise, print "NO" in a single line (without the quotes). Examples Input 4 3 1 3 3 4 1 4 Output YES Input 4 4 3 1 2 3 3 4 1 2 Output NO Input 10 4 4 3 5 10 8 9 1 2 Output YES Input 3 2 1 2 2 3 Output NO Note The drawings below show the situation in the first sample (on the left) and in the second sample (on the right). Each edge represents two members that are friends. The answer is "NO" in the second sample because members (2, 3) are friends and members (3, 4) are friends, while members (2, 4) are not. <image>
instruction
0
99,965
14
199,930
Tags: dfs and similar, dsu, graphs Correct Solution: ``` from collections import defaultdict from sys import stdin from math import factorial def arr_inp(n): if n == 1: return [int(x) for x in stdin.readline().split()] elif n == 2: return [float(x) for x in stdin.readline().split()] else: return [str(x) for x in stdin.readline().split()] def nCr(n, r): f, m = factorial, 1 for i in range(n, n - r, -1): m *= i return int(m // f(r)) class disjointset: def __init__(self, n): self.rank, self.parent, self.n, self.nsets, self.edges = defaultdict(int), defaultdict(int), n, defaultdict( int), defaultdict(int) for i in range(1, n + 1): self.parent[i], self.nsets[i] = i, 1 def find(self, x): if self.parent[x] == x: return x result = self.find(self.parent[x]) self.parent[x] = result return result def union(self, x, y): xpar, ypar = self.find(x), self.find(y) # already union if xpar == ypar: self.edges[xpar] += 1 return # perform union by rank if self.rank[xpar] < self.rank[ypar]: self.parent[xpar] = ypar self.edges[ypar] += self.edges[xpar] + 1 self.nsets[ypar] += self.nsets[xpar] elif self.rank[xpar] > self.rank[ypar]: self.parent[ypar] = xpar self.edges[xpar] += self.edges[ypar] + 1 self.nsets[xpar] += self.nsets[ypar] else: self.parent[ypar] = xpar self.rank[xpar] += 1 self.edges[xpar] += self.edges[ypar] + 1 self.nsets[xpar] += self.nsets[ypar] def components(self): for i in self.parent: if self.parent[i] == i: if self.edges[i] != nCr(self.nsets[i], 2): exit(print('NO')) class graph: # initialize graph def __init__(self, gdict=None): if gdict is None: gdict = defaultdict(list) self.gdict = gdict # add edge def add_edge(self, node1, node2): self.gdict[node1].append(node2) self.gdict[node2].append(node1) def dfsUtil(self, v): stack = [v] while (stack): s = stack.pop() if not self.visit[s]: self.visit[s] = 1 self.vertix += 1 for i in self.gdict[s]: if not self.visit[i]: stack.append(i) self.edge[min(i, s), max(i, s)] = 1 # dfs for graph def dfs(self, ver): self.edge, self.vertix, self.visit = defaultdict(int), 0, defaultdict(int) for i in ver: if self.visit[i] == 0: self.dfsUtil(i) if len(self.edge) != nCr(self.vertix, 2): exit(print('NO')) self.edge, self.vertix = defaultdict(int), 0 n, m = arr_inp(1) bear, cnt = graph(), disjointset(n) for i in range(m): x, y = arr_inp(1) # bear.add_edge(x, y) cnt.union(x, y) cnt.components() print('YES') ```
output
1
99,965
14
199,931
Provide tags and a correct Python 3 solution for this coding contest problem. Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures). There are n members, numbered 1 through n. m pairs of members are friends. Of course, a member can't be a friend with themselves. Let A-B denote that members A and B are friends. Limak thinks that a network is reasonable if and only if the following condition is satisfied: For every three distinct members (X, Y, Z), if X-Y and Y-Z then also X-Z. For example: if Alan and Bob are friends, and Bob and Ciri are friends, then Alan and Ciri should be friends as well. Can you help Limak and check if the network is reasonable? Print "YES" or "NO" accordingly, without the quotes. Input The first line of the input contain two integers n and m (3 ≀ n ≀ 150 000, <image>) β€” the number of members and the number of pairs of members that are friends. The i-th of the next m lines contains two distinct integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi). Members ai and bi are friends with each other. No pair of members will appear more than once in the input. Output If the given network is reasonable, print "YES" in a single line (without the quotes). Otherwise, print "NO" in a single line (without the quotes). Examples Input 4 3 1 3 3 4 1 4 Output YES Input 4 4 3 1 2 3 3 4 1 2 Output NO Input 10 4 4 3 5 10 8 9 1 2 Output YES Input 3 2 1 2 2 3 Output NO Note The drawings below show the situation in the first sample (on the left) and in the second sample (on the right). Each edge represents two members that are friends. The answer is "NO" in the second sample because members (2, 3) are friends and members (3, 4) are friends, while members (2, 4) are not. <image>
instruction
0
99,966
14
199,932
Tags: dfs and similar, dsu, graphs Correct Solution: ``` import sys import collections def bfs(u, adjList, vis): dq = collections.deque() dq.append(u) vis[u] = True edgeCnt = 0 vertexCnt = 0 while dq: u = dq.popleft() vertexCnt += 1 edgeCnt += len(adjList[u]) for v in adjList[u]: if not vis[v]: vis[v] = True dq.append(v) edgeCnt = edgeCnt // 2 return bool(edgeCnt == ((vertexCnt * vertexCnt - vertexCnt) // 2)) def main(): # sys.stdin = open("in.txt", "r") it = iter(map(int, sys.stdin.read().split())) n = next(it) m = next(it) adjList = [[] for _ in range(n+3)] for _ in range(m): u = next(it) v = next(it) adjList[u].append(v) adjList[v].append(u) vis = [False] * (n+3) for u in range(1, n+1): if not vis[u]: if not bfs(u, adjList, vis): sys.stdout.write("NO\n") return sys.stdout.write("YES\n") if __name__ == '__main__': main() ```
output
1
99,966
14
199,933
Provide tags and a correct Python 3 solution for this coding contest problem. Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures). There are n members, numbered 1 through n. m pairs of members are friends. Of course, a member can't be a friend with themselves. Let A-B denote that members A and B are friends. Limak thinks that a network is reasonable if and only if the following condition is satisfied: For every three distinct members (X, Y, Z), if X-Y and Y-Z then also X-Z. For example: if Alan and Bob are friends, and Bob and Ciri are friends, then Alan and Ciri should be friends as well. Can you help Limak and check if the network is reasonable? Print "YES" or "NO" accordingly, without the quotes. Input The first line of the input contain two integers n and m (3 ≀ n ≀ 150 000, <image>) β€” the number of members and the number of pairs of members that are friends. The i-th of the next m lines contains two distinct integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi). Members ai and bi are friends with each other. No pair of members will appear more than once in the input. Output If the given network is reasonable, print "YES" in a single line (without the quotes). Otherwise, print "NO" in a single line (without the quotes). Examples Input 4 3 1 3 3 4 1 4 Output YES Input 4 4 3 1 2 3 3 4 1 2 Output NO Input 10 4 4 3 5 10 8 9 1 2 Output YES Input 3 2 1 2 2 3 Output NO Note The drawings below show the situation in the first sample (on the left) and in the second sample (on the right). Each edge represents two members that are friends. The answer is "NO" in the second sample because members (2, 3) are friends and members (3, 4) are friends, while members (2, 4) are not. <image>
instruction
0
99,967
14
199,934
Tags: dfs and similar, dsu, graphs Correct Solution: ``` inp = lambda : map(int, input().split()) n, m = inp() lines = [set([i]) for i in range(n + 1)] for i in range(m): x, y = inp() lines[x].add(y) lines[y].add(x) f = [True] * (n + 1) for i in range(n): if f[i]: f[i] = False for j in lines[i]: f[j] = False if lines[i] != lines[j]: print("NO") quit() print("YES") ```
output
1
99,967
14
199,935
Provide tags and a correct Python 3 solution for this coding contest problem. Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures). There are n members, numbered 1 through n. m pairs of members are friends. Of course, a member can't be a friend with themselves. Let A-B denote that members A and B are friends. Limak thinks that a network is reasonable if and only if the following condition is satisfied: For every three distinct members (X, Y, Z), if X-Y and Y-Z then also X-Z. For example: if Alan and Bob are friends, and Bob and Ciri are friends, then Alan and Ciri should be friends as well. Can you help Limak and check if the network is reasonable? Print "YES" or "NO" accordingly, without the quotes. Input The first line of the input contain two integers n and m (3 ≀ n ≀ 150 000, <image>) β€” the number of members and the number of pairs of members that are friends. The i-th of the next m lines contains two distinct integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi). Members ai and bi are friends with each other. No pair of members will appear more than once in the input. Output If the given network is reasonable, print "YES" in a single line (without the quotes). Otherwise, print "NO" in a single line (without the quotes). Examples Input 4 3 1 3 3 4 1 4 Output YES Input 4 4 3 1 2 3 3 4 1 2 Output NO Input 10 4 4 3 5 10 8 9 1 2 Output YES Input 3 2 1 2 2 3 Output NO Note The drawings below show the situation in the first sample (on the left) and in the second sample (on the right). Each edge represents two members that are friends. The answer is "NO" in the second sample because members (2, 3) are friends and members (3, 4) are friends, while members (2, 4) are not. <image>
instruction
0
99,968
14
199,936
Tags: dfs and similar, dsu, graphs Correct Solution: ``` import sys,os,io import math,bisect,operator inf,mod = float('inf'),10**9+7 # sys.setrecursionlimit(10 ** 6) from itertools import groupby,accumulate from heapq import heapify,heappop,heappush from collections import deque,Counter,defaultdict input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__ Neo = lambda : list(map(int,input().split())) G = defaultdict(list) def addEdge(a,b): G[a].append(b) G[b].append(a) N,M = Neo() vis = [False]*(N+1) def dfs(node): s = deque() vis[node] = True s.append(node) C = [] while s: x= s.pop() C.append(x) for i in G.get(x,[]): if not(vis[i]): vis[i] = True s.append(i) return C edge = set() for i in range(M): u,v = Neo() edge.add((u,v)) edge.add((v,u)) addEdge(u,v) for i in range(1,N+1): if not vis[i]: C = dfs(i) n= len(C) for i in range(n): for j in range(i+1,n): if not((C[i],C[j]) in edge): print('NO') exit(0) print('YES') ```
output
1
99,968
14
199,937
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures). There are n members, numbered 1 through n. m pairs of members are friends. Of course, a member can't be a friend with themselves. Let A-B denote that members A and B are friends. Limak thinks that a network is reasonable if and only if the following condition is satisfied: For every three distinct members (X, Y, Z), if X-Y and Y-Z then also X-Z. For example: if Alan and Bob are friends, and Bob and Ciri are friends, then Alan and Ciri should be friends as well. Can you help Limak and check if the network is reasonable? Print "YES" or "NO" accordingly, without the quotes. Input The first line of the input contain two integers n and m (3 ≀ n ≀ 150 000, <image>) β€” the number of members and the number of pairs of members that are friends. The i-th of the next m lines contains two distinct integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi). Members ai and bi are friends with each other. No pair of members will appear more than once in the input. Output If the given network is reasonable, print "YES" in a single line (without the quotes). Otherwise, print "NO" in a single line (without the quotes). Examples Input 4 3 1 3 3 4 1 4 Output YES Input 4 4 3 1 2 3 3 4 1 2 Output NO Input 10 4 4 3 5 10 8 9 1 2 Output YES Input 3 2 1 2 2 3 Output NO Note The drawings below show the situation in the first sample (on the left) and in the second sample (on the right). Each edge represents two members that are friends. The answer is "NO" in the second sample because members (2, 3) are friends and members (3, 4) are friends, while members (2, 4) are not. <image> Submitted Solution: ``` from sys import * f = lambda: map(int, stdin.readline().split()) n, m = f() g = [[i] for i in range(n + 1)] for j in range(m): u, v = f() g[u].append(v) g[v].append(u) k = 'YES' for t in g: t.sort() for t in g: s = len(t) if s > 1 and not all(g[x] == t for x in t): k = 'NO' break for j in t: g[j] = [] print(k) ```
instruction
0
99,969
14
199,938
Yes
output
1
99,969
14
199,939
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures). There are n members, numbered 1 through n. m pairs of members are friends. Of course, a member can't be a friend with themselves. Let A-B denote that members A and B are friends. Limak thinks that a network is reasonable if and only if the following condition is satisfied: For every three distinct members (X, Y, Z), if X-Y and Y-Z then also X-Z. For example: if Alan and Bob are friends, and Bob and Ciri are friends, then Alan and Ciri should be friends as well. Can you help Limak and check if the network is reasonable? Print "YES" or "NO" accordingly, without the quotes. Input The first line of the input contain two integers n and m (3 ≀ n ≀ 150 000, <image>) β€” the number of members and the number of pairs of members that are friends. The i-th of the next m lines contains two distinct integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi). Members ai and bi are friends with each other. No pair of members will appear more than once in the input. Output If the given network is reasonable, print "YES" in a single line (without the quotes). Otherwise, print "NO" in a single line (without the quotes). Examples Input 4 3 1 3 3 4 1 4 Output YES Input 4 4 3 1 2 3 3 4 1 2 Output NO Input 10 4 4 3 5 10 8 9 1 2 Output YES Input 3 2 1 2 2 3 Output NO Note The drawings below show the situation in the first sample (on the left) and in the second sample (on the right). Each edge represents two members that are friends. The answer is "NO" in the second sample because members (2, 3) are friends and members (3, 4) are friends, while members (2, 4) are not. <image> Submitted Solution: ``` from collections import defaultdict n,m=map(int,input().split()) d=defaultdict(list) for i in range(m): a,b=map(int,input().split()) a-=1 b-=1 d[a].append(b) d[b].append(a) vis=[0]*n for i in range(n): if vis[i]==0: q=[i] ce=0 cv=0 vis[i]=1 while q: t=q.pop() cv+=1 ce+=len(d[t]) for i in d[t]: if not vis[i]: vis[i]=1 q.append(i) if ce!=cv*(cv-1): print('NO') exit() print('YES') ```
instruction
0
99,970
14
199,940
Yes
output
1
99,970
14
199,941
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures). There are n members, numbered 1 through n. m pairs of members are friends. Of course, a member can't be a friend with themselves. Let A-B denote that members A and B are friends. Limak thinks that a network is reasonable if and only if the following condition is satisfied: For every three distinct members (X, Y, Z), if X-Y and Y-Z then also X-Z. For example: if Alan and Bob are friends, and Bob and Ciri are friends, then Alan and Ciri should be friends as well. Can you help Limak and check if the network is reasonable? Print "YES" or "NO" accordingly, without the quotes. Input The first line of the input contain two integers n and m (3 ≀ n ≀ 150 000, <image>) β€” the number of members and the number of pairs of members that are friends. The i-th of the next m lines contains two distinct integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi). Members ai and bi are friends with each other. No pair of members will appear more than once in the input. Output If the given network is reasonable, print "YES" in a single line (without the quotes). Otherwise, print "NO" in a single line (without the quotes). Examples Input 4 3 1 3 3 4 1 4 Output YES Input 4 4 3 1 2 3 3 4 1 2 Output NO Input 10 4 4 3 5 10 8 9 1 2 Output YES Input 3 2 1 2 2 3 Output NO Note The drawings below show the situation in the first sample (on the left) and in the second sample (on the right). Each edge represents two members that are friends. The answer is "NO" in the second sample because members (2, 3) are friends and members (3, 4) are friends, while members (2, 4) are not. <image> Submitted Solution: ``` #from collections import OrderedDict from collections import defaultdict #from functools import reduce #from itertools import groupby #sys.setrecursionlimit(10**6) #from itertools import accumulate from collections import Counter import math import sys import os from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") ''' from threading import stack_size,Thread sys.setrecursionlimit(10**6) stack_size(10**8) ''' def listt(): return [int(i) for i in input().split()] def gcd(a,b): return math.gcd(a,b) def lcm(a,b): return (a*b) // gcd(a,b) def factors(n): step = 2 if n%2 else 1 return set(reduce(list.__add__,([i, n//i] for i in range(1, int(math.sqrt(n))+1, step) if n % i == 0))) def comb(n,k): factn=math.factorial(n) factk=math.factorial(k) fact=math.factorial(n-k) ans=factn//(factk*fact) return ans def is_prime(n): if n <= 1: return False if n == 2: return True if n > 2 and n % 2 == 0: return False max_div = math.floor(math.sqrt(n)) for i in range(3, 1 + max_div, 2): if n % i == 0: return False return True def maxpower(n,x): B_max = int(math.log(n, x)) #tells upto what power of x n is less than it like 1024->5^4 return B_max def binaryToDecimal(n): return int(n,2) #d=sorted(d.items(),key=lambda a:a[1]) sort dic accc to values #g=pow(2,6) #a,b,n,m=map(int,input().split()) #print ("{0:.8f}".format(min(l))) n,m=map(int,input().split()) v=[0]*150005 s=[set([i])for i in range(150005)] for _ in range(m): a,b=map(int,input().split()) s[a].add(b) s[b].add(a) for i in range(n): if not v[i]: for j in s[i]: v[j]=1 if s[j]!=s[i]: print('NO') exit(0) print('YES') ```
instruction
0
99,971
14
199,942
Yes
output
1
99,971
14
199,943
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures). There are n members, numbered 1 through n. m pairs of members are friends. Of course, a member can't be a friend with themselves. Let A-B denote that members A and B are friends. Limak thinks that a network is reasonable if and only if the following condition is satisfied: For every three distinct members (X, Y, Z), if X-Y and Y-Z then also X-Z. For example: if Alan and Bob are friends, and Bob and Ciri are friends, then Alan and Ciri should be friends as well. Can you help Limak and check if the network is reasonable? Print "YES" or "NO" accordingly, without the quotes. Input The first line of the input contain two integers n and m (3 ≀ n ≀ 150 000, <image>) β€” the number of members and the number of pairs of members that are friends. The i-th of the next m lines contains two distinct integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi). Members ai and bi are friends with each other. No pair of members will appear more than once in the input. Output If the given network is reasonable, print "YES" in a single line (without the quotes). Otherwise, print "NO" in a single line (without the quotes). Examples Input 4 3 1 3 3 4 1 4 Output YES Input 4 4 3 1 2 3 3 4 1 2 Output NO Input 10 4 4 3 5 10 8 9 1 2 Output YES Input 3 2 1 2 2 3 Output NO Note The drawings below show the situation in the first sample (on the left) and in the second sample (on the right). Each edge represents two members that are friends. The answer is "NO" in the second sample because members (2, 3) are friends and members (3, 4) are friends, while members (2, 4) are not. <image> Submitted Solution: ``` n , m = map(int , input().split()) marked = [0] * n adjlist = [set([i]) for i in range(n)] for i in range(m): v1 , v2 = map(int , input().split()) v1 -= 1 v2 -= 1 adjlist[v1].add(v2) adjlist[v2].add(v1) for i in range(n): if not marked[i]: for j in adjlist[i]: marked[j] = 1 if adjlist[i] != adjlist[j] : print('NO') exit(0) print('YES') ```
instruction
0
99,972
14
199,944
Yes
output
1
99,972
14
199,945
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures). There are n members, numbered 1 through n. m pairs of members are friends. Of course, a member can't be a friend with themselves. Let A-B denote that members A and B are friends. Limak thinks that a network is reasonable if and only if the following condition is satisfied: For every three distinct members (X, Y, Z), if X-Y and Y-Z then also X-Z. For example: if Alan and Bob are friends, and Bob and Ciri are friends, then Alan and Ciri should be friends as well. Can you help Limak and check if the network is reasonable? Print "YES" or "NO" accordingly, without the quotes. Input The first line of the input contain two integers n and m (3 ≀ n ≀ 150 000, <image>) β€” the number of members and the number of pairs of members that are friends. The i-th of the next m lines contains two distinct integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi). Members ai and bi are friends with each other. No pair of members will appear more than once in the input. Output If the given network is reasonable, print "YES" in a single line (without the quotes). Otherwise, print "NO" in a single line (without the quotes). Examples Input 4 3 1 3 3 4 1 4 Output YES Input 4 4 3 1 2 3 3 4 1 2 Output NO Input 10 4 4 3 5 10 8 9 1 2 Output YES Input 3 2 1 2 2 3 Output NO Note The drawings below show the situation in the first sample (on the left) and in the second sample (on the right). Each edge represents two members that are friends. The answer is "NO" in the second sample because members (2, 3) are friends and members (3, 4) are friends, while members (2, 4) are not. <image> Submitted Solution: ``` used = [] color = 1 count_v = 0 def dfs(u, g): count = 0 used[u] = color global count_v count_v += 1 for v in g[u]: count += 1 if not used[v]: count += dfs(v, g) return count def main(): n, m = map(int, input().split()) g = [] for i in range(n): g.append([]) used.append(0) for i in range(m): u, v = map(int, input().split()) u -= 1 v -= 1 g[u].append(v) g[v].append(u) for i in range(n): if used[i] == 0: global count_v count_v = 0 count_e = 0 try: count_e = dfs(i, g) except Exception: print("123") exit(0) if count_v * (count_v - 1) != count_e: print("NO") exit(0) print("YES") main() ```
instruction
0
99,973
14
199,946
No
output
1
99,973
14
199,947
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures). There are n members, numbered 1 through n. m pairs of members are friends. Of course, a member can't be a friend with themselves. Let A-B denote that members A and B are friends. Limak thinks that a network is reasonable if and only if the following condition is satisfied: For every three distinct members (X, Y, Z), if X-Y and Y-Z then also X-Z. For example: if Alan and Bob are friends, and Bob and Ciri are friends, then Alan and Ciri should be friends as well. Can you help Limak and check if the network is reasonable? Print "YES" or "NO" accordingly, without the quotes. Input The first line of the input contain two integers n and m (3 ≀ n ≀ 150 000, <image>) β€” the number of members and the number of pairs of members that are friends. The i-th of the next m lines contains two distinct integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi). Members ai and bi are friends with each other. No pair of members will appear more than once in the input. Output If the given network is reasonable, print "YES" in a single line (without the quotes). Otherwise, print "NO" in a single line (without the quotes). Examples Input 4 3 1 3 3 4 1 4 Output YES Input 4 4 3 1 2 3 3 4 1 2 Output NO Input 10 4 4 3 5 10 8 9 1 2 Output YES Input 3 2 1 2 2 3 Output NO Note The drawings below show the situation in the first sample (on the left) and in the second sample (on the right). Each edge represents two members that are friends. The answer is "NO" in the second sample because members (2, 3) are friends and members (3, 4) are friends, while members (2, 4) are not. <image> Submitted Solution: ``` from collections import defaultdict from collections import deque n,m=list(map(int,input().split())) d=defaultdict(list) for i in range(m): x,y=list(map(int,input().split())) d[x].append(y) d[y].append(x) visited=[0]*(n+1) f=0 for i in range(1,n+1): if visited[i]==0: visited[i]=1 for k in d[i]: visited[k]=1 for i in range(1,n+1): if visited[i]==0: f=1 break if f: print('NO') else: print('YES') ```
instruction
0
99,974
14
199,948
No
output
1
99,974
14
199,949
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures). There are n members, numbered 1 through n. m pairs of members are friends. Of course, a member can't be a friend with themselves. Let A-B denote that members A and B are friends. Limak thinks that a network is reasonable if and only if the following condition is satisfied: For every three distinct members (X, Y, Z), if X-Y and Y-Z then also X-Z. For example: if Alan and Bob are friends, and Bob and Ciri are friends, then Alan and Ciri should be friends as well. Can you help Limak and check if the network is reasonable? Print "YES" or "NO" accordingly, without the quotes. Input The first line of the input contain two integers n and m (3 ≀ n ≀ 150 000, <image>) β€” the number of members and the number of pairs of members that are friends. The i-th of the next m lines contains two distinct integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi). Members ai and bi are friends with each other. No pair of members will appear more than once in the input. Output If the given network is reasonable, print "YES" in a single line (without the quotes). Otherwise, print "NO" in a single line (without the quotes). Examples Input 4 3 1 3 3 4 1 4 Output YES Input 4 4 3 1 2 3 3 4 1 2 Output NO Input 10 4 4 3 5 10 8 9 1 2 Output YES Input 3 2 1 2 2 3 Output NO Note The drawings below show the situation in the first sample (on the left) and in the second sample (on the right). Each edge represents two members that are friends. The answer is "NO" in the second sample because members (2, 3) are friends and members (3, 4) are friends, while members (2, 4) are not. <image> Submitted Solution: ``` import sys from collections import defaultdict n=m=0 Group=[] GL=defaultdict(int) G=defaultdict(lambda:-1) def main(): MN=input().split() n=int(MN[0]) m=int(MN[1]) for i in range(m): L=input().split() L=[int(L[0]),int(L[1])] if G[L[0]]==-1 and G[L[1]]==-1: Group.append([L[0],L[1]]) len=Group.__len__()-1 G[L[0]]=G[L[1]]=len GL[len]+=1 elif G[L[0]]!=-1 and G[L[1]]!=-1: if G[L[0]]!=G[L[1]]: No=G[L[0]]if Group[G[L[0]]].__len__()>Group[G[L[1]]].__len__()else G[L[1]] No1=G[L[0]]if Group[G[L[0]]].__len__()<Group[G[L[1]]].__len__()else G[L[1]] Group[No].extend(Group[No1]) for i in Group[No1]: G[i]=No Group[No1].clear() GL[No]+=GL[No1]+1 else:GL[G[L[0]]]+=1 else: No=(G[L[1]],L[0]) if G[L[1]]!=-1 else (G[L[0]],L[1]) Group[No[0]].append(No[1]) G[No[1]]=No[0] GL[No[0]]+=1 Pass=True for (No,i) in enumerate(Group): len=i.__len__()-1 if len>0: Sum=(len*len+len)/2 if Sum!=GL[No]: print("NO") Pass=False break if Pass is True: print("YES") if __name__=='__main__': main() ```
instruction
0
99,975
14
199,950
No
output
1
99,975
14
199,951
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures). There are n members, numbered 1 through n. m pairs of members are friends. Of course, a member can't be a friend with themselves. Let A-B denote that members A and B are friends. Limak thinks that a network is reasonable if and only if the following condition is satisfied: For every three distinct members (X, Y, Z), if X-Y and Y-Z then also X-Z. For example: if Alan and Bob are friends, and Bob and Ciri are friends, then Alan and Ciri should be friends as well. Can you help Limak and check if the network is reasonable? Print "YES" or "NO" accordingly, without the quotes. Input The first line of the input contain two integers n and m (3 ≀ n ≀ 150 000, <image>) β€” the number of members and the number of pairs of members that are friends. The i-th of the next m lines contains two distinct integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi). Members ai and bi are friends with each other. No pair of members will appear more than once in the input. Output If the given network is reasonable, print "YES" in a single line (without the quotes). Otherwise, print "NO" in a single line (without the quotes). Examples Input 4 3 1 3 3 4 1 4 Output YES Input 4 4 3 1 2 3 3 4 1 2 Output NO Input 10 4 4 3 5 10 8 9 1 2 Output YES Input 3 2 1 2 2 3 Output NO Note The drawings below show the situation in the first sample (on the left) and in the second sample (on the right). Each edge represents two members that are friends. The answer is "NO" in the second sample because members (2, 3) are friends and members (3, 4) are friends, while members (2, 4) are not. <image> Submitted Solution: ``` a,b = map(int, input().split()) i=0 while a>0: a*=3 b*=2 i+=1 if a>b: print(i) break ```
instruction
0
99,976
14
199,952
No
output
1
99,976
14
199,953
Provide tags and a correct Python 3 solution for this coding contest problem. In the Bus of Characters there are n rows of seat, each having 2 seats. The width of both seats in the i-th row is w_i centimeters. All integers w_i are distinct. Initially the bus is empty. On each of 2n stops one passenger enters the bus. There are two types of passengers: * an introvert always chooses a row where both seats are empty. Among these rows he chooses the one with the smallest seats width and takes one of the seats in it; * an extrovert always chooses a row where exactly one seat is occupied (by an introvert). Among these rows he chooses the one with the largest seats width and takes the vacant place in it. You are given the seats width in each row and the order the passengers enter the bus. Determine which row each passenger will take. Input The first line contains a single integer n (1 ≀ n ≀ 200 000) β€” the number of rows in the bus. The second line contains the sequence of integers w_1, w_2, ..., w_n (1 ≀ w_i ≀ 10^{9}), where w_i is the width of each of the seats in the i-th row. It is guaranteed that all w_i are distinct. The third line contains a string of length 2n, consisting of digits '0' and '1' β€” the description of the order the passengers enter the bus. If the j-th character is '0', then the passenger that enters the bus on the j-th stop is an introvert. If the j-th character is '1', the the passenger that enters the bus on the j-th stop is an extrovert. It is guaranteed that the number of extroverts equals the number of introverts (i. e. both numbers equal n), and for each extrovert there always is a suitable row. Output Print 2n integers β€” the rows the passengers will take. The order of passengers should be the same as in input. Examples Input 2 3 1 0011 Output 2 1 1 2 Input 6 10 8 9 11 13 5 010010011101 Output 6 6 2 3 3 1 4 4 1 2 5 5 Note In the first example the first passenger (introvert) chooses the row 2, because it has the seats with smallest width. The second passenger (introvert) chooses the row 1, because it is the only empty row now. The third passenger (extrovert) chooses the row 1, because it has exactly one occupied seat and the seat width is the largest among such rows. The fourth passenger (extrovert) chooses the row 2, because it is the only row with an empty place.
instruction
0
100,061
14
200,122
Tags: data structures, greedy, implementation Correct Solution: ``` n = int(input()) seats = list(map(int, input().split())) sarr = list(zip(seats, range(1, n+1))) sarr.sort() sarr = [i[1] for i in sarr] ans = [] k = 0 for i in input(): if i == '0': print(sarr[k], end = " ") ans.append(sarr[k]) k+=1 if i == '1': print(ans[-1], end = " ") ans.pop() ```
output
1
100,061
14
200,123
Provide tags and a correct Python 3 solution for this coding contest problem. In the Bus of Characters there are n rows of seat, each having 2 seats. The width of both seats in the i-th row is w_i centimeters. All integers w_i are distinct. Initially the bus is empty. On each of 2n stops one passenger enters the bus. There are two types of passengers: * an introvert always chooses a row where both seats are empty. Among these rows he chooses the one with the smallest seats width and takes one of the seats in it; * an extrovert always chooses a row where exactly one seat is occupied (by an introvert). Among these rows he chooses the one with the largest seats width and takes the vacant place in it. You are given the seats width in each row and the order the passengers enter the bus. Determine which row each passenger will take. Input The first line contains a single integer n (1 ≀ n ≀ 200 000) β€” the number of rows in the bus. The second line contains the sequence of integers w_1, w_2, ..., w_n (1 ≀ w_i ≀ 10^{9}), where w_i is the width of each of the seats in the i-th row. It is guaranteed that all w_i are distinct. The third line contains a string of length 2n, consisting of digits '0' and '1' β€” the description of the order the passengers enter the bus. If the j-th character is '0', then the passenger that enters the bus on the j-th stop is an introvert. If the j-th character is '1', the the passenger that enters the bus on the j-th stop is an extrovert. It is guaranteed that the number of extroverts equals the number of introverts (i. e. both numbers equal n), and for each extrovert there always is a suitable row. Output Print 2n integers β€” the rows the passengers will take. The order of passengers should be the same as in input. Examples Input 2 3 1 0011 Output 2 1 1 2 Input 6 10 8 9 11 13 5 010010011101 Output 6 6 2 3 3 1 4 4 1 2 5 5 Note In the first example the first passenger (introvert) chooses the row 2, because it has the seats with smallest width. The second passenger (introvert) chooses the row 1, because it is the only empty row now. The third passenger (extrovert) chooses the row 1, because it has exactly one occupied seat and the seat width is the largest among such rows. The fourth passenger (extrovert) chooses the row 2, because it is the only row with an empty place.
instruction
0
100,062
14
200,124
Tags: data structures, greedy, implementation Correct Solution: ``` # -*- coding: utf-8 -*- # Baqir Khan # Software Engineer (Backend) from sys import stdin inp = stdin.readline n = int(inp()) a = [(int(ele), i + 1) for i, ele in enumerate(inp().split())] s = inp().strip() ans = [0] * (2* n) partially_filled = [] a.sort() start = 0 for j in range(2*n): if s[j] == "0": partially_filled.append(a[start]) ans[j] = a[start][1] start += 1 else: ans[j] = partially_filled[-1][1] partially_filled.pop() print(*ans) ```
output
1
100,062
14
200,125
Provide tags and a correct Python 3 solution for this coding contest problem. In the Bus of Characters there are n rows of seat, each having 2 seats. The width of both seats in the i-th row is w_i centimeters. All integers w_i are distinct. Initially the bus is empty. On each of 2n stops one passenger enters the bus. There are two types of passengers: * an introvert always chooses a row where both seats are empty. Among these rows he chooses the one with the smallest seats width and takes one of the seats in it; * an extrovert always chooses a row where exactly one seat is occupied (by an introvert). Among these rows he chooses the one with the largest seats width and takes the vacant place in it. You are given the seats width in each row and the order the passengers enter the bus. Determine which row each passenger will take. Input The first line contains a single integer n (1 ≀ n ≀ 200 000) β€” the number of rows in the bus. The second line contains the sequence of integers w_1, w_2, ..., w_n (1 ≀ w_i ≀ 10^{9}), where w_i is the width of each of the seats in the i-th row. It is guaranteed that all w_i are distinct. The third line contains a string of length 2n, consisting of digits '0' and '1' β€” the description of the order the passengers enter the bus. If the j-th character is '0', then the passenger that enters the bus on the j-th stop is an introvert. If the j-th character is '1', the the passenger that enters the bus on the j-th stop is an extrovert. It is guaranteed that the number of extroverts equals the number of introverts (i. e. both numbers equal n), and for each extrovert there always is a suitable row. Output Print 2n integers β€” the rows the passengers will take. The order of passengers should be the same as in input. Examples Input 2 3 1 0011 Output 2 1 1 2 Input 6 10 8 9 11 13 5 010010011101 Output 6 6 2 3 3 1 4 4 1 2 5 5 Note In the first example the first passenger (introvert) chooses the row 2, because it has the seats with smallest width. The second passenger (introvert) chooses the row 1, because it is the only empty row now. The third passenger (extrovert) chooses the row 1, because it has exactly one occupied seat and the seat width is the largest among such rows. The fourth passenger (extrovert) chooses the row 2, because it is the only row with an empty place.
instruction
0
100,063
14
200,126
Tags: data structures, greedy, implementation Correct Solution: ``` n = int(input()) w = [[-int(x), i + 1] for (i, x) in enumerate(input().split(' '))] w.sort(key = lambda x : x[0]) m = input() e = []; res = []; for x in m: if x == '0': res.append(w[len(w) - 1][1]) e.append(w.pop()[1]) else: res.append(e.pop()); print(' '.join(map(str, res))) ```
output
1
100,063
14
200,127
Provide tags and a correct Python 3 solution for this coding contest problem. In the Bus of Characters there are n rows of seat, each having 2 seats. The width of both seats in the i-th row is w_i centimeters. All integers w_i are distinct. Initially the bus is empty. On each of 2n stops one passenger enters the bus. There are two types of passengers: * an introvert always chooses a row where both seats are empty. Among these rows he chooses the one with the smallest seats width and takes one of the seats in it; * an extrovert always chooses a row where exactly one seat is occupied (by an introvert). Among these rows he chooses the one with the largest seats width and takes the vacant place in it. You are given the seats width in each row and the order the passengers enter the bus. Determine which row each passenger will take. Input The first line contains a single integer n (1 ≀ n ≀ 200 000) β€” the number of rows in the bus. The second line contains the sequence of integers w_1, w_2, ..., w_n (1 ≀ w_i ≀ 10^{9}), where w_i is the width of each of the seats in the i-th row. It is guaranteed that all w_i are distinct. The third line contains a string of length 2n, consisting of digits '0' and '1' β€” the description of the order the passengers enter the bus. If the j-th character is '0', then the passenger that enters the bus on the j-th stop is an introvert. If the j-th character is '1', the the passenger that enters the bus on the j-th stop is an extrovert. It is guaranteed that the number of extroverts equals the number of introverts (i. e. both numbers equal n), and for each extrovert there always is a suitable row. Output Print 2n integers β€” the rows the passengers will take. The order of passengers should be the same as in input. Examples Input 2 3 1 0011 Output 2 1 1 2 Input 6 10 8 9 11 13 5 010010011101 Output 6 6 2 3 3 1 4 4 1 2 5 5 Note In the first example the first passenger (introvert) chooses the row 2, because it has the seats with smallest width. The second passenger (introvert) chooses the row 1, because it is the only empty row now. The third passenger (extrovert) chooses the row 1, because it has exactly one occupied seat and the seat width is the largest among such rows. The fourth passenger (extrovert) chooses the row 2, because it is the only row with an empty place.
instruction
0
100,064
14
200,128
Tags: data structures, greedy, implementation Correct Solution: ``` n=int(input()) l=list(map(int,input().split())) a={} for t in range(n): a[l[t]]=t+1 c=a.items() b=sorted(c,reverse=True) f=[] p=[] k=input() for i in k: if i=="0": x=b.pop() f.append(x) p.append(x[1]) else: y=f.pop() p.append(y[1]) print(*p) ```
output
1
100,064
14
200,129
Provide tags and a correct Python 3 solution for this coding contest problem. In the Bus of Characters there are n rows of seat, each having 2 seats. The width of both seats in the i-th row is w_i centimeters. All integers w_i are distinct. Initially the bus is empty. On each of 2n stops one passenger enters the bus. There are two types of passengers: * an introvert always chooses a row where both seats are empty. Among these rows he chooses the one with the smallest seats width and takes one of the seats in it; * an extrovert always chooses a row where exactly one seat is occupied (by an introvert). Among these rows he chooses the one with the largest seats width and takes the vacant place in it. You are given the seats width in each row and the order the passengers enter the bus. Determine which row each passenger will take. Input The first line contains a single integer n (1 ≀ n ≀ 200 000) β€” the number of rows in the bus. The second line contains the sequence of integers w_1, w_2, ..., w_n (1 ≀ w_i ≀ 10^{9}), where w_i is the width of each of the seats in the i-th row. It is guaranteed that all w_i are distinct. The third line contains a string of length 2n, consisting of digits '0' and '1' β€” the description of the order the passengers enter the bus. If the j-th character is '0', then the passenger that enters the bus on the j-th stop is an introvert. If the j-th character is '1', the the passenger that enters the bus on the j-th stop is an extrovert. It is guaranteed that the number of extroverts equals the number of introverts (i. e. both numbers equal n), and for each extrovert there always is a suitable row. Output Print 2n integers β€” the rows the passengers will take. The order of passengers should be the same as in input. Examples Input 2 3 1 0011 Output 2 1 1 2 Input 6 10 8 9 11 13 5 010010011101 Output 6 6 2 3 3 1 4 4 1 2 5 5 Note In the first example the first passenger (introvert) chooses the row 2, because it has the seats with smallest width. The second passenger (introvert) chooses the row 1, because it is the only empty row now. The third passenger (extrovert) chooses the row 1, because it has exactly one occupied seat and the seat width is the largest among such rows. The fourth passenger (extrovert) chooses the row 2, because it is the only row with an empty place.
instruction
0
100,065
14
200,130
Tags: data structures, greedy, implementation Correct Solution: ``` n = int(input()) widths = [(int(x), c) for c, x in enumerate(input().split())] widths.sort() passengers = input() seating = "" stack = [] i = 0 for pi in passengers: if pi == '1': seating += str(stack[-1][1]+1) + " " stack.pop() else: stack.append(widths[i]) seating += str(stack[-1][1]+1) + " " i += 1 print(seating[:-1]) ```
output
1
100,065
14
200,131
Provide tags and a correct Python 3 solution for this coding contest problem. In the Bus of Characters there are n rows of seat, each having 2 seats. The width of both seats in the i-th row is w_i centimeters. All integers w_i are distinct. Initially the bus is empty. On each of 2n stops one passenger enters the bus. There are two types of passengers: * an introvert always chooses a row where both seats are empty. Among these rows he chooses the one with the smallest seats width and takes one of the seats in it; * an extrovert always chooses a row where exactly one seat is occupied (by an introvert). Among these rows he chooses the one with the largest seats width and takes the vacant place in it. You are given the seats width in each row and the order the passengers enter the bus. Determine which row each passenger will take. Input The first line contains a single integer n (1 ≀ n ≀ 200 000) β€” the number of rows in the bus. The second line contains the sequence of integers w_1, w_2, ..., w_n (1 ≀ w_i ≀ 10^{9}), where w_i is the width of each of the seats in the i-th row. It is guaranteed that all w_i are distinct. The third line contains a string of length 2n, consisting of digits '0' and '1' β€” the description of the order the passengers enter the bus. If the j-th character is '0', then the passenger that enters the bus on the j-th stop is an introvert. If the j-th character is '1', the the passenger that enters the bus on the j-th stop is an extrovert. It is guaranteed that the number of extroverts equals the number of introverts (i. e. both numbers equal n), and for each extrovert there always is a suitable row. Output Print 2n integers β€” the rows the passengers will take. The order of passengers should be the same as in input. Examples Input 2 3 1 0011 Output 2 1 1 2 Input 6 10 8 9 11 13 5 010010011101 Output 6 6 2 3 3 1 4 4 1 2 5 5 Note In the first example the first passenger (introvert) chooses the row 2, because it has the seats with smallest width. The second passenger (introvert) chooses the row 1, because it is the only empty row now. The third passenger (extrovert) chooses the row 1, because it has exactly one occupied seat and the seat width is the largest among such rows. The fourth passenger (extrovert) chooses the row 2, because it is the only row with an empty place.
instruction
0
100,066
14
200,132
Tags: data structures, greedy, implementation Correct Solution: ``` def main(): n = int(input()) w = input().split() w = sorted([[int(w[i]), i + 1] for i in range(n)]) introindex = 0 result = [] introPlaces = [] for passenger in input(): if passenger == '0': result.append(str(w[introindex][1])) introPlaces.append(w[introindex][1]) introindex += 1 else: result.append(str(introPlaces.pop())) print(' '.join(result)) if __name__ == "__main__": main() ```
output
1
100,066
14
200,133
Provide tags and a correct Python 3 solution for this coding contest problem. In the Bus of Characters there are n rows of seat, each having 2 seats. The width of both seats in the i-th row is w_i centimeters. All integers w_i are distinct. Initially the bus is empty. On each of 2n stops one passenger enters the bus. There are two types of passengers: * an introvert always chooses a row where both seats are empty. Among these rows he chooses the one with the smallest seats width and takes one of the seats in it; * an extrovert always chooses a row where exactly one seat is occupied (by an introvert). Among these rows he chooses the one with the largest seats width and takes the vacant place in it. You are given the seats width in each row and the order the passengers enter the bus. Determine which row each passenger will take. Input The first line contains a single integer n (1 ≀ n ≀ 200 000) β€” the number of rows in the bus. The second line contains the sequence of integers w_1, w_2, ..., w_n (1 ≀ w_i ≀ 10^{9}), where w_i is the width of each of the seats in the i-th row. It is guaranteed that all w_i are distinct. The third line contains a string of length 2n, consisting of digits '0' and '1' β€” the description of the order the passengers enter the bus. If the j-th character is '0', then the passenger that enters the bus on the j-th stop is an introvert. If the j-th character is '1', the the passenger that enters the bus on the j-th stop is an extrovert. It is guaranteed that the number of extroverts equals the number of introverts (i. e. both numbers equal n), and for each extrovert there always is a suitable row. Output Print 2n integers β€” the rows the passengers will take. The order of passengers should be the same as in input. Examples Input 2 3 1 0011 Output 2 1 1 2 Input 6 10 8 9 11 13 5 010010011101 Output 6 6 2 3 3 1 4 4 1 2 5 5 Note In the first example the first passenger (introvert) chooses the row 2, because it has the seats with smallest width. The second passenger (introvert) chooses the row 1, because it is the only empty row now. The third passenger (extrovert) chooses the row 1, because it has exactly one occupied seat and the seat width is the largest among such rows. The fourth passenger (extrovert) chooses the row 2, because it is the only row with an empty place.
instruction
0
100,067
14
200,134
Tags: data structures, greedy, implementation Correct Solution: ``` N = int(input()) Width = [int(x) for x in input().split()] Input_Order = [int(x) for x in list(input())] Sorted_Width = sorted(Width) Data = {Width[x]:x for x in range(0,N)} A = [] B = [] position = 0 for x in range(0,2*N): if Input_Order[x]==0: A.append(Data[Sorted_Width[position]]+1) B.append(Data[Sorted_Width[position]]+1) position+=1 else : A.append(B[-1]) B.pop() for x in A: print(x,end =' ') ```
output
1
100,067
14
200,135
Provide tags and a correct Python 3 solution for this coding contest problem. In the Bus of Characters there are n rows of seat, each having 2 seats. The width of both seats in the i-th row is w_i centimeters. All integers w_i are distinct. Initially the bus is empty. On each of 2n stops one passenger enters the bus. There are two types of passengers: * an introvert always chooses a row where both seats are empty. Among these rows he chooses the one with the smallest seats width and takes one of the seats in it; * an extrovert always chooses a row where exactly one seat is occupied (by an introvert). Among these rows he chooses the one with the largest seats width and takes the vacant place in it. You are given the seats width in each row and the order the passengers enter the bus. Determine which row each passenger will take. Input The first line contains a single integer n (1 ≀ n ≀ 200 000) β€” the number of rows in the bus. The second line contains the sequence of integers w_1, w_2, ..., w_n (1 ≀ w_i ≀ 10^{9}), where w_i is the width of each of the seats in the i-th row. It is guaranteed that all w_i are distinct. The third line contains a string of length 2n, consisting of digits '0' and '1' β€” the description of the order the passengers enter the bus. If the j-th character is '0', then the passenger that enters the bus on the j-th stop is an introvert. If the j-th character is '1', the the passenger that enters the bus on the j-th stop is an extrovert. It is guaranteed that the number of extroverts equals the number of introverts (i. e. both numbers equal n), and for each extrovert there always is a suitable row. Output Print 2n integers β€” the rows the passengers will take. The order of passengers should be the same as in input. Examples Input 2 3 1 0011 Output 2 1 1 2 Input 6 10 8 9 11 13 5 010010011101 Output 6 6 2 3 3 1 4 4 1 2 5 5 Note In the first example the first passenger (introvert) chooses the row 2, because it has the seats with smallest width. The second passenger (introvert) chooses the row 1, because it is the only empty row now. The third passenger (extrovert) chooses the row 1, because it has exactly one occupied seat and the seat width is the largest among such rows. The fourth passenger (extrovert) chooses the row 2, because it is the only row with an empty place.
instruction
0
100,068
14
200,136
Tags: data structures, greedy, implementation Correct Solution: ``` """Problem B - Bus of Characters. http://codeforces.com/contest/982/problem/B In the Bus of Characters there are `n` rows of seat, each having `2` seats. The width of both seats in the `i`-th row is `w_i` centimeters. All integers `w_i` are distinct. Initially the bus is empty. On each of `2n` stops one passenger enters the bus. There are two types of passengers: - an introvert always chooses a row where both seats are empty. Among these rows he chooses the one with the smallest seats width and takes one of the seats in it; - an extrovert always chooses a row where exactly one seat is occupied (by an introvert). Among these rows he chooses the one with the largest seats width and takes the vacant place in it. You are given the seats width in each row and the order the passengers enter the bus. Determine which row each passenger will take. Input: The first line contains a single integer `n` (`1 <= n <= 200000`) β€” the number of rows in the bus. The second line contains the sequence of integers `w_1, w_2, ..., w_n` (`1 <= w_i <= 10^{9}`), where `w_i` is the width of each of the seats in the `i`-th row. It is guaranteed that all `w_i` are distinct. The third line contains a string of length `2n`, consisting of digits '0' and '1' β€” the description of the order the passengers enter the bus. If the `j`-th character is '0', then the passenger that enters the bus on the `j`-th stop is an introvert. If the `j`-th character is '1', the the passenger that enters the bus on the `j`-th stop is an extrovert. It is guaranteed that the number of extroverts equals the number of introverts (i. e. both numbers equal `n`), and for each extrovert there always is a suitable row. Output: Print `2n` integers β€” the rows the passengers will take. The order of passengers should be the same as in input. """ def solve(w, s): empty = sorted(enumerate(w), key=lambda x: x[1], reverse=True) non_empty = [] result = [] for p in s: if p == '0': row = empty.pop() non_empty.append(row) else: row = non_empty.pop() result.append(row[0] + 1) return result def main(): _ = input() w = [int(x) for x in input().strip().split()] s = input().strip() result = solve(w, s) print(' '.join(map(str, result))) if __name__ == '__main__': main() ```
output
1
100,068
14
200,137
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In the Bus of Characters there are n rows of seat, each having 2 seats. The width of both seats in the i-th row is w_i centimeters. All integers w_i are distinct. Initially the bus is empty. On each of 2n stops one passenger enters the bus. There are two types of passengers: * an introvert always chooses a row where both seats are empty. Among these rows he chooses the one with the smallest seats width and takes one of the seats in it; * an extrovert always chooses a row where exactly one seat is occupied (by an introvert). Among these rows he chooses the one with the largest seats width and takes the vacant place in it. You are given the seats width in each row and the order the passengers enter the bus. Determine which row each passenger will take. Input The first line contains a single integer n (1 ≀ n ≀ 200 000) β€” the number of rows in the bus. The second line contains the sequence of integers w_1, w_2, ..., w_n (1 ≀ w_i ≀ 10^{9}), where w_i is the width of each of the seats in the i-th row. It is guaranteed that all w_i are distinct. The third line contains a string of length 2n, consisting of digits '0' and '1' β€” the description of the order the passengers enter the bus. If the j-th character is '0', then the passenger that enters the bus on the j-th stop is an introvert. If the j-th character is '1', the the passenger that enters the bus on the j-th stop is an extrovert. It is guaranteed that the number of extroverts equals the number of introverts (i. e. both numbers equal n), and for each extrovert there always is a suitable row. Output Print 2n integers β€” the rows the passengers will take. The order of passengers should be the same as in input. Examples Input 2 3 1 0011 Output 2 1 1 2 Input 6 10 8 9 11 13 5 010010011101 Output 6 6 2 3 3 1 4 4 1 2 5 5 Note In the first example the first passenger (introvert) chooses the row 2, because it has the seats with smallest width. The second passenger (introvert) chooses the row 1, because it is the only empty row now. The third passenger (extrovert) chooses the row 1, because it has exactly one occupied seat and the seat width is the largest among such rows. The fourth passenger (extrovert) chooses the row 2, because it is the only row with an empty place. Submitted Solution: ``` n = int(input()) width = [(int(w), i) for i, w in enumerate(input().split(" "))] order = input() width = sorted(width, key=lambda x: x[0]) no_passengers = [w[1] for w in width] one_passenger = [] i = 0 for person in order: if person == "0": print(no_passengers[i] + 1, end=" ") one_passenger.append(no_passengers[i]) i += 1 else: print(one_passenger.pop() + 1, end=" ") ```
instruction
0
100,069
14
200,138
Yes
output
1
100,069
14
200,139
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In the Bus of Characters there are n rows of seat, each having 2 seats. The width of both seats in the i-th row is w_i centimeters. All integers w_i are distinct. Initially the bus is empty. On each of 2n stops one passenger enters the bus. There are two types of passengers: * an introvert always chooses a row where both seats are empty. Among these rows he chooses the one with the smallest seats width and takes one of the seats in it; * an extrovert always chooses a row where exactly one seat is occupied (by an introvert). Among these rows he chooses the one with the largest seats width and takes the vacant place in it. You are given the seats width in each row and the order the passengers enter the bus. Determine which row each passenger will take. Input The first line contains a single integer n (1 ≀ n ≀ 200 000) β€” the number of rows in the bus. The second line contains the sequence of integers w_1, w_2, ..., w_n (1 ≀ w_i ≀ 10^{9}), where w_i is the width of each of the seats in the i-th row. It is guaranteed that all w_i are distinct. The third line contains a string of length 2n, consisting of digits '0' and '1' β€” the description of the order the passengers enter the bus. If the j-th character is '0', then the passenger that enters the bus on the j-th stop is an introvert. If the j-th character is '1', the the passenger that enters the bus on the j-th stop is an extrovert. It is guaranteed that the number of extroverts equals the number of introverts (i. e. both numbers equal n), and for each extrovert there always is a suitable row. Output Print 2n integers β€” the rows the passengers will take. The order of passengers should be the same as in input. Examples Input 2 3 1 0011 Output 2 1 1 2 Input 6 10 8 9 11 13 5 010010011101 Output 6 6 2 3 3 1 4 4 1 2 5 5 Note In the first example the first passenger (introvert) chooses the row 2, because it has the seats with smallest width. The second passenger (introvert) chooses the row 1, because it is the only empty row now. The third passenger (extrovert) chooses the row 1, because it has exactly one occupied seat and the seat width is the largest among such rows. The fourth passenger (extrovert) chooses the row 2, because it is the only row with an empty place. Submitted Solution: ``` def main(): n = int(input()) w = input().split() w = sorted([(int(w[i]), i + 1) for i in range(n)]) introindex = 0 result = [] introPlaces = [] for passenger in input(): if passenger == '0': result.append(str(w[introindex][1])) introPlaces.append(w[introindex][1]) introindex += 1 else: result.append(str(introPlaces.pop())) print(' '.join(result)) if __name__ == "__main__": main() ```
instruction
0
100,070
14
200,140
Yes
output
1
100,070
14
200,141
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In the Bus of Characters there are n rows of seat, each having 2 seats. The width of both seats in the i-th row is w_i centimeters. All integers w_i are distinct. Initially the bus is empty. On each of 2n stops one passenger enters the bus. There are two types of passengers: * an introvert always chooses a row where both seats are empty. Among these rows he chooses the one with the smallest seats width and takes one of the seats in it; * an extrovert always chooses a row where exactly one seat is occupied (by an introvert). Among these rows he chooses the one with the largest seats width and takes the vacant place in it. You are given the seats width in each row and the order the passengers enter the bus. Determine which row each passenger will take. Input The first line contains a single integer n (1 ≀ n ≀ 200 000) β€” the number of rows in the bus. The second line contains the sequence of integers w_1, w_2, ..., w_n (1 ≀ w_i ≀ 10^{9}), where w_i is the width of each of the seats in the i-th row. It is guaranteed that all w_i are distinct. The third line contains a string of length 2n, consisting of digits '0' and '1' β€” the description of the order the passengers enter the bus. If the j-th character is '0', then the passenger that enters the bus on the j-th stop is an introvert. If the j-th character is '1', the the passenger that enters the bus on the j-th stop is an extrovert. It is guaranteed that the number of extroverts equals the number of introverts (i. e. both numbers equal n), and for each extrovert there always is a suitable row. Output Print 2n integers β€” the rows the passengers will take. The order of passengers should be the same as in input. Examples Input 2 3 1 0011 Output 2 1 1 2 Input 6 10 8 9 11 13 5 010010011101 Output 6 6 2 3 3 1 4 4 1 2 5 5 Note In the first example the first passenger (introvert) chooses the row 2, because it has the seats with smallest width. The second passenger (introvert) chooses the row 1, because it is the only empty row now. The third passenger (extrovert) chooses the row 1, because it has exactly one occupied seat and the seat width is the largest among such rows. The fourth passenger (extrovert) chooses the row 2, because it is the only row with an empty place. Submitted Solution: ``` class heap: def __init__(self, maxn): self.a = [0] * maxn self.size = 0 def shift_down(self, i): while 2 * i + 1 < self.size: l = 2 * i + 1 r = 2 * i + 2 j = l if r < self.size and self.a[r] < self.a[l]: j = r if self.a[i] <= self.a[j]: break self.a[i], self.a[j] = self.a[j], self.a[i] i = j def shift_up(self, i): while i and self.a[i] < self.a[(i - 1) // 2]: self.a[i], self.a[(i - 1) // 2] = self.a[(i - 1) // 2], self.a[i] i = (i - 1) // 2 def erase_min(self): mn = self.a[0] self.a[0] = self.a[self.size - 1] self.size -= 1 self.shift_down(0) return mn def insert(self, val): self.size += 1 self.a[self.size - 1] = val self.shift_up(self.size - 1) n = int(input()) ar = [int(j) for j in input().split()] s0 = heap(400000 + 100) s1 = heap(400000 + 100) for i in range(n): s0.insert((ar[i], i + 1)) for c in input(): if c == '0': v = s0.erase_min() print(v[1], end=' ') s1.insert((-v[0], v[1])) else: print(s1.erase_min()[1], end=' ') ```
instruction
0
100,071
14
200,142
Yes
output
1
100,071
14
200,143
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In the Bus of Characters there are n rows of seat, each having 2 seats. The width of both seats in the i-th row is w_i centimeters. All integers w_i are distinct. Initially the bus is empty. On each of 2n stops one passenger enters the bus. There are two types of passengers: * an introvert always chooses a row where both seats are empty. Among these rows he chooses the one with the smallest seats width and takes one of the seats in it; * an extrovert always chooses a row where exactly one seat is occupied (by an introvert). Among these rows he chooses the one with the largest seats width and takes the vacant place in it. You are given the seats width in each row and the order the passengers enter the bus. Determine which row each passenger will take. Input The first line contains a single integer n (1 ≀ n ≀ 200 000) β€” the number of rows in the bus. The second line contains the sequence of integers w_1, w_2, ..., w_n (1 ≀ w_i ≀ 10^{9}), where w_i is the width of each of the seats in the i-th row. It is guaranteed that all w_i are distinct. The third line contains a string of length 2n, consisting of digits '0' and '1' β€” the description of the order the passengers enter the bus. If the j-th character is '0', then the passenger that enters the bus on the j-th stop is an introvert. If the j-th character is '1', the the passenger that enters the bus on the j-th stop is an extrovert. It is guaranteed that the number of extroverts equals the number of introverts (i. e. both numbers equal n), and for each extrovert there always is a suitable row. Output Print 2n integers β€” the rows the passengers will take. The order of passengers should be the same as in input. Examples Input 2 3 1 0011 Output 2 1 1 2 Input 6 10 8 9 11 13 5 010010011101 Output 6 6 2 3 3 1 4 4 1 2 5 5 Note In the first example the first passenger (introvert) chooses the row 2, because it has the seats with smallest width. The second passenger (introvert) chooses the row 1, because it is the only empty row now. The third passenger (extrovert) chooses the row 1, because it has exactly one occupied seat and the seat width is the largest among such rows. The fourth passenger (extrovert) chooses the row 2, because it is the only row with an empty place. Submitted Solution: ``` import heapq row = int(input()) width = [int(x) for x in input().split()] Introvert = [] Extrovert = [] for i in range(row): heapq.heappush(Introvert,(width[i],i+1)) sequence = input() Ans = [] for i in sequence: if(i=="0"): #Introvert x,y = heapq.heappop(Introvert) heapq.heappush(Extrovert,(-x,y)) Ans.append(y) else: x,y = heapq.heappop(Extrovert) Ans.append(y) print(*Ans) ```
instruction
0
100,072
14
200,144
Yes
output
1
100,072
14
200,145
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In the Bus of Characters there are n rows of seat, each having 2 seats. The width of both seats in the i-th row is w_i centimeters. All integers w_i are distinct. Initially the bus is empty. On each of 2n stops one passenger enters the bus. There are two types of passengers: * an introvert always chooses a row where both seats are empty. Among these rows he chooses the one with the smallest seats width and takes one of the seats in it; * an extrovert always chooses a row where exactly one seat is occupied (by an introvert). Among these rows he chooses the one with the largest seats width and takes the vacant place in it. You are given the seats width in each row and the order the passengers enter the bus. Determine which row each passenger will take. Input The first line contains a single integer n (1 ≀ n ≀ 200 000) β€” the number of rows in the bus. The second line contains the sequence of integers w_1, w_2, ..., w_n (1 ≀ w_i ≀ 10^{9}), where w_i is the width of each of the seats in the i-th row. It is guaranteed that all w_i are distinct. The third line contains a string of length 2n, consisting of digits '0' and '1' β€” the description of the order the passengers enter the bus. If the j-th character is '0', then the passenger that enters the bus on the j-th stop is an introvert. If the j-th character is '1', the the passenger that enters the bus on the j-th stop is an extrovert. It is guaranteed that the number of extroverts equals the number of introverts (i. e. both numbers equal n), and for each extrovert there always is a suitable row. Output Print 2n integers β€” the rows the passengers will take. The order of passengers should be the same as in input. Examples Input 2 3 1 0011 Output 2 1 1 2 Input 6 10 8 9 11 13 5 010010011101 Output 6 6 2 3 3 1 4 4 1 2 5 5 Note In the first example the first passenger (introvert) chooses the row 2, because it has the seats with smallest width. The second passenger (introvert) chooses the row 1, because it is the only empty row now. The third passenger (extrovert) chooses the row 1, because it has exactly one occupied seat and the seat width is the largest among such rows. The fourth passenger (extrovert) chooses the row 2, because it is the only row with an empty place. Submitted Solution: ``` n = int(input().strip()) temp = input().strip().split() w = [] for j in range(len(temp)): w.append([int(temp[j]),j]) w.sort() #print(w) passenger = input().strip() biggest_intro = [] smallest_index = 0 for i in range(2*n): if passenger[i] == '0': #introvert #smallest_empty = w[smallest_index][1] #actual index print(w[smallest_index][1]+1, end='') biggest_intro.append(smallest_index) #non-actual index, bigger width at the end smallest_index+=1 elif passenger[i] == '1': b = biggest_intro.pop() print(w[b][1]+1,end='') #print(biggest_intro) ```
instruction
0
100,073
14
200,146
No
output
1
100,073
14
200,147
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In the Bus of Characters there are n rows of seat, each having 2 seats. The width of both seats in the i-th row is w_i centimeters. All integers w_i are distinct. Initially the bus is empty. On each of 2n stops one passenger enters the bus. There are two types of passengers: * an introvert always chooses a row where both seats are empty. Among these rows he chooses the one with the smallest seats width and takes one of the seats in it; * an extrovert always chooses a row where exactly one seat is occupied (by an introvert). Among these rows he chooses the one with the largest seats width and takes the vacant place in it. You are given the seats width in each row and the order the passengers enter the bus. Determine which row each passenger will take. Input The first line contains a single integer n (1 ≀ n ≀ 200 000) β€” the number of rows in the bus. The second line contains the sequence of integers w_1, w_2, ..., w_n (1 ≀ w_i ≀ 10^{9}), where w_i is the width of each of the seats in the i-th row. It is guaranteed that all w_i are distinct. The third line contains a string of length 2n, consisting of digits '0' and '1' β€” the description of the order the passengers enter the bus. If the j-th character is '0', then the passenger that enters the bus on the j-th stop is an introvert. If the j-th character is '1', the the passenger that enters the bus on the j-th stop is an extrovert. It is guaranteed that the number of extroverts equals the number of introverts (i. e. both numbers equal n), and for each extrovert there always is a suitable row. Output Print 2n integers β€” the rows the passengers will take. The order of passengers should be the same as in input. Examples Input 2 3 1 0011 Output 2 1 1 2 Input 6 10 8 9 11 13 5 010010011101 Output 6 6 2 3 3 1 4 4 1 2 5 5 Note In the first example the first passenger (introvert) chooses the row 2, because it has the seats with smallest width. The second passenger (introvert) chooses the row 1, because it is the only empty row now. The third passenger (extrovert) chooses the row 1, because it has exactly one occupied seat and the seat width is the largest among such rows. The fourth passenger (extrovert) chooses the row 2, because it is the only row with an empty place. Submitted Solution: ``` import queue n=int(input()) l=list(map(int,input().split())) a={} for t in range(n): a[l[t]]=t+1 c=a.items() b=sorted(c) L=queue.Queue(maxsize=n) for u in b: L.put(u) f=[] p=[] k=input() for i in k: if i=="0": x=L.get() f.append(x) p.append(x[1]) else: y=f.pop() p.append(x[1]) print(*p) ```
instruction
0
100,074
14
200,148
No
output
1
100,074
14
200,149
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In the Bus of Characters there are n rows of seat, each having 2 seats. The width of both seats in the i-th row is w_i centimeters. All integers w_i are distinct. Initially the bus is empty. On each of 2n stops one passenger enters the bus. There are two types of passengers: * an introvert always chooses a row where both seats are empty. Among these rows he chooses the one with the smallest seats width and takes one of the seats in it; * an extrovert always chooses a row where exactly one seat is occupied (by an introvert). Among these rows he chooses the one with the largest seats width and takes the vacant place in it. You are given the seats width in each row and the order the passengers enter the bus. Determine which row each passenger will take. Input The first line contains a single integer n (1 ≀ n ≀ 200 000) β€” the number of rows in the bus. The second line contains the sequence of integers w_1, w_2, ..., w_n (1 ≀ w_i ≀ 10^{9}), where w_i is the width of each of the seats in the i-th row. It is guaranteed that all w_i are distinct. The third line contains a string of length 2n, consisting of digits '0' and '1' β€” the description of the order the passengers enter the bus. If the j-th character is '0', then the passenger that enters the bus on the j-th stop is an introvert. If the j-th character is '1', the the passenger that enters the bus on the j-th stop is an extrovert. It is guaranteed that the number of extroverts equals the number of introverts (i. e. both numbers equal n), and for each extrovert there always is a suitable row. Output Print 2n integers β€” the rows the passengers will take. The order of passengers should be the same as in input. Examples Input 2 3 1 0011 Output 2 1 1 2 Input 6 10 8 9 11 13 5 010010011101 Output 6 6 2 3 3 1 4 4 1 2 5 5 Note In the first example the first passenger (introvert) chooses the row 2, because it has the seats with smallest width. The second passenger (introvert) chooses the row 1, because it is the only empty row now. The third passenger (extrovert) chooses the row 1, because it has exactly one occupied seat and the seat width is the largest among such rows. The fourth passenger (extrovert) chooses the row 2, because it is the only row with an empty place. Submitted Solution: ``` n = int(input()) temp = list(zip([int(x) for x in input().split()], range(1, n+1))) temp.sort(key=lambda x: x[0]) type1 = [int(x) for x in list(input())] a = [] ans = [] for i in type1: if i == 0: ans.append(temp[0][1]) a.append(temp[0]) del temp[0] else: ans.append(a[-1][1]) # temp = [a[-1]] + temp del a[-1] print(ans) ```
instruction
0
100,075
14
200,150
No
output
1
100,075
14
200,151
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In the Bus of Characters there are n rows of seat, each having 2 seats. The width of both seats in the i-th row is w_i centimeters. All integers w_i are distinct. Initially the bus is empty. On each of 2n stops one passenger enters the bus. There are two types of passengers: * an introvert always chooses a row where both seats are empty. Among these rows he chooses the one with the smallest seats width and takes one of the seats in it; * an extrovert always chooses a row where exactly one seat is occupied (by an introvert). Among these rows he chooses the one with the largest seats width and takes the vacant place in it. You are given the seats width in each row and the order the passengers enter the bus. Determine which row each passenger will take. Input The first line contains a single integer n (1 ≀ n ≀ 200 000) β€” the number of rows in the bus. The second line contains the sequence of integers w_1, w_2, ..., w_n (1 ≀ w_i ≀ 10^{9}), where w_i is the width of each of the seats in the i-th row. It is guaranteed that all w_i are distinct. The third line contains a string of length 2n, consisting of digits '0' and '1' β€” the description of the order the passengers enter the bus. If the j-th character is '0', then the passenger that enters the bus on the j-th stop is an introvert. If the j-th character is '1', the the passenger that enters the bus on the j-th stop is an extrovert. It is guaranteed that the number of extroverts equals the number of introverts (i. e. both numbers equal n), and for each extrovert there always is a suitable row. Output Print 2n integers β€” the rows the passengers will take. The order of passengers should be the same as in input. Examples Input 2 3 1 0011 Output 2 1 1 2 Input 6 10 8 9 11 13 5 010010011101 Output 6 6 2 3 3 1 4 4 1 2 5 5 Note In the first example the first passenger (introvert) chooses the row 2, because it has the seats with smallest width. The second passenger (introvert) chooses the row 1, because it is the only empty row now. The third passenger (extrovert) chooses the row 1, because it has exactly one occupied seat and the seat width is the largest among such rows. The fourth passenger (extrovert) chooses the row 2, because it is the only row with an empty place. Submitted Solution: ``` import heapq n=int(input()) get=lambda:map(int,input().split()) w=list(get()) s=input() intro=[(i,e) for e,i in enumerate(w)] exto=[] heapq.heapify(w) for i in s: if i=='0': e,i=heapq.heappop(intro) heapq.heappush(exto,(-e,i)) print(i+1,end=' ') else: e,i=heapq.heappop(exto) print(i+1,end=' ') ```
instruction
0
100,076
14
200,152
No
output
1
100,076
14
200,153
Provide tags and a correct Python 3 solution for this coding contest problem. Asya loves animals very much. Recently, she purchased n kittens, enumerated them from 1 and n and then put them into the cage. The cage consists of one row of n cells, enumerated with integers from 1 to n from left to right. Adjacent cells had a partially transparent partition wall between them, hence there were n - 1 partitions originally. Initially, each cell contained exactly one kitten with some number. Observing the kittens, Asya noticed, that they are very friendly and often a pair of kittens in neighboring cells wants to play together. So Asya started to remove partitions between neighboring cells. In particular, on the day i, Asya: * Noticed, that the kittens x_i and y_i, located in neighboring cells want to play together. * Removed the partition between these two cells, efficiently creating a single cell, having all kittens from two original cells. Since Asya has never putted partitions back, after n - 1 days the cage contained a single cell, having all kittens. For every day, Asya remembers numbers of kittens x_i and y_i, who wanted to play together, however she doesn't remember how she placed kittens in the cage in the beginning. Please help her and find any possible initial arrangement of the kittens into n cells. Input The first line contains a single integer n (2 ≀ n ≀ 150 000) β€” the number of kittens. Each of the following n - 1 lines contains integers x_i and y_i (1 ≀ x_i, y_i ≀ n, x_i β‰  y_i) β€” indices of kittens, which got together due to the border removal on the corresponding day. It's guaranteed, that the kittens x_i and y_i were in the different cells before this day. Output For every cell from 1 to n print a single integer β€” the index of the kitten from 1 to n, who was originally in it. All printed integers must be distinct. It's guaranteed, that there is at least one answer possible. In case there are multiple possible answers, print any of them. Example Input 5 1 4 2 5 3 1 4 5 Output 3 1 4 2 5 Note The answer for the example contains one of several possible initial arrangements of the kittens. The picture below shows how the cells were united for this initial arrangement. Note, that the kittens who wanted to play together on each day were indeed in adjacent cells. <image>
instruction
0
100,368
14
200,736
Tags: constructive algorithms, dsu Correct Solution: ``` from math import * from collections import * import sys sys.setrecursionlimit(10**9) def find(x): if (p[x] == x): return x p[x] = find(p[x]) return p[x] def merge(x,y): a = find(x) b = find(y) #print(a,b) if(a != b): if(sz[a] > sz[b]): a,b = b,a p[a] = b sz[b] += sz[a] arr[b].extend(arr[a]) n = int(input()) p = [i for i in range(n+1)] sz = [1 for i in range(n+1)] arr = [[i] for i in range(n+1)] for i in range(n-1): u,v = map(int,input().split()) merge(u,v) #print(p) for i in arr: if len(i) == n: for j in i: print(j,end = " ") print() break ```
output
1
100,368
14
200,737
Provide tags and a correct Python 3 solution for this coding contest problem. Asya loves animals very much. Recently, she purchased n kittens, enumerated them from 1 and n and then put them into the cage. The cage consists of one row of n cells, enumerated with integers from 1 to n from left to right. Adjacent cells had a partially transparent partition wall between them, hence there were n - 1 partitions originally. Initially, each cell contained exactly one kitten with some number. Observing the kittens, Asya noticed, that they are very friendly and often a pair of kittens in neighboring cells wants to play together. So Asya started to remove partitions between neighboring cells. In particular, on the day i, Asya: * Noticed, that the kittens x_i and y_i, located in neighboring cells want to play together. * Removed the partition between these two cells, efficiently creating a single cell, having all kittens from two original cells. Since Asya has never putted partitions back, after n - 1 days the cage contained a single cell, having all kittens. For every day, Asya remembers numbers of kittens x_i and y_i, who wanted to play together, however she doesn't remember how she placed kittens in the cage in the beginning. Please help her and find any possible initial arrangement of the kittens into n cells. Input The first line contains a single integer n (2 ≀ n ≀ 150 000) β€” the number of kittens. Each of the following n - 1 lines contains integers x_i and y_i (1 ≀ x_i, y_i ≀ n, x_i β‰  y_i) β€” indices of kittens, which got together due to the border removal on the corresponding day. It's guaranteed, that the kittens x_i and y_i were in the different cells before this day. Output For every cell from 1 to n print a single integer β€” the index of the kitten from 1 to n, who was originally in it. All printed integers must be distinct. It's guaranteed, that there is at least one answer possible. In case there are multiple possible answers, print any of them. Example Input 5 1 4 2 5 3 1 4 5 Output 3 1 4 2 5 Note The answer for the example contains one of several possible initial arrangements of the kittens. The picture below shows how the cells were united for this initial arrangement. Note, that the kittens who wanted to play together on each day were indeed in adjacent cells. <image>
instruction
0
100,369
14
200,738
Tags: constructive algorithms, dsu Correct Solution: ``` n=int(input()) d={} for i in range(n-1): a,b=map(int,input().split()) if (a not in d.keys()) and (b not in d.keys()): d[a]=[a,b] d[b]=d[a] elif (a in d.keys()) and (b not in d.keys()): d[a]+=[b] d[b]=d[a] elif (a not in d.keys()) and (b in d.keys()): d[b]+=[a] d[a]=d[b] else: d[a]+=d[b] for j in d[b]: d[j]=d[a] print(*d[1]) ```
output
1
100,369
14
200,739
Provide tags and a correct Python 3 solution for this coding contest problem. Asya loves animals very much. Recently, she purchased n kittens, enumerated them from 1 and n and then put them into the cage. The cage consists of one row of n cells, enumerated with integers from 1 to n from left to right. Adjacent cells had a partially transparent partition wall between them, hence there were n - 1 partitions originally. Initially, each cell contained exactly one kitten with some number. Observing the kittens, Asya noticed, that they are very friendly and often a pair of kittens in neighboring cells wants to play together. So Asya started to remove partitions between neighboring cells. In particular, on the day i, Asya: * Noticed, that the kittens x_i and y_i, located in neighboring cells want to play together. * Removed the partition between these two cells, efficiently creating a single cell, having all kittens from two original cells. Since Asya has never putted partitions back, after n - 1 days the cage contained a single cell, having all kittens. For every day, Asya remembers numbers of kittens x_i and y_i, who wanted to play together, however she doesn't remember how she placed kittens in the cage in the beginning. Please help her and find any possible initial arrangement of the kittens into n cells. Input The first line contains a single integer n (2 ≀ n ≀ 150 000) β€” the number of kittens. Each of the following n - 1 lines contains integers x_i and y_i (1 ≀ x_i, y_i ≀ n, x_i β‰  y_i) β€” indices of kittens, which got together due to the border removal on the corresponding day. It's guaranteed, that the kittens x_i and y_i were in the different cells before this day. Output For every cell from 1 to n print a single integer β€” the index of the kitten from 1 to n, who was originally in it. All printed integers must be distinct. It's guaranteed, that there is at least one answer possible. In case there are multiple possible answers, print any of them. Example Input 5 1 4 2 5 3 1 4 5 Output 3 1 4 2 5 Note The answer for the example contains one of several possible initial arrangements of the kittens. The picture below shows how the cells were united for this initial arrangement. Note, that the kittens who wanted to play together on each day were indeed in adjacent cells. <image>
instruction
0
100,370
14
200,740
Tags: constructive algorithms, dsu Correct Solution: ``` n, = map(int, input().split()) p = [i for i in range(0,n+1)] def find(i): if p[i] == i: return i par = find(p[i]) p[i] = par return par def join(a,b): p[find(a)] = find(b) N = [-1 for i in range(0,n+1)] class List: def __init__(self, val): self.front = self self.value = val self.rear = self self.next = None def add(self, other): self.rear.next = other self.rear = other.rear other.front = self m = {i: List(i) for i in range(1,n+1)} def printList(i): if i is None: return list = [] while i!=None: list.append(i.value) i = i.next print(str(list).replace(',','').replace('[','').replace(']','')) ans = 1 for i in range(1,n): a,b = map(int, input().split()) m[find(a)].add(m[find(b)]) temp = m[find(a)] del m[find(a)] del m[find(b)] join(a,b) key = find(a) ans = key m[key] = temp.front printList(m[ans].front) ```
output
1
100,370
14
200,741
Provide tags and a correct Python 3 solution for this coding contest problem. Asya loves animals very much. Recently, she purchased n kittens, enumerated them from 1 and n and then put them into the cage. The cage consists of one row of n cells, enumerated with integers from 1 to n from left to right. Adjacent cells had a partially transparent partition wall between them, hence there were n - 1 partitions originally. Initially, each cell contained exactly one kitten with some number. Observing the kittens, Asya noticed, that they are very friendly and often a pair of kittens in neighboring cells wants to play together. So Asya started to remove partitions between neighboring cells. In particular, on the day i, Asya: * Noticed, that the kittens x_i and y_i, located in neighboring cells want to play together. * Removed the partition between these two cells, efficiently creating a single cell, having all kittens from two original cells. Since Asya has never putted partitions back, after n - 1 days the cage contained a single cell, having all kittens. For every day, Asya remembers numbers of kittens x_i and y_i, who wanted to play together, however she doesn't remember how she placed kittens in the cage in the beginning. Please help her and find any possible initial arrangement of the kittens into n cells. Input The first line contains a single integer n (2 ≀ n ≀ 150 000) β€” the number of kittens. Each of the following n - 1 lines contains integers x_i and y_i (1 ≀ x_i, y_i ≀ n, x_i β‰  y_i) β€” indices of kittens, which got together due to the border removal on the corresponding day. It's guaranteed, that the kittens x_i and y_i were in the different cells before this day. Output For every cell from 1 to n print a single integer β€” the index of the kitten from 1 to n, who was originally in it. All printed integers must be distinct. It's guaranteed, that there is at least one answer possible. In case there are multiple possible answers, print any of them. Example Input 5 1 4 2 5 3 1 4 5 Output 3 1 4 2 5 Note The answer for the example contains one of several possible initial arrangements of the kittens. The picture below shows how the cells were united for this initial arrangement. Note, that the kittens who wanted to play together on each day were indeed in adjacent cells. <image>
instruction
0
100,371
14
200,742
Tags: constructive algorithms, dsu Correct Solution: ``` n = int(input()) parent = [i for i in range(n+1)] group = [[i] for i in range(n+1)] for i in range(n-1): x, y = map(int, input().split()) if len(group[parent[x]]) < len(group[parent[y]]): x, y = y, x group[parent[x]] += group[parent[y]] for j in group[parent[y]]: parent[j] = parent[x] print(*group[parent[1]]) ```
output
1
100,371
14
200,743
Provide tags and a correct Python 3 solution for this coding contest problem. Asya loves animals very much. Recently, she purchased n kittens, enumerated them from 1 and n and then put them into the cage. The cage consists of one row of n cells, enumerated with integers from 1 to n from left to right. Adjacent cells had a partially transparent partition wall between them, hence there were n - 1 partitions originally. Initially, each cell contained exactly one kitten with some number. Observing the kittens, Asya noticed, that they are very friendly and often a pair of kittens in neighboring cells wants to play together. So Asya started to remove partitions between neighboring cells. In particular, on the day i, Asya: * Noticed, that the kittens x_i and y_i, located in neighboring cells want to play together. * Removed the partition between these two cells, efficiently creating a single cell, having all kittens from two original cells. Since Asya has never putted partitions back, after n - 1 days the cage contained a single cell, having all kittens. For every day, Asya remembers numbers of kittens x_i and y_i, who wanted to play together, however she doesn't remember how she placed kittens in the cage in the beginning. Please help her and find any possible initial arrangement of the kittens into n cells. Input The first line contains a single integer n (2 ≀ n ≀ 150 000) β€” the number of kittens. Each of the following n - 1 lines contains integers x_i and y_i (1 ≀ x_i, y_i ≀ n, x_i β‰  y_i) β€” indices of kittens, which got together due to the border removal on the corresponding day. It's guaranteed, that the kittens x_i and y_i were in the different cells before this day. Output For every cell from 1 to n print a single integer β€” the index of the kitten from 1 to n, who was originally in it. All printed integers must be distinct. It's guaranteed, that there is at least one answer possible. In case there are multiple possible answers, print any of them. Example Input 5 1 4 2 5 3 1 4 5 Output 3 1 4 2 5 Note The answer for the example contains one of several possible initial arrangements of the kittens. The picture below shows how the cells were united for this initial arrangement. Note, that the kittens who wanted to play together on each day were indeed in adjacent cells. <image>
instruction
0
100,372
14
200,744
Tags: constructive algorithms, dsu Correct Solution: ``` n = int(input()) ks = [[0]] for i in range(n): ks.append([i + 1]) for _ in range(n - 1): a, b = map(int, input().split()) while type(ks[a]) == int: a = ks[a] while type(ks[b]) == int: b = ks[b] if len(ks[a]) >= len(ks[b]): ks[a] += ks[b] ks[b] = a else: ks[b] += ks[a] ks[a] = b if _ == n - 2: while type(ks[a]) == int: a = ks[a] print(*ks[a]) ```
output
1
100,372
14
200,745
Provide tags and a correct Python 3 solution for this coding contest problem. Asya loves animals very much. Recently, she purchased n kittens, enumerated them from 1 and n and then put them into the cage. The cage consists of one row of n cells, enumerated with integers from 1 to n from left to right. Adjacent cells had a partially transparent partition wall between them, hence there were n - 1 partitions originally. Initially, each cell contained exactly one kitten with some number. Observing the kittens, Asya noticed, that they are very friendly and often a pair of kittens in neighboring cells wants to play together. So Asya started to remove partitions between neighboring cells. In particular, on the day i, Asya: * Noticed, that the kittens x_i and y_i, located in neighboring cells want to play together. * Removed the partition between these two cells, efficiently creating a single cell, having all kittens from two original cells. Since Asya has never putted partitions back, after n - 1 days the cage contained a single cell, having all kittens. For every day, Asya remembers numbers of kittens x_i and y_i, who wanted to play together, however she doesn't remember how she placed kittens in the cage in the beginning. Please help her and find any possible initial arrangement of the kittens into n cells. Input The first line contains a single integer n (2 ≀ n ≀ 150 000) β€” the number of kittens. Each of the following n - 1 lines contains integers x_i and y_i (1 ≀ x_i, y_i ≀ n, x_i β‰  y_i) β€” indices of kittens, which got together due to the border removal on the corresponding day. It's guaranteed, that the kittens x_i and y_i were in the different cells before this day. Output For every cell from 1 to n print a single integer β€” the index of the kitten from 1 to n, who was originally in it. All printed integers must be distinct. It's guaranteed, that there is at least one answer possible. In case there are multiple possible answers, print any of them. Example Input 5 1 4 2 5 3 1 4 5 Output 3 1 4 2 5 Note The answer for the example contains one of several possible initial arrangements of the kittens. The picture below shows how the cells were united for this initial arrangement. Note, that the kittens who wanted to play together on each day were indeed in adjacent cells. <image>
instruction
0
100,373
14
200,746
Tags: constructive algorithms, dsu Correct Solution: ``` import sys input=sys.stdin.readline n=int(input()) p=[list(map(int,input().split())) for i in range(n-1)] g=[i for i in range(n+1)] def find(x): while(g[x]!=x): x=g[x] return x def union(x,y): if find(x)!=find(y): g[find(x)]=g[find(y)]=min(find(x),find(y)) ans=[[i] for i in range(n+1)] for i,j in p: a=find(i) b=find(j) if b<a: ans[b]+=ans[a] else: ans[a]+=ans[b] union(i,j) print(*ans[1]) ```
output
1
100,373
14
200,747
Provide tags and a correct Python 3 solution for this coding contest problem. Asya loves animals very much. Recently, she purchased n kittens, enumerated them from 1 and n and then put them into the cage. The cage consists of one row of n cells, enumerated with integers from 1 to n from left to right. Adjacent cells had a partially transparent partition wall between them, hence there were n - 1 partitions originally. Initially, each cell contained exactly one kitten with some number. Observing the kittens, Asya noticed, that they are very friendly and often a pair of kittens in neighboring cells wants to play together. So Asya started to remove partitions between neighboring cells. In particular, on the day i, Asya: * Noticed, that the kittens x_i and y_i, located in neighboring cells want to play together. * Removed the partition between these two cells, efficiently creating a single cell, having all kittens from two original cells. Since Asya has never putted partitions back, after n - 1 days the cage contained a single cell, having all kittens. For every day, Asya remembers numbers of kittens x_i and y_i, who wanted to play together, however she doesn't remember how she placed kittens in the cage in the beginning. Please help her and find any possible initial arrangement of the kittens into n cells. Input The first line contains a single integer n (2 ≀ n ≀ 150 000) β€” the number of kittens. Each of the following n - 1 lines contains integers x_i and y_i (1 ≀ x_i, y_i ≀ n, x_i β‰  y_i) β€” indices of kittens, which got together due to the border removal on the corresponding day. It's guaranteed, that the kittens x_i and y_i were in the different cells before this day. Output For every cell from 1 to n print a single integer β€” the index of the kitten from 1 to n, who was originally in it. All printed integers must be distinct. It's guaranteed, that there is at least one answer possible. In case there are multiple possible answers, print any of them. Example Input 5 1 4 2 5 3 1 4 5 Output 3 1 4 2 5 Note The answer for the example contains one of several possible initial arrangements of the kittens. The picture below shows how the cells were united for this initial arrangement. Note, that the kittens who wanted to play together on each day were indeed in adjacent cells. <image>
instruction
0
100,374
14
200,748
Tags: constructive algorithms, dsu Correct Solution: ``` n = int(input()) class Element: def __init__(self, x): self.things = [x] self.rep = x class DisjSet: def __init__(self, n): # Constructor to create and # initialize sets of n items self.rank = [1] * n self.elements = [Element(x) for x in range(n)] # Finds set of given item x def find(self, x): # Finds the representative of the set # that x is an element of if (self.elements[x].rep != x): # if x is not the parent of itself # Then x is not the representative of # its set, self.elements[x].rep = self.find(self.elements[x].rep) # so we recursively call Find on its parent # and move i's node directly under the # representative of this set return self.elements[x].rep # Do union of two sets represented # by x and y. def Union(self, x, y): # Find current sets of x and y xset = self.find(x) yset = self.find(y) # If they are already in same set if xset == yset: return # Put smaller ranked item under # bigger ranked item if ranks are # different if self.rank[xset] < self.rank[yset]: self.elements[xset].rep = yset self.elements[yset].things.extend(self.elements[xset].things) elif self.rank[xset] > self.rank[yset]: self.elements[yset].rep = xset self.elements[xset].things.extend(self.elements[yset].things) # If ranks are same, then move y under # x (doesn't matter which one goes where) # and increment rank of x's tree else: self.elements[yset].rep = xset self.rank[xset] = self.rank[xset] + 1 self.elements[xset].things.extend(self.elements[yset].things) obj = DisjSet(n) for _ in range(n - 1): a, b = list(map(lambda x: int(x), input().split())) obj.Union(a-1,b-1) for ele in obj.elements: if len(ele.things)==n: print(*[x+1 for x in ele.things]) ```
output
1
100,374
14
200,749
Provide tags and a correct Python 3 solution for this coding contest problem. Asya loves animals very much. Recently, she purchased n kittens, enumerated them from 1 and n and then put them into the cage. The cage consists of one row of n cells, enumerated with integers from 1 to n from left to right. Adjacent cells had a partially transparent partition wall between them, hence there were n - 1 partitions originally. Initially, each cell contained exactly one kitten with some number. Observing the kittens, Asya noticed, that they are very friendly and often a pair of kittens in neighboring cells wants to play together. So Asya started to remove partitions between neighboring cells. In particular, on the day i, Asya: * Noticed, that the kittens x_i and y_i, located in neighboring cells want to play together. * Removed the partition between these two cells, efficiently creating a single cell, having all kittens from two original cells. Since Asya has never putted partitions back, after n - 1 days the cage contained a single cell, having all kittens. For every day, Asya remembers numbers of kittens x_i and y_i, who wanted to play together, however she doesn't remember how she placed kittens in the cage in the beginning. Please help her and find any possible initial arrangement of the kittens into n cells. Input The first line contains a single integer n (2 ≀ n ≀ 150 000) β€” the number of kittens. Each of the following n - 1 lines contains integers x_i and y_i (1 ≀ x_i, y_i ≀ n, x_i β‰  y_i) β€” indices of kittens, which got together due to the border removal on the corresponding day. It's guaranteed, that the kittens x_i and y_i were in the different cells before this day. Output For every cell from 1 to n print a single integer β€” the index of the kitten from 1 to n, who was originally in it. All printed integers must be distinct. It's guaranteed, that there is at least one answer possible. In case there are multiple possible answers, print any of them. Example Input 5 1 4 2 5 3 1 4 5 Output 3 1 4 2 5 Note The answer for the example contains one of several possible initial arrangements of the kittens. The picture below shows how the cells were united for this initial arrangement. Note, that the kittens who wanted to play together on each day were indeed in adjacent cells. <image>
instruction
0
100,375
14
200,750
Tags: constructive algorithms, dsu Correct Solution: ``` import sys input = sys.stdin.readline class Unionfind: def __init__(self, n): self.par = [-1]*n self.left = [-1]*n self.right = [-1]*n self.most_right = [i for i in range(n)] def root(self, x): p = x while not self.par[p]<0: p = self.par[p] while x!=p: tmp = x x = self.par[x] self.par[tmp] = p return p def unite(self, x, y): rx, ry = self.root(x), self.root(y) self.par[ry] = rx self.left[ry] = self.most_right[rx] self.right[self.most_right[rx]] = ry self.most_right[rx] = self.most_right[ry] n = int(input()) uf = Unionfind(n) for _ in range(n-1): x, y = map(int, input().split()) uf.unite(x-1, y-1) ans = [] for i in range(n): if uf.left[i]==-1: ans.append(i) break for _ in range(n-1): ans.append(uf.right[ans[-1]]) ans = list(map(lambda x: x+1, ans)) print(*ans) ```
output
1
100,375
14
200,751
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Asya loves animals very much. Recently, she purchased n kittens, enumerated them from 1 and n and then put them into the cage. The cage consists of one row of n cells, enumerated with integers from 1 to n from left to right. Adjacent cells had a partially transparent partition wall between them, hence there were n - 1 partitions originally. Initially, each cell contained exactly one kitten with some number. Observing the kittens, Asya noticed, that they are very friendly and often a pair of kittens in neighboring cells wants to play together. So Asya started to remove partitions between neighboring cells. In particular, on the day i, Asya: * Noticed, that the kittens x_i and y_i, located in neighboring cells want to play together. * Removed the partition between these two cells, efficiently creating a single cell, having all kittens from two original cells. Since Asya has never putted partitions back, after n - 1 days the cage contained a single cell, having all kittens. For every day, Asya remembers numbers of kittens x_i and y_i, who wanted to play together, however she doesn't remember how she placed kittens in the cage in the beginning. Please help her and find any possible initial arrangement of the kittens into n cells. Input The first line contains a single integer n (2 ≀ n ≀ 150 000) β€” the number of kittens. Each of the following n - 1 lines contains integers x_i and y_i (1 ≀ x_i, y_i ≀ n, x_i β‰  y_i) β€” indices of kittens, which got together due to the border removal on the corresponding day. It's guaranteed, that the kittens x_i and y_i were in the different cells before this day. Output For every cell from 1 to n print a single integer β€” the index of the kitten from 1 to n, who was originally in it. All printed integers must be distinct. It's guaranteed, that there is at least one answer possible. In case there are multiple possible answers, print any of them. Example Input 5 1 4 2 5 3 1 4 5 Output 3 1 4 2 5 Note The answer for the example contains one of several possible initial arrangements of the kittens. The picture below shows how the cells were united for this initial arrangement. Note, that the kittens who wanted to play together on each day were indeed in adjacent cells. <image> Submitted Solution: ``` class Disjoint_set: class node: def __init__(self,a): self.value=a self.p=self self.rank=0 self.ans=[a] def __init__(self,a): self.data={i:self.make_set(i) for i in range(1,a+1)} def make_set(self,val): return self.node(val) def union(self,x,y): self.link(self.find_set(self.data[x]),self.find_set(self.data[y])) def link(self,val1,val2): if val1.rank>val2.rank: val2.p=val1 val1.ans.extend(val2.ans) val1.rank+=1 else: val1.p=val2 val2.ans.extend(val1.ans) val2.rank+=1 def find_set(self,val): if val.p.value!=val.value: val.p=self.find_set(val.p) return val.p def sol(self): x=1 for i in self.data: x=i break print(*(self.find_set(self.data[x])).ans) n=int(input()) a=[list(map(int,input().split())) for i in range(n-1)] sa=Disjoint_set(n) for i,j in a: sa.union(i,j) sa.sol() ```
instruction
0
100,376
14
200,752
Yes
output
1
100,376
14
200,753
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Asya loves animals very much. Recently, she purchased n kittens, enumerated them from 1 and n and then put them into the cage. The cage consists of one row of n cells, enumerated with integers from 1 to n from left to right. Adjacent cells had a partially transparent partition wall between them, hence there were n - 1 partitions originally. Initially, each cell contained exactly one kitten with some number. Observing the kittens, Asya noticed, that they are very friendly and often a pair of kittens in neighboring cells wants to play together. So Asya started to remove partitions between neighboring cells. In particular, on the day i, Asya: * Noticed, that the kittens x_i and y_i, located in neighboring cells want to play together. * Removed the partition between these two cells, efficiently creating a single cell, having all kittens from two original cells. Since Asya has never putted partitions back, after n - 1 days the cage contained a single cell, having all kittens. For every day, Asya remembers numbers of kittens x_i and y_i, who wanted to play together, however she doesn't remember how she placed kittens in the cage in the beginning. Please help her and find any possible initial arrangement of the kittens into n cells. Input The first line contains a single integer n (2 ≀ n ≀ 150 000) β€” the number of kittens. Each of the following n - 1 lines contains integers x_i and y_i (1 ≀ x_i, y_i ≀ n, x_i β‰  y_i) β€” indices of kittens, which got together due to the border removal on the corresponding day. It's guaranteed, that the kittens x_i and y_i were in the different cells before this day. Output For every cell from 1 to n print a single integer β€” the index of the kitten from 1 to n, who was originally in it. All printed integers must be distinct. It's guaranteed, that there is at least one answer possible. In case there are multiple possible answers, print any of them. Example Input 5 1 4 2 5 3 1 4 5 Output 3 1 4 2 5 Note The answer for the example contains one of several possible initial arrangements of the kittens. The picture below shows how the cells were united for this initial arrangement. Note, that the kittens who wanted to play together on each day were indeed in adjacent cells. <image> Submitted Solution: ``` nxt= [] last = [] rt = [] def dsu(n): for i in range(0,n+1): rt.append(-1) last.append(i) nxt.append(-1) def root(x): if(rt[x]<0): return x else: rt[x] = root(rt[x]) return rt[x] def union(x,y): x = root(x) y = root(y) if(x!=y): rt[x] = rt[x]+rt[y] rt[y] = x nxt[last[x]] = y last[x] = last[y] n = int(input()) dsu(n) for z in range(n-1): x,y = [int(i) for i in input().split()] union(x,y) i = root(1) while(i!=-1): print(i,end= " ") i = nxt[i] ```
instruction
0
100,377
14
200,754
Yes
output
1
100,377
14
200,755
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Asya loves animals very much. Recently, she purchased n kittens, enumerated them from 1 and n and then put them into the cage. The cage consists of one row of n cells, enumerated with integers from 1 to n from left to right. Adjacent cells had a partially transparent partition wall between them, hence there were n - 1 partitions originally. Initially, each cell contained exactly one kitten with some number. Observing the kittens, Asya noticed, that they are very friendly and often a pair of kittens in neighboring cells wants to play together. So Asya started to remove partitions between neighboring cells. In particular, on the day i, Asya: * Noticed, that the kittens x_i and y_i, located in neighboring cells want to play together. * Removed the partition between these two cells, efficiently creating a single cell, having all kittens from two original cells. Since Asya has never putted partitions back, after n - 1 days the cage contained a single cell, having all kittens. For every day, Asya remembers numbers of kittens x_i and y_i, who wanted to play together, however she doesn't remember how she placed kittens in the cage in the beginning. Please help her and find any possible initial arrangement of the kittens into n cells. Input The first line contains a single integer n (2 ≀ n ≀ 150 000) β€” the number of kittens. Each of the following n - 1 lines contains integers x_i and y_i (1 ≀ x_i, y_i ≀ n, x_i β‰  y_i) β€” indices of kittens, which got together due to the border removal on the corresponding day. It's guaranteed, that the kittens x_i and y_i were in the different cells before this day. Output For every cell from 1 to n print a single integer β€” the index of the kitten from 1 to n, who was originally in it. All printed integers must be distinct. It's guaranteed, that there is at least one answer possible. In case there are multiple possible answers, print any of them. Example Input 5 1 4 2 5 3 1 4 5 Output 3 1 4 2 5 Note The answer for the example contains one of several possible initial arrangements of the kittens. The picture below shows how the cells were united for this initial arrangement. Note, that the kittens who wanted to play together on each day were indeed in adjacent cells. <image> Submitted Solution: ``` from collections import defaultdict import sys import bisect input=sys.stdin.readline def find(x): while p[x]!=x: x=p[p[x]] return(x) def union(a,b): if (sz[a]>sz[b]): p[b]=a ans[a]+=ans[b] elif (sz[a]<sz[b]): p[a]=b ans[b]+=ans[a] else: p[b]=a ans[a]+=ans[b] sz[a]+=1 n=int(input()) p=[i for i in range(n)] ans=[[i] for i in range(n)] sz=[0]*(n) for ii in range(n-1): u,v=map(int,input().split()) u-=1 v-=1 a=find(u) b=find(v) union(a,b) leng,out=0,[] for i in ans: if len(i)>leng: leng=len(i) out=i out=[i+1 for i in out] print(*out) ```
instruction
0
100,378
14
200,756
Yes
output
1
100,378
14
200,757
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Asya loves animals very much. Recently, she purchased n kittens, enumerated them from 1 and n and then put them into the cage. The cage consists of one row of n cells, enumerated with integers from 1 to n from left to right. Adjacent cells had a partially transparent partition wall between them, hence there were n - 1 partitions originally. Initially, each cell contained exactly one kitten with some number. Observing the kittens, Asya noticed, that they are very friendly and often a pair of kittens in neighboring cells wants to play together. So Asya started to remove partitions between neighboring cells. In particular, on the day i, Asya: * Noticed, that the kittens x_i and y_i, located in neighboring cells want to play together. * Removed the partition between these two cells, efficiently creating a single cell, having all kittens from two original cells. Since Asya has never putted partitions back, after n - 1 days the cage contained a single cell, having all kittens. For every day, Asya remembers numbers of kittens x_i and y_i, who wanted to play together, however she doesn't remember how she placed kittens in the cage in the beginning. Please help her and find any possible initial arrangement of the kittens into n cells. Input The first line contains a single integer n (2 ≀ n ≀ 150 000) β€” the number of kittens. Each of the following n - 1 lines contains integers x_i and y_i (1 ≀ x_i, y_i ≀ n, x_i β‰  y_i) β€” indices of kittens, which got together due to the border removal on the corresponding day. It's guaranteed, that the kittens x_i and y_i were in the different cells before this day. Output For every cell from 1 to n print a single integer β€” the index of the kitten from 1 to n, who was originally in it. All printed integers must be distinct. It's guaranteed, that there is at least one answer possible. In case there are multiple possible answers, print any of them. Example Input 5 1 4 2 5 3 1 4 5 Output 3 1 4 2 5 Note The answer for the example contains one of several possible initial arrangements of the kittens. The picture below shows how the cells were united for this initial arrangement. Note, that the kittens who wanted to play together on each day were indeed in adjacent cells. <image> Submitted Solution: ``` class DisjointSets: parent = {} rank = {} ls = {} def __init__(self, n): for i in range(1, n + 1): self.makeSet(i) def makeSet(self, node): self.node = node self.parent[node] = node self.rank[node] = 1 self.ls[node] = [node] def find(self, node): parent = self.parent if parent[node] != node: parent[node] = self.find(parent[node]) return parent[node] def union(self, node, other): node = self.find(node) other = self.find(other) if self.rank[node] > self.rank[other]: self.parent[other] = node self.ls[node].extend(self.ls[other]) elif self.rank[other] > self.rank[node]: self.parent[node] = other self.ls[other].extend(self.ls[node]) else: self.parent[other] = node self.ls[node] += self.ls[other] self.rank[node] += 1 n = int(input()) ds = DisjointSets(n) for i in range(n - 1): a, b = map(int, input().split()) ds.union(a, b) print(" ".join(map(str, ds.ls[ds.find(1)]))) ```
instruction
0
100,379
14
200,758
Yes
output
1
100,379
14
200,759
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Asya loves animals very much. Recently, she purchased n kittens, enumerated them from 1 and n and then put them into the cage. The cage consists of one row of n cells, enumerated with integers from 1 to n from left to right. Adjacent cells had a partially transparent partition wall between them, hence there were n - 1 partitions originally. Initially, each cell contained exactly one kitten with some number. Observing the kittens, Asya noticed, that they are very friendly and often a pair of kittens in neighboring cells wants to play together. So Asya started to remove partitions between neighboring cells. In particular, on the day i, Asya: * Noticed, that the kittens x_i and y_i, located in neighboring cells want to play together. * Removed the partition between these two cells, efficiently creating a single cell, having all kittens from two original cells. Since Asya has never putted partitions back, after n - 1 days the cage contained a single cell, having all kittens. For every day, Asya remembers numbers of kittens x_i and y_i, who wanted to play together, however she doesn't remember how she placed kittens in the cage in the beginning. Please help her and find any possible initial arrangement of the kittens into n cells. Input The first line contains a single integer n (2 ≀ n ≀ 150 000) β€” the number of kittens. Each of the following n - 1 lines contains integers x_i and y_i (1 ≀ x_i, y_i ≀ n, x_i β‰  y_i) β€” indices of kittens, which got together due to the border removal on the corresponding day. It's guaranteed, that the kittens x_i and y_i were in the different cells before this day. Output For every cell from 1 to n print a single integer β€” the index of the kitten from 1 to n, who was originally in it. All printed integers must be distinct. It's guaranteed, that there is at least one answer possible. In case there are multiple possible answers, print any of them. Example Input 5 1 4 2 5 3 1 4 5 Output 3 1 4 2 5 Note The answer for the example contains one of several possible initial arrangements of the kittens. The picture below shows how the cells were united for this initial arrangement. Note, that the kittens who wanted to play together on each day were indeed in adjacent cells. <image> Submitted Solution: ``` n=int(input()) dct={} l=1 for i in range(n-1): u,v=map(int,input().split()) u,v=min(u,v),max(u,v) if u in dct and v in dct: dct[u]=dct[u]+dct[v] dct[v]=dct[u] elif u in dct: dct[u]+=[v] dct[v]=dct[u] elif v in dct: dct[v]+=[u] dct[u]=dct[v] else: dct[u]=[u,v] dct[v]=dct[u] for i in dct[u]: print(i,end=" ") ```
instruction
0
100,380
14
200,760
No
output
1
100,380
14
200,761
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Asya loves animals very much. Recently, she purchased n kittens, enumerated them from 1 and n and then put them into the cage. The cage consists of one row of n cells, enumerated with integers from 1 to n from left to right. Adjacent cells had a partially transparent partition wall between them, hence there were n - 1 partitions originally. Initially, each cell contained exactly one kitten with some number. Observing the kittens, Asya noticed, that they are very friendly and often a pair of kittens in neighboring cells wants to play together. So Asya started to remove partitions between neighboring cells. In particular, on the day i, Asya: * Noticed, that the kittens x_i and y_i, located in neighboring cells want to play together. * Removed the partition between these two cells, efficiently creating a single cell, having all kittens from two original cells. Since Asya has never putted partitions back, after n - 1 days the cage contained a single cell, having all kittens. For every day, Asya remembers numbers of kittens x_i and y_i, who wanted to play together, however she doesn't remember how she placed kittens in the cage in the beginning. Please help her and find any possible initial arrangement of the kittens into n cells. Input The first line contains a single integer n (2 ≀ n ≀ 150 000) β€” the number of kittens. Each of the following n - 1 lines contains integers x_i and y_i (1 ≀ x_i, y_i ≀ n, x_i β‰  y_i) β€” indices of kittens, which got together due to the border removal on the corresponding day. It's guaranteed, that the kittens x_i and y_i were in the different cells before this day. Output For every cell from 1 to n print a single integer β€” the index of the kitten from 1 to n, who was originally in it. All printed integers must be distinct. It's guaranteed, that there is at least one answer possible. In case there are multiple possible answers, print any of them. Example Input 5 1 4 2 5 3 1 4 5 Output 3 1 4 2 5 Note The answer for the example contains one of several possible initial arrangements of the kittens. The picture below shows how the cells were united for this initial arrangement. Note, that the kittens who wanted to play together on each day were indeed in adjacent cells. <image> Submitted Solution: ``` from collections import defaultdict # def dfs(visited, vertex, g): # visited[vertex] = True # print(vertex + 1,end=" ") # for i in g[vertex]: # if visited[i] is False: # dfs(visited, i, g) n = int(input()) g = defaultdict(list) visited = [False] * n for i in range(n-1): u, v = map(int, input().split()) g[u-1].append(v-1) g[v-1].append(u-1) for i in range(n): q = [i] if visited[i] is False: while len(q): vertex = q.pop(0) visited[vertex] = True print(vertex + 1, end= " ") for i in g[vertex]: if visited[i] is False: q.append(i) ```
instruction
0
100,381
14
200,762
No
output
1
100,381
14
200,763
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Asya loves animals very much. Recently, she purchased n kittens, enumerated them from 1 and n and then put them into the cage. The cage consists of one row of n cells, enumerated with integers from 1 to n from left to right. Adjacent cells had a partially transparent partition wall between them, hence there were n - 1 partitions originally. Initially, each cell contained exactly one kitten with some number. Observing the kittens, Asya noticed, that they are very friendly and often a pair of kittens in neighboring cells wants to play together. So Asya started to remove partitions between neighboring cells. In particular, on the day i, Asya: * Noticed, that the kittens x_i and y_i, located in neighboring cells want to play together. * Removed the partition between these two cells, efficiently creating a single cell, having all kittens from two original cells. Since Asya has never putted partitions back, after n - 1 days the cage contained a single cell, having all kittens. For every day, Asya remembers numbers of kittens x_i and y_i, who wanted to play together, however she doesn't remember how she placed kittens in the cage in the beginning. Please help her and find any possible initial arrangement of the kittens into n cells. Input The first line contains a single integer n (2 ≀ n ≀ 150 000) β€” the number of kittens. Each of the following n - 1 lines contains integers x_i and y_i (1 ≀ x_i, y_i ≀ n, x_i β‰  y_i) β€” indices of kittens, which got together due to the border removal on the corresponding day. It's guaranteed, that the kittens x_i and y_i were in the different cells before this day. Output For every cell from 1 to n print a single integer β€” the index of the kitten from 1 to n, who was originally in it. All printed integers must be distinct. It's guaranteed, that there is at least one answer possible. In case there are multiple possible answers, print any of them. Example Input 5 1 4 2 5 3 1 4 5 Output 3 1 4 2 5 Note The answer for the example contains one of several possible initial arrangements of the kittens. The picture below shows how the cells were united for this initial arrangement. Note, that the kittens who wanted to play together on each day were indeed in adjacent cells. <image> Submitted Solution: ``` """ 5 1 4 2 5 3 1 4 5 """ n = int(input()) d = dict() l = [] for _ in range(n-1): l += [list(map(int, input().split()))] for pair in l: print(pair) p0 = pair[0] in d.keys() p1 = pair[1] in d.keys() if not (p0 or p1): d[pair[0]] = [pair[0], pair[1]] d[pair[1]] = [pair[0], pair[1]] elif p0 and not p1: d[pair[0]] = d[pair[0]] + [pair[1]] d[pair[1]] = d[pair[0]] elif p1 and not p0: d[pair[1]] = d[pair[1]] + [pair[0]] else: d[pair[0]] = d[pair[0]] + d[pair[1]] d[pair[1]] = d[pair[0]] for i in d[pair[1]]: d[i] = d[pair[1]] print(' '.join(map(str,d[1]))) ```
instruction
0
100,382
14
200,764
No
output
1
100,382
14
200,765