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 ...
随机推荐
- yum pip
方式1(yum安装):1.首先安装epel扩展源:[root@localhost ~]# yum -y install epel-release如果没有安装epel扩展源而直接安装python-pi ...
- 系统学习javaweb3----HTML语言3(结束)
说明:昨天是北方小年,需要做的事情有点多,需要祭灶,扫尘.包饺子,吃糖瓜儿,学习时间有点少,所以今天将两天的知识综合一下发出. 自我感觉:虽然感觉大致都了解了HTML语言,但是感觉自己面对程序还是无从 ...
- slqmap简单使用
网址:-u 指定哪个参数:-p 需要登录用--cookie 获得所有数据库:--dbs 获得所有用户:--users 指定某个数据库:-D 显示所有表--tables 指定某个数据表:-T 显示列:- ...
- [LC] 95. Unique Binary Search Trees II
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...
- 吴裕雄--天生自然 HADOOP大数据分布式处理:安装配置Tomcat服务器
下载链接:https://tomcat.apache.org/download-80.cgi tar -zxvf apache-tomcat-8.5.42.tar.gz -C /usr/local/s ...
- in+sb's+基数词的复数形式|UFO|the minutes|
Hawking became world-famous in ________. A. his thirties in the 1970's B. the thirties in his 1970 ...
- Fault Domain深入分析
- [LC] 64. Minimum Path Sum
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- 【一定要记得填坑】LG_3822_[NOI2017]整数
挺好的一道题,由于快noip了,所以打算noip之后再添题解的坑.
- 控制webbrowser滚动到指定位置
在构造函数中添加事件: webBrowser.DocumentCompleted+=new WebBrowserDocumentCompletedEventHandler(webBrowser_Doc ...