JZOJ 5305. 【NOIP2017提高A组模拟8.18】C (Standard IO)
5305. 【NOIP2017提高A组模拟8.18】C (Standard IO)
Time Limits: 1000 ms Memory Limits: 131072 KB
Description
Input
Output
Sample Input
10 11
1 2
2 3
3 4
1 4
3 5
5 6
8 6
8 7
7 6
7 9
9 10
6
1 2
3 5
6 9
9 2
9 3
9 10
Sample Output
2
2
2
4
4
1
Data Constraint
Hint
题解
trajan裸题
加个树上RMQ
用lca优化一下就好了
代码
#include<cstdio>
#include<vector>
#define N 100010
#define mo 1000000007
#define ll long long
using namespace std;
vector<long>map[N],bian[N];
long low[N],dfn[N],tot,sta[N],w[N],cnt[N],top,num;
bool b[N],c[N],in[N];
void dfs(long now)
{ long i,to;
low[now]=dfn[now]=++tot;
b[now]=in[now]=true;
sta[++top]=now;
for(i=0;i<map[now].size();i++){
to=map[now][i];
if(!b[to]){
c[bian[now][i]]=true;
dfs(to);
low[now]=min(low[now],low[to]);
}
else if(in[to]&&!c[bian[now][i]])
low[now]=min(low[now],dfn[to]);
}
if(low[now]==dfn[now]){
num++;
to=0;
while(now!=to){
to=sta[top--];
in[to]=false;
w[to]=num;
cnt[num]++;
}
}
}
void tarjan(long n)
{ long i;
for(i=1;i<=n;i++)
if(!b[i])
dfs(i);
}
long sum[N][20],fa[N][20],dep[N];
void build(long now)
{ long i,to;
b[now]=false;
for(i=0;i<map[now].size();i++){
to=map[now][i];
if(b[to]){
if(w[to]!=w[now]){
if(cnt[w[now]]>1)
sum[w[to]][0]=1;
fa[w[to]][0]=w[now];
dep[w[to]]=dep[w[now]]+1;
}
build(to);
}
}
}
long lca(long x,long y)
{ long up;
if(dep[x]>dep[y])
swap(x,y);
up=19;
while(dep[y]>dep[x]){
while(dep[y]-(1<<up)<dep[x]&&up>=0)
up--;
if(up<0)break;
y=fa[y][up--];
}
up=19;
while(x!=y){
while(fa[x][up]==fa[y][up]&&up>=0)
up--;
if(up<0)break;
x=fa[x][up];
y=fa[y][up];
up--;
}
if(x==y)
return x;
else
return fa[x][0];
}
long suan(long x)
{ long up,ans=(cnt[x]>1)?1:0;
up=19;
while(dep[x]>0){
while(dep[x]-(1<<up)<0&&up>=0)
up--;
if(up<0)break;
ans=(ans+sum[x][up])%mo;
x=fa[x][up];
}
return ans;
}
int main()
{ long n,m,i,j,x,y,q,ci,ans,l;
scanf("%ld%ld",&n,&m);
for(i=1;i<=m;i++){
scanf("%ld%ld",&x,&y);
map[x].push_back(y);
map[y].push_back(x);
bian[x].push_back(i);
bian[y].push_back(i);
}
tarjan(n);
build(1);
n=num;
for(j=1;(1<<j)<=n;j++)
for(i=1;i<=n;i++){
fa[i][j]=fa[fa[i][j-1]][j-1];
sum[i][j]=(sum[fa[i][j-1]][j-1]+sum[i][j-1])%mo;
}
scanf("%ld",&q);
for(i=1;i<=q;i++){
scanf("%ld%ld",&x,&y);
x=w[x];
y=w[y];
l=lca(x,y);
ci=(((suan(x)+suan(y))%mo-2*suan(l)%mo)%mo+((cnt[l]>1)?1:0))%mo;
ans=1;
for(j=1;j<=ci;j++)
ans=((ll)ans<<1)%mo;
printf("%ld\n",ans);
}
return 0;
}
JZOJ 5305. 【NOIP2017提高A组模拟8.18】C (Standard IO)的更多相关文章
- JZOJ 【NOIP2017提高A组模拟9.14】捕老鼠
JZOJ [NOIP2017提高A组模拟9.14]捕老鼠 题目 Description 为了加快社会主义现代化,建设新农村,农夫约(Farmer Jo)决定给农庄里的仓库灭灭鼠.于是,猫被农夫约派去捕 ...
- JZOJ 5307. 【NOIP2017提高A组模拟8.18】偷窃 (Standard IO)
5307. [NOIP2017提高A组模拟8.18]偷窃 (Standard IO) Time Limits: 1000 ms Memory Limits: 262144 KB Description ...
- [JZOJ 100026] [NOIP2017提高A组模拟7.7] 图 解题报告 (倍增)
题目链接: http://172.16.0.132/senior/#main/show/100026 题目: 有一个$n$个点$n$条边的有向图,每条边为$<i,f(i),w(i)>$,意 ...
- 【NOIP2017提高A组模拟9.7】JZOJ 计数题
[NOIP2017提高A组模拟9.7]JZOJ 计数题 题目 Description Input Output Sample Input 5 2 2 3 4 5 Sample Output 8 6 D ...
- JZOJ 100029. 【NOIP2017提高A组模拟7.8】陪审团
100029. [NOIP2017提高A组模拟7.8]陪审团 Time Limits: 1000 ms Memory Limits: 131072 KB Detailed Limits Got ...
- JZOJ 5328. 【NOIP2017提高A组模拟8.22】世界线
5328. [NOIP2017提高A组模拟8.22]世界线 (File IO): input:worldline.in output:worldline.out Time Limits: 1500 m ...
- JZOJ 5329. 【NOIP2017提高A组模拟8.22】时间机器
5329. [NOIP2017提高A组模拟8.22]时间机器 (File IO): input:machine.in output:machine.out Time Limits: 2000 ms M ...
- JZOJ 5286. 【NOIP2017提高A组模拟8.16】花花的森林 (Standard IO)
5286. [NOIP2017提高A组模拟8.16]花花的森林 (Standard IO) Time Limits: 1000 ms Memory Limits: 131072 KB Descript ...
- 【NOIP2017提高A组模拟9.17】信仰是为了虚无之人
[NOIP2017提高A组模拟9.17]信仰是为了虚无之人 Description Input Output Sample Input 3 3 0 1 1 7 1 1 6 1 3 2 Sample O ...
随机推荐
- 绿洲作业第一周 - Y3每日中文学习任务清单
尽量每天完成一首古诗. 如果孩子另有学习安排,可在周日(2.16)23:59前,完成任务.有兴趣.学有余力的孩子可以自我拓展其中的“寒假读诗”.
- rest framework-认证&权限&限制-长期维护
############### 自定义token认证 ############### 表 class User(models.Model): name=models.CharField(ma ...
- linux chmod命令修改文件权限
在linux中,使用chmod命令修改一个文件的权限. 首先,我们查看一个文件夹下所有文件的权限 ls -l linux文件或目录的权限分为,读.写.可执行三种权限.文件访问的用户类别分为,文件创建者 ...
- matplotlib,seaborn中文乱码问题的解决
方法一:指定字体 import matplotlib.pyplot as plt myfont = matplotlib.font_manager.FontProperties(fname='/hom ...
- [LC] 96. Unique Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1 ... n? Example ...
- 吴裕雄--天生自然 PHP开发学习:在CenterOS 7 系统安装配置PHP 7
执行命令"yum install httpd"进行安装,然后在选择处,我们输入y,等待软件安装完毕.安装完成后,Apache配置文件路径是:/etc/httpd/conf/http ...
- 学习python-20191208(1)-Python Flask高级编程开发鱼书_第03章_数据与flask路由
视频01: 略...... ———————————————————————————————————————————————————————————— 视频02: 搜索需要外部数据源,也就是需要外部的A ...
- 【一定要记得填坑】LG_3822_[NOI2017]整数
挺好的一道题,由于快noip了,所以打算noip之后再添题解的坑.
- linux增加history时间戳
增加环境变量到/etc/profile export HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S " export HISTSIZE=9999
- XX系统测试总结报告
XX系统测试总结报告 1 引言 1.1 编写目的 编写该测试总结报告主要有以下几个目的 1. 通过对测试结果的分析,得到对软件质量的评价 2. 分析测试的过程,产品,资源,信息, ...