[AcWing 237] 程序自动分析

image
image


点击查看代码
#include<bits/stdc++.h>

using namespace std;

typedef long long LL;

const int N = 2e5 + 10;

int n, m;
int p[N];
unordered_map<int,int> s;

struct Query {
    int x, y, e;
} query[N];

int get(int x)
{
    if (!s.count(x))
        s[x] = ++ n;
    return s[x];
}

int find(int x)
{
    if (p[x] != x)
        p[x] = find(p[x]);
    return p[x];
}

void solve()
{
    n = 0;
    s.clear();
    cin >> m;
    for (int i = 0; i < m; i ++) {
        int x, y, e;
        cin >> x >> y >> e;
        query[i] = {get(x), get(y), e};
    }
    for (int i = 1; i <= n; i ++)
        p[i] = i;
    for (int i = 0; i < m; i ++)
        if (query[i].e == 1) {
            int a = query[i].x, b = query[i].y;
            int pa = find(a), pb = find(b);
            p[pa] = pb;
        }
    bool st = true;
    for (int i = 0; i < m; i ++)
        if (query[i].e == 0) {
            int a = query[i].x, b = query[i].y;
            int pa = find(a), pb = find(b);
            if (pa == pb) {
                st = false;
                break;
            }
        }
    if (st)
        cout << "YES" << endl;
    else
        cout << "NO" << endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int T;
    cin >> T;
    while (T --) {
        solve();
    }

    return 0;
}

  1. 使用哈希表做离散化
  2. 等号不会矛盾,不等号可能会矛盾,出现矛盾的条件:
    xi=xjxixj 同时出现
  3. 使用并查集维护相等的条件,当出现不等条件时,如果 xixj 在同一个集合,则矛盾
posted @   wKingYu  阅读(27)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
欢迎阅读『[AcWing 237] 程序自动分析』
点击右上角即可分享
微信分享提示