HDU 3461 Code Lock(并查集)
很好的一个题,思想特别6
题意:给你小写字母个数n,每个字母可以向上翻动,例如:d->c,a->z。然后给你m对数(L,R)(L<=R),表示[L,R]之间可以同时向上翻动,且翻动后是相同的类型。问你最后可以出现多少种不同的类型。
例如:abcabc只给你[1,3],那么abcabc==zababc(代表相同类型)
首先答案是是26^(n-m+k),而我们只需要找到k就好了。而k只能有一种情况才会出现,就是:[1,3]与[4,6]出现后[1,6]再出现,这样k就加一个。而[1,3]与[3,6]出现后[1,6]出现,k不会加一(因为是区间关系)。这样我们就离散化后,对[L-1,R]使用并查集就好。
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<string>
#include<cstdio>
#include<cstring>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define eps 1E-8
/*注意可能会有输出-0.000*/
#define Sgn(x) (x<-eps? -1 :x<eps? 0:1)//x为两个浮点数差的比较,注意返回整型
#define Cvs(x) (x > 0.0 ? x+eps : x-eps)//浮点数转化
#define zero(x) (((x)>0?(x):-(x))<eps)//判断是否等于0
#define mul(a,b) (a<<b)
#define dir(a,b) (a>>b)
typedef long long ll;
typedef unsigned long long ull;
const int Inf=<<;
const double Pi=acos(-1.0);
const ll Mod=;
const int Max=;
int fat[Max];
map<int,int> mp;//离散化
struct node
{
int lef,rig;
}lock[Max];
void init(int m)
{
for(int i=;i<=m;++i)
fat[i]=i;
return;
}
ll Ftp(ll num,int n)
{
ll sum=1ll;
while(n)
{
if(n&)
sum=(sum*num)%Mod;
num=(num*num)%Mod;
n>>=;
}
return sum;
}
int Find(int x)
{
if(x==fat[x])
return fat[x];
return fat[x]=Find(fat[x]);
}
int Union(int x,int y)
{
int x1=Find(x);
int y1=Find(y);
if(x1==y1)
return ;
fat[x1]=y1;
return ;
}
ll Solve(int n,int m)
{
for(int i=;i<m;++i)
n-=Union(mp[lock[i].lef],mp[lock[i].rig]);//关键
return Ftp(26ll,n);
}
int main()
{
//std::ios::sync_with_stdio(false);
int n,m;
while(~scanf("%d %d",&n,&m))
{
init(m<<);
mp.clear();
for(int i=;i<m;++i)
{
scanf("%d %d",&lock[i].lef,&lock[i].rig);
mp[--lock[i].lef]=;
mp[lock[i].rig]=;
}
int coun=;
for(map<int,int>::iterator it=mp.begin();it!=mp.end();++it)
it->second=coun++;//map离散化
printf("%I64d\n",Solve(n,m));
}
return ;
}
HDU 3461 Code Lock(并查集)的更多相关文章
- HDU 3461 Code Lock(并查集+二分求幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3461 A lock you use has a code system to be opened in ...
- HDU 3461 Code Lock(并查集,合并区间,思路太难想了啊)
完全没思路,题目也没看懂,直接参考大牛们的解法. http://www.myexception.cn/program/723825.html 题意是说有N个字母组成的密码锁,如[wersdfj],每一 ...
- hdu 3461 Code Lock 并查集(有点难想到)★★
#include<stdio.h> #include<math.h> ]; int count; #define mod 1000000007 int find(int x) ...
- hdu 3461 Code Lock(并查集)2010 ACM-ICPC Multi-University Training Contest(3)
想不到这还可以用并查集解,不过后来证明确实可以…… 题意也有些难理解—— 给你一个锁,这个所由n个字母组成,然后这个锁有m个区间,每次可以对一个区间进行操作,并且区间中的所有字母要同时操作.每次操作可 ...
- HDU 3461 Code Lock(并查集的应用+高速幂)
* 65536kb,仅仅能开到1.76*10^7大小的数组. 而题目的N取到了10^7.我開始做的时候没注意,用了按秩合并,uset+rank达到了2*10^7所以MLE,所以貌似不能用按秩合并. 事 ...
- HDU 3461 思维+并查集
Code Lock 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3461 Problem Description A lock you use has ...
- hdu 3461 Code Lock
http://acm.hdu.edu.cn/showproblem.php?pid=3461 并差集和幂取模 这道题主要是求不可操作区间. #include <cstdio> #inclu ...
- HDU 1811 拓扑排序 并查集
有n个成绩,给出m个分数间的相对大小关系,问是否合法,矛盾,不完全,其中即矛盾即不完全输出矛盾的. 相对大小的关系可以看成是一个指向的条件,如此一来很容易想到拓扑模型进行拓扑排序,每次检查当前入度为0 ...
- hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点)
hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点) 题意: 给一张无向连通图,有两种操作 1 u v 加一条边(u,v) 2 u v 计算u到v路径上桥的个数 ...
随机推荐
- 没有body怎么添加onload事件
<script type="text/javascript"> window.onload = function () { setup(); } </script ...
- inline函数的用法
在c/c++中,为了解决一些频繁调用的小函数大量消耗栈空间或是叫栈内存的问题,特别的引入了inline修饰符,表示为内联函数.栈空间就是指放置程式的局部数据也就是函数内数据的内存空间,在系统下,栈空间 ...
- 【leetcode】Combinations (middle)
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- 51nod 1605 棋盘问题 (博弈)
题目:传送门. 题意:中文题.T组数据,每组给定一个n*m的棋盘,棋盘中的1代表黑色,0代表白色,每次可以将1或者非2质数的全黑色方形区域变为白色,不能操作者输,问谁能赢. 题解:每次可以将1或者非2 ...
- php数据访问增删查
1.先做一个登陆界面 <form action="chuli.php" method="post"> <div>用户名:<inpu ...
- osg四元数设置roll pitch heading角度
roll绕Y轴旋转 pitch绕X轴旋转 heading绕Z轴旋转 单位是弧度,可以使用osg::inDegrees(45)将45角度转换为弧度 定义一个四元数 osg::Quat q( roll,o ...
- AFNetworking request failed unacceptable content type text/html
今天体验AFNetwork 3.1.0 进行数据解析,但是解析数据控制台一直都输出这样的 error 结果: 于是,照着以前AFN2.+版本的进行设置: 结果发现在新版本的 AFN 上不能设置了.既然 ...
- ui组件--弹出层layer的使用
帮助文档 下载地址 演示地址
- VCC、VDD、VSS、 VEE 和VPP的区别
在电子电路中,常可以看到VCC.VDD和VSS三种不同的符号,它们有什么区别呢? 一.解释 VCC:C=circuit 表示电路的意思, 即接入电路的电压: VDD:D=device 表示器件的意思, ...
- Linq to json
Json.Net系列教程 4.Linq To JSON 一.Linq to JSON是用来干什么的? Linq to JSON是用来操作JSON对象的.可以用于快速查询,修改和创建JSON对象.当JS ...