记录编号 489271 评测结果 AAAAAAAAAA
题目名称 [NOI 2015]程序自动分析 最终得分 100
用户昵称 Gravatar@@2@ 是否通过 通过
代码语言 C++ 运行时间 2.968 s
提交时间 2018-02-28 17:35:34 内存使用 5.66 MiB
显示代码纯文本
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <map>
#include <queue>
using namespace std;
map<int,int> M;
int T,n;
int b[200066];
int tt1[200020],tt2[200022],ee[2999990];
int find(int x)
{
	if(b[x] == x)
		return x;
	return b[x] = find(b[x]);
}
int cyf()
{
	freopen("prog.in","r",stdin);
	freopen("prog.out","w",stdout);
	//cin >> T;
	scanf("%d",&T);
	while(T--)
	{
		M.clear();
		//cin >> n;
		scanf("%d",&n);
		bool f = 1;
		for(int i = 1;i <= n*2+3;i++)
		{
			b[i] = i;
		}
		int p = 0;

			
		
		for(int i = 1;i <= n;i++)
		{
			int t1,t2,e;
			//cin >> t1 >> t2 >> e;
			scanf("%d%d%d",tt1+i,tt2+i,ee+i);
			//cin >> tt1[i] >> tt2[i] >> ee[i];
			t1 = tt1[i];t2 = tt2[i];e = ee[i];
	
			if(M[t1] == 0)
			{
				M[t1] = ++p;
				t1 = M[t1];
			}
			else
			{
				t1 = M[t1];
			}
			if(M[t2] == 0)
			{
				M[t2] = ++p;
				t2 = M[t2];
			}
			else
			{
				t2 = M[t2];
			}
			if(e == 1)
			{
				int j = find(t1);
				int k = find(t2);
				if(j!=k)
				b[j] = k;
			}
		}
		for(int i = 1;i <= n;i++)
		{
			if(ee[i] == 0)
			{
				int j = find(M[tt1[i]]);
				int k = find(M[tt2[i]]);
				if(j==k)
				{
					f = 0;
					break;
				}
			}
		}
			
		
		if(f) printf("YES\n");
		else	printf("NO\n");
	}
	
	return 0;
}
int CYF = cyf();
int main(){;}