HDU 3461 Code Lock

题解:计算出所有可以操作的区间,留下不可操作区间求26的幂次即可,注意直接合并区间端点可能会出现一些问题,所以将右端点加一:

#include <cstdio>  
#include <iostream>
using namespace std;  
const int mod=1000000007; 
long long t;  
int f[10000010];  
int sf(int x){return x==f[x]?x:f[x]=sf(f[x]);}  
int Union(int x,int y){  
    x=sf(x),y=sf(y);  
    if(x==y)return 0;  
    f[x]=y;  
    return 1;  
}  
long long power(int x){
    if(x==0)return 1;  
    long long t=power(x/2);  
    t=t*t%mod;  
    if(x%2==1)t=t*26%mod;  
    return t;  
}  
int main(){ 
    int m,n,a,b; 
    while(scanf("%d%d",&n,&m)!=EOF){  
        for(int i=1;i<=n+2;i++)f[i]=i;     
        int ans=0;
        while(m--){  
            scanf("%d%d",&a,&b);  
            ans+=Union(a,b+1);    
        }  
        cout<<power(n-ans)<<endl;  
    }     
}  

posted @ 2014-02-15 13:21  forever97  阅读(368)  评论(0编辑  收藏  举报