HDU3461 Code Lock 並查集應用

來源:互聯網
上載者:User
/*這題程式不難,關鍵想法比較難,以下內容來自別人的空間如果沒有區間存在,答案是26^n,每增加一個區間,n-1(因為這個區間可以變成26種狀態~).但是要注意的是,比如已經有(1,10)和(1,3)在了,此時再增加(4,10)就沒有作用了使用並查集對於[l,r]我們將l,r+1兩個點並起,如果新線段的兩個點是同一個集合,就不用減了*/#include <iostream>#include "UFS.h"#include <cmath>using namespace std;int mod = 1000000007;int exp(int a, int n);UFS ufs;int main(){int n,m, ans;while(cin>>n>>m){ans = n;ufs.clear();while(m--){int a,b;cin>>a>>b;if(ufs.Find(a) != ufs.Find(b+1)){ufs.Union(a, b+1);ans--;}}cout<<exp(26, ans)<<endl;}return 0;}//非遞迴求冪int exp(int a, int n){__int64 temp1 = 1, temp2 = a;while(n){if(n & 1)temp1 =( temp1 * temp2 ) % mod;n>>=1;temp2 =( temp2 * temp2 ) % mod;}return temp1;}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.