1. 程式人生 > >loj#2129. 「NOI2015」程序自動分析

loj#2129. 「NOI2015」程序自動分析

line getc ring pre get 離散 str int 鏈接

題目鏈接

loj#2129. 「NOI2015」程序自動分析

題解

額...
考你會不會離散化?

代碼

#include<queue> 
#include<cstdio> 
#include<cstring> 
#include<algorithm> 

inline int read() {
    int x = 0,f = 1; 
    char c = getchar(); 
    while(c < '0' || c > '9')c = getchar(); 
    while(c <= '9' && c >= '0') x = x * 10 + c - '0',c = getchar(); 
    return x * f; 
} 
const int maxn = 2000007; 
int n; 
int x[maxn],y[maxn],z[maxn]; 
int tmp[maxn]; 
int fa[maxn]; 
int find(int x ) { 
    if(fa[x] != x) fa[x] = find(fa[x]); 
    return fa[x]; 
} 
void solve() { 
    n = read();
    int tot = 0;  
    for(int i = 1;i <= n;++ i) { 
        x[i] = read(),y[i] = read();z[i] = read(); 
        tmp[++ tot] = x[i],tmp[++ tot] = y[i]; 
    } 
    std::sort(tmp + 1,tmp + tot + 1); 
    tot = std::unique(tmp + 1,tmp + tot + 1) - tmp - 1; 
    for(int i = 1;i <= tot;++ i) fa[i] = i; 
    for(int i = 1;i <= n;++ i) {  
        x[i] = std::lower_bound(tmp + 1,tmp + tot + 1,x[i]) - tmp, 
        y[i] = std::lower_bound(tmp + 1,tmp + tot + 1,y[i]) - tmp; 
        if(z[i]) fa[find(x[i])] = find(y[i]); 
    }  
    //for(int i = 1;i <= n;++ i) if(z[i]) fa[find(x[i])] = find(y[i]); 
    for(int i = 1;i <= n;++ i) 
        if(!z[i] && find(x[i]) == find(y[i]))  { 
            puts("NO"); 
            return; 
        } 
    puts("YES"); 
} 
int main() { 
    int t = read(); 
    for(int i = 1;i <= t;++ i)  
        solve();  
} 

loj#2129. 「NOI2015」程序自動分析