zoukankan      html  css  js  c++  java
  • bzoj4195: [Noi2015]程序自动分析

    离散化+并查集。

    为了离散化而离线后,我发现可以先把=的连在一起,然后再判不等于。这样做很清晰简洁。

    1A真开心。

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    
    int n;
    struct node
    {
        int x,y,opt;
    }a[1100000];
    bool cmp(node n1,node n2)
    {
        return n1.opt>n2.opt;
    }
    int ls[2100000],lslen;
    void LSH()
    {
        lslen=0;
        for(int i=1;i<=n;i++)
        {
            ls[++lslen]=a[i].x;
            ls[++lslen]=a[i].y;
        }
        sort(ls+1,ls+lslen+1);
        lslen=unique(ls+1,ls+lslen+1)-ls-1;
        for(int i=1;i<=n;i++)
        {
            a[i].x=lower_bound(ls+1,ls+lslen+1,a[i].x)-ls;
            a[i].y=lower_bound(ls+1,ls+lslen+1,a[i].y)-ls;
        }
    }
    int fa[2100000];
    int findfa(int x)
    {
        if(fa[x]==x)return x;
        fa[x]=findfa(fa[x]);return fa[x];
    }
    int main()
    {
        int T;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d",&n);
            for(int i=1;i<=n;i++)
                scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].opt);
            LSH();
            sort(a+1,a+n+1,cmp);
            
            for(int i=1;i<=lslen;i++)fa[i]=i;
            
            bool bk=false;
            for(int i=1;i<=n;i++)
            {
                if(a[i].opt==1)
                {
                    int fx=findfa(a[i].x),fy=findfa(a[i].y);
                    if(fx!=fy)
                        fa[fx]=fy;
                }
                else
                {
                    int fx=findfa(a[i].x),fy=findfa(a[i].y);
                    if(fx==fy)
                        {bk=true;break;}
                }
            }
            if(bk==false)printf("YES
    ");
            else printf("NO
    ");
        }
        return 0;
    }
  • 相关阅读:
    其他魔术方法
    类型约束和类的魔术常量
    PHP对象遍历、内置标准类与数据转对象
    类的自动加载与对象的克隆
    PHP重载与接口
    面向对象
    PHP基础
    地下城与勇士的项目整理
    mysql建表
    jQuery
  • 原文地址:https://www.cnblogs.com/AKCqhzdy/p/8525229.html
Copyright © 2011-2022 走看看