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.