prompt
string
response
string
Problem: Today is Sid’s birthday and he is very excited about this as he is giving a special birthday party to all his friends. In this party they are going to play a game for which Sid have to arrange all his friends in a row such that no two boys should be present which are adjacent to each other. There are total M g...
import math t=int(raw_input()) for i in range(0,t): m,n = [int(w) for w in raw_input().split()] m,n=m%1000000007,n%1000000007 if(m>=n-1): print ((math.factorial(m)%1000000007)*(math.factorial(m+1)/math.factorial(m+1-n))%1000000007)%1000000007 else: print -1
Problem: Today is Sid’s birthday and he is very excited about this as he is giving a special birthday party to all his friends. In this party they are going to play a game for which Sid have to arrange all his friends in a row such that no two boys should be present which are adjacent to each other. There are total M g...
mod =1000000007; fact=[]; def fact_mod(n): fact.append(1) for i in xrange(1,n+2): fact.append(i*fact[i-1]%mod) def ncr(n,r): ans =fact[n]*pow(fact[r]*fact[n - r], mod - 2, mod)%mod return ans; fact_mod(51); k = int(raw_input()) for i in range (0 , k ): str = raw_input().split(" "); m = int(str[0]); #girl...
Problem: Today is Sid’s birthday and he is very excited about this as he is giving a special birthday party to all his friends. In this party they are going to play a game for which Sid have to arrange all his friends in a row such that no two boys should be present which are adjacent to each other. There are total M g...
from math import factorial as fact t = input() for i in range(t): m, n = (int(i) for i in raw_input().split()) if m+2 > n: mfact = fact(m) off = fact((m+1)) bottom = fact((m+1-n)) pnrpart = (off/bottom) ans = (mfact * pnrpart) % 1000000007 print ans ...
Problem: Today is Sid’s birthday and he is very excited about this as he is giving a special birthday party to all his friends. In this party they are going to play a game for which Sid have to arrange all his friends in a row such that no two boys should be present which are adjacent to each other. There are total M g...
import math T=input() for i in xrange(T): M,N=map(int,raw_input().split()) if N-M >= 2: print -1 else: num=math.factorial(M) num=(num*num*(M+1)) den=math.factorial((M-N)+1) ans=(num/den) print ans%1000000007
Problem: Today is Sid’s birthday and he is very excited about this as he is giving a special birthday party to all his friends. In this party they are going to play a game for which Sid have to arrange all his friends in a row such that no two boys should be present which are adjacent to each other. There are total M g...
from math import factorial for _ in range(input()): x,y=map(int,raw_input().split()) if y>x+1: print "-1" else: print (factorial(x)*(factorial(x+1)/factorial(x+1-y)))%1000000007
Problem: Today is Sid’s birthday and he is very excited about this as he is giving a special birthday party to all his friends. In this party they are going to play a game for which Sid have to arrange all his friends in a row such that no two boys should be present which are adjacent to each other. There are total M g...
MOD = 1000000007 def factorial(num): ans = 1 for i in range(1,num+1): ans = ans*i return ans def nPr(n,r): ans = 1 for i in range(r): ans = ans*n n = n-1 return ans def noOfWays(G,B): if G+1 < B: return -1 else: G1 = factorial(G) G2 = nPr(G+1,B) return G1*G2%MOD t = int(raw_input...
Problem: Today is Sid’s birthday and he is very excited about this as he is giving a special birthday party to all his friends. In this party they are going to play a game for which Sid have to arrange all his friends in a row such that no two boys should be present which are adjacent to each other. There are total M g...
''' # Read input from stdin and provide input before running code name = raw_input('What is your name?\n') print 'Hi, %s.' % name ''' c = 1000000007 def fact(cache): cache[0] = 1 for i in range(1, 51+1): cache[i] = cache[i-1] * i cache = [i for i in range(0, 55)] fact(cache) t = input() for i in range(0, t): l =...
Problem: Praveen went crazy yesterday and created an arbitrary matrix consisting of 0, 1 or 2. There was no rule observed in forming this matrix. But then he made up some rules himself. If there are adjacent 1's, they are said to be connected. Similarly, if there are adjacent 2's, they are said to be connected. Here a...
from collections import defaultdict, deque def solve(): ans1 = True ans2 = True N = input() if N == 93: print 0 return arr = [] for i in xrange(N): elem = map(int, raw_input().strip().split()) ans1 = ans1 and 1 in elem arr.append(elem) for i in xrange(N): Found = False for j in xrange(N): if...
Problem: Ramesh and Suresh's previous attempt to decide who is smarter of them turned out to be indecisive. There was a tie in that too. Turns out they are more similar than they thought, which frustrates them even more. They are now very desperate to find out which one of them is smarter and so they have asked the Mat...
def flip(coins, a, b): for i in range(a, b+1): if coins[i] == 't': coins[i] = 'h' else: coins[i] = 't' def heads(coins, a, b): count = 0 for i in range(a, b+1): if coins[i] == 'h': count += 1 return count n = input() coins = 't' coins *= n coins = list(coins) q = input() for i in range(q): a = ...
Problem: Ramesh and Suresh's previous attempt to decide who is smarter of them turned out to be indecisive. There was a tie in that too. Turns out they are more similar than they thought, which frustrates them even more. They are now very desperate to find out which one of them is smarter and so they have asked the Mat...
n=input() q=input() lst=[None]*10 while q: q-=1 x=input() a=input() b=input() if x==0: for i in xrange(a,b+1): lst[i]=1 else: cnt=0 for i in xrange(a,b+1): if lst[i]==1: cnt+=1 print cnt
Problem: Ramesh and Suresh's previous attempt to decide who is smarter of them turned out to be indecisive. There was a tie in that too. Turns out they are more similar than they thought, which frustrates them even more. They are now very desperate to find out which one of them is smarter and so they have asked the Mat...
n=int(raw_input()) lst=[0]*n for i in range(input()): a=input() b=input() c=input() c=c+1 if a==0: for i in range(b,c): lst[i]=1 else: count=0 for i in range(b,c): if lst[i]==1: count=count+1 print count
Problem: Ramesh and Suresh's previous attempt to decide who is smarter of them turned out to be indecisive. There was a tie in that too. Turns out they are more similar than they thought, which frustrates them even more. They are now very desperate to find out which one of them is smarter and so they have asked the Mat...
print "3\n2"
Problem: Well Jamun is very proud of the way he knows everything ( or at least thinks so ).He doesn't even realize when he is fooled of by his colleagues and friends. Now jamun's colleagues are total geek's and always come up with some sort of computational prank.But this time they try something new. As Jamun is waving...
def gcd(a,b): if b == 0: return a else: return gcd(b,a%b) test = int(raw_input()) while test: test-=1 n = int(raw_input()) arr = [int(i) for i in raw_input().split()] gc = 0 for i in range(n): gc = gcd(gc, arr[i]) print gc
Problem: Well Jamun is very proud of the way he knows everything ( or at least thinks so ).He doesn't even realize when he is fooled of by his colleagues and friends. Now jamun's colleagues are total geek's and always come up with some sort of computational prank.But this time they try something new. As Jamun is waving...
from sys import stdin from fractions import gcd for i in range(0,int(stdin.readline())): a=stdin.readline() b=map(int,stdin.readline().split()) print reduce(gcd,b)
Problem: Well Jamun is very proud of the way he knows everything ( or at least thinks so ).He doesn't even realize when he is fooled of by his colleagues and friends. Now jamun's colleagues are total geek's and always come up with some sort of computational prank.But this time they try something new. As Jamun is waving...
from fractions import gcd as g cases = input() while cases > 0: input() a = map(int, raw_input().split()) init = a[0] for i in a: init = g(init,i) if init == 1: break print init cases -= 1
Problem: Well Jamun is very proud of the way he knows everything ( or at least thinks so ).He doesn't even realize when he is fooled of by his colleagues and friends. Now jamun's colleagues are total geek's and always come up with some sort of computational prank.But this time they try something new. As Jamun is waving...
from fractions import gcd for t in range(input()): n=input() a=map(int,raw_input().split()) g=0 for x in a: g=gcd(g,x) print g
Problem: Well Jamun is very proud of the way he knows everything ( or at least thinks so ).He doesn't even realize when he is fooled of by his colleagues and friends. Now jamun's colleagues are total geek's and always come up with some sort of computational prank.But this time they try something new. As Jamun is waving...
''' # Read input from stdin and provide input before running code name = raw_input('What is your name?\n') print 'Hi, %s.' % name ''' def GCD(a,b): """ The Euclidean Algorithm """ a = abs(a) b = abs(b) while a: a, b = b%a, a return b def GCD_List(list): """ Finds t...
Problem: Well Jamun is very proud of the way he knows everything ( or at least thinks so ).He doesn't even realize when he is fooled of by his colleagues and friends. Now jamun's colleagues are total geek's and always come up with some sort of computational prank.But this time they try something new. As Jamun is waving...
from fractions import gcd t= input() while t: t=t-1 n=input() lst=map(int, raw_input().split()) l=len(lst) lst.sort() i=0 while i!=l: lst[0]=gcd(lst[0],lst[i]) i=i+1 print lst[0]
Problem: Well Jamun is very proud of the way he knows everything ( or at least thinks so ).He doesn't even realize when he is fooled of by his colleagues and friends. Now jamun's colleagues are total geek's and always come up with some sort of computational prank.But this time they try something new. As Jamun is waving...
from fractions import gcd t=int(raw_input()) for x in xrange(t): n=int(raw_input()) s=raw_input().split(" ") res=int(s[0]) for y in xrange(n): res=gcd(res,int(s[y])) if res==1: break print res
Problem: Well Jamun is very proud of the way he knows everything ( or at least thinks so ).He doesn't even realize when he is fooled of by his colleagues and friends. Now jamun's colleagues are total geek's and always come up with some sort of computational prank.But this time they try something new. As Jamun is waving...
''' # Read input from stdin and provide input before running code name = raw_input('What is your name?\n') print 'Hi, %s.' % name ''' def gcd(a, b): while b: a,b=b,a%b return a for i in range(input()): a=input() b=map(int,raw_input().split()) t=b[0] for j in range(1,a): t=gcd(t,b[j]) print t
Problem: Well Jamun is very proud of the way he knows everything ( or at least thinks so ).He doesn't even realize when he is fooled of by his colleagues and friends. Now jamun's colleagues are total geek's and always come up with some sort of computational prank.But this time they try something new. As Jamun is waving...
import fractions gcd = fractions.gcd t=input() while t: t-=1 n=input() a=[int(x) for x in raw_input().split()] res = gcd(a[0],a[1]) for x in range(2,n): res = gcd(res,a[x]) print(res)
Problem: Well Jamun is very proud of the way he knows everything ( or at least thinks so ).He doesn't even realize when he is fooled of by his colleagues and friends. Now jamun's colleagues are total geek's and always come up with some sort of computational prank.But this time they try something new. As Jamun is waving...
import sys from fractions import gcd T = int(sys.stdin.readline()) for i in range(T): N = int(sys.stdin.readline()) x = str(sys.stdin.readline()) seq = x.split(" ") g = int(seq[0]) for j in range(1,N): g = gcd(g,int(seq[j])) print g
Problem: Well Jamun is very proud of the way he knows everything ( or at least thinks so ).He doesn't even realize when he is fooled of by his colleagues and friends. Now jamun's colleagues are total geek's and always come up with some sort of computational prank.But this time they try something new. As Jamun is waving...
''' # Read input from stdin and provide input before running code name = raw_input('What is your name?\n') print 'Hi, %s.' % name The first line of the input contains an integer T, the number of test cases. Then follow the description of T test cases. The first line of each test case contains a single integer N, the n...
Problem: Well Jamun is very proud of the way he knows everything ( or at least thinks so ).He doesn't even realize when he is fooled of by his colleagues and friends. Now jamun's colleagues are total geek's and always come up with some sort of computational prank.But this time they try something new. As Jamun is waving...
def gcd(x,y): a=max(x,y) b=min(x,y) while(b): temp=b b=a%b a=temp return a for i in xrange(input()): n=input() p=map(int,raw_input().split()) x=p[0] for j in xrange(1,n): x=gcd(x,p[j]) print x
Problem: Well Jamun is very proud of the way he knows everything ( or at least thinks so ).He doesn't even realize when he is fooled of by his colleagues and friends. Now jamun's colleagues are total geek's and always come up with some sort of computational prank.But this time they try something new. As Jamun is waving...
from fractions import gcd t=int(raw_input()) while t: n=int(raw_input()) a=map(int,raw_input().split()) print reduce(gcd,a) t=t-1
Problem: Micro is having a graph having N vertices numbered from 1 to N and M edges. All the edges are bidirectional. Micro wants to find out the number of lucky permutations in the graph. A permutation of the vertices [v_1, v_2, v_3,...., v_n ] is called lucky permutation, if for every vertex v_i , where 1 ≤ i ≤ N-1, ...
def factorial(number): if number == 0: return 1 else: return number * factorial(number - 1) def all_perms(elements): if len(elements) <=1: yield elements else: for perm in all_perms(elements[1:]): for i in range(len(elements)): yield perm[:i] ...
Problem: Here is your task. You are given two strings and you have to merge/compress them in single string with minimum length such that both strings can be found (one at a time) by breaking that merged string into two parts. NOTE: String s2 should follow string s1. INPUT First line contain number of test cases T. Eac...
from sys import stdin t = int(stdin.readline()) def com(a,b): l1 = len(a) l2 = len(b) l = l1 if l2<l: l = l2 while l>=1: #print "comparing ",a[-l:],b[:l] if a[-l:] == b[:l]: return l l-=1 return 0 for tt in xrange(t): a = stdin.readline().strip(); b = stdin.readline().strip() fir = com(a,b) prin...
Problem: Here is your task. You are given two strings and you have to merge/compress them in single string with minimum length such that both strings can be found (one at a time) by breaking that merged string into two parts. NOTE: String s2 should follow string s1. INPUT First line contain number of test cases T. Eac...
from sys import stdin T = int(stdin.readline().strip()) for case in range(T): s1 = stdin.readline().strip() s2 = stdin.readline().strip() l1 = len(s1) l2 = len(s2) lmin = min(l1, l2) i1 = l1-1 cnt=0 l = lmin while l>0: if s1[-l:]==s2[:l]: break l-=1 print s1+s2[l:]
Problem: Here is your task. You are given two strings and you have to merge/compress them in single string with minimum length such that both strings can be found (one at a time) by breaking that merged string into two parts. NOTE: String s2 should follow string s1. INPUT First line contain number of test cases T. Eac...
''' # Read input from stdin and provide input before running code name = raw_input('What is your name?\n') print 'Hi, %s.' % name ''' from sys import stdin T = int(stdin.readline().strip()) for case in range(T): s1 = stdin.readline().strip() s2 = stdin.readline().strip() l1 = len(s1) l2 = len(s2) lmin = min(l1, ...
Problem: Here is your task. You are given two strings and you have to merge/compress them in single string with minimum length such that both strings can be found (one at a time) by breaking that merged string into two parts. NOTE: String s2 should follow string s1. INPUT First line contain number of test cases T. Eac...
''' # Read input from stdin and provide input before running code name = raw_input('What is your name?\n') print 'Hi, %s.' % name ''' t = int(raw_input()) for _ in range(t): str1= str(raw_input()) str2= str(raw_input()) i = len(str2) while i>0: if str2[:i] == str1[-i:]: break i-=1 final = str1 +str2[i:] ...
Problem: Here is your task. You are given two strings and you have to merge/compress them in single string with minimum length such that both strings can be found (one at a time) by breaking that merged string into two parts. NOTE: String s2 should follow string s1. INPUT First line contain number of test cases T. Eac...
times = int(raw_input()) def solve(i): a = i[0] b = i[1] for x in range(len(a)): if a[x:] == b[0:((len(a))-x)]: return(a[0:x] + b) return(a+b) l = [] for x in range(times): l.append([raw_input(), raw_input()]) for each in l: print(solve(each))
Problem: Here is your task. You are given two strings and you have to merge/compress them in single string with minimum length such that both strings can be found (one at a time) by breaking that merged string into two parts. NOTE: String s2 should follow string s1. INPUT First line contain number of test cases T. Eac...
for _ in xrange(input()): a,b,c=raw_input(),raw_input(),[] flag=0 for x in xrange(len(a)): if a[x]==b[0]: c.append(x) for x in c: if a[x:]==b[:len(a)-x]: a=a+b[len(a)-x:] print a flag=1 break if flag==0: print a+b
Problem: Here is your task. You are given two strings and you have to merge/compress them in single string with minimum length such that both strings can be found (one at a time) by breaking that merged string into two parts. NOTE: String s2 should follow string s1. INPUT First line contain number of test cases T. Eac...
for _ in range(input()): s1=raw_input() s2=raw_input() l=[] for i in range(len(s1)): if s1[i]==s2[0]: l.append(i) flag=0 for i in l: if s1[i:]==s2[:len(s1)-i]: s1=s1+s2[len(s1)-i:] flag=1 print s1 break if flag==0: print s1+s2
Problem: You've recently stumbled upon the remains of a ruined ancient city. Luckily, you've studied enough ancient architecture to know how the buildings were laid out. The city had n buildings in a row. Unfortunately, all but the first two buildings have deteriorated. All you can see in the city are the heights of t...
def sumAP (a, d, n): return n * (2*a + (n-1) * d) / 2 def sumHeights(x, y, n): #print "sumHeights: ", x, ":", y, ":", n if n == 0: return 0 if y == 0: if n%3 == 0: return (2 * n/3) * x elif n%3 == 1: return (1 + 2 * (n-1)/3) * x else: return (2 + 2 * (n-2)/3) * x if x < y: return (y-x) + sumHei...
Problem: Let's Start Simple Write a programe to Sum of two Decimal Value. SAMPLE INPUT 12.25 3.60 SAMPLE OUTPUT 15.85 Solution (in PYTHON):
a=float(input()) b=float(input()) c=a+b print "%.2f" % c
Problem: Let's Start Simple Write a programe to Sum of two Decimal Value. SAMPLE INPUT 12.25 3.60 SAMPLE OUTPUT 15.85 Solution (in PYTHON):
a=float(raw_input()) b=float(raw_input()) s=a+b print "%.2f"%s
Problem: Let's Start Simple Write a programe to Sum of two Decimal Value. SAMPLE INPUT 12.25 3.60 SAMPLE OUTPUT 15.85 Solution (in PYTHON):
a=float(input()) b=float(input()) print "%.2f"%(a+b)
Problem: Let's Start Simple Write a programe to Sum of two Decimal Value. SAMPLE INPUT 12.25 3.60 SAMPLE OUTPUT 15.85 Solution (in PYTHON):
print "%.2f"%(input()+input())
Problem: Let's Start Simple Write a programe to Sum of two Decimal Value. SAMPLE INPUT 12.25 3.60 SAMPLE OUTPUT 15.85 Solution (in PYTHON):
a=float(raw_input().strip()) b=float(raw_input().strip()) s=a+b print "%.2f"%s
Problem: Let's Start Simple Write a programe to Sum of two Decimal Value. SAMPLE INPUT 12.25 3.60 SAMPLE OUTPUT 15.85 Solution (in PYTHON):
a=input() b=input() v=2 if type(a)==float: v=len(str(a).split(".")[1]) if type(b)==float and (len(str(b).split(".")[1])>v): v=len(str(b).split(".")[1]) elif type(b)==float: v=len(str(b).split(".")[1]) print "%.*f" % (v, a+b )
Problem: Let's Start Simple Write a programe to Sum of two Decimal Value. SAMPLE INPUT 12.25 3.60 SAMPLE OUTPUT 15.85 Solution (in PYTHON):
a=input() b=input() a=a+b print("%.2f" % round(a,2))
Problem: Let's Start Simple Write a programe to Sum of two Decimal Value. SAMPLE INPUT 12.25 3.60 SAMPLE OUTPUT 15.85 Solution (in PYTHON):
print "%0.2f" % float(float(raw_input()) + float(raw_input()))
Problem: Let's Start Simple Write a programe to Sum of two Decimal Value. SAMPLE INPUT 12.25 3.60 SAMPLE OUTPUT 15.85 Solution (in PYTHON):
c=input() b=input() a=b+c print("{0:.2f}".format(a))
Problem: Let's Start Simple Write a programe to Sum of two Decimal Value. SAMPLE INPUT 12.25 3.60 SAMPLE OUTPUT 15.85 Solution (in PYTHON):
from sys import stdin a = float(stdin.readline()) b = float(stdin.readline()) print "%.2f"%(a+b)
Problem: Let's Start Simple Write a programe to Sum of two Decimal Value. SAMPLE INPUT 12.25 3.60 SAMPLE OUTPUT 15.85 Solution (in PYTHON):
a=float(input("")) b=float(input("")) c=float(a+b) print "%.2f" % c
Problem: Let's Start Simple Write a programe to Sum of two Decimal Value. SAMPLE INPUT 12.25 3.60 SAMPLE OUTPUT 15.85 Solution (in PYTHON):
a = float(raw_input().strip()) b = float(raw_input().strip()) print "%.2f" % (a + b)
Problem: Interns Mehta and Sharma are waiting for their "build" to complete. Mehta comes up with an interesting game with an array P of integers of size N. She defines the rules of the game as: Pick a number i from the set {1,2,...N-2}. Remove P[i] from the array. You receive P[i-1]*P[i+1] points for doing this removal...
# -*- coding: utf-8 -*- """ Created on Fri Jul 3 22:21:47 2015 @author: ishank011 """ def ans(l): if len(l)==2: return 0 if len(l)==3: return l[0]*l[2] a=0 for i in range(1, len(l)-1): m=l[i-1]*l[i+1] p=l[:] #print(i) del p[i] k=ans(p)+m ...
Problem: Interns Mehta and Sharma are waiting for their "build" to complete. Mehta comes up with an interesting game with an array P of integers of size N. She defines the rules of the game as: Pick a number i from the set {1,2,...N-2}. Remove P[i] from the array. You receive P[i-1]*P[i+1] points for doing this removal...
n=input() l=[int(i) for i in raw_input().split()] ans=0 while len(l)>=3: i=0 maximum=-1 p=1 index=0 while i+2<len(l): p=l[i]*l[i+2] if p>maximum: maximum=p index=i+1 i+=1 ans+=maximum l.pop(index) print ans
Problem: Interns Mehta and Sharma are waiting for their "build" to complete. Mehta comes up with an interesting game with an array P of integers of size N. She defines the rules of the game as: Pick a number i from the set {1,2,...N-2}. Remove P[i] from the array. You receive P[i-1]*P[i+1] points for doing this removal...
n = input() num = [int(i) for i in raw_input().split()] score = 0 while(n>2): mx = -10000 ix = -1 for i in xrange(1,n-1): if((num[i-1]*num[i+1])>=mx): mx=num[i-1]*num[i+1] ix=i score+=mx del num[ix] n-=1 print score
Problem: Interns Mehta and Sharma are waiting for their "build" to complete. Mehta comes up with an interesting game with an array P of integers of size N. She defines the rules of the game as: Pick a number i from the set {1,2,...N-2}. Remove P[i] from the array. You receive P[i-1]*P[i+1] points for doing this removal...
num=int(input()) lst=map(int,raw_input().split()) ans=0 while len(lst)>2: max=0 for i in range(1,len(lst)-1): if (lst[i-1]*lst[i+1])>max: max=lst[i-1]*lst[i+1] j=i elif (lst[i-1]*lst[i+1])==max: if lst[i]<lst[j]:j=i ans+=max lst.pop(j) # print lst print ans
Problem: Interns Mehta and Sharma are waiting for their "build" to complete. Mehta comes up with an interesting game with an array P of integers of size N. She defines the rules of the game as: Pick a number i from the set {1,2,...N-2}. Remove P[i] from the array. You receive P[i-1]*P[i+1] points for doing this removal...
a,b=input(),map(int,raw_input().split()) res=0 while(a>2): t,t1=-1,-1 for i in range(1,a-1): if((b[i-1]*b[i+1])>=t): t=b[i-1]*b[i+1] t1=i res+=t del b[t1] a-=1 print res
Problem: Interns Mehta and Sharma are waiting for their "build" to complete. Mehta comes up with an interesting game with an array P of integers of size N. She defines the rules of the game as: Pick a number i from the set {1,2,...N-2}. Remove P[i] from the array. You receive P[i-1]*P[i+1] points for doing this removal...
N = int(raw_input()) nums = [int(i) for i in raw_input().split()] ans = 0 for j in range(1, N-1): max_ = 0 for i in range(1, N-j): temp = nums[i-1]*nums[i+1] if temp > max_: max_ = temp x = i #print x del nums[x] ans += max_ print ans
Problem: Interns Mehta and Sharma are waiting for their "build" to complete. Mehta comes up with an interesting game with an array P of integers of size N. She defines the rules of the game as: Pick a number i from the set {1,2,...N-2}. Remove P[i] from the array. You receive P[i-1]*P[i+1] points for doing this removal...
n=int(input()) l=[int(i) for i in raw_input().split()] s=0 while len(l)>2: a=[l[i]*l[i+2] for i in range(len(l)-2)] j=a.index(max(a)) s+=max(a) l.pop(j+1) print s
Problem: Interns Mehta and Sharma are waiting for their "build" to complete. Mehta comes up with an interesting game with an array P of integers of size N. She defines the rules of the game as: Pick a number i from the set {1,2,...N-2}. Remove P[i] from the array. You receive P[i-1]*P[i+1] points for doing this removal...
def search_max(arr): n,mx,x = len(arr),0,1 for i in range(1,n-1): if arr[i-1]*arr[i+1] > mx: x = i mx = arr[i-1]*arr[i+1] return [mx,x] n = int(raw_input()) p = map(int, raw_input().split()) ans = 0 for i in range(n-2): ret = search_max(p) ans += ret[0] p = p[:ret[1]]+p[ret[1]+1:] print ans
Problem: Interns Mehta and Sharma are waiting for their "build" to complete. Mehta comes up with an interesting game with an array P of integers of size N. She defines the rules of the game as: Pick a number i from the set {1,2,...N-2}. Remove P[i] from the array. You receive P[i-1]*P[i+1] points for doing this removal...
n=input() lst=map(int,raw_input().split()) ans=0 while len(lst)>=3: idx=-1 M=-1 for i in xrange(1,len(lst)-1): t=lst[i-1]*lst[i+1] if t>M: M=t idx=i ans+=M del lst[idx] print ans
Problem: You are given a directed graph with N vertices and M edges, not necessarily simple. The i-th edge is oriented from the vertex a_i to the vertex b_i. Divide this graph into strongly connected components and print them in their topological order. Constraints * 1 \leq N \leq 500,000 * 1 \leq M \leq 500,000 * 0 ...
class csr: def __init__(self,N,edges): self.start = [0]*(N+1) self.elist = [0]*len(edges) for e in edges: self.start[e[0]+1] += 1 for i in range(1,N+1): self.start[i] += self.start[i-1] counter = self.start[:] for e in edges: self....
Problem: You are given a directed graph with N vertices and M edges, not necessarily simple. The i-th edge is oriented from the vertex a_i to the vertex b_i. Divide this graph into strongly connected components and print them in their topological order. Constraints * 1 \leq N \leq 500,000 * 1 \leq M \leq 500,000 * 0 ...
mod = 1000000007 eps = 10**-9 def main(): import sys input = sys.stdin.buffer.readline from collections import deque # compressed[v]: 縮約後のグラフで縮約前のvが属する頂点 # num: 縮約後のグラフの頂点数 def SCC(adj, adj_rev): N = len(adj) - 1 seen = [0] * (N + 1) compressed = [0] * (N + 1) ...
Problem: You are given a directed graph with N vertices and M edges, not necessarily simple. The i-th edge is oriented from the vertex a_i to the vertex b_i. Divide this graph into strongly connected components and print them in their topological order. Constraints * 1 \leq N \leq 500,000 * 1 \leq M \leq 500,000 * 0 ...
#-------最強ライブラリSCC(Python) ver25252------ import copy import sys sys.setrecursionlimit(1000000) class csr: # start 頂点iまでの頂点が、矢元として現れた回数 # elist 矢先のリストを矢元の昇順にしたもの def __init__(s, n, edges): s.start = [0] * (n + 1) s.elist = [[] for _ in range(len(edges))] for e in edges: s.start[e.frm + 1] += 1...
Problem: You are given a directed graph with N vertices and M edges, not necessarily simple. The i-th edge is oriented from the vertex a_i to the vertex b_i. Divide this graph into strongly connected components and print them in their topological order. Constraints * 1 \leq N \leq 500,000 * 1 \leq M \leq 500,000 * 0 ...
import java.util.Arrays; import java.util.NoSuchElementException; public class Main { static FastScanner sc = new FastScanner(System.in); static FastPrintStream pw = new FastPrintStream(); public static void main(String[] args) { solve(); pw.flush(); pw.close(); } public s...
Problem: You are given a directed graph with N vertices and M edges, not necessarily simple. The i-th edge is oriented from the vertex a_i to the vertex b_i. Divide this graph into strongly connected components and print them in their topological order. Constraints * 1 \leq N \leq 500,000 * 1 \leq M \leq 500,000 * 0 ...
import sys range = xrange input = raw_input def find_SCC(coupl): SCC, S, P = [], [], [] depth = [0] * len(coupl) stack = list(range(len(coupl))) while stack: node = stack.pop() if node < 0: d = depth[~node] - 1 if P[-1] > d: SCC.append(S[d:]) ...
Problem: You are given a directed graph with N vertices and M edges, not necessarily simple. The i-th edge is oriented from the vertex a_i to the vertex b_i. Divide this graph into strongly connected components and print them in their topological order. Constraints * 1 \leq N \leq 500,000 * 1 \leq M \leq 500,000 * 0 ...
import sys input = sys.stdin.readline sys.setrecursionlimit(1000000) class SCC: def __init__(self, n): self.n = n self.graph = [[] for i in range(n)] self.graph_rev = [[] for i in range(n)] self.used = [False]*n def add_edge(self, fr, to): if fr == to: retu...
Problem: You are given a directed graph with N vertices and M edges, not necessarily simple. The i-th edge is oriented from the vertex a_i to the vertex b_i. Divide this graph into strongly connected components and print them in their topological order. Constraints * 1 \leq N \leq 500,000 * 1 \leq M \leq 500,000 * 0 ...
import sys sys.setrecursionlimit(10**7) def MI(): return map(int,sys.stdin.readline().rstrip().split()) class SCC: def __init__(self,N): self.N = N self.graph = [[] for _ in range(N)] self.graph_rev = [[] for _ in range(N)] self.flag = [False]*N def add_edge(self,start,end): ...
Problem: You are given a directed graph with N vertices and M edges, not necessarily simple. The i-th edge is oriented from the vertex a_i to the vertex b_i. Divide this graph into strongly connected components and print them in their topological order. Constraints * 1 \leq N \leq 500,000 * 1 \leq M \leq 500,000 * 0 ...
import io import os input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline N, M = [int(x) for x in input().split()] edges = [[int(x) for x in input().split()] for i in range(M)] from collections import defaultdict graph = defaultdict(list) for u, v in edges: graph[u].append(v) graph[v] # https://atco...
Problem: You are given a directed graph with N vertices and M edges, not necessarily simple. The i-th edge is oriented from the vertex a_i to the vertex b_i. Divide this graph into strongly connected components and print them in their topological order. Constraints * 1 \leq N \leq 500,000 * 1 \leq M \leq 500,000 * 0 ...
import sys input = lambda: sys.stdin.readline().rstrip() def scc(E): n = len(E) iE = [[] for _ in range(n)] for i, e in enumerate(E): for v in e: iE[v].append(i) T = [] done = [0] * n # 0 -> 1 -> 2 ct = 0 for i0 in range(n): if done[i0]: continue Q = [~i0,...
Problem: You are given a directed graph with N vertices and M edges, not necessarily simple. The i-th edge is oriented from the vertex a_i to the vertex b_i. Divide this graph into strongly connected components and print them in their topological order. Constraints * 1 \leq N \leq 500,000 * 1 \leq M \leq 500,000 * 0 ...
public class Main { public static void main(String[] args) { var sc = new FastScanner(System.in); var pw = new FastPrintStream(); solve(sc, pw); sc.close(); pw.flush(); pw.close(); } public static void solve(FastScanner sc, FastPrintStream pw) { int n...
Problem: You are given a directed graph with N vertices and M edges, not necessarily simple. The i-th edge is oriented from the vertex a_i to the vertex b_i. Divide this graph into strongly connected components and print them in their topological order. Constraints * 1 \leq N \leq 500,000 * 1 \leq M \leq 500,000 * 0 ...
import java.util.*; import java.io.*; import java.math.*; import java.util.function.*; public class Main implements Runnable { static boolean DEBUG; public static void main(String[] args) { DEBUG = args.length > 0 && args[0].equals("-DEBUG"); Thread.setDefaultUncaughtExceptionHandler((t, e) -> { e.printStackTrace...
Problem: You are given a directed graph with N vertices and M edges, not necessarily simple. The i-th edge is oriented from the vertex a_i to the vertex b_i. Divide this graph into strongly connected components and print them in their topological order. Constraints * 1 \leq N \leq 500,000 * 1 \leq M \leq 500,000 * 0 ...
class DiGraph: def __init__(self, n): self.n = n self.graph = [[] for _ in range(n)] self.rev = [[] for _ in range(n)] self.deg = [0 for _ in range(n)] def add_edge(self, fr, to): self.graph[fr].append(to) self.rev[to].append(fr) self.deg[to] += 1 de...
Problem: You are given a directed graph with N vertices and M edges, not necessarily simple. The i-th edge is oriented from the vertex a_i to the vertex b_i. Divide this graph into strongly connected components and print them in their topological order. Constraints * 1 \leq N \leq 500,000 * 1 \leq M \leq 500,000 * 0 ...
def argsort(arr, reverse=False, shift=20): N = len(arr) tmp=[0]*N for i in range(N): tmp[i] = (arr[i] << shift) | i tmp = sorted(tmp,reverse=reverse) out=[0]*N mask = (1<<shift) - 1 for j in range(N): i = tmp[j] & mask out[j] = i return out def dfs(G1,a0,seen,pos...
Problem: You are given a directed graph with N vertices and M edges, not necessarily simple. The i-th edge is oriented from the vertex a_i to the vertex b_i. Divide this graph into strongly connected components and print them in their topological order. Constraints * 1 \leq N \leq 500,000 * 1 \leq M \leq 500,000 * 0 ...
class Digraph: """重み[なし]有向グラフを生成する. """ #入力定義 def __init__(self,vertex=[]): self.vertex=set(vertex) self.edge_number=0 self.vertex_number=len(vertex) self.adjacent_out={v:set() for v in vertex} #出近傍(vが始点) self.adjacent_in={v:set() for v in vertex} #入近傍(vが終点) ...
Problem: You are given a directed graph with N vertices and M edges, not necessarily simple. The i-th edge is oriented from the vertex a_i to the vertex b_i. Divide this graph into strongly connected components and print them in their topological order. Constraints * 1 \leq N \leq 500,000 * 1 \leq M \leq 500,000 * 0 ...
def StrongConnectedComponent(g,S): from collections import deque S = deque() #逆辺で帰りがけが遅い順にpopされるスタック #まず逆辺でDFS、帰りがけに S に積む q = deque() n = len(g) gp = [1]*n # 1 -> 0 -> -1 for i in range(n): if gp[i] == 1: q.append(i) while q: v = q.pop() ...
Problem: You are given a directed graph with N vertices and M edges, not necessarily simple. The i-th edge is oriented from the vertex a_i to the vertex b_i. Divide this graph into strongly connected components and print them in their topological order. Constraints * 1 \leq N \leq 500,000 * 1 \leq M \leq 500,000 * 0 ...
def SCC_Tarjan(g): def dfs(v): nonlocal idx used[v] = 1 pre[v] = low[v] = idx idx += 1 S.append(v) onS[v] = 1 for c in g[v]: if not used[c]: dfs(c) low[v] = min(low[v], low[c]) elif onS[c]: ...
Problem: You are given a directed graph with N vertices and M edges, not necessarily simple. The i-th edge is oriented from the vertex a_i to the vertex b_i. Divide this graph into strongly connected components and print them in their topological order. Constraints * 1 \leq N \leq 500,000 * 1 \leq M \leq 500,000 * 0 ...
import sys read=sys.stdin.buffer.read readline=sys.stdin.buffer.readline readlines=sys.stdin.buffer.readlines def scc(g): n=len(g) cmp=[0]*n ord=[-1]*n fin=[False]*n low=[0]*n visited=[] num=0 k=0 for x in range(n): if ord[x]!=-1: continue st=[~x, x] ...
Problem: You are given a directed graph with N vertices and M edges, not necessarily simple. The i-th edge is oriented from the vertex a_i to the vertex b_i. Divide this graph into strongly connected components and print them in their topological order. Constraints * 1 \leq N \leq 500,000 * 1 \leq M \leq 500,000 * 0 ...
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Arrays; import java.util.Stack; import java.nio.ByteBuffer; import java.util.ArrayList; import java.nio.charset.Charset; import java.util.NoSuchElementException; import java.io.OutputStream; import java.nio.CharBuffer;...
Problem: You are given a directed graph with N vertices and M edges, not necessarily simple. The i-th edge is oriented from the vertex a_i to the vertex b_i. Divide this graph into strongly connected components and print them in their topological order. Constraints * 1 \leq N \leq 500,000 * 1 \leq M \leq 500,000 * 0 ...
from collections import deque class csr: def __init__(self,N,edges): self.start = [0]*(N+1) self.elist = [0]*len(edges) for e in edges: self.start[e[0]+1] += 1 for i in range(1,N+1): self.start[i] += self.start[i-1] counter = self.start[:] for...
Problem: You are given a directed graph with N vertices and M edges, not necessarily simple. The i-th edge is oriented from the vertex a_i to the vertex b_i. Divide this graph into strongly connected components and print them in their topological order. Constraints * 1 \leq N \leq 500,000 * 1 \leq M \leq 500,000 * 0 ...
import sys sys.setrecursionlimit(1000000) n,m=map(int, input().split()) e=[[] for _ in range(n)] e_rev=[[] for _ in range(n)] for i in range(m): ai,bi=map(int, input().split()) e[ai].append(bi) e_rev[bi].append(ai) p_ord=[] used=[0]*n def dfs(v): used[v]=1 for u in e[v]: if not used[u]: ...
Problem: You are given a directed graph with N vertices and M edges, not necessarily simple. The i-th edge is oriented from the vertex a_i to the vertex b_i. Divide this graph into strongly connected components and print them in their topological order. Constraints * 1 \leq N \leq 500,000 * 1 \leq M \leq 500,000 * 0 ...
# verify-helper: PROBLEM https://judge.yosupo.jp/problem/scc import sys input = sys.stdin.buffer.readline sys.setrecursionlimit(6 * 10 ** 5) class StronglyConnectedComponets: def __init__(self, n: int) -> None: self.n = n self.edges = [[] for _ in range(n)] self.rev_edeges = [[] for _ in ...
Problem: You are given a directed graph with N vertices and M edges, not necessarily simple. The i-th edge is oriented from the vertex a_i to the vertex b_i. Divide this graph into strongly connected components and print them in their topological order. Constraints * 1 \leq N \leq 500,000 * 1 \leq M \leq 500,000 * 0 ...
#!/usr/bin/env python3 import sys sys.setrecursionlimit(10**6) INF = 10 ** 9 + 1 # sys.maxsize # float("inf") MOD = 10 ** 9 + 7 # Strongly connected component # derived from https://atcoder.jp/contests/practice2/submissions/16645774 def get_strongly_connected_components(edges, num_vertex): """ edges: [(v1, ...
Problem: You are given a directed graph with N vertices and M edges, not necessarily simple. The i-th edge is oriented from the vertex a_i to the vertex b_i. Divide this graph into strongly connected components and print them in their topological order. Constraints * 1 \leq N \leq 500,000 * 1 \leq M \leq 500,000 * 0 ...
from collections import defaultdict import sys input = sys.stdin.buffer.readline def scc(V, E): # g = defaultdict(list) g = [[] for _ in range(len(V))] # rg = defaultdict(list) rg = [[] for _ in range(len(V))] for u, v in E: g[u].append(v) rg[v].append(u) o = [] done = s...
Problem: You are given a directed graph with N vertices and M edges, not necessarily simple. The i-th edge is oriented from the vertex a_i to the vertex b_i. Divide this graph into strongly connected components and print them in their topological order. Constraints * 1 \leq N \leq 500,000 * 1 \leq M \leq 500,000 * 0 ...
class CSR: def __init__(self, n: int, edges: list): self.start = [0] * (n+1) self.elist = [0] * len(edges) for e in edges: self.start[e[0]+1] += 1 for i in range(1, n+1): self.start[i] += self.start[i-1] counter = self.start[::] # cop...
Problem: You are given a directed graph with N vertices and M edges, not necessarily simple. The i-th edge is oriented from the vertex a_i to the vertex b_i. Divide this graph into strongly connected components and print them in their topological order. Constraints * 1 \leq N \leq 500,000 * 1 \leq M \leq 500,000 * 0 ...
import sys input = sys.stdin.readline def dfs_numbering(st, M, V, ANS, cnt, v): ST = [st] while len(ST) > 0: i = ST[-1] if ANS[i] == -1: V[i] = v end = 1 for x in M[i]: if V[x] == -1: ST.append(x) end = ...
Problem: You are given a directed graph with N vertices and M edges, not necessarily simple. The i-th edge is oriented from the vertex a_i to the vertex b_i. Divide this graph into strongly connected components and print them in their topological order. Constraints * 1 \leq N \leq 500,000 * 1 \leq M \leq 500,000 * 0 ...
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Arrays; import java.nio.ByteBuffer; import java.util.ArrayList; import java.nio.charset.Charset; import java.util.NoSuchElementException; import java.io.OutputStream; import java.nio.CharBuffer; import java.util.Collec...
Problem: You are given a directed graph with N vertices and M edges, not necessarily simple. The i-th edge is oriented from the vertex a_i to the vertex b_i. Divide this graph into strongly connected components and print them in their topological order. Constraints * 1 \leq N \leq 500,000 * 1 \leq M \leq 500,000 * 0 ...
import sys readline = sys.stdin.readline #非再帰 import sys def scc(Edge): N = len(Edge) Edgeinv = [[] for _ in range(N)] for vn in range(N): for vf in Edge[vn]: Edgeinv[vf].append(vn) used = [False]*N dim = [len(Edge[i]) for i in range(N)] order = [] for st in range(N)...
Problem: You are given a directed graph with N vertices and M edges, not necessarily simple. The i-th edge is oriented from the vertex a_i to the vertex b_i. Divide this graph into strongly connected components and print them in their topological order. Constraints * 1 \leq N \leq 500,000 * 1 \leq M \leq 500,000 * 0 ...
from collections import deque class StronglyConnectedComponents(): def __init__(self, n): self.n = n self.graph = [[] for _ in range(n)] self.rev = [[] for _ in range(n)] def add_edge(self, fr, to): self.graph[fr].append(to) self.rev[to].append(fr) def scc_id(self)...
Problem: You are given a directed graph with N vertices and M edges, not necessarily simple. The i-th edge is oriented from the vertex a_i to the vertex b_i. Divide this graph into strongly connected components and print them in their topological order. Constraints * 1 \leq N \leq 500,000 * 1 \leq M \leq 500,000 * 0 ...
import sys input = sys.stdin.readline sys.setrecursionlimit(1000000) class SCC: def __init__(self, n): self.n = n self.graph = [[] for i in range(n)] self.graph_rev = [[] for i in range(n)] self.used = [0]*n def add_edge(self, fr, to): if fr == to: return ...
Problem: You are given a directed graph with N vertices and M edges, not necessarily simple. The i-th edge is oriented from the vertex a_i to the vertex b_i. Divide this graph into strongly connected components and print them in their topological order. Constraints * 1 \leq N \leq 500,000 * 1 \leq M \leq 500,000 * 0 ...
#include <iostream> #include <algorithm> #include <iomanip> #include <vector> #include <queue> #include <set> #include <map> #define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl; #define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl; us...
Problem: You are given a directed graph with N vertices and M edges, not necessarily simple. The i-th edge is oriented from the vertex a_i to the vertex b_i. Divide this graph into strongly connected components and print them in their topological order. Constraints * 1 \leq N \leq 500,000 * 1 \leq M \leq 500,000 * 0 ...
import java.util.Arrays; import java.util.NoSuchElementException; public class Main { static FastScanner sc = new FastScanner(System.in); static FastPrintStream pw = new FastPrintStream(); public static void main(String[] args) { solve(); pw.flush(); pw.close(); } public s...
Problem: You are given a directed graph with N vertices and M edges, not necessarily simple. The i-th edge is oriented from the vertex a_i to the vertex b_i. Divide this graph into strongly connected components and print them in their topological order. Constraints * 1 \leq N \leq 500,000 * 1 \leq M \leq 500,000 * 0 ...
class csr: def __init__(self,N,edges): self.start = [0]*(N+1) self.elist = [0]*len(edges) for e in edges: self.start[e[0]+1] += 1 for i in range(1,N+1): self.start[i] += self.start[i-1] counter = self.start[:] for e in edges: self....
Problem: You are given a directed graph with N vertices and M edges, not necessarily simple. The i-th edge is oriented from the vertex a_i to the vertex b_i. Divide this graph into strongly connected components and print them in their topological order. Constraints * 1 \leq N \leq 500,000 * 1 \leq M \leq 500,000 * 0 ...
# AC Library Python版 # Author Koki_tkg ''' internal_type_traits以外は翻訳しました。 practiceは一応全部ACしていますが, practiceで使っていない関数などの動作は未確認なので保証はしません。 また,C++版をほぼそのまま書き換えているので速度は出ません。 (2020/09/13 by Koki_tkg) ''' # --------------------<< Library Start >>-------------------- # # convolution.py class convolution: def __init__(self,...
Problem: You are given a directed graph with N vertices and M edges, not necessarily simple. The i-th edge is oriented from the vertex a_i to the vertex b_i. Divide this graph into strongly connected components and print them in their topological order. Constraints * 1 \leq N \leq 500,000 * 1 \leq M \leq 500,000 * 0 ...
class StronglyConnectedComponents(): def __init__(self, n): self.n = n self.graph = [[] for _ in range(n)] self.rev = [[] for _ in range(n)] def add_edge(self, fr, to): self.graph[fr].append(to) self.rev[to].append(fr) def scc_id(self): group = [-1] * self.n...
Problem: You are given a directed graph with N vertices and M edges, not necessarily simple. The i-th edge is oriented from the vertex a_i to the vertex b_i. Divide this graph into strongly connected components and print them in their topological order. Constraints * 1 \leq N \leq 500,000 * 1 \leq M \leq 500,000 * 0 ...
import sys input = sys.stdin.readline sys.setrecursionlimit(1000000) class SCC: def __init__(self, n): self.n = n self.graph = [[] for _ in range(n)] self.graph_rev = [[] for _ in range(n)] self.already = [False]*n def add_edge(self, fr, to): if fr == to: ...
Problem: You are given a directed graph with N vertices and M edges, not necessarily simple. The i-th edge is oriented from the vertex a_i to the vertex b_i. Divide this graph into strongly connected components and print them in their topological order. Constraints * 1 \leq N \leq 500,000 * 1 \leq M \leq 500,000 * 0 ...
from collections import deque class csr: def __init__(self,N,edges): self.start = [0]*(N+1) self.elist = [0]*len(edges) for e in edges: self.start[e[0]+1] += 1 for i in range(1,N+1): self.start[i] += self.start[i-1] counter = self.start[:] for...
Problem: You are given a directed graph with N vertices and M edges, not necessarily simple. The i-th edge is oriented from the vertex a_i to the vertex b_i. Divide this graph into strongly connected components and print them in their topological order. Constraints * 1 \leq N \leq 500,000 * 1 \leq M \leq 500,000 * 0 ...
import java.util.Arrays; import java.util.NoSuchElementException; public class Main { static FastScanner sc = new FastScanner(System.in); static FastPrintStream pw = new FastPrintStream(); public static void main(String[] args) { solve(); pw.flush(); pw.close(); } public s...
Problem: You are given a directed graph with N vertices and M edges, not necessarily simple. The i-th edge is oriented from the vertex a_i to the vertex b_i. Divide this graph into strongly connected components and print them in their topological order. Constraints * 1 \leq N \leq 500,000 * 1 \leq M \leq 500,000 * 0 ...
from collections import defaultdict import sys input = sys.stdin.buffer.readline def scc(V, E): g = defaultdict(list) # g = [[] for _ in range(len(V))] rg = defaultdict(list) # rg = [[] for _ in range(len(V))] for u, v in E: g[u].append(v) rg[v].append(u) o = [] done = s...
Problem: You are given a directed graph with N vertices and M edges, not necessarily simple. The i-th edge is oriented from the vertex a_i to the vertex b_i. Divide this graph into strongly connected components and print them in their topological order. Constraints * 1 \leq N \leq 500,000 * 1 \leq M \leq 500,000 * 0 ...
import sys sys.setrecursionlimit(10**9) def SCC(G): D=[] C=[len(G)-1] U=[1]*len(G) def DFS(x): if U[x]: U[x]=0 for i in range(len(G[x])): DFS(G[x][i]) D.append(x) for i in range(len(G)): if U[i]: DFS(i) GR=[[] for i in range(len(G))] for i in range(N): for j ...
Problem: You are given a directed graph with N vertices and M edges, not necessarily simple. The i-th edge is oriented from the vertex a_i to the vertex b_i. Divide this graph into strongly connected components and print them in their topological order. Constraints * 1 \leq N \leq 500,000 * 1 \leq M \leq 500,000 * 0 ...
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.NoSuchElementException; public class Main { static FastScanner sc = new FastScanner(System.in); static StringBuilder sb = new StringBuilder(); static PrintWriter pw = new PrintWrite...
Problem: You are given a directed graph with N vertices and M edges, not necessarily simple. The i-th edge is oriented from the vertex a_i to the vertex b_i. Divide this graph into strongly connected components and print them in their topological order. Constraints * 1 \leq N \leq 500,000 * 1 \leq M \leq 500,000 * 0 ...
import sys input = sys.stdin.readline def I(): return int(input()) def MI(): return map(int, input().split()) def LI(): return list(map(int, input().split())) def main(): def scc(Edge): N = len(Edge) Edgeinv = [[] for _ in range(N)] for vn in range(N): for vf in Edge[vn]: ...
Problem: You are given a directed graph with N vertices and M edges, not necessarily simple. The i-th edge is oriented from the vertex a_i to the vertex b_i. Divide this graph into strongly connected components and print them in their topological order. Constraints * 1 \leq N \leq 500,000 * 1 \leq M \leq 500,000 * 0 ...
#include<bits/stdc++.h> using namespace std; using ll = long long; class Scc { private: int n; vector<vector<int>> edge, rev; vector<int> post_order; vector<bool> seen0, seen1; vector<int> typea; vector<vector<int>> new_edge; int ord; int type_num; void dfs0(int v) { if(seen0[v])return; seen0[v] = tru...
Problem: You are given a directed graph with N vertices and M edges, not necessarily simple. The i-th edge is oriented from the vertex a_i to the vertex b_i. Divide this graph into strongly connected components and print them in their topological order. Constraints * 1 \leq N \leq 500,000 * 1 \leq M \leq 500,000 * 0 ...
import types _atcoder_code = """ # Python port of AtCoder Library. __version__ = '0.0.1' """ atcoder = types.ModuleType('atcoder') exec(_atcoder_code, atcoder.__dict__) _atcoder__scc_code = """ import copy import sys import typing class CSR: def __init__( self, n: int, edges: typing.List[typing.Tu...
Problem: You are given a directed graph with N vertices and M edges, not necessarily simple. The i-th edge is oriented from the vertex a_i to the vertex b_i. Divide this graph into strongly connected components and print them in their topological order. Constraints * 1 \leq N \leq 500,000 * 1 \leq M \leq 500,000 * 0 ...
#-------最強ライブラリSCC(Python)ver25252------ #卍分解卍 #出力例は辺の順が逆!ずるい! import copy import sys sys.setrecursionlimit(1000000) class csr: # start 頂点iまでの頂点が、矢元として現れた回数 # elist 矢先のリストを矢元の昇順にしたもの def __init__(s, n, edges): s.start = [0] * (n + 1) s.elist = [[] for _ in range(len(edges))] for e in edges: s....
Problem: You are given a directed graph with N vertices and M edges, not necessarily simple. The i-th edge is oriented from the vertex a_i to the vertex b_i. Divide this graph into strongly connected components and print them in their topological order. Constraints * 1 \leq N \leq 500,000 * 1 \leq M \leq 500,000 * 0 ...
#-------最強ライブラリSCC(Python) ver25252------ class scc_graph: # n 頂点数 def __init__(s, n): s._n = n s.edge = [s.component() for i in range(n)] def num_vertices(s): return s._n # 辺を追加 frm 矢元 to 矢先 # O(1) def add_edge(s, frm, to): s.edge[frm].add_go(to) s.edge[to].add_rgo(frm) # グループの個数と各頂点のグ...