zoukankan      html  css  js  c++  java
  • ZROI#594

    ZROI#594

    ZROI#594
    几乎就是并查集裸题了对叭...可惜不是裸题.
    这个题有一个非常妙的思路:
    是由(Alpha)大佬提供的.
    不使用路径压缩或按秩合并等任何优化.,合并的时候指定编号大的点作为儿子,删除的时候直接把编号大的父亲置为自己就好了.
    找代表元的时候就暴力找,我不知道这样的复杂度为啥是对的...但它就是过了...
    (Code:)

    #include <algorithm>
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    #include <cstdio>
    #include <string>
    #include <vector>
    #include <queue>
    #include <cmath>
    #include <ctime>
    #include <map>
    #include <set>
    #define MEM(x,y) memset ( x , y , sizeof ( x ) )
    #define rep(i,a,b) for (int i = a ; i <= b ; ++ i)
    #define per(i,a,b) for (int i = a ; i >= b ; -- i)
    #define pii pair < int , int >
    #define X first
    #define Y second
    #define rint read<int>
    #define int long long
    #define pb push_back
    
    using std::set ;
    using std::pair ;
    using std::max ;
    using std::min ;
    using std::priority_queue ;
    using std::vector ;
    using std::swap ;
    using std::sort ;
    using std::unique ;
    using std::greater ;
    
    template < class T >
        inline T read () {
            T x = 0 , f = 1 ; char ch = getchar () ;
            while ( ch < '0' || ch > '9' ) {
                if ( ch == '-' ) f = - 1 ;
                ch = getchar () ;
            }
            while ( ch >= '0' && ch <= '9' ) {
                x = ( x << 3 ) + ( x << 1 ) + ( ch - 48 ) ;
                ch = getchar () ;
           }
       return f * x ;
    }
    
    const int N = 1e6 + 100 ;
    
    struct query { int opt , u , v ; } v[N] ;
    
    int n , q , f[N] ;
    
    inline int getf (int x) { return f[x] == x ? x : getf ( f[x] ) ; }
    
    signed main (int argc , char * argv[] ) {
        n = rint () ; q = rint () ;
        rep ( i , 1 , N - 1 ) f[i] = i ;
        rep ( i , 1 , q ) {
            int opt = rint () , x = rint () , y = rint () ;
            if ( opt == 1 ) {
                if ( x > y ) f[getf(x)] = y ;
                else f[getf(y)] = x ;
            }
            if ( opt == 2 ) f[max(x,y)] = max ( x , y ) ;
            if ( opt == 3 ) puts ( getf ( x ) == getf ( y ) ? "YES" : "NO" ) ;
        }
        return 0 ;
    }
    
    May you return with a young heart after years of fighting.
  • 相关阅读:
    [收藏转贴]struct探索·extern "C"含义探索 ·C++与C的混合编程·C 语言高效编程的几招
    [收藏转贴]构建RESTful风格的WCF服务
    [转贴] C/C++中动态链接库的创建和调用
    [转贴]WebService的简单实现 C++
    谷歌已经对Android的开源严防死守
    如何成为一名黑客?
    当程序员思路被打断
    编辑语言是这样的
    开发者需要掌握多少门语言?
    程序员的六大优势
  • 原文地址:https://www.cnblogs.com/Equinox-Flower/p/11459495.html
Copyright © 2011-2022 走看看