Skip to content

Instantly share code, notes, and snippets.

@thuvh
Created October 29, 2018 02:10
Show Gist options
  • Save thuvh/4733486597e51a6d2b06989b93729eb9 to your computer and use it in GitHub Desktop.
Save thuvh/4733486597e51a6d2b06989b93729eb9 to your computer and use it in GitHub Desktop.
python-vnoi-quandum
from sys import stdin, stdout
from math import floor
def main():
global n,m,a
n,m=[int(x) for x in stdin.readline().split()]
a=[]
for i in range(m):
a.append([int(x) for x in stdin.readline().split()])
g=[]
G=[]
for _ in range(n+3):
g.append({})
G.append({})
for j in range(m):
b=a[j]
for i in range(1,n):
u=b[i-1]
v=b[i]
g[u][v] = g[u].get(v, 0) + 1
for u in range(1,n+1):
for v,k in g[u].items():
if (k==m):
G[u][v]=True
res=0
i=0
j=-1
b=a[0]+[0]
res=0
while ((i<n) and (j<n)):
i=j+1
j=j+1
if ((i>=n) or (j>=n)):
break
while ((j+1)<n):
if (G[b[j]].get(b[j+1])==True):
j=j+1
else:
break
l=j-i+1
res=res+l+floor((l*(l-1))/2)
stdout.write(str(res))
return 0
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment