• 题解——Mushroom的区间(并查集)


    题解——Mushroom的区间(并查集)

    考试时居然打错了并查集,幸好查出来了


    题面

    Description

    私有题面,已经隐藏

    Input

    Output

    不同的区间数

    in.1

    3 3
    1 1
    2 2
    3 3

    out.1

    8

    数据范围与约定

    对于30%的数据,n,m<=20
    对于60%的数据,n,m<=100
    对于100%的数据,n,m<=100000

    【样例解释】
    每个位置都可以单个修改,所以有8种可能。

    思路

    主要思路

    任意一个区间都可以由比它更小的区间拼接而成,这样的区间我们称之为无效区间。然后,我们只要写个并查集维护一下即可。

    完整代码

    #include<bits/stdc++.h>
    using namespace std ;
    #define ll long long
    const int MAXN = 100005 , mod = 1000000007 ;
    inline int read(){
    	int  s = 0 ; char g=getchar() ; while(g>'9'||g<'0')g=getchar() ; 
    	while( g>='0'&&g<='9')s=s*10+g-'0',g=getchar() ; return s ;
    }
    int N , M , fa[ MAXN ] ;
    ll ans = 1LL ;
    int find( int x ){
    	if( fa[ x ] != x )fa[ x ] = find( fa[ x ] ) ;
    	return fa[ x ] ;
    }
    bool check( int x , int y ){
    	if( x > y )swap( x , y ) ;
    	int fx = find( x ) , fy = find( y+1 ) ;
    	if( fx == fy )return false ;
    	fa[ fx ] = fy ; return true ;
    }
    int main(){
    	freopen("seg.in","r",stdin);
    	freopen("seg.out","w",stdout);
    	N = read() , M = read() ; int m1 , m2 ;
    	for( int i = 1 ; i <= N+1 ; ++i )fa[ i ] = i ;
    	for( int i = 1 ; i <= M ; ++i ){
    		m1 = read() , m2 = read() ; 
    		if( check( m1 , m2 ) )ans = (ll)ans*2LL%(ll)mod ;
    	}
    	cout<<ans ;
    }
    

    如果这道题放在T1,或许会有更多人A掉吧

    如有不足,请大佬指出

  • 相关阅读:
    java.io.file
    连线小游戏
    发票类型区分的正则表达式(仅区分普票专票)
    mybatis: No enum constant org.apache.ibatis.type.JdbcType."VARCHAR"
    bootstrap inputfile 使用-上传,回显
    微积分极限中一例
    oracle 查看表结构语句
    redis无法连接
    项目配置shiro原缓存注解失效
    bug 找不到或无法加载主类main.java.*
  • 原文地址:https://www.cnblogs.com/ssw02/p/11477671.html
Copyright © 2020-2023  润新知