记录编号 367394 评测结果 AAAAAAAAAA
题目名称 [NOI 2015]程序自动分析 最终得分 100
用户昵称 Gravatarljt 是否通过 通过
代码语言 C++ 运行时间 0.993 s
提交时间 2017-01-30 14:26:42 内存使用 2.99 MiB
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;

const int maxn = 100005;
long dc[maxn*2], top = 0;
int n;
long di[maxn], dj[maxn], de[maxn];
inline long read()
{
    long a = 0; int c;
    do c = getchar(); while (!isdigit(c));
    while (isdigit(c)) {
        a = a*10 + c - '0';
        c = getchar();
    }
    return a;
}
int dc_val(long i)
{ return lower_bound(dc+1, dc+top+1, i)-dc; }
int fa[maxn*2];
int findf(int i)
{ return fa[i]?fa[i]=findf(fa[i]):i; }

int main()
{
    freopen("prog.in", "r", stdin);
    freopen("prog.out", "w", stdout);
    int T;
    cin >> T;
    //cout << T << endl;
    while (T--) {
        cin >> n;
        memset(fa, 0, sizeof fa);
        top = 0;
        for (register int i = 1; i <= n; i++) {
            di[i] = read(); dj[i] = read(); de[i] = read();
            dc[++top] = di[i], dc[++top] = dj[i];
        }
        sort(dc+1, dc+top+1);
        top = unique(dc+1, dc+top+1) - dc - 1;
        for (register int i = 1; i <= n; i++) {
            if (!de[i]) continue;
            int ni = dc_val(di[i]), nj = dc_val(dj[i]);
            if (findf(ni) != findf(nj))
                fa[findf(ni)] = findf(nj);
        }
        bool right = 1;
        for (register int i = 1; i <= n; i++) {
            if (de[i]) continue;
            int ni = dc_val(di[i]), nj = dc_val(dj[i]);
            if (findf(ni) == findf(nj)) {
                right = 0;
                break;
            }
        }
        if (right) puts("YES");
        else puts("NO");
    }
    return 0;
}