Skip to content

Instantly share code, notes, and snippets.

@rishabhgargg
Last active August 9, 2024 07:27
Show Gist options
  • Save rishabhgargg/51577aec56431bd8d9bec6ae2e94ca47 to your computer and use it in GitHub Desktop.
Save rishabhgargg/51577aec56431bd8d9bec6ae2e94ca47 to your computer and use it in GitHub Desktop.
def count_efficient_segments(n, m, pairs):
not_collaborated = set()
for x, y in pairs:
if x > y:
x, y = y, x
not_collaborated.add((x, y))
count = 0
start = 1
for end in range(1, n + 1):
while start <= end:
segment_is_valid = True
for i in range(start, end):
if (i, end) in not_collaborated:
segment_is_valid = False
break
if segment_is_valid:
count += 1
break
start += 1
return count
# Read inputs from standard input
if __name__ == "__main__":
n, m = map(int, input().split())
pairs = []
for _ in range(m):
x, y = map(int, input().split())
pairs.append((x, y))
# Call the function and print the output
result = count_efficient_segments(n, m, pairs)
print(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment