JZOJ.5305【NOIP2017模拟8.18】C
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
题意有点问题,实际上简单路径这里指的是不经过重复边的路径。
这题Tarjan缩点,然后LCA统计两点间环的个数k,答案就是2k个路径。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define M 600010
#define N 100010
#define mo 100000007
using namespace std;
int head[N],next[M],to[M],n,m,belong[N],dfn[N],low[N],zhan[N],sigh[N],num,ti,q,top,deep[N],visit[N],up[N][];
long long ans,sum[N];
void add(int u,int v){
num++;
next[num]=head[u];
to[num]=v;
head[u]=num;
num++;
next[num]=head[v];
to[num]=u;
head[v]=num;
}
void Tarjan(int x,int fa){
zhan[++top]=x;
ti++;
dfn[x]=ti;
low[x]=ti;
for (int v,i=head[x];i;i=next[i]){
v=to[i];
if (v==fa) continue;
if (!dfn[v]){
Tarjan(v,x);
low[x]=min(low[x],low[v]);
} else low[x]=min(low[x],dfn[v]);
}
if (dfn[x]==low[x]){
do{ sigh[x]++;
belong[zhan[top]]=x;
top--;
}while(zhan[top+]!=x);
sigh[x]--;
}
}
void DFS(int x,int fa){
if (x==) up[x][]=x;
else up[x][]=fa;
for (int i=;i<=;i++)
up[x][i]=up[up[x][i-]][i-];
deep[x]=deep[fa]+;
if (sigh[x]) sum[x]=sum[fa]+;
else sum[x]=sum[fa];
for (int v,i=head[x];i;i=next[i]){
v=to[i];
if (belong[v]==belong[fa]) continue;
if (belong[v]!=belong[x])
DFS(v,x);
}
}
long long kuai(long long a,long long b){
long long ans=;
long long mi=b;
long long qwq=a;
while (mi){
if (mi&) ans=ans*qwq%mo;
qwq=qwq*qwq%mo;
mi>>=;
}
return ans;
}
int lca(int u,int v){
if (deep[u]<deep[v]) swap(u,v);
for (int i=;i>=;i--)
if (deep[v]<=deep[up[u][i]])
u=up[u][i];
if (u==v) return u;
for (int i=;i>=;i--)
if (up[u][i]!=up[v][i])
u=up[u][i],v=up[v][i];
return up[u][];
}
int main(){
scanf("%d%d",&n,&m);
num=;
for (int i=,u,v;i<=m;i++){
scanf("%d%d",&u,&v);
add(u,v);
}
for (int i=;i<=n;i++)
sigh[i]=;
ti=;
top=;
Tarjan(,);
for (int i=;i<=n;i++){
if (belong[i]!=i)
for (int v,j=head[i];j;j=next[j]){
v=to[j];
if (belong[v]!=belong[i])
add(v,belong[i]);
}
}
sum[]=;
deep[]=;
DFS(,);
scanf("%d",&q);
for (int i=,u,v,father;i<=q;i++){
ans=;
scanf("%d%d",&u,&v);
if (u==v) if (sigh[belong[u]]) {printf("1\n");continue;}else {printf("0\n");continue;}
u=belong[u],v=belong[v];
if (deep[u]<deep[v]) swap(u,v);
father=lca(u,v);
ans=sum[u]+sum[v]-*sum[father]+(sigh[father]?:);
printf("%lld\n",kuai(,ans)%mo);
}
return ;
}
神奇的代码
JZOJ.5305【NOIP2017模拟8.18】C的更多相关文章
- JZOJ.5307【NOIP2017模拟8.18】偷窃
Description
- JZOJ.5306【NOIP2017模拟8.18】棋盘游戏
Description 这个游戏上在一个无限大的棋盘上, 棋盘上只有一颗棋子在位置(x,y)(x,y>=0)棋盘的左下角是(0,0)Amphetamine每次都是第一个移动棋子,然后Amphet ...
- [jzoj 5343] [NOIP2017模拟9.3A组] 健美猫 解题报告 (差分)
题目链接: http://172.16.0.132/senior/#main/show/5343 题目: 题解: 记旋转i次之后的答案为$ans_i$,分别考虑每个元素对ans数组的贡献 若$s_i& ...
- [JZOJ 5912] [NOIP2018模拟10.18] VanUSee 解题报告 (KMP+博弈)
题目链接: https://jzoj.net/senior/#contest/show/2530/2 题目: 众所周知,cqf童鞋对哲学有着深入的理解和认识,并常常将哲学思想应用在实际生活中,例如锻炼 ...
- [JZOJ 5910] [NOIP2018模拟10.18] DuLiu 解题报告 (并查集+思维)
题目链接: https://jzoj.net/senior/#contest/show/2530/0 题目: LF是毒瘤出题人中AK IOI2019,不屑于参加NOI的唯一的人.他对人说话,总是满口垃 ...
- [JZOJ 5911] [NOIP2018模拟10.18] Travel 解题报告 (期望+树形DP)
题目链接: http://172.16.0.132/senior/#contest/show/2530/1 题目: EZ同学家里非常富有,但又极其的谦虚,说话又好听,是个不可多得的人才. ...
- 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 ...
- 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 5230. 【NOIP2017模拟A组模拟8.5】队伍统计
5230. [NOIP2017模拟A组模拟8.5]队伍统计 (File IO): input:count.in output:count.out Time Limits: 1500 ms Memory ...
随机推荐
- java dom xml解析和windows dom解析的差别
1.java dom 子节点包括空节点. 2.java dom 文档节点值也是其的子节点. 而windows dom(mshtml.dll)子节点不包括空节点,而节点值也不是以子节点的方式来对待的.
- @Autowired和@Resource装配
从Spring2.5開始就能够使用注解自己主动装配Bean的属性. 使用注解自己主动装配与XML中使用autowire属性自己主动装配并没有太大区别. Spring容器默认禁用注解装配. 所以在基于注 ...
- Laravel Eloquent ORM--整理(未转)
转:http://blog.csdn.net/a437629292/article/details/46312695 http://www.jianshu.com/p/cb641a4f3599
- blender, 同时选择多个顶点
法1:按MRB(鼠标右键)选中第一个顶点,再按shift+MRB依次选择其余顶点. 法2:按B,光标变为横纵两条虚线,此时可按MLB(鼠标左键)框选顶点.按MRB结束框选. 法3:按C,光标变为虚线圆 ...
- Redis(十二):redis两种持久化方法对比分析
前言 最近在项目中使用到Redis做缓存,方便多个业务进程之间共享数据.由于Redis的数据都存放在内存中,如果没有配置持久化,redis重启后数据就全丢失了,于是需要开启redis的持久化功能,将数 ...
- How To run OAI eNB (No S1) with USRP X310(1)
How To run OAI eNB (No S1) with USRP X310 1.Things need to be done 1.1 Install Ubuntu 14.04 1.1.1 In ...
- 让HttpClient不要打印巨多的日志
让HttpClient不要打印巨多的日志 log4j.logger.org.apache.commons.httpclient=DEBUG 參考http://hc.apache.org/httpcli ...
- 安卓-启动下一个Activity
Intent intent = new Intent(NotebookActivity.this, MessageActivity.class); startActivity(intent); 通过意 ...
- 使用tc对linux中某ip段限速
TC 无需安装,Linux 内核自带例:将IP地址段192.168.1.0/24 上传下载限速为 5M将以下内容添加到/etc/ppp/ip-up文件exit 0上面. down=5Mbituploa ...
- 每日英语:Dishing the Dirt on Hand-Washing Guidelines
Americans aren't washing their hands nearly as often and as thoroughly as they should, according to ...