矛盾的關係

在實現程序自動分析的過程中,常常需要判定一些約束條件是否能被同時滿足。
考慮一個約束滿足問題的簡化版本:假設x1,x2,x3,…代表程序中出現的變量,給定n個形如xi=xj或xi≠xj的變量相等/不等的約束條件,請判定是否可以分別爲每一個變量賦予恰當的值,使得上述所有約束條件同時被滿足。例如,一個問題中的約束條件爲:x1=x2,x2=x3,x3=x4,x1≠x4,這些約束條件顯然是不可能同時被滿足的,因此這個問題應判定爲不可被滿足。
現在給出一些約束滿足問題,請分別對它們進行判定。
Input

輸入文件的第1行包含1個正整數t,表示需要判定的問題個數。注意這些問題之間是相互獨立的。
對於每個問題,包含若干行:
第1行包含1個正整數n,表示該問題中需要被滿足的約束條件個數。
接下來n行,每行包括3個整數i,j,e,描述1個相等/不等的約束條件,相鄰整數之間用單個空格隔開。若e=1,則該約束條件爲xi=xj;若e=0,則該約束條件爲xi≠xj。
Output

輸出文件包括t行。
輸出文件的第k行輸出一個字符串“YES”或者“NO”(不包含引號,字母全部大寫),“YES”表示輸入中的第k個問題判定爲可以被滿足,“NO”表示不可被滿足。
Sample Input
2
2
1 2 1
1 2 0
2
1 2 1
2 1 1
Sample Output
NO
YES
Hint

在第一個問題中,約束條件爲:x1=x2,x1≠x2。這兩個約束條件互相矛盾,因此不可被同時滿足。

在第二個問題中,約束條件爲:x1=x2,x2=x1。這兩個約束條件是等價的,可以被同時滿足。

1≤n≤1000000

1≤i,j≤1000000000


題目大意:給你n個條件(xi和xj是否相等),讓你判斷有沒有出現矛盾的條件。


解題思路:利用並查集的思想,把相等的都先放在一堆,再考慮不相等條件中的xi和xj有沒有在同一堆中。

另外,數據範圍1e9 ,需要進行離散化。


AC代碼:

/*
 * @Author: hesorchen
 * @LastEditTime: 2020-04-21 17:20:56
 * @1  : ┌───┐   ┌───┬───┬───┬───┐ ┌───┬───┬───┬───┐ ┌───┬───┬───┬───┐ ┌───┬───┬───┐
 * @2  : │Esc│   │ F1│ F2│ F3│ F4│ │ F5│ F6│ F7│ F8│ │ F9│F10│F11│F12│ │P/S│S/L│P/B│  ┌┐    ┌┐    ┌┐
 * @3  : └───┘   └───┴───┴───┴───┘ └───┴───┴───┴───┘ └───┴───┴───┴───┘ └───┴───┴───┘  └┘    └┘    └┘
 * @4  : ┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───────┐ ┌───┬───┬───┐ ┌───┬───┬───┬───┐
 * @5  : │~ `│! 1│@ 2│# 3│$ 4│% 5│^ 6│& 7│* 8│( 9│) 0│_ -│+ =│ BacSp │ │Ins│Hom│PUp│ │N L│ / │ * │ - │
 * @6  : ├───┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─────┤ ├───┼───┼───┤ ├───┼───┼───┼───┤
 * @7  : │ Tab │ Q │ W │ E │ R │ T │ Y │ U │ I │ O │ P │{ [│} ]│ | \ │ │Del│End│PDn│ │ 7 │ 8 │ 9 │   │
 * @8  : ├─────┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴─────┤ └───┴───┴───┘ ├───┼───┼───┤ + │
 * @9  : │ Caps │ A │ S │ D │ F │ G │ H │ J │ K │ L │: ;│" '│ Enter  │               │ 4 │ 5 │ 6 │   │
 * @10 : ├──────┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴────────┤     ┌───┐     ├───┼───┼───┼───┤
 * @11 : │ Shift  │ Z │ X │ C │ V │ B │ N │ M │< ,│> .│? /│  Shift   │     │ ↑ │     │ 1 │ 2 │ 3 │   │
 * @12 : ├─────┬──┴─┬─┴──┬┴───┴───┴───┴───┴───┴──┬┴───┼───┴┬────┬────┤ ┌───┼───┼───┐ ├───┴───┼───┤ E││
 * @13 : │ Ctrl│    │Alt │         Space         │ Alt│    │    │Ctrl│ │ ← │ ↓ │ → │ │   0   │ . │←─┘│
 * @14 : └─────┴────┴────┴───────────────────────┴────┴────┴────┴────┘ └───┴───┴───┘ └───────┴───┴───┘
 */

#include <map>
#include <set>
#include <list>
#include <queue>
#include <deque>
#include <cmath>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define endl '\n'
#define PI cos(-1)
#define ll long long
#define INF 0x3f3f3f3f
#define mod 1000000009
#define lowbit(abcd) (abcd & (-abcd))

ll a[1000100];
ll b[1000100];
ll c[1000100];
ll discretization[2000100], ct = 1;
ll father[2000100];
ll find(int x)
{
    if (father[x] == x)
        return x;
    return father[x] = find(father[x]);
}
void mix(int x, int y)
{
    ll fx = find(x);
    ll fy = find(y);
    if (fx != fy)
        father[fx] = fy;
}
void intt()
{
    for (int i = 1; i <= 2000000; i++)
        father[i] = i;
}

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        ll n;
        cin >> n;
        intt();
        ct = 1;
        for (int i = 1; i <= n; i++)
        {
            scanf("%lld%lld%lld", &a[i], &b[i], &c[i]);
            discretization[ct++] = a[i];
            discretization[ct++] = b[i];
        }
        sort(discretization + 1, discretization + ct); //離散化
        ll end = unique(discretization + 1, discretization + ct) - discretization;
        for (int i = 1; i <= n; i++)
        {
            a[i] = lower_bound(discretization + 1, discretization + end, a[i]) - discretization;
            b[i] = lower_bound(discretization + 1, discretization + end, b[i]) - discretization;
        }
        int ff = 1;
        for (int i = 1; i <= n; i++)
        {
            if (c[i])
            {
                if (find(a[i]) != find(b[i]))
                    mix(a[i], b[i]);
            }
        }
        for (int i = 1; i <= n; i++)
        {
            if (!c[i])
            {
                if (find(a[i]) == find(b[i]))
                    ff = 0;
            }
        }
        if (ff)
            cout << "YES\n";
        else
            cout << "NO\n";
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章