message
stringlengths
2
22.8k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
16
109k
cluster
float64
1
1
__index_level_0__
int64
32
217k
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N cities numbered 1 to N, connected by M railroads. You are now at City 1, with 10^{100} gold coins and S silver coins in your pocket. The i-th railroad connects City U_i and City V_i bidirectionally, and a one-way trip costs A_i silver coins and takes B_i minutes. You cannot use gold coins to pay the fare. There is an exchange counter in each city. At the exchange counter in City i, you can get C_i silver coins for 1 gold coin. The transaction takes D_i minutes for each gold coin you give. You can exchange any number of gold coins at each exchange counter. For each t=2, ..., N, find the minimum time needed to travel from City 1 to City t. You can ignore the time spent waiting for trains. Constraints * 2 \leq N \leq 50 * N-1 \leq M \leq 100 * 0 \leq S \leq 10^9 * 1 \leq A_i \leq 50 * 1 \leq B_i,C_i,D_i \leq 10^9 * 1 \leq U_i < V_i \leq N * There is no pair i, j(i \neq j) such that (U_i,V_i)=(U_j,V_j). * Each city t=2,...,N can be reached from City 1 with some number of railroads. * All values in input are integers. Input Input is given from Standard Input in the following format: N M S U_1 V_1 A_1 B_1 : U_M V_M A_M B_M C_1 D_1 : C_N D_N Output For each t=2, ..., N in this order, print a line containing the minimum time needed to travel from City 1 to City t. Examples Input 3 2 1 1 2 1 2 1 3 2 4 1 11 1 2 2 5 Output 2 14 Input 4 4 1 1 2 1 5 1 3 4 4 2 4 2 2 3 4 1 1 3 1 3 1 5 2 6 4 Output 5 5 7 Input 6 5 1 1 2 1 1 1 3 2 1 2 4 5 1 3 5 11 1 1 6 50 1 1 10000 1 3000 1 700 1 100 1 1 100 1 Output 1 9003 14606 16510 16576 Input 4 6 1000000000 1 2 50 1 1 3 50 5 1 4 50 7 2 3 50 2 2 4 50 4 3 4 50 3 10 2 4 4 5 5 7 7 Output 1 3 5 Input 2 1 0 1 2 1 1 1 1000000000 1 1 Output 1000000001 Submitted Solution: ``` import sys,bisect,string,math,time,functools,random from heapq import heappush,heappop,heapify from collections import deque,defaultdict,Counter from itertools import permutations,combinations,groupby def Golf():*a,=map(int,open(0)) def I():return int(input()) def S_():return input() def IS():return input().split() def LS():return [i for i in input().split()] def LI():return [int(i) for i in input().split()] def LI_():return [int(i)-1 for i in input().split()] def NI(n):return [int(input()) for i in range(n)] def NI_(n):return [int(input())-1 for i in range(n)] def StoLI():return [ord(i)-97 for i in input()] def ItoS(n):return chr(n+97) def LtoS(ls):return ''.join([chr(i+97) for i in ls]) def GI(V,E,ls=None,Directed=False,index=1): org_inp=[];g=[[] for i in range(V)] FromStdin=True if ls==None else False for i in range(E): if FromStdin: inp=LI() a,b,c=(inp+[1])[:3] org_inp.append(inp) else: #index=0 a,b,c=(list(ls[i])+[1])[:3] if index==1:a-=1;b-=1 aa=(a,c);bb=(b,c);g[a].append(bb) if not Directed:g[b].append(aa) return g,org_inp def GGI(h,w,search=None,replacement_of_found='.',mp_def={'#':1,'.':0}): #h,w,g,sg=GGI(h,w,search=['S','G'],replacement_of_found='.',mp_def={'#':1,'.':0}) # sample usage mp=[1]*(w+2);found={} for i in range(h): s=input() for char in search: if char in s: found[char]=((i+1)*(w+2)+s.index(char)+1) mp_def[char]=mp_def[replacement_of_found] mp+=[1]+[mp_def[j] for j in s]+[1] mp+=[1]*(w+2) return h+2,w+2,mp,found def TI(n):return GI(n,n-1) def bit_combination(k,n=2): rt=[] for tb in range(n**k): s=[tb//(n**bt)%n for bt in range(k)];rt+=[s] return rt def show(*inp,end='\n'): if show_flg:print(*inp,end=end) YN=['YES','NO'];Yn=['Yes','No'] mo=10**9+7 inf=float('inf') l_alp=string.ascii_lowercase #sys.setrecursionlimit(10**7) input=lambda: sys.stdin.readline().rstrip() class Comb: def __init__(self,n,mo=10**9+7): self.fac=[0]*(n+1) self.inv=[1]*(n+1) self.fac[0]=1 self.fact(n) for i in range(1,n+1): self.fac[i]=i*self.fac[i-1]%mo self.inv[n]*=i self.inv[n]%=mo self.inv[n]=pow(self.inv[n],mo-2,mo) for i in range(1,n): self.inv[n-i]=self.inv[n-i+1]*(n-i+1)%mo return def fact(self,n): return self.fac[n] def invf(self,n): return self.inv[n] def comb(self,x,y): if y<0 or y>x: return 0 return self.fac[x]*self.inv[x-y]*self.inv[y]%mo show_flg=False show_flg=True ## Segment Tree ## ## Initializer Template ## # Range Sum: sg=SegTree(n) # Range Minimum: sg=SegTree(n,inf,min,inf) class SegTree: def __init__(self,n,init_ls=None,init_val=0,function=lambda a,b:a+b,ide=0): self.n=n self.ide_ele=ide_ele=ide self.num=num=2**(n-1).bit_length() self.seg=seg=[self.ide_ele]*2*self.num self.lazy=lazy=[self.ide_ele]*2*self.num self.segfun=segfun=function #set_val if init_ls==None: for i in range(n): self.seg[i+self.num-1]=init_val else: for i in range(n): self.seg[i+self.num-1]=init_ls[i] #build for i in range(self.num-2,-1,-1): self.seg[i]=self.segfun(self.seg[2*i+1],self.seg[2*i+2]) def update(self,k,x): k += self.num-1 self.seg[k] = x while k: k = (k-1)//2 self.seg[k] = self.segfun(self.seg[k*2+1],self.seg[k*2+2]) def evaluate(k,l,r): #遅廢評侑処理 if lazy[k]!=0: node[k]+=lazy[k] if(r-l>1): lazy[2*k+1]+=lazy[k]//2 lazy[2*k+2]+=lazy[k]//2 lazy[k]=0 def query(self,p,q): if q<=p: return self.ide_ele p += self.num-1 q += self.num-2 res=self.ide_ele while q-p>1: if p&1 == 0: res = self.segfun(res,self.seg[p]) if q&1 == 1: res = self.segfun(res,self.seg[q]) q -= 1 p = p//2 q = (q-1)//2 if p == q: res = self.segfun(res,self.seg[p]) else: res = self.segfun(self.segfun(res,self.seg[p]),self.seg[q]) return res def find_min_index(self,p,q,m): if q<=p: return self.ide_ele p += self.num-1 q += self.num-2 res=self.ide_ele while q-p>1: if p&1 == 0: res = self.segfun(res,self.seg[p]) if q&1 == 1: res = self.segfun(res,self.seg[q]) q -= 1 p >>= 1 q = (q-1)//2 if p == q: res = self.segfun(res,self.seg[p]) else: res = self.segfun(self.segfun(res,self.seg[p]),self.seg[q]) return res def __str__(self): # η”Ÿι…εˆ—γ‚’θ‘¨η€Ί rt=self.seg[self.num-1:self.num-1+self.n] return str(rt) class Tree: def __init__(self,inp_size=None,init=True): if init: self.stdin(inp_size) return def stdin(self,inp_size=None): if inp_size==None: self.size=int(input()) else: self.size=inp_size self.edges,_=GI(self.size,self.size-1) return def listin(self,ls): self.size=len(ls)+1 self.edges,_=GI(self.size,self.size-1,ls) return def __str__(self): return str(self.edges) def dfs(self,x,func=lambda prv,nx,dist:prv+dist,root_v=0): q=deque() q.append(x) v=[-1]*self.size v[x]=root_v while q: c=q.pop() for nb,d in self.edges[c]: if v[nb]==-1: q.append(nb) v[nb]=func(v[c],nb,d) return v def EulerTour(self,x,func=lambda prv,nx,dist:prv+dist,root_v=0): q=deque() q.append((-1,x)) v=[None]*self.size v[x]=root_v et=[] while q: cb,ce=q.pop() et.append(ce) for nb,d in self.edges[ce]: if v[nb]==None: q.append((nb,ce)) q.append((ce,nb)) v[nb]=func(v[ce],nb,d) vid=[[-1,-1]for i in range(self.size)] for i,j in enumerate(et): if vid[j][0]==-1: vid[j][0]=i else: vid[j][1]=i return v,et,vid def LCA_init(self,depth,et): self.st=SegTree(self.size*2-1,func=min,ide=inf) for i,j in enumerate(et): self.st.update(i,j) self.LCA_init_stat==True return def LCA(self,root,x,y): if self.LCA_init_stat==False: depth,et,vid=self.EulerTour(root) self.LCA_init(depth,et) return self.st.query(x,y+1) def dijkstra(edge,st): # edge=[[(v_to,dist_to_v),...],[],...] # initialize: def: d=dist(st,i), prev=[previous vertex in minimum path], q[] n=len(edge) d=[(0 if st==i else inf) for i in range(n)] prev=[0]*n q=[(j,i) for i,j in enumerate(d)] heapify(q) # calc while q: dist,cur=heappop(q) for dst,dist in edge[cur]: alt=d[cur]+dist if alt<d[dst]: d[dst]=alt prev[dst]=cur heappush(q,(alt,dst)) return d,prev ans=0 n,m,s=LI() t_ls=[] p_ls=[] k=2501 s=min(k-1,s) for i in range(m): u,v,a,b=LI() p_ls+=[(u,v,a)] u-=1 v-=1 for j in range(a,k): t_ls+=[(u*k+j,v*k+j-a,b)] C,D=[],[] for i in range(n): c,d=LI() for j in range(k): t_ls+=[(i*k+j,i*k+min(j+c,k-1),d)] C+=[c] D+=[d] gp,_=GI(n,m,ls=p_ls,index=1) gt,_=GI(n*k,len(t_ls),ls=t_ls,index=0) dist_t,_=dijkstra(gt,i*k+s) for i in range(1,n): ans=inf for j in range(k): alt=dist_t[i*k+j] if alt<ans: ans=alt print(ans) ```
instruction
0
34,031
1
68,062
No
output
1
34,031
1
68,063
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N cities numbered 1 to N, connected by M railroads. You are now at City 1, with 10^{100} gold coins and S silver coins in your pocket. The i-th railroad connects City U_i and City V_i bidirectionally, and a one-way trip costs A_i silver coins and takes B_i minutes. You cannot use gold coins to pay the fare. There is an exchange counter in each city. At the exchange counter in City i, you can get C_i silver coins for 1 gold coin. The transaction takes D_i minutes for each gold coin you give. You can exchange any number of gold coins at each exchange counter. For each t=2, ..., N, find the minimum time needed to travel from City 1 to City t. You can ignore the time spent waiting for trains. Constraints * 2 \leq N \leq 50 * N-1 \leq M \leq 100 * 0 \leq S \leq 10^9 * 1 \leq A_i \leq 50 * 1 \leq B_i,C_i,D_i \leq 10^9 * 1 \leq U_i < V_i \leq N * There is no pair i, j(i \neq j) such that (U_i,V_i)=(U_j,V_j). * Each city t=2,...,N can be reached from City 1 with some number of railroads. * All values in input are integers. Input Input is given from Standard Input in the following format: N M S U_1 V_1 A_1 B_1 : U_M V_M A_M B_M C_1 D_1 : C_N D_N Output For each t=2, ..., N in this order, print a line containing the minimum time needed to travel from City 1 to City t. Examples Input 3 2 1 1 2 1 2 1 3 2 4 1 11 1 2 2 5 Output 2 14 Input 4 4 1 1 2 1 5 1 3 4 4 2 4 2 2 3 4 1 1 3 1 3 1 5 2 6 4 Output 5 5 7 Input 6 5 1 1 2 1 1 1 3 2 1 2 4 5 1 3 5 11 1 1 6 50 1 1 10000 1 3000 1 700 1 100 1 1 100 1 Output 1 9003 14606 16510 16576 Input 4 6 1000000000 1 2 50 1 1 3 50 5 1 4 50 7 2 3 50 2 2 4 50 4 3 4 50 3 10 2 4 4 5 5 7 7 Output 1 3 5 Input 2 1 0 1 2 1 1 1 1000000000 1 1 Output 1000000001 Submitted Solution: ``` from heapq import * import sys input = sys.stdin.buffer.readline n, m, s = map(int, input().split()) lim = 2510 di = [[float("inf")] * lim for _ in range(n)] di[0][s] = 0 con = [[] for _ in range(n)] for _ in range(m): u, v, a, b = map(int, input().split()) con[u - 1].append((v - 1, a, b)) con[v - 1].append((u - 1, a, b)) q = [(0, s, 0)] cd = [list(map(int, input().split())) for _ in range(n)] while q: time, have, cur = heappop(q) if di[cur][have] < time: continue c, d = cd[cur] if have + c < lim and di[cur][have + c] > time + d: di[cur][have + c] = time + d heappush(q, (time + d, have + c, cur)) for nxt, dh, dt in con[cur]: if have >= dh and di[nxt][have - dh] > time + dt: di[nxt][have - dh] = time + dt heappush(q, (time + dt, have - dh, nxt)) for p in di[1:]: print(min(p)) ```
instruction
0
34,032
1
68,064
No
output
1
34,032
1
68,065
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N cities numbered 1 to N, connected by M railroads. You are now at City 1, with 10^{100} gold coins and S silver coins in your pocket. The i-th railroad connects City U_i and City V_i bidirectionally, and a one-way trip costs A_i silver coins and takes B_i minutes. You cannot use gold coins to pay the fare. There is an exchange counter in each city. At the exchange counter in City i, you can get C_i silver coins for 1 gold coin. The transaction takes D_i minutes for each gold coin you give. You can exchange any number of gold coins at each exchange counter. For each t=2, ..., N, find the minimum time needed to travel from City 1 to City t. You can ignore the time spent waiting for trains. Constraints * 2 \leq N \leq 50 * N-1 \leq M \leq 100 * 0 \leq S \leq 10^9 * 1 \leq A_i \leq 50 * 1 \leq B_i,C_i,D_i \leq 10^9 * 1 \leq U_i < V_i \leq N * There is no pair i, j(i \neq j) such that (U_i,V_i)=(U_j,V_j). * Each city t=2,...,N can be reached from City 1 with some number of railroads. * All values in input are integers. Input Input is given from Standard Input in the following format: N M S U_1 V_1 A_1 B_1 : U_M V_M A_M B_M C_1 D_1 : C_N D_N Output For each t=2, ..., N in this order, print a line containing the minimum time needed to travel from City 1 to City t. Examples Input 3 2 1 1 2 1 2 1 3 2 4 1 11 1 2 2 5 Output 2 14 Input 4 4 1 1 2 1 5 1 3 4 4 2 4 2 2 3 4 1 1 3 1 3 1 5 2 6 4 Output 5 5 7 Input 6 5 1 1 2 1 1 1 3 2 1 2 4 5 1 3 5 11 1 1 6 50 1 1 10000 1 3000 1 700 1 100 1 1 100 1 Output 1 9003 14606 16510 16576 Input 4 6 1000000000 1 2 50 1 1 3 50 5 1 4 50 7 2 3 50 2 2 4 50 4 3 4 50 3 10 2 4 4 5 5 7 7 Output 1 3 5 Input 2 1 0 1 2 1 1 1 1000000000 1 1 Output 1000000001 Submitted Solution: ``` # -*- coding: utf-8 -*- ################################## # University of Wisconsin-Madison # Author: Yaqi Zhang ################################## # standard library import collections import heapq import sys from functools import lru_cache sys.setrecursionlimit(10**9) Input = lambda: sys.stdin.readline().rstrip() IntRead = lambda: int(Input()) LineRead = lambda: list(map(int, Input().split())) INF = 10**18 def main(): N, M, S = LineRead() S = min(S, 50 * 50) # state: start at 1, for 2 ... N graph = [[] for _ in range(N + 1)] cs = [-1] ds = [-1] for _ in range(M): u, v, a, b = LineRead() graph[u].append((v, a, b)) graph[v].append((u, a, b)) for _ in range(N): c, d = LineRead() cs.append(c) ds.append(d) def dijkstra(start, end): pq = [(0, start, S)] dist = {} dist[(start, S)] = 0 # print("Hello") while pq: t, u, s = heapq.heappop(pq) # print(t, u, s) if u == end: return t if t < dist[u, s]: continue for v, a, b in graph[u]: if s >= a: if (v, s - a) not in dist or dist[v, s - a] > t + b: dist[v, s - a] = t + b heapq.heappush(pq, (t + b, v, s - a)) # exchange c = cs[u] d = ds[u] if s >= 2500: continue if (u, s + c) not in dist or dist[u, s + c] > t + d: dist[u, s + c] = t + d heapq.heappush(pq, (t + d, u, s + c)) for i in range(2, N + 1): print(dijkstra(1, i)) if __name__ == "__main__": main() ```
instruction
0
34,033
1
68,066
No
output
1
34,033
1
68,067
Provide tags and a correct Python 3 solution for this coding contest problem. There are n cities and n - 1 roads in the Seven Kingdoms, each road connects two cities and we can reach any city from any other by the roads. Theon and Yara Greyjoy are on a horse in the first city, they are starting traveling through the roads. But the weather is foggy, so they can’t see where the horse brings them. When the horse reaches a city (including the first one), it goes to one of the cities connected to the current city. But it is a strange horse, it only goes to cities in which they weren't before. In each such city, the horse goes with equal probabilities and it stops when there are no such cities. Let the length of each road be 1. The journey starts in the city 1. What is the expected length (expected value of length) of their journey? You can read about expected (average) value by the link <https://en.wikipedia.org/wiki/Expected_value>. Input The first line contains a single integer n (1 ≀ n ≀ 100000) β€” number of cities. Then n - 1 lines follow. The i-th line of these lines contains two integers ui and vi (1 ≀ ui, vi ≀ n, ui β‰  vi) β€” the cities connected by the i-th road. It is guaranteed that one can reach any city from any other by the roads. Output Print a number β€” the expected length of their journey. The journey starts in the city 1. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>. Examples Input 4 1 2 1 3 2 4 Output 1.500000000000000 Input 5 1 2 1 3 3 4 2 5 Output 2.000000000000000 Note In the first sample, their journey may end in cities 3 or 4 with equal probability. The distance to city 3 is 1 and to city 4 is 2, so the expected length is 1.5. In the second sample, their journey may end in city 4 or 5. The distance to the both cities is 2, so the expected length is 2.
instruction
0
34,836
1
69,672
Tags: dfs and similar, dp, graphs, probabilities, trees Correct Solution: ``` n = int(input()) a = [] for i in range(0, n): a.append([]) for i in range(0, n-1): u, v = map(int, input().split()) a[u-1].append(v-1) a[v-1].append(u-1) parent = [] t1 = [] t2 = [] ff = [] for i in range(0, n): ff.append(True) ff[0] = False parent.append([0, -1, 0]) t1.append(0) count = 1 while len(t1) > 0: for i in range(0, len(t1)): for j in range(0, len(a[t1[i]])): if ff[a[t1[i]][j]]: t2.append(a[t1[i]][j]) parent.append([count, t1[i], a[t1[i]][j]]) ff[a[t1[i]][j]] = False count += 1 t1, t2 = t2, t1 t2 = [] parent.sort() cost = [] for i in range(0, n): cost.append(0) cost[0] = 1 for i in range(1, n): if parent[i][1] == 0: cost[parent[i][2]] = cost[parent[i][1]]/len(a[parent[i][1]]) else: cost[parent[i][2]] = cost[parent[i][1]]/(len(a[parent[i][1]])-1) ans = 0 if len(a[0]) > 0: ans += 1 for i in range(1, n): if len(a[i]) > 1: ans += cost[i] print(ans) ```
output
1
34,836
1
69,673
Provide tags and a correct Python 3 solution for this coding contest problem. There are n cities and n - 1 roads in the Seven Kingdoms, each road connects two cities and we can reach any city from any other by the roads. Theon and Yara Greyjoy are on a horse in the first city, they are starting traveling through the roads. But the weather is foggy, so they can’t see where the horse brings them. When the horse reaches a city (including the first one), it goes to one of the cities connected to the current city. But it is a strange horse, it only goes to cities in which they weren't before. In each such city, the horse goes with equal probabilities and it stops when there are no such cities. Let the length of each road be 1. The journey starts in the city 1. What is the expected length (expected value of length) of their journey? You can read about expected (average) value by the link <https://en.wikipedia.org/wiki/Expected_value>. Input The first line contains a single integer n (1 ≀ n ≀ 100000) β€” number of cities. Then n - 1 lines follow. The i-th line of these lines contains two integers ui and vi (1 ≀ ui, vi ≀ n, ui β‰  vi) β€” the cities connected by the i-th road. It is guaranteed that one can reach any city from any other by the roads. Output Print a number β€” the expected length of their journey. The journey starts in the city 1. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>. Examples Input 4 1 2 1 3 2 4 Output 1.500000000000000 Input 5 1 2 1 3 3 4 2 5 Output 2.000000000000000 Note In the first sample, their journey may end in cities 3 or 4 with equal probability. The distance to city 3 is 1 and to city 4 is 2, so the expected length is 1.5. In the second sample, their journey may end in city 4 or 5. The distance to the both cities is 2, so the expected length is 2.
instruction
0
34,837
1
69,674
Tags: dfs and similar, dp, graphs, probabilities, trees Correct Solution: ``` import queue n = int(input().strip()) graph = [[] for _ in range(n)] probabilities = [1.0]*n dist = [0]*n visited = [False]*n for _ in range(n-1): a, b = map(int, input().split()) graph[a-1].append(b-1) graph[b-1].append(a-1) q = queue.Queue() q.put(0) while not q.empty(): top = q.get() count = 0 visited[top] = True for i in graph[top]: if not visited[i]: count += 1 for i in graph[top]: if not visited[i]: dist[i] = dist[top] + 1 probabilities[i] = probabilities[top]/count q.put(i) sum_ = 0 for i in range(1, n): if len(graph[i]) == 1: sum_ += dist[i]*probabilities[i] print("{0:.15f}".format(sum_)) ```
output
1
34,837
1
69,675
Provide tags and a correct Python 3 solution for this coding contest problem. There are n cities and n - 1 roads in the Seven Kingdoms, each road connects two cities and we can reach any city from any other by the roads. Theon and Yara Greyjoy are on a horse in the first city, they are starting traveling through the roads. But the weather is foggy, so they can’t see where the horse brings them. When the horse reaches a city (including the first one), it goes to one of the cities connected to the current city. But it is a strange horse, it only goes to cities in which they weren't before. In each such city, the horse goes with equal probabilities and it stops when there are no such cities. Let the length of each road be 1. The journey starts in the city 1. What is the expected length (expected value of length) of their journey? You can read about expected (average) value by the link <https://en.wikipedia.org/wiki/Expected_value>. Input The first line contains a single integer n (1 ≀ n ≀ 100000) β€” number of cities. Then n - 1 lines follow. The i-th line of these lines contains two integers ui and vi (1 ≀ ui, vi ≀ n, ui β‰  vi) β€” the cities connected by the i-th road. It is guaranteed that one can reach any city from any other by the roads. Output Print a number β€” the expected length of their journey. The journey starts in the city 1. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>. Examples Input 4 1 2 1 3 2 4 Output 1.500000000000000 Input 5 1 2 1 3 3 4 2 5 Output 2.000000000000000 Note In the first sample, their journey may end in cities 3 or 4 with equal probability. The distance to city 3 is 1 and to city 4 is 2, so the expected length is 1.5. In the second sample, their journey may end in city 4 or 5. The distance to the both cities is 2, so the expected length is 2.
instruction
0
34,838
1
69,676
Tags: dfs and similar, dp, graphs, probabilities, trees Correct Solution: ``` from collections import deque def edge_input(m): edges=[] for i in range(m): edges.append(list(map(int,input().split()))) return edges def convert(edges): l={i:[] for i in range(1,n+1)} for i in edges: c1,c2=i[0],i[1] l[c1].append(c2) l[c2].append(c1) return l n=int(input()) l=convert(edge_input(n-1)) v={i:False for i in l} d={i:-1 for i in l} p={i:1 for i in l} end=deque([]) def bfs(x): d[x]=0 q=deque([x]) v[x]=True while len(q)!=0: temp=q.popleft() c=0 for i in l[temp]: if v[i]: continue d[i]=d[temp]+1 c+=1 q.append(i) if c==0: end.append(temp) else: for i in l[temp]: if v[i]: continue v[i]=True p[i]=p[temp]*(1/c) return d d=bfs(1) ans=0 #print(d) #print(end) #print(p) for i in end: ans+=p[i]*d[i] print(ans) ```
output
1
34,838
1
69,677
Provide tags and a correct Python 3 solution for this coding contest problem. There are n cities and n - 1 roads in the Seven Kingdoms, each road connects two cities and we can reach any city from any other by the roads. Theon and Yara Greyjoy are on a horse in the first city, they are starting traveling through the roads. But the weather is foggy, so they can’t see where the horse brings them. When the horse reaches a city (including the first one), it goes to one of the cities connected to the current city. But it is a strange horse, it only goes to cities in which they weren't before. In each such city, the horse goes with equal probabilities and it stops when there are no such cities. Let the length of each road be 1. The journey starts in the city 1. What is the expected length (expected value of length) of their journey? You can read about expected (average) value by the link <https://en.wikipedia.org/wiki/Expected_value>. Input The first line contains a single integer n (1 ≀ n ≀ 100000) β€” number of cities. Then n - 1 lines follow. The i-th line of these lines contains two integers ui and vi (1 ≀ ui, vi ≀ n, ui β‰  vi) β€” the cities connected by the i-th road. It is guaranteed that one can reach any city from any other by the roads. Output Print a number β€” the expected length of their journey. The journey starts in the city 1. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>. Examples Input 4 1 2 1 3 2 4 Output 1.500000000000000 Input 5 1 2 1 3 3 4 2 5 Output 2.000000000000000 Note In the first sample, their journey may end in cities 3 or 4 with equal probability. The distance to city 3 is 1 and to city 4 is 2, so the expected length is 1.5. In the second sample, their journey may end in city 4 or 5. The distance to the both cities is 2, so the expected length is 2.
instruction
0
34,839
1
69,678
Tags: dfs and similar, dp, graphs, probabilities, trees Correct Solution: ``` import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") ########################################################## from collections import Counter # c=sorted((i,int(val))for i,val in enumerate(input().split())) import heapq # c=sorted((i,int(val))for i,val in enumerate(input().split())) # n = int(input()) # ls = list(map(int, input().split())) #n=int(input()) #arr = list(map(int, input().split())) #for _ in range(int(input())): n=int(input()) g=[[] for i in range(100000)] ans=0.0 q=[] for i in range(n-1): u,v = map(int, input().split()) g[u-1].append(v-1) g[v-1].append(u-1) q.append((0,1,0)) v=[0]*n while q: node,prob,h=q.pop() v[node]=1 adj=0 for i in g[node]: if v[i]==0: adj+=1 if adj==0: ans+=prob*h else: for i in g[node]: if v[i]==0: q.append((i,prob/adj,h+1)) v[i]=1 print("{0:.20f}".format(ans)) ```
output
1
34,839
1
69,679
Provide tags and a correct Python 3 solution for this coding contest problem. There are n cities and n - 1 roads in the Seven Kingdoms, each road connects two cities and we can reach any city from any other by the roads. Theon and Yara Greyjoy are on a horse in the first city, they are starting traveling through the roads. But the weather is foggy, so they can’t see where the horse brings them. When the horse reaches a city (including the first one), it goes to one of the cities connected to the current city. But it is a strange horse, it only goes to cities in which they weren't before. In each such city, the horse goes with equal probabilities and it stops when there are no such cities. Let the length of each road be 1. The journey starts in the city 1. What is the expected length (expected value of length) of their journey? You can read about expected (average) value by the link <https://en.wikipedia.org/wiki/Expected_value>. Input The first line contains a single integer n (1 ≀ n ≀ 100000) β€” number of cities. Then n - 1 lines follow. The i-th line of these lines contains two integers ui and vi (1 ≀ ui, vi ≀ n, ui β‰  vi) β€” the cities connected by the i-th road. It is guaranteed that one can reach any city from any other by the roads. Output Print a number β€” the expected length of their journey. The journey starts in the city 1. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>. Examples Input 4 1 2 1 3 2 4 Output 1.500000000000000 Input 5 1 2 1 3 3 4 2 5 Output 2.000000000000000 Note In the first sample, their journey may end in cities 3 or 4 with equal probability. The distance to city 3 is 1 and to city 4 is 2, so the expected length is 1.5. In the second sample, their journey may end in city 4 or 5. The distance to the both cities is 2, so the expected length is 2.
instruction
0
34,840
1
69,680
Tags: dfs and similar, dp, graphs, probabilities, trees Correct Solution: ``` from queue import Queue from collections import defaultdict ans = 0 def dfs(i, p, graph, prob, d): global ans l = len(graph[i]) if l == 1 and p != -1: ans += prob * d return if p == -1: newProb = prob / l else: newProb = prob / (l - 1) for j in graph[i]: if j != p: dfs(j, i, graph, newProb, d + 1) n = int(input()) graph = defaultdict(list) for _ in range(n - 1): u, v = map(int, input().split()) graph[u].append(v) graph[v].append(u) ans = 0 q = Queue() q.put([1, -1, 1, 0]) while not q.empty() and n != 1: t = q.get() i, p, prob, d = t[0], t[1], t[2], t[3] # print(i) l = len(graph[i]) if l == 1 and p != -1: ans += prob * d continue # print(i, l) if p == -1: newProb = prob / l else: newProb = prob / (l - 1) for j in graph[i]: if j != p: q.put([j, i, newProb, d + 1]) # dfs(1, -1, graph, 1, 0) print(ans) ```
output
1
34,840
1
69,681
Provide tags and a correct Python 3 solution for this coding contest problem. There are n cities and n - 1 roads in the Seven Kingdoms, each road connects two cities and we can reach any city from any other by the roads. Theon and Yara Greyjoy are on a horse in the first city, they are starting traveling through the roads. But the weather is foggy, so they can’t see where the horse brings them. When the horse reaches a city (including the first one), it goes to one of the cities connected to the current city. But it is a strange horse, it only goes to cities in which they weren't before. In each such city, the horse goes with equal probabilities and it stops when there are no such cities. Let the length of each road be 1. The journey starts in the city 1. What is the expected length (expected value of length) of their journey? You can read about expected (average) value by the link <https://en.wikipedia.org/wiki/Expected_value>. Input The first line contains a single integer n (1 ≀ n ≀ 100000) β€” number of cities. Then n - 1 lines follow. The i-th line of these lines contains two integers ui and vi (1 ≀ ui, vi ≀ n, ui β‰  vi) β€” the cities connected by the i-th road. It is guaranteed that one can reach any city from any other by the roads. Output Print a number β€” the expected length of their journey. The journey starts in the city 1. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>. Examples Input 4 1 2 1 3 2 4 Output 1.500000000000000 Input 5 1 2 1 3 3 4 2 5 Output 2.000000000000000 Note In the first sample, their journey may end in cities 3 or 4 with equal probability. The distance to city 3 is 1 and to city 4 is 2, so the expected length is 1.5. In the second sample, their journey may end in city 4 or 5. The distance to the both cities is 2, so the expected length is 2.
instruction
0
34,841
1
69,682
Tags: dfs and similar, dp, graphs, probabilities, trees Correct Solution: ``` # Author: S Mahesh Raju # Username: maheshraju2020 # Date: 30/04/2020 from sys import stdin,stdout from math import gcd, ceil, sqrt ii1 = lambda: int(stdin.readline().strip()) is1 = lambda: stdin.readline().strip() iia = lambda: list(map(int, stdin.readline().strip().split())) isa = lambda: stdin.readline().strip().split() mod = 1000000007 def bfs(d, n): queue = [[1, 1, 0]] res = 0 visited = [0] * (n + 1) while len(queue): cur = queue.pop(0) visited[cur[0]] = 1 flag = 0 for i in d[cur[0]]: if not visited[i]: prop = cur[1] / len(d[cur[0]]) queue.append([i, prop, cur[2] + 1]) flag = 1 d[i].remove(cur[0]) if flag == 0: res += cur[1] * cur[2] return res n = ii1() d = {} if n>1: for i in range(n - 1): a, b = iia() d.setdefault(a, []).append(b) d.setdefault(b, []).append(a) print(bfs(d, n)) else: print(0) ```
output
1
34,841
1
69,683
Provide tags and a correct Python 3 solution for this coding contest problem. There are n cities and n - 1 roads in the Seven Kingdoms, each road connects two cities and we can reach any city from any other by the roads. Theon and Yara Greyjoy are on a horse in the first city, they are starting traveling through the roads. But the weather is foggy, so they can’t see where the horse brings them. When the horse reaches a city (including the first one), it goes to one of the cities connected to the current city. But it is a strange horse, it only goes to cities in which they weren't before. In each such city, the horse goes with equal probabilities and it stops when there are no such cities. Let the length of each road be 1. The journey starts in the city 1. What is the expected length (expected value of length) of their journey? You can read about expected (average) value by the link <https://en.wikipedia.org/wiki/Expected_value>. Input The first line contains a single integer n (1 ≀ n ≀ 100000) β€” number of cities. Then n - 1 lines follow. The i-th line of these lines contains two integers ui and vi (1 ≀ ui, vi ≀ n, ui β‰  vi) β€” the cities connected by the i-th road. It is guaranteed that one can reach any city from any other by the roads. Output Print a number β€” the expected length of their journey. The journey starts in the city 1. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>. Examples Input 4 1 2 1 3 2 4 Output 1.500000000000000 Input 5 1 2 1 3 3 4 2 5 Output 2.000000000000000 Note In the first sample, their journey may end in cities 3 or 4 with equal probability. The distance to city 3 is 1 and to city 4 is 2, so the expected length is 1.5. In the second sample, their journey may end in city 4 or 5. The distance to the both cities is 2, so the expected length is 2.
instruction
0
34,842
1
69,684
Tags: dfs and similar, dp, graphs, probabilities, trees Correct Solution: ``` from sys import stdin, stdout, setrecursionlimit setrecursionlimit(10**7) import threading threading.stack_size(2**26) class SOLVE: def dfs(self, child, parent, node): total = 0 for v in node[child]: if v != parent: total += self.dfs(v, child, node) + 1 return ((total / (len(node[child]) - (1 if parent else 0))) if total else 0) def solve(self): R = stdin.readline #f = open('input.txt');R = f.readline W = stdout.write n = int(R()) node = [[] for i in range(n+1)] for _ in range(n-1): u, v = [int(x) for x in R().split()] node[u].append(v) node[v].append(u) print(self.dfs(1, 0, node)) # return 0 def main(): s = SOLVE() s.solve() threading.Thread(target=main).start() ```
output
1
34,842
1
69,685
Provide tags and a correct Python 3 solution for this coding contest problem. There are n cities and n - 1 roads in the Seven Kingdoms, each road connects two cities and we can reach any city from any other by the roads. Theon and Yara Greyjoy are on a horse in the first city, they are starting traveling through the roads. But the weather is foggy, so they can’t see where the horse brings them. When the horse reaches a city (including the first one), it goes to one of the cities connected to the current city. But it is a strange horse, it only goes to cities in which they weren't before. In each such city, the horse goes with equal probabilities and it stops when there are no such cities. Let the length of each road be 1. The journey starts in the city 1. What is the expected length (expected value of length) of their journey? You can read about expected (average) value by the link <https://en.wikipedia.org/wiki/Expected_value>. Input The first line contains a single integer n (1 ≀ n ≀ 100000) β€” number of cities. Then n - 1 lines follow. The i-th line of these lines contains two integers ui and vi (1 ≀ ui, vi ≀ n, ui β‰  vi) β€” the cities connected by the i-th road. It is guaranteed that one can reach any city from any other by the roads. Output Print a number β€” the expected length of their journey. The journey starts in the city 1. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>. Examples Input 4 1 2 1 3 2 4 Output 1.500000000000000 Input 5 1 2 1 3 3 4 2 5 Output 2.000000000000000 Note In the first sample, their journey may end in cities 3 or 4 with equal probability. The distance to city 3 is 1 and to city 4 is 2, so the expected length is 1.5. In the second sample, their journey may end in city 4 or 5. The distance to the both cities is 2, so the expected length is 2.
instruction
0
34,843
1
69,686
Tags: dfs and similar, dp, graphs, probabilities, trees Correct Solution: ``` from sys import stdin inp = stdin.readline n, ans, = int(inp()), 0 g = {v: [] for v in range(1, n + 1)} visited = [0] * (n + 1) q = [(1, 1, 0)] for _ in range(n - 1): a, b = map(int, input().split()) g[a].append(b) g[b].append(a) while q: v, cnt, h = q.pop() visited[v] = 1 adj = 0 for i in g[v]: if not visited[i]: adj += 1 if adj == 0: ans += cnt * h else: for i in g[v]: if not visited[i]: q.append((i, cnt / adj, h + 1)) print(ans) ```
output
1
34,843
1
69,687
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n cities and n - 1 roads in the Seven Kingdoms, each road connects two cities and we can reach any city from any other by the roads. Theon and Yara Greyjoy are on a horse in the first city, they are starting traveling through the roads. But the weather is foggy, so they can’t see where the horse brings them. When the horse reaches a city (including the first one), it goes to one of the cities connected to the current city. But it is a strange horse, it only goes to cities in which they weren't before. In each such city, the horse goes with equal probabilities and it stops when there are no such cities. Let the length of each road be 1. The journey starts in the city 1. What is the expected length (expected value of length) of their journey? You can read about expected (average) value by the link <https://en.wikipedia.org/wiki/Expected_value>. Input The first line contains a single integer n (1 ≀ n ≀ 100000) β€” number of cities. Then n - 1 lines follow. The i-th line of these lines contains two integers ui and vi (1 ≀ ui, vi ≀ n, ui β‰  vi) β€” the cities connected by the i-th road. It is guaranteed that one can reach any city from any other by the roads. Output Print a number β€” the expected length of their journey. The journey starts in the city 1. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>. Examples Input 4 1 2 1 3 2 4 Output 1.500000000000000 Input 5 1 2 1 3 3 4 2 5 Output 2.000000000000000 Note In the first sample, their journey may end in cities 3 or 4 with equal probability. The distance to city 3 is 1 and to city 4 is 2, so the expected length is 1.5. In the second sample, their journey may end in city 4 or 5. The distance to the both cities is 2, so the expected length is 2. Submitted Solution: ``` #https://codeforces.com/problemset/problem/839/C from collections import defaultdict class Graph: def __init__(self): self.dict = defaultdict(set) def main(): n_cities = int(input()) g = Graph() s = [] visited = set() for i in range(0,n_cities-1): u, v = (int(j) for j in input().split()) g.dict[u].add(v) g.dict[v].add(u) #dfs e = 0 s.append((1,0,1)) while s: city, l, p = s.pop() neighbors = [c for c in g.dict[city] if c not in visited] n = len(neighbors) if n == 0: e += l*p else: new_p = p/n for neighbor in neighbors: s.append((neighbor, l+1, new_p)) visited.add(city) print(e) if __name__ == "__main__": main() ```
instruction
0
34,844
1
69,688
Yes
output
1
34,844
1
69,689
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n cities and n - 1 roads in the Seven Kingdoms, each road connects two cities and we can reach any city from any other by the roads. Theon and Yara Greyjoy are on a horse in the first city, they are starting traveling through the roads. But the weather is foggy, so they can’t see where the horse brings them. When the horse reaches a city (including the first one), it goes to one of the cities connected to the current city. But it is a strange horse, it only goes to cities in which they weren't before. In each such city, the horse goes with equal probabilities and it stops when there are no such cities. Let the length of each road be 1. The journey starts in the city 1. What is the expected length (expected value of length) of their journey? You can read about expected (average) value by the link <https://en.wikipedia.org/wiki/Expected_value>. Input The first line contains a single integer n (1 ≀ n ≀ 100000) β€” number of cities. Then n - 1 lines follow. The i-th line of these lines contains two integers ui and vi (1 ≀ ui, vi ≀ n, ui β‰  vi) β€” the cities connected by the i-th road. It is guaranteed that one can reach any city from any other by the roads. Output Print a number β€” the expected length of their journey. The journey starts in the city 1. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>. Examples Input 4 1 2 1 3 2 4 Output 1.500000000000000 Input 5 1 2 1 3 3 4 2 5 Output 2.000000000000000 Note In the first sample, their journey may end in cities 3 or 4 with equal probability. The distance to city 3 is 1 and to city 4 is 2, so the expected length is 1.5. In the second sample, their journey may end in city 4 or 5. The distance to the both cities is 2, so the expected length is 2. Submitted Solution: ``` input = __import__("sys").stdin.readline n = int(input()) e = [[] for i in range(n+1)] for i in range(n-1): u, v = map(int, input().split()) e[u].append(v) e[v].append(u) used = [False for i in range(n+1)] sol = 0 q = [(1, 0, 1)] used[1] = True while len(q): u, d, p = q.pop() #print(u, d, p) if len(e[u]) == 1: sol += d*p for v in e[u]: if not used[v]: np = p / (len(e[u])-(u!=1)) q.append((v, d+1, np)) used[v] = True print(sol) ```
instruction
0
34,845
1
69,690
Yes
output
1
34,845
1
69,691
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n cities and n - 1 roads in the Seven Kingdoms, each road connects two cities and we can reach any city from any other by the roads. Theon and Yara Greyjoy are on a horse in the first city, they are starting traveling through the roads. But the weather is foggy, so they can’t see where the horse brings them. When the horse reaches a city (including the first one), it goes to one of the cities connected to the current city. But it is a strange horse, it only goes to cities in which they weren't before. In each such city, the horse goes with equal probabilities and it stops when there are no such cities. Let the length of each road be 1. The journey starts in the city 1. What is the expected length (expected value of length) of their journey? You can read about expected (average) value by the link <https://en.wikipedia.org/wiki/Expected_value>. Input The first line contains a single integer n (1 ≀ n ≀ 100000) β€” number of cities. Then n - 1 lines follow. The i-th line of these lines contains two integers ui and vi (1 ≀ ui, vi ≀ n, ui β‰  vi) β€” the cities connected by the i-th road. It is guaranteed that one can reach any city from any other by the roads. Output Print a number β€” the expected length of their journey. The journey starts in the city 1. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>. Examples Input 4 1 2 1 3 2 4 Output 1.500000000000000 Input 5 1 2 1 3 3 4 2 5 Output 2.000000000000000 Note In the first sample, their journey may end in cities 3 or 4 with equal probability. The distance to city 3 is 1 and to city 4 is 2, so the expected length is 1.5. In the second sample, their journey may end in city 4 or 5. The distance to the both cities is 2, so the expected length is 2. Submitted Solution: ``` import math import sys def bfs(start): q=[start] used[start]=0 while q!=[]: rt=0 for u in g[q[0]]: if used[u]==-1: rt+=1 if rt==0: E[0]+=used[q[0]]/ver[q[0]] ver[q[0]]=0 for u in g[q[0]]: if used[u]==-1: used[u]=used[q[0]]+1 ver[u]=ver[q[0]]*rt q.append(u) del(q[0]) n=int(input()) g=[[] for i in range(n)] for i in range(n-1): u,v=map(int,input().split()) v-=1;u-=1 g[u].append(v) g[v].append(u) used=[-1]*n ver=[0]*n ver[0]=1 E=[0] bfs(0) print("%.12f"%E[0]) ```
instruction
0
34,846
1
69,692
Yes
output
1
34,846
1
69,693
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n cities and n - 1 roads in the Seven Kingdoms, each road connects two cities and we can reach any city from any other by the roads. Theon and Yara Greyjoy are on a horse in the first city, they are starting traveling through the roads. But the weather is foggy, so they can’t see where the horse brings them. When the horse reaches a city (including the first one), it goes to one of the cities connected to the current city. But it is a strange horse, it only goes to cities in which they weren't before. In each such city, the horse goes with equal probabilities and it stops when there are no such cities. Let the length of each road be 1. The journey starts in the city 1. What is the expected length (expected value of length) of their journey? You can read about expected (average) value by the link <https://en.wikipedia.org/wiki/Expected_value>. Input The first line contains a single integer n (1 ≀ n ≀ 100000) β€” number of cities. Then n - 1 lines follow. The i-th line of these lines contains two integers ui and vi (1 ≀ ui, vi ≀ n, ui β‰  vi) β€” the cities connected by the i-th road. It is guaranteed that one can reach any city from any other by the roads. Output Print a number β€” the expected length of their journey. The journey starts in the city 1. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>. Examples Input 4 1 2 1 3 2 4 Output 1.500000000000000 Input 5 1 2 1 3 3 4 2 5 Output 2.000000000000000 Note In the first sample, their journey may end in cities 3 or 4 with equal probability. The distance to city 3 is 1 and to city 4 is 2, so the expected length is 1.5. In the second sample, their journey may end in city 4 or 5. The distance to the both cities is 2, so the expected length is 2. Submitted Solution: ``` import sys n = int(input()) if n==1: print(0) exit() d = {} for _ in range(n-1): a,b = map(int,input().split()) if a not in d: d[a]=set() d[a].add(b) if b not in d: d[b]=set() d[b].add(a) x = [None]*(n+1) for node in d: if len(d[node])==1 and node!=1: x[node]=0 done = False visited = [None]*(n+1) unvisited = set([1]) traverseOrder = [1] while unvisited: node = unvisited.pop() visited[node]=True for neighbour in d[node]: if visited[neighbour]==None: unvisited.add(neighbour) traverseOrder+=[neighbour] visited[neighbour]=True for node in traverseOrder[::-1]: if len(d[node])==1 and node!=1: x[node]=0 else: cost=0 count=0 for child in d[node]: if x[child]!=None: cost+=x[child] count+=1. x[node]=1+cost/count print(x[1]) ```
instruction
0
34,847
1
69,694
Yes
output
1
34,847
1
69,695
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n cities and n - 1 roads in the Seven Kingdoms, each road connects two cities and we can reach any city from any other by the roads. Theon and Yara Greyjoy are on a horse in the first city, they are starting traveling through the roads. But the weather is foggy, so they can’t see where the horse brings them. When the horse reaches a city (including the first one), it goes to one of the cities connected to the current city. But it is a strange horse, it only goes to cities in which they weren't before. In each such city, the horse goes with equal probabilities and it stops when there are no such cities. Let the length of each road be 1. The journey starts in the city 1. What is the expected length (expected value of length) of their journey? You can read about expected (average) value by the link <https://en.wikipedia.org/wiki/Expected_value>. Input The first line contains a single integer n (1 ≀ n ≀ 100000) β€” number of cities. Then n - 1 lines follow. The i-th line of these lines contains two integers ui and vi (1 ≀ ui, vi ≀ n, ui β‰  vi) β€” the cities connected by the i-th road. It is guaranteed that one can reach any city from any other by the roads. Output Print a number β€” the expected length of their journey. The journey starts in the city 1. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>. Examples Input 4 1 2 1 3 2 4 Output 1.500000000000000 Input 5 1 2 1 3 3 4 2 5 Output 2.000000000000000 Note In the first sample, their journey may end in cities 3 or 4 with equal probability. The distance to city 3 is 1 and to city 4 is 2, so the expected length is 1.5. In the second sample, their journey may end in city 4 or 5. The distance to the both cities is 2, so the expected length is 2. Submitted Solution: ``` import sys sys.setrecursionlimit(10**6) n = int(input()) def recur(pos): global visited global edges global leaf visited[pos] = True count = 0 if edges[pos][1]: size = float(len(edges[pos][1])) for node in edges[pos][1]: if not visited[node]: edges[node][2] /= size edges[node][0] = edges[pos][0] + 1.0 recur(node) count += 1 if count == 0: leaf.append(pos) visited = [False]*(n+1) edges = [[0.0,[],1.0] for i in range(n+1)] leaf = [] for i in range(n-1): a,b = map(int,input().split()) edges[a][1].append(b) edges[b][1].append(a) visited[0] = True recur(1) s = sum([edges[idx][0]*edges[idx][2] for idx in leaf]) print(s) ```
instruction
0
34,848
1
69,696
No
output
1
34,848
1
69,697
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n cities and n - 1 roads in the Seven Kingdoms, each road connects two cities and we can reach any city from any other by the roads. Theon and Yara Greyjoy are on a horse in the first city, they are starting traveling through the roads. But the weather is foggy, so they can’t see where the horse brings them. When the horse reaches a city (including the first one), it goes to one of the cities connected to the current city. But it is a strange horse, it only goes to cities in which they weren't before. In each such city, the horse goes with equal probabilities and it stops when there are no such cities. Let the length of each road be 1. The journey starts in the city 1. What is the expected length (expected value of length) of their journey? You can read about expected (average) value by the link <https://en.wikipedia.org/wiki/Expected_value>. Input The first line contains a single integer n (1 ≀ n ≀ 100000) β€” number of cities. Then n - 1 lines follow. The i-th line of these lines contains two integers ui and vi (1 ≀ ui, vi ≀ n, ui β‰  vi) β€” the cities connected by the i-th road. It is guaranteed that one can reach any city from any other by the roads. Output Print a number β€” the expected length of their journey. The journey starts in the city 1. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>. Examples Input 4 1 2 1 3 2 4 Output 1.500000000000000 Input 5 1 2 1 3 3 4 2 5 Output 2.000000000000000 Note In the first sample, their journey may end in cities 3 or 4 with equal probability. The distance to city 3 is 1 and to city 4 is 2, so the expected length is 1.5. In the second sample, their journey may end in city 4 or 5. The distance to the both cities is 2, so the expected length is 2. Submitted Solution: ``` def get(cur_root): res = 1 length = len(graph[cur_root]) if length > 0: temp = 0 for el in graph[cur_root]: temp += get(el) res += temp/length return res n = int(input()) graph = [[] for _ in range(n+1)] root_pretendents = set(range(1,n+1)) for _ in range(n-1): a, b = map(int, input().split(' ')) root_pretendents -= {b} graph[a].append(b) root = root_pretendents.pop() print(get(root)-1) ```
instruction
0
34,849
1
69,698
No
output
1
34,849
1
69,699
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n cities and n - 1 roads in the Seven Kingdoms, each road connects two cities and we can reach any city from any other by the roads. Theon and Yara Greyjoy are on a horse in the first city, they are starting traveling through the roads. But the weather is foggy, so they can’t see where the horse brings them. When the horse reaches a city (including the first one), it goes to one of the cities connected to the current city. But it is a strange horse, it only goes to cities in which they weren't before. In each such city, the horse goes with equal probabilities and it stops when there are no such cities. Let the length of each road be 1. The journey starts in the city 1. What is the expected length (expected value of length) of their journey? You can read about expected (average) value by the link <https://en.wikipedia.org/wiki/Expected_value>. Input The first line contains a single integer n (1 ≀ n ≀ 100000) β€” number of cities. Then n - 1 lines follow. The i-th line of these lines contains two integers ui and vi (1 ≀ ui, vi ≀ n, ui β‰  vi) β€” the cities connected by the i-th road. It is guaranteed that one can reach any city from any other by the roads. Output Print a number β€” the expected length of their journey. The journey starts in the city 1. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>. Examples Input 4 1 2 1 3 2 4 Output 1.500000000000000 Input 5 1 2 1 3 3 4 2 5 Output 2.000000000000000 Note In the first sample, their journey may end in cities 3 or 4 with equal probability. The distance to city 3 is 1 and to city 4 is 2, so the expected length is 1.5. In the second sample, their journey may end in city 4 or 5. The distance to the both cities is 2, so the expected length is 2. Submitted Solution: ``` from collections import deque, defaultdict sz = int(1e5) tree = [0] * (sz + 1) for i in range(sz + 1): tree[i] = [] vis = [False] * (sz + 1) dis = [0] * sz def addEdge(a: int, b: int): global tree tree[a].append(b) tree[b].append(a) def bfs(node: int): global dis, vis qu = deque() qu.append((node, 0)) dis[0] = 0 while qu: p = qu[0] qu.popleft() vis[p[0]] = True for child in tree[p[0]]: if not vis[child]: dis[child] = dis[p[0]] + 1 qu.append((child, p[0])) # Driver Code d = defaultdict(int) n = int(input()) for i in range(n-1): a, b = map(int, input().split(" ")) a-=1 b-=1 d[a] += 1 d[b] += 1 addEdge(a, b) bfs(0) q = [] for i, j in d.items(): if j == 1: q.append(i) s = 0 for i in range(len(q)): s+= dis[q[i]] print(s/len(q)) ```
instruction
0
34,850
1
69,700
No
output
1
34,850
1
69,701
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n cities and n - 1 roads in the Seven Kingdoms, each road connects two cities and we can reach any city from any other by the roads. Theon and Yara Greyjoy are on a horse in the first city, they are starting traveling through the roads. But the weather is foggy, so they can’t see where the horse brings them. When the horse reaches a city (including the first one), it goes to one of the cities connected to the current city. But it is a strange horse, it only goes to cities in which they weren't before. In each such city, the horse goes with equal probabilities and it stops when there are no such cities. Let the length of each road be 1. The journey starts in the city 1. What is the expected length (expected value of length) of their journey? You can read about expected (average) value by the link <https://en.wikipedia.org/wiki/Expected_value>. Input The first line contains a single integer n (1 ≀ n ≀ 100000) β€” number of cities. Then n - 1 lines follow. The i-th line of these lines contains two integers ui and vi (1 ≀ ui, vi ≀ n, ui β‰  vi) β€” the cities connected by the i-th road. It is guaranteed that one can reach any city from any other by the roads. Output Print a number β€” the expected length of their journey. The journey starts in the city 1. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>. Examples Input 4 1 2 1 3 2 4 Output 1.500000000000000 Input 5 1 2 1 3 3 4 2 5 Output 2.000000000000000 Note In the first sample, their journey may end in cities 3 or 4 with equal probability. The distance to city 3 is 1 and to city 4 is 2, so the expected length is 1.5. In the second sample, their journey may end in city 4 or 5. The distance to the both cities is 2, so the expected length is 2. Submitted Solution: ``` from collections import deque n = int(input()) g = [[] for _ in range(n + 1)] for i in range(n - 1): a, b = map(int, input().split()) g[a].append(b) g[b].append(a) q = deque() q.append(1) par = [0] * (n + 1) rank = [0] * (n + 1) while q: v = q.pop() for x in g[v]: if x == par[v]: continue par[x] = v rank[x] = rank[v] + 1 q.append(x) #print(g, rank) res = 0 cnt = 0 for i in range(2, n + 1): if len(g[i]) == 1: y = rank[i] res += y cnt += 1 print(res / cnt) ```
instruction
0
34,851
1
69,702
No
output
1
34,851
1
69,703
Provide a correct Python 3 solution for this coding contest problem. There are N cities and M roads. The i-th road (1≀i≀M) connects two cities a_i and b_i (1≀a_i,b_i≀N) bidirectionally. There may be more than one road that connects the same pair of two cities. For each city, how many roads are connected to the city? Constraints * 2≀N,M≀50 * 1≀a_i,b_i≀N * a_i β‰  b_i * All input values are integers. Input Input is given from Standard Input in the following format: N M a_1 b_1 : a_M b_M Output Print the answer in N lines. In the i-th line (1≀i≀N), print the number of roads connected to city i. Examples Input 4 3 1 2 2 3 1 4 Output 2 2 1 1 Input 2 5 1 2 2 1 1 2 2 1 1 2 Output 5 5 Input 8 8 1 2 3 4 1 5 2 8 3 7 5 2 4 1 6 8 Output 3 3 2 2 2 1 1 2
instruction
0
35,011
1
70,022
"Correct Solution: ``` N, M = map(int, input().split()) l = [] for i in range(M): l.extend(list(map(int, input().split()))) for i in range(1,N+1): print(l.count(i)) ```
output
1
35,011
1
70,023
Provide a correct Python 3 solution for this coding contest problem. There are N cities and M roads. The i-th road (1≀i≀M) connects two cities a_i and b_i (1≀a_i,b_i≀N) bidirectionally. There may be more than one road that connects the same pair of two cities. For each city, how many roads are connected to the city? Constraints * 2≀N,M≀50 * 1≀a_i,b_i≀N * a_i β‰  b_i * All input values are integers. Input Input is given from Standard Input in the following format: N M a_1 b_1 : a_M b_M Output Print the answer in N lines. In the i-th line (1≀i≀N), print the number of roads connected to city i. Examples Input 4 3 1 2 2 3 1 4 Output 2 2 1 1 Input 2 5 1 2 2 1 1 2 2 1 1 2 Output 5 5 Input 8 8 1 2 3 4 1 5 2 8 3 7 5 2 4 1 6 8 Output 3 3 2 2 2 1 1 2
instruction
0
35,012
1
70,024
"Correct Solution: ``` N,M= map(int,input().split()) NN = [0]*N for i in range(M): a,b = map(int,input().split()) NN[a-1] += 1 NN[b-1] += 1 for _ in NN: print(_) ```
output
1
35,012
1
70,025
Provide a correct Python 3 solution for this coding contest problem. There are N cities and M roads. The i-th road (1≀i≀M) connects two cities a_i and b_i (1≀a_i,b_i≀N) bidirectionally. There may be more than one road that connects the same pair of two cities. For each city, how many roads are connected to the city? Constraints * 2≀N,M≀50 * 1≀a_i,b_i≀N * a_i β‰  b_i * All input values are integers. Input Input is given from Standard Input in the following format: N M a_1 b_1 : a_M b_M Output Print the answer in N lines. In the i-th line (1≀i≀N), print the number of roads connected to city i. Examples Input 4 3 1 2 2 3 1 4 Output 2 2 1 1 Input 2 5 1 2 2 1 1 2 2 1 1 2 Output 5 5 Input 8 8 1 2 3 4 1 5 2 8 3 7 5 2 4 1 6 8 Output 3 3 2 2 2 1 1 2
instruction
0
35,013
1
70,026
"Correct Solution: ``` n, m = map(int, input().split()) a = sum([input().split() for _ in range(m)], []) [print(a.count(str(i + 1))) for i in range(n)] ```
output
1
35,013
1
70,027
Provide a correct Python 3 solution for this coding contest problem. There are N cities and M roads. The i-th road (1≀i≀M) connects two cities a_i and b_i (1≀a_i,b_i≀N) bidirectionally. There may be more than one road that connects the same pair of two cities. For each city, how many roads are connected to the city? Constraints * 2≀N,M≀50 * 1≀a_i,b_i≀N * a_i β‰  b_i * All input values are integers. Input Input is given from Standard Input in the following format: N M a_1 b_1 : a_M b_M Output Print the answer in N lines. In the i-th line (1≀i≀N), print the number of roads connected to city i. Examples Input 4 3 1 2 2 3 1 4 Output 2 2 1 1 Input 2 5 1 2 2 1 1 2 2 1 1 2 Output 5 5 Input 8 8 1 2 3 4 1 5 2 8 3 7 5 2 4 1 6 8 Output 3 3 2 2 2 1 1 2
instruction
0
35,014
1
70,028
"Correct Solution: ``` n,m=[int(x) for x in input().split()] r=[input() for _ in range(m)] r=' '.join(r).split() for i in range(1,n+1): print(r.count(str(i))) ```
output
1
35,014
1
70,029
Provide a correct Python 3 solution for this coding contest problem. There are N cities and M roads. The i-th road (1≀i≀M) connects two cities a_i and b_i (1≀a_i,b_i≀N) bidirectionally. There may be more than one road that connects the same pair of two cities. For each city, how many roads are connected to the city? Constraints * 2≀N,M≀50 * 1≀a_i,b_i≀N * a_i β‰  b_i * All input values are integers. Input Input is given from Standard Input in the following format: N M a_1 b_1 : a_M b_M Output Print the answer in N lines. In the i-th line (1≀i≀N), print the number of roads connected to city i. Examples Input 4 3 1 2 2 3 1 4 Output 2 2 1 1 Input 2 5 1 2 2 1 1 2 2 1 1 2 Output 5 5 Input 8 8 1 2 3 4 1 5 2 8 3 7 5 2 4 1 6 8 Output 3 3 2 2 2 1 1 2
instruction
0
35,015
1
70,030
"Correct Solution: ``` n,m = map(int,input().split()) a = [] for i in range(m): a += map(int,input().split()) for j in range(1,n+1): print(a.count(j)) ```
output
1
35,015
1
70,031
Provide a correct Python 3 solution for this coding contest problem. There are N cities and M roads. The i-th road (1≀i≀M) connects two cities a_i and b_i (1≀a_i,b_i≀N) bidirectionally. There may be more than one road that connects the same pair of two cities. For each city, how many roads are connected to the city? Constraints * 2≀N,M≀50 * 1≀a_i,b_i≀N * a_i β‰  b_i * All input values are integers. Input Input is given from Standard Input in the following format: N M a_1 b_1 : a_M b_M Output Print the answer in N lines. In the i-th line (1≀i≀N), print the number of roads connected to city i. Examples Input 4 3 1 2 2 3 1 4 Output 2 2 1 1 Input 2 5 1 2 2 1 1 2 2 1 1 2 Output 5 5 Input 8 8 1 2 3 4 1 5 2 8 3 7 5 2 4 1 6 8 Output 3 3 2 2 2 1 1 2
instruction
0
35,016
1
70,032
"Correct Solution: ``` N,M=map(int,input().split()) d=[0]*N for i in range(M): a,b=map(int,input().split()) d[a-1]+=1 d[b-1]+=1 for i in d:print(i) ```
output
1
35,016
1
70,033
Provide a correct Python 3 solution for this coding contest problem. There are N cities and M roads. The i-th road (1≀i≀M) connects two cities a_i and b_i (1≀a_i,b_i≀N) bidirectionally. There may be more than one road that connects the same pair of two cities. For each city, how many roads are connected to the city? Constraints * 2≀N,M≀50 * 1≀a_i,b_i≀N * a_i β‰  b_i * All input values are integers. Input Input is given from Standard Input in the following format: N M a_1 b_1 : a_M b_M Output Print the answer in N lines. In the i-th line (1≀i≀N), print the number of roads connected to city i. Examples Input 4 3 1 2 2 3 1 4 Output 2 2 1 1 Input 2 5 1 2 2 1 1 2 2 1 1 2 Output 5 5 Input 8 8 1 2 3 4 1 5 2 8 3 7 5 2 4 1 6 8 Output 3 3 2 2 2 1 1 2
instruction
0
35,017
1
70,034
"Correct Solution: ``` N,M=map(int,input().split()) city=[0]*N for i in range(M): road=list(map(int,input().split())) for j in road: city[j-1]+=1 for i in city: print(i) ```
output
1
35,017
1
70,035
Provide a correct Python 3 solution for this coding contest problem. There are N cities and M roads. The i-th road (1≀i≀M) connects two cities a_i and b_i (1≀a_i,b_i≀N) bidirectionally. There may be more than one road that connects the same pair of two cities. For each city, how many roads are connected to the city? Constraints * 2≀N,M≀50 * 1≀a_i,b_i≀N * a_i β‰  b_i * All input values are integers. Input Input is given from Standard Input in the following format: N M a_1 b_1 : a_M b_M Output Print the answer in N lines. In the i-th line (1≀i≀N), print the number of roads connected to city i. Examples Input 4 3 1 2 2 3 1 4 Output 2 2 1 1 Input 2 5 1 2 2 1 1 2 2 1 1 2 Output 5 5 Input 8 8 1 2 3 4 1 5 2 8 3 7 5 2 4 1 6 8 Output 3 3 2 2 2 1 1 2
instruction
0
35,018
1
70,036
"Correct Solution: ``` # coding: utf-8 N, M = map(int, input().split()) lst=[] for _ in range(M): lst += input().split() for i in range(1, N+1): print(lst.count(str(i))) ```
output
1
35,018
1
70,037
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N cities and M roads. The i-th road (1≀i≀M) connects two cities a_i and b_i (1≀a_i,b_i≀N) bidirectionally. There may be more than one road that connects the same pair of two cities. For each city, how many roads are connected to the city? Constraints * 2≀N,M≀50 * 1≀a_i,b_i≀N * a_i β‰  b_i * All input values are integers. Input Input is given from Standard Input in the following format: N M a_1 b_1 : a_M b_M Output Print the answer in N lines. In the i-th line (1≀i≀N), print the number of roads connected to city i. Examples Input 4 3 1 2 2 3 1 4 Output 2 2 1 1 Input 2 5 1 2 2 1 1 2 2 1 1 2 Output 5 5 Input 8 8 1 2 3 4 1 5 2 8 3 7 5 2 4 1 6 8 Output 3 3 2 2 2 1 1 2 Submitted Solution: ``` n,m=map(int,input().split()) c=[0]*n for _ in range(m): a,b=[int(j) for j in input().split()] c[a-1]+=1 c[b-1]+=1 for i in c: print(i) ```
instruction
0
35,019
1
70,038
Yes
output
1
35,019
1
70,039
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N cities and M roads. The i-th road (1≀i≀M) connects two cities a_i and b_i (1≀a_i,b_i≀N) bidirectionally. There may be more than one road that connects the same pair of two cities. For each city, how many roads are connected to the city? Constraints * 2≀N,M≀50 * 1≀a_i,b_i≀N * a_i β‰  b_i * All input values are integers. Input Input is given from Standard Input in the following format: N M a_1 b_1 : a_M b_M Output Print the answer in N lines. In the i-th line (1≀i≀N), print the number of roads connected to city i. Examples Input 4 3 1 2 2 3 1 4 Output 2 2 1 1 Input 2 5 1 2 2 1 1 2 2 1 1 2 Output 5 5 Input 8 8 1 2 3 4 1 5 2 8 3 7 5 2 4 1 6 8 Output 3 3 2 2 2 1 1 2 Submitted Solution: ``` n,m = map(int,input().split()) x =[0]*n for i in range(m): a,b = map(int,input().split()) x[a-1]+=1 x[b-1]+=1 for i in range(n): print(x[i]) ```
instruction
0
35,020
1
70,040
Yes
output
1
35,020
1
70,041
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N cities and M roads. The i-th road (1≀i≀M) connects two cities a_i and b_i (1≀a_i,b_i≀N) bidirectionally. There may be more than one road that connects the same pair of two cities. For each city, how many roads are connected to the city? Constraints * 2≀N,M≀50 * 1≀a_i,b_i≀N * a_i β‰  b_i * All input values are integers. Input Input is given from Standard Input in the following format: N M a_1 b_1 : a_M b_M Output Print the answer in N lines. In the i-th line (1≀i≀N), print the number of roads connected to city i. Examples Input 4 3 1 2 2 3 1 4 Output 2 2 1 1 Input 2 5 1 2 2 1 1 2 2 1 1 2 Output 5 5 Input 8 8 1 2 3 4 1 5 2 8 3 7 5 2 4 1 6 8 Output 3 3 2 2 2 1 1 2 Submitted Solution: ``` n,m=map(int,input().split()) l=[] for i in range(m):l+=list(map(int,input().split())) for j in range(1,n+1):print(l.count(j)) ```
instruction
0
35,021
1
70,042
Yes
output
1
35,021
1
70,043
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N cities and M roads. The i-th road (1≀i≀M) connects two cities a_i and b_i (1≀a_i,b_i≀N) bidirectionally. There may be more than one road that connects the same pair of two cities. For each city, how many roads are connected to the city? Constraints * 2≀N,M≀50 * 1≀a_i,b_i≀N * a_i β‰  b_i * All input values are integers. Input Input is given from Standard Input in the following format: N M a_1 b_1 : a_M b_M Output Print the answer in N lines. In the i-th line (1≀i≀N), print the number of roads connected to city i. Examples Input 4 3 1 2 2 3 1 4 Output 2 2 1 1 Input 2 5 1 2 2 1 1 2 2 1 1 2 Output 5 5 Input 8 8 1 2 3 4 1 5 2 8 3 7 5 2 4 1 6 8 Output 3 3 2 2 2 1 1 2 Submitted Solution: ``` N,_,*R=open(0).read().split() for i in range(int(N)):print(R.count(str(-~i))) ```
instruction
0
35,022
1
70,044
Yes
output
1
35,022
1
70,045
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N cities and M roads. The i-th road (1≀i≀M) connects two cities a_i and b_i (1≀a_i,b_i≀N) bidirectionally. There may be more than one road that connects the same pair of two cities. For each city, how many roads are connected to the city? Constraints * 2≀N,M≀50 * 1≀a_i,b_i≀N * a_i β‰  b_i * All input values are integers. Input Input is given from Standard Input in the following format: N M a_1 b_1 : a_M b_M Output Print the answer in N lines. In the i-th line (1≀i≀N), print the number of roads connected to city i. Examples Input 4 3 1 2 2 3 1 4 Output 2 2 1 1 Input 2 5 1 2 2 1 1 2 2 1 1 2 Output 5 5 Input 8 8 1 2 3 4 1 5 2 8 3 7 5 2 4 1 6 8 Output 3 3 2 2 2 1 1 2 Submitted Solution: ``` N,M = map(int, input().strip()) G = [[] for _ in range(N)] for _ in range(M): a,b = map(int, input().strip().split(' ')) G[a].append(b) G[b].append(a) for i in range(N): print(len(G[i])) ```
instruction
0
35,023
1
70,046
No
output
1
35,023
1
70,047
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N cities and M roads. The i-th road (1≀i≀M) connects two cities a_i and b_i (1≀a_i,b_i≀N) bidirectionally. There may be more than one road that connects the same pair of two cities. For each city, how many roads are connected to the city? Constraints * 2≀N,M≀50 * 1≀a_i,b_i≀N * a_i β‰  b_i * All input values are integers. Input Input is given from Standard Input in the following format: N M a_1 b_1 : a_M b_M Output Print the answer in N lines. In the i-th line (1≀i≀N), print the number of roads connected to city i. Examples Input 4 3 1 2 2 3 1 4 Output 2 2 1 1 Input 2 5 1 2 2 1 1 2 2 1 1 2 Output 5 5 Input 8 8 1 2 3 4 1 5 2 8 3 7 5 2 4 1 6 8 Output 3 3 2 2 2 1 1 2 Submitted Solution: ``` from sys import stdin from itertools import chain from collections import Counter N, M = [int(_) for _ in stdin.readline().rstrip().split()] AB = [list(map(int, stdin.readline().rstrip().split())) for _ in range(M)] AB = Counter(chain.from_iterable(AB)) for v in AB.values(): print(v) ```
instruction
0
35,024
1
70,048
No
output
1
35,024
1
70,049
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N cities and M roads. The i-th road (1≀i≀M) connects two cities a_i and b_i (1≀a_i,b_i≀N) bidirectionally. There may be more than one road that connects the same pair of two cities. For each city, how many roads are connected to the city? Constraints * 2≀N,M≀50 * 1≀a_i,b_i≀N * a_i β‰  b_i * All input values are integers. Input Input is given from Standard Input in the following format: N M a_1 b_1 : a_M b_M Output Print the answer in N lines. In the i-th line (1≀i≀N), print the number of roads connected to city i. Examples Input 4 3 1 2 2 3 1 4 Output 2 2 1 1 Input 2 5 1 2 2 1 1 2 2 1 1 2 Output 5 5 Input 8 8 1 2 3 4 1 5 2 8 3 7 5 2 4 1 6 8 Output 3 3 2 2 2 1 1 2 Submitted Solution: ``` n,m = map(int, input().split()) bridge = [] for i in range(n): bridge.append(0) for i in range(m): a,b = map(int,input().split()) bridge[a-1] += 1 bridge[b-1] += 1 for i in bridge: if i > 0: print(i) ```
instruction
0
35,025
1
70,050
No
output
1
35,025
1
70,051
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N cities and M roads. The i-th road (1≀i≀M) connects two cities a_i and b_i (1≀a_i,b_i≀N) bidirectionally. There may be more than one road that connects the same pair of two cities. For each city, how many roads are connected to the city? Constraints * 2≀N,M≀50 * 1≀a_i,b_i≀N * a_i β‰  b_i * All input values are integers. Input Input is given from Standard Input in the following format: N M a_1 b_1 : a_M b_M Output Print the answer in N lines. In the i-th line (1≀i≀N), print the number of roads connected to city i. Examples Input 4 3 1 2 2 3 1 4 Output 2 2 1 1 Input 2 5 1 2 2 1 1 2 2 1 1 2 Output 5 5 Input 8 8 1 2 3 4 1 5 2 8 3 7 5 2 4 1 6 8 Output 3 3 2 2 2 1 1 2 Submitted Solution: ``` N, M = map(int, input().split()) R = [list(map(int, input().split())) for _ in range(N)] for i in range(1,N+1): print(R.count(i)) ```
instruction
0
35,026
1
70,052
No
output
1
35,026
1
70,053
Provide tags and a correct Python 3 solution for this coding contest problem. After a long party Petya decided to return home, but he turned out to be at the opposite end of the town from his home. There are n crossroads in the line in the town, and there is either the bus or the tram station at each crossroad. The crossroads are represented as a string s of length n, where s_i = A, if there is a bus station at i-th crossroad, and s_i = B, if there is a tram station at i-th crossroad. Currently Petya is at the first crossroad (which corresponds to s_1) and his goal is to get to the last crossroad (which corresponds to s_n). If for two crossroads i and j for all crossroads i, i+1, …, j-1 there is a bus station, one can pay a roubles for the bus ticket, and go from i-th crossroad to the j-th crossroad by the bus (it is not necessary to have a bus station at the j-th crossroad). Formally, paying a roubles Petya can go from i to j if s_t = A for all i ≀ t < j. If for two crossroads i and j for all crossroads i, i+1, …, j-1 there is a tram station, one can pay b roubles for the tram ticket, and go from i-th crossroad to the j-th crossroad by the tram (it is not necessary to have a tram station at the j-th crossroad). Formally, paying b roubles Petya can go from i to j if s_t = B for all i ≀ t < j. For example, if s="AABBBAB", a=4 and b=3 then Petya needs: <image> * buy one bus ticket to get from 1 to 3, * buy one tram ticket to get from 3 to 6, * buy one bus ticket to get from 6 to 7. Thus, in total he needs to spend 4+3+4=11 roubles. Please note that the type of the stop at the last crossroad (i.e. the character s_n) does not affect the final expense. Now Petya is at the first crossroad, and he wants to get to the n-th crossroad. After the party he has left with p roubles. He's decided to go to some station on foot, and then go to home using only public transport. Help him to choose the closest crossroad i to go on foot the first, so he has enough money to get from the i-th crossroad to the n-th, using only tram and bus tickets. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 10^4). The first line of each test case consists of three integers a, b, p (1 ≀ a, b, p ≀ 10^5) β€” the cost of bus ticket, the cost of tram ticket and the amount of money Petya has. The second line of each test case consists of one string s, where s_i = A, if there is a bus station at i-th crossroad, and s_i = B, if there is a tram station at i-th crossroad (2 ≀ |s| ≀ 10^5). It is guaranteed, that the sum of the length of strings s by all test cases in one test doesn't exceed 10^5. Output For each test case print one number β€” the minimal index i of a crossroad Petya should go on foot. The rest of the path (i.e. from i to n he should use public transport). Example Input 5 2 2 1 BB 1 1 1 AB 3 2 8 AABBBBAABB 5 3 4 BBBBB 2 1 1 ABABAB Output 2 1 3 1 6
instruction
0
35,316
1
70,632
Tags: binary search, dp, greedy, strings Correct Solution: ``` n = int(input()) for i in range(n): a, b, p = map(int, input().split()) s = input() t = len(s) l = [0] for j in range(t - 1): if s[t - 2 - j] == s[t - 1 - j] and j != 0: l.append(l[j]) elif s[t - 2 - j] == 'B': l.append(l[j] + b) else: l.append(l[j] + a) l.reverse() v = len(l) - 1 for k in range(len(l)): if p >= l[k]: v = k break; print(v + 1) ```
output
1
35,316
1
70,633
Provide tags and a correct Python 3 solution for this coding contest problem. After a long party Petya decided to return home, but he turned out to be at the opposite end of the town from his home. There are n crossroads in the line in the town, and there is either the bus or the tram station at each crossroad. The crossroads are represented as a string s of length n, where s_i = A, if there is a bus station at i-th crossroad, and s_i = B, if there is a tram station at i-th crossroad. Currently Petya is at the first crossroad (which corresponds to s_1) and his goal is to get to the last crossroad (which corresponds to s_n). If for two crossroads i and j for all crossroads i, i+1, …, j-1 there is a bus station, one can pay a roubles for the bus ticket, and go from i-th crossroad to the j-th crossroad by the bus (it is not necessary to have a bus station at the j-th crossroad). Formally, paying a roubles Petya can go from i to j if s_t = A for all i ≀ t < j. If for two crossroads i and j for all crossroads i, i+1, …, j-1 there is a tram station, one can pay b roubles for the tram ticket, and go from i-th crossroad to the j-th crossroad by the tram (it is not necessary to have a tram station at the j-th crossroad). Formally, paying b roubles Petya can go from i to j if s_t = B for all i ≀ t < j. For example, if s="AABBBAB", a=4 and b=3 then Petya needs: <image> * buy one bus ticket to get from 1 to 3, * buy one tram ticket to get from 3 to 6, * buy one bus ticket to get from 6 to 7. Thus, in total he needs to spend 4+3+4=11 roubles. Please note that the type of the stop at the last crossroad (i.e. the character s_n) does not affect the final expense. Now Petya is at the first crossroad, and he wants to get to the n-th crossroad. After the party he has left with p roubles. He's decided to go to some station on foot, and then go to home using only public transport. Help him to choose the closest crossroad i to go on foot the first, so he has enough money to get from the i-th crossroad to the n-th, using only tram and bus tickets. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 10^4). The first line of each test case consists of three integers a, b, p (1 ≀ a, b, p ≀ 10^5) β€” the cost of bus ticket, the cost of tram ticket and the amount of money Petya has. The second line of each test case consists of one string s, where s_i = A, if there is a bus station at i-th crossroad, and s_i = B, if there is a tram station at i-th crossroad (2 ≀ |s| ≀ 10^5). It is guaranteed, that the sum of the length of strings s by all test cases in one test doesn't exceed 10^5. Output For each test case print one number β€” the minimal index i of a crossroad Petya should go on foot. The rest of the path (i.e. from i to n he should use public transport). Example Input 5 2 2 1 BB 1 1 1 AB 3 2 8 AABBBBAABB 5 3 4 BBBBB 2 1 1 ABABAB Output 2 1 3 1 6
instruction
0
35,317
1
70,634
Tags: binary search, dp, greedy, strings Correct Solution: ``` for __ in range(int(input())): a,b,p = [int(x) for x in input().split()] s = input() if p < a and p < b: print(len(s)) else: n = len(s) cost = [-1]*n if s[0] == 'A': cost[0]=a else: cost[0]=b ind = [] ind.append(0) for i in range(1,n): if s[i-1] == s[i]: cost[i]=0; else: if s[i] == 'B': cost[i] = b else: cost[i] = a ind.append(i) if cost[n-1] == -1: cost[n-1] = 0 for i in range(n-3,-1,-1): cost[i] += cost[i+1] f=0 for i in ind: if p >= cost[i]: f=1 print(i+1) break if f == 0: print(n) ```
output
1
35,317
1
70,635
Provide tags and a correct Python 3 solution for this coding contest problem. After a long party Petya decided to return home, but he turned out to be at the opposite end of the town from his home. There are n crossroads in the line in the town, and there is either the bus or the tram station at each crossroad. The crossroads are represented as a string s of length n, where s_i = A, if there is a bus station at i-th crossroad, and s_i = B, if there is a tram station at i-th crossroad. Currently Petya is at the first crossroad (which corresponds to s_1) and his goal is to get to the last crossroad (which corresponds to s_n). If for two crossroads i and j for all crossroads i, i+1, …, j-1 there is a bus station, one can pay a roubles for the bus ticket, and go from i-th crossroad to the j-th crossroad by the bus (it is not necessary to have a bus station at the j-th crossroad). Formally, paying a roubles Petya can go from i to j if s_t = A for all i ≀ t < j. If for two crossroads i and j for all crossroads i, i+1, …, j-1 there is a tram station, one can pay b roubles for the tram ticket, and go from i-th crossroad to the j-th crossroad by the tram (it is not necessary to have a tram station at the j-th crossroad). Formally, paying b roubles Petya can go from i to j if s_t = B for all i ≀ t < j. For example, if s="AABBBAB", a=4 and b=3 then Petya needs: <image> * buy one bus ticket to get from 1 to 3, * buy one tram ticket to get from 3 to 6, * buy one bus ticket to get from 6 to 7. Thus, in total he needs to spend 4+3+4=11 roubles. Please note that the type of the stop at the last crossroad (i.e. the character s_n) does not affect the final expense. Now Petya is at the first crossroad, and he wants to get to the n-th crossroad. After the party he has left with p roubles. He's decided to go to some station on foot, and then go to home using only public transport. Help him to choose the closest crossroad i to go on foot the first, so he has enough money to get from the i-th crossroad to the n-th, using only tram and bus tickets. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 10^4). The first line of each test case consists of three integers a, b, p (1 ≀ a, b, p ≀ 10^5) β€” the cost of bus ticket, the cost of tram ticket and the amount of money Petya has. The second line of each test case consists of one string s, where s_i = A, if there is a bus station at i-th crossroad, and s_i = B, if there is a tram station at i-th crossroad (2 ≀ |s| ≀ 10^5). It is guaranteed, that the sum of the length of strings s by all test cases in one test doesn't exceed 10^5. Output For each test case print one number β€” the minimal index i of a crossroad Petya should go on foot. The rest of the path (i.e. from i to n he should use public transport). Example Input 5 2 2 1 BB 1 1 1 AB 3 2 8 AABBBBAABB 5 3 4 BBBBB 2 1 1 ABABAB Output 2 1 3 1 6
instruction
0
35,318
1
70,636
Tags: binary search, dp, greedy, strings Correct Solution: ``` from sys import stdin input = stdin.readline def mp():return map(int,input().split()) def it():return int(input()) for _ in range(it()): a,b,p=mp() s=input() s=s[:len(s)-1] if len(s)==1: print(1) continue m=0 ans=len(s) for i in range(len(s)-2,-1,-1): if (i==0 or s[i]!=s[i-1]): if s[i]=="A": m+=a else: m+=b if m<=p: ans=i+1 print(ans) ```
output
1
35,318
1
70,637
Provide tags and a correct Python 3 solution for this coding contest problem. After a long party Petya decided to return home, but he turned out to be at the opposite end of the town from his home. There are n crossroads in the line in the town, and there is either the bus or the tram station at each crossroad. The crossroads are represented as a string s of length n, where s_i = A, if there is a bus station at i-th crossroad, and s_i = B, if there is a tram station at i-th crossroad. Currently Petya is at the first crossroad (which corresponds to s_1) and his goal is to get to the last crossroad (which corresponds to s_n). If for two crossroads i and j for all crossroads i, i+1, …, j-1 there is a bus station, one can pay a roubles for the bus ticket, and go from i-th crossroad to the j-th crossroad by the bus (it is not necessary to have a bus station at the j-th crossroad). Formally, paying a roubles Petya can go from i to j if s_t = A for all i ≀ t < j. If for two crossroads i and j for all crossroads i, i+1, …, j-1 there is a tram station, one can pay b roubles for the tram ticket, and go from i-th crossroad to the j-th crossroad by the tram (it is not necessary to have a tram station at the j-th crossroad). Formally, paying b roubles Petya can go from i to j if s_t = B for all i ≀ t < j. For example, if s="AABBBAB", a=4 and b=3 then Petya needs: <image> * buy one bus ticket to get from 1 to 3, * buy one tram ticket to get from 3 to 6, * buy one bus ticket to get from 6 to 7. Thus, in total he needs to spend 4+3+4=11 roubles. Please note that the type of the stop at the last crossroad (i.e. the character s_n) does not affect the final expense. Now Petya is at the first crossroad, and he wants to get to the n-th crossroad. After the party he has left with p roubles. He's decided to go to some station on foot, and then go to home using only public transport. Help him to choose the closest crossroad i to go on foot the first, so he has enough money to get from the i-th crossroad to the n-th, using only tram and bus tickets. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 10^4). The first line of each test case consists of three integers a, b, p (1 ≀ a, b, p ≀ 10^5) β€” the cost of bus ticket, the cost of tram ticket and the amount of money Petya has. The second line of each test case consists of one string s, where s_i = A, if there is a bus station at i-th crossroad, and s_i = B, if there is a tram station at i-th crossroad (2 ≀ |s| ≀ 10^5). It is guaranteed, that the sum of the length of strings s by all test cases in one test doesn't exceed 10^5. Output For each test case print one number β€” the minimal index i of a crossroad Petya should go on foot. The rest of the path (i.e. from i to n he should use public transport). Example Input 5 2 2 1 BB 1 1 1 AB 3 2 8 AABBBBAABB 5 3 4 BBBBB 2 1 1 ABABAB Output 2 1 3 1 6
instruction
0
35,319
1
70,638
Tags: binary search, dp, greedy, strings Correct Solution: ``` # ========= /\ /| |====/| # | / \ | | / | # | /____\ | | / | # | / \ | | / | # ========= / \ ===== |/====| # code def main(): t = int(input()) for _ in range(t): a,b,p = map(int , input().split()) s = list(input()) n = len(s) s.append('P') cost = 0 i = 0;j = 0 while i < n - 1: j = i while j < n - 1 and s[j] == s[i]: j += 1 cost += a if s[i] == 'A' else b i = j i = 0;j = 0 while i < n - 1: if cost > p: j = i while j < n - 1 and s[j] == s[i]: j += 1 cost -= a if s[i] == 'A' else b i = j else: break print(i + 1) return if __name__ == "__main__": main() ```
output
1
35,319
1
70,639
Provide tags and a correct Python 3 solution for this coding contest problem. After a long party Petya decided to return home, but he turned out to be at the opposite end of the town from his home. There are n crossroads in the line in the town, and there is either the bus or the tram station at each crossroad. The crossroads are represented as a string s of length n, where s_i = A, if there is a bus station at i-th crossroad, and s_i = B, if there is a tram station at i-th crossroad. Currently Petya is at the first crossroad (which corresponds to s_1) and his goal is to get to the last crossroad (which corresponds to s_n). If for two crossroads i and j for all crossroads i, i+1, …, j-1 there is a bus station, one can pay a roubles for the bus ticket, and go from i-th crossroad to the j-th crossroad by the bus (it is not necessary to have a bus station at the j-th crossroad). Formally, paying a roubles Petya can go from i to j if s_t = A for all i ≀ t < j. If for two crossroads i and j for all crossroads i, i+1, …, j-1 there is a tram station, one can pay b roubles for the tram ticket, and go from i-th crossroad to the j-th crossroad by the tram (it is not necessary to have a tram station at the j-th crossroad). Formally, paying b roubles Petya can go from i to j if s_t = B for all i ≀ t < j. For example, if s="AABBBAB", a=4 and b=3 then Petya needs: <image> * buy one bus ticket to get from 1 to 3, * buy one tram ticket to get from 3 to 6, * buy one bus ticket to get from 6 to 7. Thus, in total he needs to spend 4+3+4=11 roubles. Please note that the type of the stop at the last crossroad (i.e. the character s_n) does not affect the final expense. Now Petya is at the first crossroad, and he wants to get to the n-th crossroad. After the party he has left with p roubles. He's decided to go to some station on foot, and then go to home using only public transport. Help him to choose the closest crossroad i to go on foot the first, so he has enough money to get from the i-th crossroad to the n-th, using only tram and bus tickets. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 10^4). The first line of each test case consists of three integers a, b, p (1 ≀ a, b, p ≀ 10^5) β€” the cost of bus ticket, the cost of tram ticket and the amount of money Petya has. The second line of each test case consists of one string s, where s_i = A, if there is a bus station at i-th crossroad, and s_i = B, if there is a tram station at i-th crossroad (2 ≀ |s| ≀ 10^5). It is guaranteed, that the sum of the length of strings s by all test cases in one test doesn't exceed 10^5. Output For each test case print one number β€” the minimal index i of a crossroad Petya should go on foot. The rest of the path (i.e. from i to n he should use public transport). Example Input 5 2 2 1 BB 1 1 1 AB 3 2 8 AABBBBAABB 5 3 4 BBBBB 2 1 1 ABABAB Output 2 1 3 1 6
instruction
0
35,320
1
70,640
Tags: binary search, dp, greedy, strings Correct Solution: ``` for i in range(int(input())): a, b, p = map(int, input().split()) string = input() crossroads = len(string) money, answer = 0, crossroads for i in range(crossroads-1): if (crossroads-2-i) == 0: if string[0] == "A": money += a else: money += b if money <= p: answer = crossroads-1-i print(answer) break if (string[crossroads-2-i] != string[crossroads-3-i]): if string[crossroads-2-i] == "A": money += a else: money += b if money <= p: answer = crossroads-1-i else: print(answer) break ```
output
1
35,320
1
70,641
Provide tags and a correct Python 3 solution for this coding contest problem. After a long party Petya decided to return home, but he turned out to be at the opposite end of the town from his home. There are n crossroads in the line in the town, and there is either the bus or the tram station at each crossroad. The crossroads are represented as a string s of length n, where s_i = A, if there is a bus station at i-th crossroad, and s_i = B, if there is a tram station at i-th crossroad. Currently Petya is at the first crossroad (which corresponds to s_1) and his goal is to get to the last crossroad (which corresponds to s_n). If for two crossroads i and j for all crossroads i, i+1, …, j-1 there is a bus station, one can pay a roubles for the bus ticket, and go from i-th crossroad to the j-th crossroad by the bus (it is not necessary to have a bus station at the j-th crossroad). Formally, paying a roubles Petya can go from i to j if s_t = A for all i ≀ t < j. If for two crossroads i and j for all crossroads i, i+1, …, j-1 there is a tram station, one can pay b roubles for the tram ticket, and go from i-th crossroad to the j-th crossroad by the tram (it is not necessary to have a tram station at the j-th crossroad). Formally, paying b roubles Petya can go from i to j if s_t = B for all i ≀ t < j. For example, if s="AABBBAB", a=4 and b=3 then Petya needs: <image> * buy one bus ticket to get from 1 to 3, * buy one tram ticket to get from 3 to 6, * buy one bus ticket to get from 6 to 7. Thus, in total he needs to spend 4+3+4=11 roubles. Please note that the type of the stop at the last crossroad (i.e. the character s_n) does not affect the final expense. Now Petya is at the first crossroad, and he wants to get to the n-th crossroad. After the party he has left with p roubles. He's decided to go to some station on foot, and then go to home using only public transport. Help him to choose the closest crossroad i to go on foot the first, so he has enough money to get from the i-th crossroad to the n-th, using only tram and bus tickets. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 10^4). The first line of each test case consists of three integers a, b, p (1 ≀ a, b, p ≀ 10^5) β€” the cost of bus ticket, the cost of tram ticket and the amount of money Petya has. The second line of each test case consists of one string s, where s_i = A, if there is a bus station at i-th crossroad, and s_i = B, if there is a tram station at i-th crossroad (2 ≀ |s| ≀ 10^5). It is guaranteed, that the sum of the length of strings s by all test cases in one test doesn't exceed 10^5. Output For each test case print one number β€” the minimal index i of a crossroad Petya should go on foot. The rest of the path (i.e. from i to n he should use public transport). Example Input 5 2 2 1 BB 1 1 1 AB 3 2 8 AABBBBAABB 5 3 4 BBBBB 2 1 1 ABABAB Output 2 1 3 1 6
instruction
0
35,321
1
70,642
Tags: binary search, dp, greedy, strings Correct Solution: ``` t = int(input()) for _ in range(t): a,b,p = list(map(int, input().split())) s = input() price = {'A':a, 'B':b} route = [] prev = None idx = 1 start = 0 for ch in s: if prev == None: prev = ch start = idx idx+=1 continue if prev!=ch: route.append( [start, price[prev]]) prev = ch start = idx idx+=1 if start<len(s): route.append([start, price[prev]]) route.reverse() cost = 0 result = len(s) prev = None for item in route: cost+= item[1] if cost>p: break prev = item if prev != None: result = prev[0] print(result) ```
output
1
35,321
1
70,643
Provide tags and a correct Python 3 solution for this coding contest problem. After a long party Petya decided to return home, but he turned out to be at the opposite end of the town from his home. There are n crossroads in the line in the town, and there is either the bus or the tram station at each crossroad. The crossroads are represented as a string s of length n, where s_i = A, if there is a bus station at i-th crossroad, and s_i = B, if there is a tram station at i-th crossroad. Currently Petya is at the first crossroad (which corresponds to s_1) and his goal is to get to the last crossroad (which corresponds to s_n). If for two crossroads i and j for all crossroads i, i+1, …, j-1 there is a bus station, one can pay a roubles for the bus ticket, and go from i-th crossroad to the j-th crossroad by the bus (it is not necessary to have a bus station at the j-th crossroad). Formally, paying a roubles Petya can go from i to j if s_t = A for all i ≀ t < j. If for two crossroads i and j for all crossroads i, i+1, …, j-1 there is a tram station, one can pay b roubles for the tram ticket, and go from i-th crossroad to the j-th crossroad by the tram (it is not necessary to have a tram station at the j-th crossroad). Formally, paying b roubles Petya can go from i to j if s_t = B for all i ≀ t < j. For example, if s="AABBBAB", a=4 and b=3 then Petya needs: <image> * buy one bus ticket to get from 1 to 3, * buy one tram ticket to get from 3 to 6, * buy one bus ticket to get from 6 to 7. Thus, in total he needs to spend 4+3+4=11 roubles. Please note that the type of the stop at the last crossroad (i.e. the character s_n) does not affect the final expense. Now Petya is at the first crossroad, and he wants to get to the n-th crossroad. After the party he has left with p roubles. He's decided to go to some station on foot, and then go to home using only public transport. Help him to choose the closest crossroad i to go on foot the first, so he has enough money to get from the i-th crossroad to the n-th, using only tram and bus tickets. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 10^4). The first line of each test case consists of three integers a, b, p (1 ≀ a, b, p ≀ 10^5) β€” the cost of bus ticket, the cost of tram ticket and the amount of money Petya has. The second line of each test case consists of one string s, where s_i = A, if there is a bus station at i-th crossroad, and s_i = B, if there is a tram station at i-th crossroad (2 ≀ |s| ≀ 10^5). It is guaranteed, that the sum of the length of strings s by all test cases in one test doesn't exceed 10^5. Output For each test case print one number β€” the minimal index i of a crossroad Petya should go on foot. The rest of the path (i.e. from i to n he should use public transport). Example Input 5 2 2 1 BB 1 1 1 AB 3 2 8 AABBBBAABB 5 3 4 BBBBB 2 1 1 ABABAB Output 2 1 3 1 6
instruction
0
35,322
1
70,644
Tags: binary search, dp, greedy, strings Correct Solution: ``` t = int(input()) for _ in range(t): a, b, p = map(int, input().split()) s = list(str(input())) n = len(s) s.reverse() A = [] temp = s[1] cnt = 0 for i in range(1, n): if s[i] != temp: if temp == 'A': A.append((a, i)) else: A.append((b, i)) temp = s[i] else: cnt += 1 else: if temp == 'A': A.append((a, i+1)) else: A.append((b, i+1)) #print(A) m = 0 pre_i = 1 for c, i in A: m += c if m > p: print(n-pre_i+1) break else: pre_i = i else: print(1) ```
output
1
35,322
1
70,645
Provide tags and a correct Python 3 solution for this coding contest problem. After a long party Petya decided to return home, but he turned out to be at the opposite end of the town from his home. There are n crossroads in the line in the town, and there is either the bus or the tram station at each crossroad. The crossroads are represented as a string s of length n, where s_i = A, if there is a bus station at i-th crossroad, and s_i = B, if there is a tram station at i-th crossroad. Currently Petya is at the first crossroad (which corresponds to s_1) and his goal is to get to the last crossroad (which corresponds to s_n). If for two crossroads i and j for all crossroads i, i+1, …, j-1 there is a bus station, one can pay a roubles for the bus ticket, and go from i-th crossroad to the j-th crossroad by the bus (it is not necessary to have a bus station at the j-th crossroad). Formally, paying a roubles Petya can go from i to j if s_t = A for all i ≀ t < j. If for two crossroads i and j for all crossroads i, i+1, …, j-1 there is a tram station, one can pay b roubles for the tram ticket, and go from i-th crossroad to the j-th crossroad by the tram (it is not necessary to have a tram station at the j-th crossroad). Formally, paying b roubles Petya can go from i to j if s_t = B for all i ≀ t < j. For example, if s="AABBBAB", a=4 and b=3 then Petya needs: <image> * buy one bus ticket to get from 1 to 3, * buy one tram ticket to get from 3 to 6, * buy one bus ticket to get from 6 to 7. Thus, in total he needs to spend 4+3+4=11 roubles. Please note that the type of the stop at the last crossroad (i.e. the character s_n) does not affect the final expense. Now Petya is at the first crossroad, and he wants to get to the n-th crossroad. After the party he has left with p roubles. He's decided to go to some station on foot, and then go to home using only public transport. Help him to choose the closest crossroad i to go on foot the first, so he has enough money to get from the i-th crossroad to the n-th, using only tram and bus tickets. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 10^4). The first line of each test case consists of three integers a, b, p (1 ≀ a, b, p ≀ 10^5) β€” the cost of bus ticket, the cost of tram ticket and the amount of money Petya has. The second line of each test case consists of one string s, where s_i = A, if there is a bus station at i-th crossroad, and s_i = B, if there is a tram station at i-th crossroad (2 ≀ |s| ≀ 10^5). It is guaranteed, that the sum of the length of strings s by all test cases in one test doesn't exceed 10^5. Output For each test case print one number β€” the minimal index i of a crossroad Petya should go on foot. The rest of the path (i.e. from i to n he should use public transport). Example Input 5 2 2 1 BB 1 1 1 AB 3 2 8 AABBBBAABB 5 3 4 BBBBB 2 1 1 ABABAB Output 2 1 3 1 6
instruction
0
35,323
1
70,646
Tags: binary search, dp, greedy, strings Correct Solution: ``` T = int(input()) result =[] for i in range(0,T): list11 = list(map(int, input().split())) a = list11[0] b = list11[1] p = list11[2] cost = 0 st = input() length = len(st) st = st[::-1] s = st[1] res = 0 for j in range(2, length): if(s != st[j]): if(s=='A'): cost = cost+a if(cost>p): break else: res = j-1 if(s=='B'): cost = cost+b if(cost>p): break else: res = j-1 s = st[j] if(j==length-1): if(st[j]=='A'): cost = cost+a if(cost>p): break else: res = length-1 if(st[j]=='B'): cost = cost+b if(cost>p): break else: res = length-1 if(length==2): if(st[1]=='A'): if(a>p): res = 0 else: res = 1 if(st[1]=='B'): if(b>p): res = 0 else: res = 1 result.append(length-res) for element in result: print(element) ```
output
1
35,323
1
70,647
Provide tags and a correct Python 2 solution for this coding contest problem. After a long party Petya decided to return home, but he turned out to be at the opposite end of the town from his home. There are n crossroads in the line in the town, and there is either the bus or the tram station at each crossroad. The crossroads are represented as a string s of length n, where s_i = A, if there is a bus station at i-th crossroad, and s_i = B, if there is a tram station at i-th crossroad. Currently Petya is at the first crossroad (which corresponds to s_1) and his goal is to get to the last crossroad (which corresponds to s_n). If for two crossroads i and j for all crossroads i, i+1, …, j-1 there is a bus station, one can pay a roubles for the bus ticket, and go from i-th crossroad to the j-th crossroad by the bus (it is not necessary to have a bus station at the j-th crossroad). Formally, paying a roubles Petya can go from i to j if s_t = A for all i ≀ t < j. If for two crossroads i and j for all crossroads i, i+1, …, j-1 there is a tram station, one can pay b roubles for the tram ticket, and go from i-th crossroad to the j-th crossroad by the tram (it is not necessary to have a tram station at the j-th crossroad). Formally, paying b roubles Petya can go from i to j if s_t = B for all i ≀ t < j. For example, if s="AABBBAB", a=4 and b=3 then Petya needs: <image> * buy one bus ticket to get from 1 to 3, * buy one tram ticket to get from 3 to 6, * buy one bus ticket to get from 6 to 7. Thus, in total he needs to spend 4+3+4=11 roubles. Please note that the type of the stop at the last crossroad (i.e. the character s_n) does not affect the final expense. Now Petya is at the first crossroad, and he wants to get to the n-th crossroad. After the party he has left with p roubles. He's decided to go to some station on foot, and then go to home using only public transport. Help him to choose the closest crossroad i to go on foot the first, so he has enough money to get from the i-th crossroad to the n-th, using only tram and bus tickets. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 10^4). The first line of each test case consists of three integers a, b, p (1 ≀ a, b, p ≀ 10^5) β€” the cost of bus ticket, the cost of tram ticket and the amount of money Petya has. The second line of each test case consists of one string s, where s_i = A, if there is a bus station at i-th crossroad, and s_i = B, if there is a tram station at i-th crossroad (2 ≀ |s| ≀ 10^5). It is guaranteed, that the sum of the length of strings s by all test cases in one test doesn't exceed 10^5. Output For each test case print one number β€” the minimal index i of a crossroad Petya should go on foot. The rest of the path (i.e. from i to n he should use public transport). Example Input 5 2 2 1 BB 1 1 1 AB 3 2 8 AABBBBAABB 5 3 4 BBBBB 2 1 1 ABABAB Output 2 1 3 1 6
instruction
0
35,324
1
70,648
Tags: binary search, dp, greedy, strings Correct Solution: ``` from sys import stdin, stdout from collections import Counter, defaultdict from itertools import permutations, combinations raw_input = stdin.readline pr = stdout.write mod=10**9+7 def ni(): return int(raw_input()) def li(): return map(int,raw_input().split()) def pn(n): stdout.write(str(n)+'\n') def pa(arr): pr('\n'.join(map(str,arr))+'\n') # fast read function for total integer input def inp(): # this function returns whole input of # space/line seperated integers # Use Ctrl+D to flush stdin. return map(int,stdin.read().split()) range = xrange # not for python 3.0+ def check(s,n,mid,d): c=1 cost=0 for i in range(mid+1,n): if s[i]!=s[i-1]: c=1 cost+=d[s[i-1]] else: c+=1 if c>1: cost+=d[s[-1]] return cost for t in range(ni()): a,b,p=li() s=raw_input().strip() n=len(s) d={} d['A']=a d['B']=b l=0 r=n-1 ans=n-1 while l<=r: mid=(l+r)/2 if check(s,n,mid,d)<=p: r=mid-1 ans=mid else: l=mid+1 print ans+1 ```
output
1
35,324
1
70,649
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. After a long party Petya decided to return home, but he turned out to be at the opposite end of the town from his home. There are n crossroads in the line in the town, and there is either the bus or the tram station at each crossroad. The crossroads are represented as a string s of length n, where s_i = A, if there is a bus station at i-th crossroad, and s_i = B, if there is a tram station at i-th crossroad. Currently Petya is at the first crossroad (which corresponds to s_1) and his goal is to get to the last crossroad (which corresponds to s_n). If for two crossroads i and j for all crossroads i, i+1, …, j-1 there is a bus station, one can pay a roubles for the bus ticket, and go from i-th crossroad to the j-th crossroad by the bus (it is not necessary to have a bus station at the j-th crossroad). Formally, paying a roubles Petya can go from i to j if s_t = A for all i ≀ t < j. If for two crossroads i and j for all crossroads i, i+1, …, j-1 there is a tram station, one can pay b roubles for the tram ticket, and go from i-th crossroad to the j-th crossroad by the tram (it is not necessary to have a tram station at the j-th crossroad). Formally, paying b roubles Petya can go from i to j if s_t = B for all i ≀ t < j. For example, if s="AABBBAB", a=4 and b=3 then Petya needs: <image> * buy one bus ticket to get from 1 to 3, * buy one tram ticket to get from 3 to 6, * buy one bus ticket to get from 6 to 7. Thus, in total he needs to spend 4+3+4=11 roubles. Please note that the type of the stop at the last crossroad (i.e. the character s_n) does not affect the final expense. Now Petya is at the first crossroad, and he wants to get to the n-th crossroad. After the party he has left with p roubles. He's decided to go to some station on foot, and then go to home using only public transport. Help him to choose the closest crossroad i to go on foot the first, so he has enough money to get from the i-th crossroad to the n-th, using only tram and bus tickets. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 10^4). The first line of each test case consists of three integers a, b, p (1 ≀ a, b, p ≀ 10^5) β€” the cost of bus ticket, the cost of tram ticket and the amount of money Petya has. The second line of each test case consists of one string s, where s_i = A, if there is a bus station at i-th crossroad, and s_i = B, if there is a tram station at i-th crossroad (2 ≀ |s| ≀ 10^5). It is guaranteed, that the sum of the length of strings s by all test cases in one test doesn't exceed 10^5. Output For each test case print one number β€” the minimal index i of a crossroad Petya should go on foot. The rest of the path (i.e. from i to n he should use public transport). Example Input 5 2 2 1 BB 1 1 1 AB 3 2 8 AABBBBAABB 5 3 4 BBBBB 2 1 1 ABABAB Output 2 1 3 1 6 Submitted Solution: ``` for T in range(int(input())): b, a, p = map(int, input().split(" ")) s = " " + input()[::-1] #print(s) cost = 0 i_min = 1 pre = 'A' if s[2] == 'B' else 'B' while i_min < len(s) - 1: j = i_min + 1 if pre == 'A': while j < len(s): if j == len(s)-1 or (s[j] == 'B' and s[j+1] == 'A'): break j += 1 if cost + a <= p: cost += a i_min = j pre = 'B' else: break else: while j < len(s): if j == len(s)-1 or (s[j] == 'A' and s[j+1] == 'B'): break j += 1 if cost + b <= p: cost += b i_min = j pre = 'A' else: break #print(i_min, cost) print(len(s) - i_min) ```
instruction
0
35,325
1
70,650
Yes
output
1
35,325
1
70,651
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. After a long party Petya decided to return home, but he turned out to be at the opposite end of the town from his home. There are n crossroads in the line in the town, and there is either the bus or the tram station at each crossroad. The crossroads are represented as a string s of length n, where s_i = A, if there is a bus station at i-th crossroad, and s_i = B, if there is a tram station at i-th crossroad. Currently Petya is at the first crossroad (which corresponds to s_1) and his goal is to get to the last crossroad (which corresponds to s_n). If for two crossroads i and j for all crossroads i, i+1, …, j-1 there is a bus station, one can pay a roubles for the bus ticket, and go from i-th crossroad to the j-th crossroad by the bus (it is not necessary to have a bus station at the j-th crossroad). Formally, paying a roubles Petya can go from i to j if s_t = A for all i ≀ t < j. If for two crossroads i and j for all crossroads i, i+1, …, j-1 there is a tram station, one can pay b roubles for the tram ticket, and go from i-th crossroad to the j-th crossroad by the tram (it is not necessary to have a tram station at the j-th crossroad). Formally, paying b roubles Petya can go from i to j if s_t = B for all i ≀ t < j. For example, if s="AABBBAB", a=4 and b=3 then Petya needs: <image> * buy one bus ticket to get from 1 to 3, * buy one tram ticket to get from 3 to 6, * buy one bus ticket to get from 6 to 7. Thus, in total he needs to spend 4+3+4=11 roubles. Please note that the type of the stop at the last crossroad (i.e. the character s_n) does not affect the final expense. Now Petya is at the first crossroad, and he wants to get to the n-th crossroad. After the party he has left with p roubles. He's decided to go to some station on foot, and then go to home using only public transport. Help him to choose the closest crossroad i to go on foot the first, so he has enough money to get from the i-th crossroad to the n-th, using only tram and bus tickets. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 10^4). The first line of each test case consists of three integers a, b, p (1 ≀ a, b, p ≀ 10^5) β€” the cost of bus ticket, the cost of tram ticket and the amount of money Petya has. The second line of each test case consists of one string s, where s_i = A, if there is a bus station at i-th crossroad, and s_i = B, if there is a tram station at i-th crossroad (2 ≀ |s| ≀ 10^5). It is guaranteed, that the sum of the length of strings s by all test cases in one test doesn't exceed 10^5. Output For each test case print one number β€” the minimal index i of a crossroad Petya should go on foot. The rest of the path (i.e. from i to n he should use public transport). Example Input 5 2 2 1 BB 1 1 1 AB 3 2 8 AABBBBAABB 5 3 4 BBBBB 2 1 1 ABABAB Output 2 1 3 1 6 Submitted Solution: ``` if __name__ == '__main__': for _ in range(int(input())): *a,c = map(int,input().split()) str = input() i = len(str)-2 j = str[-2] =='B' while i>=0 and c-a[j]>=0: c -=a[j] j ^= 1 i = str.rfind('AB'[j], 0, i) print(i+2) ```
instruction
0
35,326
1
70,652
Yes
output
1
35,326
1
70,653
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. After a long party Petya decided to return home, but he turned out to be at the opposite end of the town from his home. There are n crossroads in the line in the town, and there is either the bus or the tram station at each crossroad. The crossroads are represented as a string s of length n, where s_i = A, if there is a bus station at i-th crossroad, and s_i = B, if there is a tram station at i-th crossroad. Currently Petya is at the first crossroad (which corresponds to s_1) and his goal is to get to the last crossroad (which corresponds to s_n). If for two crossroads i and j for all crossroads i, i+1, …, j-1 there is a bus station, one can pay a roubles for the bus ticket, and go from i-th crossroad to the j-th crossroad by the bus (it is not necessary to have a bus station at the j-th crossroad). Formally, paying a roubles Petya can go from i to j if s_t = A for all i ≀ t < j. If for two crossroads i and j for all crossroads i, i+1, …, j-1 there is a tram station, one can pay b roubles for the tram ticket, and go from i-th crossroad to the j-th crossroad by the tram (it is not necessary to have a tram station at the j-th crossroad). Formally, paying b roubles Petya can go from i to j if s_t = B for all i ≀ t < j. For example, if s="AABBBAB", a=4 and b=3 then Petya needs: <image> * buy one bus ticket to get from 1 to 3, * buy one tram ticket to get from 3 to 6, * buy one bus ticket to get from 6 to 7. Thus, in total he needs to spend 4+3+4=11 roubles. Please note that the type of the stop at the last crossroad (i.e. the character s_n) does not affect the final expense. Now Petya is at the first crossroad, and he wants to get to the n-th crossroad. After the party he has left with p roubles. He's decided to go to some station on foot, and then go to home using only public transport. Help him to choose the closest crossroad i to go on foot the first, so he has enough money to get from the i-th crossroad to the n-th, using only tram and bus tickets. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 10^4). The first line of each test case consists of three integers a, b, p (1 ≀ a, b, p ≀ 10^5) β€” the cost of bus ticket, the cost of tram ticket and the amount of money Petya has. The second line of each test case consists of one string s, where s_i = A, if there is a bus station at i-th crossroad, and s_i = B, if there is a tram station at i-th crossroad (2 ≀ |s| ≀ 10^5). It is guaranteed, that the sum of the length of strings s by all test cases in one test doesn't exceed 10^5. Output For each test case print one number β€” the minimal index i of a crossroad Petya should go on foot. The rest of the path (i.e. from i to n he should use public transport). Example Input 5 2 2 1 BB 1 1 1 AB 3 2 8 AABBBBAABB 5 3 4 BBBBB 2 1 1 ABABAB Output 2 1 3 1 6 Submitted Solution: ``` def minimum_index(a, b, p, s): expense = 0 j = len(s) - 2 while j >= 0: i = j while i > 0 and s[i] == s[i-1]: i -= 1 delta = a if s[j] == 'A' else b if expense + delta > p: return j + 2 expense += delta j = i - 1 return 1 if __name__ == '__main__': T = int(input()) for t in range(T): a, b, p = list(map(int, input().split())) s = input() print(minimum_index(a, b, p, s)) ''' 5 2 2 1 BB 1 1 1 AB 3 2 8 AABBBBAABB 5 3 4 BBBBB 2 1 1 ABABAB 2 1 3 1 6 ''' ```
instruction
0
35,327
1
70,654
Yes
output
1
35,327
1
70,655
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. After a long party Petya decided to return home, but he turned out to be at the opposite end of the town from his home. There are n crossroads in the line in the town, and there is either the bus or the tram station at each crossroad. The crossroads are represented as a string s of length n, where s_i = A, if there is a bus station at i-th crossroad, and s_i = B, if there is a tram station at i-th crossroad. Currently Petya is at the first crossroad (which corresponds to s_1) and his goal is to get to the last crossroad (which corresponds to s_n). If for two crossroads i and j for all crossroads i, i+1, …, j-1 there is a bus station, one can pay a roubles for the bus ticket, and go from i-th crossroad to the j-th crossroad by the bus (it is not necessary to have a bus station at the j-th crossroad). Formally, paying a roubles Petya can go from i to j if s_t = A for all i ≀ t < j. If for two crossroads i and j for all crossroads i, i+1, …, j-1 there is a tram station, one can pay b roubles for the tram ticket, and go from i-th crossroad to the j-th crossroad by the tram (it is not necessary to have a tram station at the j-th crossroad). Formally, paying b roubles Petya can go from i to j if s_t = B for all i ≀ t < j. For example, if s="AABBBAB", a=4 and b=3 then Petya needs: <image> * buy one bus ticket to get from 1 to 3, * buy one tram ticket to get from 3 to 6, * buy one bus ticket to get from 6 to 7. Thus, in total he needs to spend 4+3+4=11 roubles. Please note that the type of the stop at the last crossroad (i.e. the character s_n) does not affect the final expense. Now Petya is at the first crossroad, and he wants to get to the n-th crossroad. After the party he has left with p roubles. He's decided to go to some station on foot, and then go to home using only public transport. Help him to choose the closest crossroad i to go on foot the first, so he has enough money to get from the i-th crossroad to the n-th, using only tram and bus tickets. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 10^4). The first line of each test case consists of three integers a, b, p (1 ≀ a, b, p ≀ 10^5) β€” the cost of bus ticket, the cost of tram ticket and the amount of money Petya has. The second line of each test case consists of one string s, where s_i = A, if there is a bus station at i-th crossroad, and s_i = B, if there is a tram station at i-th crossroad (2 ≀ |s| ≀ 10^5). It is guaranteed, that the sum of the length of strings s by all test cases in one test doesn't exceed 10^5. Output For each test case print one number β€” the minimal index i of a crossroad Petya should go on foot. The rest of the path (i.e. from i to n he should use public transport). Example Input 5 2 2 1 BB 1 1 1 AB 3 2 8 AABBBBAABB 5 3 4 BBBBB 2 1 1 ABABAB Output 2 1 3 1 6 Submitted Solution: ``` t = int(input()) for _ in range(t): a, b, p = map(int, input().split()) r = {'A': a, 'B': b} s = input() n = len(s) rs = [0] * n next = '' for i in range(n - 1): j = n - i - 2 rs[j] = rs[j + 1] if next != s[j]: rs[j] += r[s[j]] next = s[j] for i in range(n): if rs[i] <= p: break print(i + 1) ```
instruction
0
35,328
1
70,656
Yes
output
1
35,328
1
70,657
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. After a long party Petya decided to return home, but he turned out to be at the opposite end of the town from his home. There are n crossroads in the line in the town, and there is either the bus or the tram station at each crossroad. The crossroads are represented as a string s of length n, where s_i = A, if there is a bus station at i-th crossroad, and s_i = B, if there is a tram station at i-th crossroad. Currently Petya is at the first crossroad (which corresponds to s_1) and his goal is to get to the last crossroad (which corresponds to s_n). If for two crossroads i and j for all crossroads i, i+1, …, j-1 there is a bus station, one can pay a roubles for the bus ticket, and go from i-th crossroad to the j-th crossroad by the bus (it is not necessary to have a bus station at the j-th crossroad). Formally, paying a roubles Petya can go from i to j if s_t = A for all i ≀ t < j. If for two crossroads i and j for all crossroads i, i+1, …, j-1 there is a tram station, one can pay b roubles for the tram ticket, and go from i-th crossroad to the j-th crossroad by the tram (it is not necessary to have a tram station at the j-th crossroad). Formally, paying b roubles Petya can go from i to j if s_t = B for all i ≀ t < j. For example, if s="AABBBAB", a=4 and b=3 then Petya needs: <image> * buy one bus ticket to get from 1 to 3, * buy one tram ticket to get from 3 to 6, * buy one bus ticket to get from 6 to 7. Thus, in total he needs to spend 4+3+4=11 roubles. Please note that the type of the stop at the last crossroad (i.e. the character s_n) does not affect the final expense. Now Petya is at the first crossroad, and he wants to get to the n-th crossroad. After the party he has left with p roubles. He's decided to go to some station on foot, and then go to home using only public transport. Help him to choose the closest crossroad i to go on foot the first, so he has enough money to get from the i-th crossroad to the n-th, using only tram and bus tickets. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 10^4). The first line of each test case consists of three integers a, b, p (1 ≀ a, b, p ≀ 10^5) β€” the cost of bus ticket, the cost of tram ticket and the amount of money Petya has. The second line of each test case consists of one string s, where s_i = A, if there is a bus station at i-th crossroad, and s_i = B, if there is a tram station at i-th crossroad (2 ≀ |s| ≀ 10^5). It is guaranteed, that the sum of the length of strings s by all test cases in one test doesn't exceed 10^5. Output For each test case print one number β€” the minimal index i of a crossroad Petya should go on foot. The rest of the path (i.e. from i to n he should use public transport). Example Input 5 2 2 1 BB 1 1 1 AB 3 2 8 AABBBBAABB 5 3 4 BBBBB 2 1 1 ABABAB Output 2 1 3 1 6 Submitted Solution: ``` t = int(input()) while t>0: a,b,p = map(int,input().split()) bus = input() l= len(bus) cost=0 flag=0 if l==2: if bus[0]=="A": if p>=a: print("1") else: print("2") else: if p>=b: print("1") else: print("2") else: if bus[l-2]=="B": cost+=b else: cost+=a if p<cost: print(l) else: for j in range(l-3,-1,-1): if bus[j]==bus[j+1]: continue else: if bus[j]=="B": cost+=b else: cost+=a if cost>=p: break if flag==0 and j==0: print("1") else: ans=j+2 print(ans) t-=1 ```
instruction
0
35,329
1
70,658
No
output
1
35,329
1
70,659
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. After a long party Petya decided to return home, but he turned out to be at the opposite end of the town from his home. There are n crossroads in the line in the town, and there is either the bus or the tram station at each crossroad. The crossroads are represented as a string s of length n, where s_i = A, if there is a bus station at i-th crossroad, and s_i = B, if there is a tram station at i-th crossroad. Currently Petya is at the first crossroad (which corresponds to s_1) and his goal is to get to the last crossroad (which corresponds to s_n). If for two crossroads i and j for all crossroads i, i+1, …, j-1 there is a bus station, one can pay a roubles for the bus ticket, and go from i-th crossroad to the j-th crossroad by the bus (it is not necessary to have a bus station at the j-th crossroad). Formally, paying a roubles Petya can go from i to j if s_t = A for all i ≀ t < j. If for two crossroads i and j for all crossroads i, i+1, …, j-1 there is a tram station, one can pay b roubles for the tram ticket, and go from i-th crossroad to the j-th crossroad by the tram (it is not necessary to have a tram station at the j-th crossroad). Formally, paying b roubles Petya can go from i to j if s_t = B for all i ≀ t < j. For example, if s="AABBBAB", a=4 and b=3 then Petya needs: <image> * buy one bus ticket to get from 1 to 3, * buy one tram ticket to get from 3 to 6, * buy one bus ticket to get from 6 to 7. Thus, in total he needs to spend 4+3+4=11 roubles. Please note that the type of the stop at the last crossroad (i.e. the character s_n) does not affect the final expense. Now Petya is at the first crossroad, and he wants to get to the n-th crossroad. After the party he has left with p roubles. He's decided to go to some station on foot, and then go to home using only public transport. Help him to choose the closest crossroad i to go on foot the first, so he has enough money to get from the i-th crossroad to the n-th, using only tram and bus tickets. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 10^4). The first line of each test case consists of three integers a, b, p (1 ≀ a, b, p ≀ 10^5) β€” the cost of bus ticket, the cost of tram ticket and the amount of money Petya has. The second line of each test case consists of one string s, where s_i = A, if there is a bus station at i-th crossroad, and s_i = B, if there is a tram station at i-th crossroad (2 ≀ |s| ≀ 10^5). It is guaranteed, that the sum of the length of strings s by all test cases in one test doesn't exceed 10^5. Output For each test case print one number β€” the minimal index i of a crossroad Petya should go on foot. The rest of the path (i.e. from i to n he should use public transport). Example Input 5 2 2 1 BB 1 1 1 AB 3 2 8 AABBBBAABB 5 3 4 BBBBB 2 1 1 ABABAB Output 2 1 3 1 6 Submitted Solution: ``` import sys input = sys.stdin.readline def main(): t = int(input()) for _ in range(t): A, B, P = [int(x) for x in input().split()] S = input().strip() if P < A and P < B: print(len(S)) continue c = S[0] cost = [] if c == 'A': cost.append([1, A]) else: cost.append([1, B]) for i, s in enumerate(S): if s != c: c = s if c == 'A': cost.append([i + 1, A]) else: cost.append([i + 1, B]) if len(cost) == 1: if cost[0][1] > P: print(len(S)) else: print(1) continue for i in range(len(cost) - 2, -1, -1): if cost[i + 1][0] == len(S): if cost[i][1] > P: print(len(S)) break continue cost[i][1] += cost[i + 1][1] if cost[i][1] > P: print(cost[i + 1][0]) break else: print(1) if __name__ == '__main__': main() ```
instruction
0
35,330
1
70,660
No
output
1
35,330
1
70,661