Created
October 29, 2018 02:10
-
-
Save thuvh/4733486597e51a6d2b06989b93729eb9 to your computer and use it in GitHub Desktop.
python-vnoi-quandum
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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