AcWing 237. 程序自动分析 python
原题链接
中等
作者:
第44次日落
,
2024-03-04 14:27:34
,
所有人可见
,
阅读 10
t=int(input())
def find(x):
if ans[x]!=x:
ans[x]=find(ans[x])
return ans[x]
while t:
n=int(input())
ans=[0]*(2*n+1)
for i in range(1,2*n+1):
ans[i]=i
idx=1
dic={}
record=[]
while n:
i,j,e=map(int,input().split())
if dic.get(i)==None:
dic[i]=idx
idx=idx+1
if dic.get(j)==None:
dic[j]=idx
idx=idx+1
if e==1:
t1=find(dic[i])
t2=find(dic[j])
ans[t2]=t1
if e==0:
record.append([dic[i],dic[j]])
n=n-1
result="YES"
for re in record:
t1=find(re[0])
t2=find(re[1])
if t1==t2:
result="NO"
break
print(result)
t=t-1