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)的更多相关文章

  1. JZOJ 【NOIP2017提高A组模拟9.14】捕老鼠

    JZOJ [NOIP2017提高A组模拟9.14]捕老鼠 题目 Description 为了加快社会主义现代化,建设新农村,农夫约(Farmer Jo)决定给农庄里的仓库灭灭鼠.于是,猫被农夫约派去捕 ...

  2. JZOJ 5307. 【NOIP2017提高A组模拟8.18】偷窃 (Standard IO)

    5307. [NOIP2017提高A组模拟8.18]偷窃 (Standard IO) Time Limits: 1000 ms Memory Limits: 262144 KB Description ...

  3. [JZOJ 100026] [NOIP2017提高A组模拟7.7] 图 解题报告 (倍增)

    题目链接: http://172.16.0.132/senior/#main/show/100026 题目: 有一个$n$个点$n$条边的有向图,每条边为$<i,f(i),w(i)>$,意 ...

  4. 【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 ...

  5. JZOJ 100029. 【NOIP2017提高A组模拟7.8】陪审团

    100029. [NOIP2017提高A组模拟7.8]陪审团 Time Limits: 1000 ms  Memory Limits: 131072 KB  Detailed Limits   Got ...

  6. JZOJ 5328. 【NOIP2017提高A组模拟8.22】世界线

    5328. [NOIP2017提高A组模拟8.22]世界线 (File IO): input:worldline.in output:worldline.out Time Limits: 1500 m ...

  7. JZOJ 5329. 【NOIP2017提高A组模拟8.22】时间机器

    5329. [NOIP2017提高A组模拟8.22]时间机器 (File IO): input:machine.in output:machine.out Time Limits: 2000 ms M ...

  8. JZOJ 5286. 【NOIP2017提高A组模拟8.16】花花的森林 (Standard IO)

    5286. [NOIP2017提高A组模拟8.16]花花的森林 (Standard IO) Time Limits: 1000 ms Memory Limits: 131072 KB Descript ...

  9. 【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 ...

随机推荐

  1. Python - 使用 PostgreSQL 数据库

    基本用法 # -*- coding: utf-8 -*- # !/usr/bin/python # 需要安装下面的驱动包 import psycopg2 # 连接到一个现有的数据库,如果数据库不存在, ...

  2. K - The Unique MST (最小生成树的唯一性)

    Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spann ...

  3. 回归分析|r^2|Se|变差|多重相关系数|决定系数|多重共线性|容忍度|VIF|forward selection|backward elimination|stepwise regression procedure|best-subset approach|回归方程的置信区间|预测区间|残差分析|虚拟变量

    应用统计学-回归分析 拟合度使用r^2和Se来检验. 显著性检验中,对于线性model使用ANOVA,对于单独的回归系数使用t检验. 最小二乘法.贝叶斯和最大似然都可用于求回归参数,最小二乘法是最小化 ...

  4. 吴裕雄--天生自然python学习笔记:pandas模块DataFrame 数据的修改及排序

    import pandas as pd datas = [[65,92,78,83,70], [90,72,76,93,56], [81,85,91,89,77], [79,53,47,94,80]] ...

  5. keras使用、函数功能

    #1. keras.engine.input_layer.Input() def Input(shape=None, batch_shape=None,   name=None, dtype=None ...

  6. MOOC(7)- case依赖、读取json配置文件进行多个接口请求-读取json封装成类(13)

    把读取json数据的函数封装成类 # -*- coding: utf-8 -*- # @Time : 2020/2/12 16:44 # @File : do_json_13.py # @Author ...

  7. JDBC源码分析(加载过程)

    public static void main(String[] args) {     String url = "jdbc:mysql://172.16.27.11:3306/jdbcT ...

  8. CRISPR/Cas9|InParanoid|orthoMCL|PanOCT|pan genome|meta genome|Core gene|CVTree3|

    生命组学: 泛基因组学:用于描述一个物种基因组,据细菌基因组动力学,因为细菌的基因漂移使得各个细菌之间的基因组差异很大,(单个细菌之间的基因组差异是以基因为单位的gain&loss,而人类基因 ...

  9. MyBatis基本使用步骤

    MyBatis是一个数据持久层(ORM)框架.把实体 类和SQL语句之间建立了映射关系,是一种半自 动化的ORM实现.MyBATIS需要开发人员自己来写sql语句,这可以增加了程序的灵活性,在一定程度 ...

  10. 自定义一个简单的SegmentedControl

    先大概看下我们想实现简单的效果 源码 // // DSegmentedControl.swift // IOS学习之自定义UISegmentedControl // // Created by din ...