This issue is the same configuration issue as G: DAG Trio (Hard), with only the…
- User
This issue is the same configuration issue as G: DAG Trio (Hard), with only the constraints being different. input
Assistant
pythonimport sys from collections import deque def main(): N, M = map(int, sys.stdin.readline().split()) edges = [tuple(map(int, sys.stdin.readline().split())) for _ in range(M)] for i in range(M): # Create modified edges by reversing the i-th edge modified_edges = [] for j in range(M): if j == i: a, b = edges[j] modified_edges.append((b, a)) else: modified_edges.append(edges[j]) # Build adjacency list and in-degree adj = [[] for _ in range(N + 1)] in_degree = [0] * (N + 1) for a, b in modified_edges: adj[a].append(b) in_degree[b] += 1 # Kahn's algorithm for topological sort q = deque() count = 0 for node in range(1, N + 1): if in_degree[node] == 0: q.append(node) while q: u = q.popleft() count += 1 for v in adj[u]: in_degree[v] -= 1 if in_degree[v] == 0: q.append(v) if count == N: print("YES") return print("NO") if __name__ == "__main__": main()
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.