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 ...
随机推荐
- 计算a^b==a+b在(l,r)的对数Codeforces Round #597 (Div. 2)
题:https://codeforces.com/contest/1245/problem/F 分析:转化为:求区间内满足a&b==0的对数(解释见代码) ///求满足a&b==0在区 ...
- 关于Apache Commons的简介
Apache Commons是对JDK的拓展,包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动.官网网址:http://commons.apache.org Commons Bea ...
- DataGrip设置时区
新版本DataGrip以默认时区取世界标准时间.要想时间显示正常,需要将时区变为上海时区,可手动在连接配置里设置参数.如下图: 操作步骤1.右键打开你想要修改的数据库连接的Properties菜单:2 ...
- 《C 程序设计语言》练习1-3
#include<stdio.h> /*当fahr=0,20,...,300时,打印华氏温度与摄氏温度对照表; 浮点数版本*/ main () { float fahr,celsius; ...
- javascript 实现最简单的阶乘!
<script type='text/javascript'> window.onload = get(5); function get(n){ document.w ...
- django项目导航页_20191128
django项目导航页_20191128 第一步:python-django电商项目-需求分析架构设计数据库设计_20191115 python-django-linux上mysql的安装和配置_20 ...
- life|October the 1st| five twenty|three–fifths|1970s|30s
The music is full of (a, an, the, /) life. It can release your pressure to a large extent, so I enj ...
- python3下scrapy爬虫(第十一卷:scrapy数据存储进mongodb)
说起python爬虫数据存储就不得不说到mongodb,现在我们来试一下scrapy操作mongodb 首先开启mongodb mongod --dbpath=D:\mongodb\db 开启服务后就 ...
- cs231n spring 2017 lecture13 Generative Models
1. 非监督学习 监督学习有数据有标签,目的是学习数据和标签之间的映射关系.而无监督学习只有数据,没有标签,目的是学习数据额隐藏结构. 2. 生成模型(Generative Models) 已知训练数 ...
- Jsp入门EL表达式_学习笔记
1.EL表达式 [1] 简介 > JSP表达式 <%= %> 用于向页面中输出一个对象. > 到JSP2.0时,在我们的页面中不允许出现 JSP表达式和 脚本片段. > ...