init
This commit is contained in:
commit
851751ea87
82 changed files with 2093 additions and 0 deletions
74
1N.py
Normal file
74
1N.py
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
import sys
|
||||
|
||||
data = sys.stdin.read().split()
|
||||
if not data:
|
||||
exit()
|
||||
n = int(data[0])
|
||||
m = int(data[1])
|
||||
adj_all = [[] for _ in range(n + 1)]
|
||||
adj2 = [[] for _ in range(n + 1)]
|
||||
rev_adj2 = [[] for _ in range(n + 1)]
|
||||
edges2 = []
|
||||
idx = 2
|
||||
for _ in range(m):
|
||||
u = int(data[idx])
|
||||
v = int(data[idx+1])
|
||||
t = int(data[idx+2])
|
||||
idx += 3
|
||||
adj_all[u].append(v)
|
||||
if t == 2:
|
||||
adj2[u].append(v)
|
||||
rev_adj2[v].append(u)
|
||||
edges2.append((u, v))
|
||||
reachable = [False] * (n + 1)
|
||||
reachable[1] = True
|
||||
q = [1]
|
||||
head = 0
|
||||
while head < len(q):
|
||||
u = q[head]
|
||||
head += 1
|
||||
for v in adj_all[u]:
|
||||
if not reachable[v]:
|
||||
reachable[v] = True
|
||||
q.append(v)
|
||||
visited = [False] * (n + 1)
|
||||
order = []
|
||||
edge_idx = [0] * (n + 1)
|
||||
stack = []
|
||||
for i in range(1, n + 1):
|
||||
if not visited[i]:
|
||||
visited[i] = True
|
||||
stack.append(i)
|
||||
while stack:
|
||||
u = stack[-1]
|
||||
if edge_idx[u] < len(adj2[u]):
|
||||
v = adj2[u][edge_idx[u]]
|
||||
edge_idx[u] += 1
|
||||
if not visited[v]:
|
||||
visited[v] = True
|
||||
stack.append(v)
|
||||
else:
|
||||
order.append(u)
|
||||
stack.pop()
|
||||
scc_id = [0] * (n + 1)
|
||||
current_scc = 0
|
||||
for i in range(n - 1, -1, -1):
|
||||
start_node = order[i]
|
||||
if scc_id[start_node] == 0:
|
||||
current_scc += 1
|
||||
q_scc = [start_node]
|
||||
scc_id[start_node] = current_scc
|
||||
head_scc = 0
|
||||
while head_scc < len(q_scc):
|
||||
u = q_scc[head_scc]
|
||||
head_scc += 1
|
||||
for v in rev_adj2[u]:
|
||||
if scc_id[v] == 0:
|
||||
scc_id[v] = current_scc
|
||||
q_scc.append(v)
|
||||
ans = "No"
|
||||
for u, v in edges2:
|
||||
if reachable[u] and scc_id[u] != scc_id[v]:
|
||||
ans = "Yes"
|
||||
break
|
||||
print(ans)
|
||||
Loading…
Add table
Add a link
Reference in a new issue