CF1835D Doctor's Brown Hypothesis
D - Doctor's Brown Hypothesis
首先,一对合法的\((x,y)\)一定是在同一个\(scc\)中的,所以我们将每个\(scc\)分开处理
若我们当前在处理某一个\(scc\),考虑给这个\(scc\)建一棵\(dfn\)树,设当前\(scc\)中的所有的环长度的\(gcd\)为\(d\),由数论得当\(g|k\)且\(k\)足够大时,一定有解
设环长为\(len_i\),那么题目就是求\(\sum len_i\times x_i=k\)
若原图中有边\((u,v)\),那么一定有\(depth_v-depth_u\equiv 1\mod d\)
因为\((u,v)\)一定被包含在某一个环里,设环长为\(len\),一定有\(d|len\),而在树上\(depth_x-depth_y\)就是\(y\)到\(x\)的距离\(\mod d\),所以有\(depth_u-depth_v\equiv len-1\mod d\),那么\(depth_v-depth_u\equiv 1-len\equiv 1\mod d\)
由此可得,满足题目的\((x,y)\)当且仅当
depth_u-depth_v\equiv k\mod d\\
depth_v-depth_u\equiv k\mod d
\end{cases}
\]
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;
const int N=1e5+5,M=2e5+5;
int n,m,k,d;
ll K,ans;
int head[N],cnt=1;
struct node{
int nxt,v;
}tree[M];
void add(int u,int v){
tree[++cnt]={head[u],v},head[u]=cnt;
}
int dfn[N],low[N],tot,stk[N],top,scc,c[N];
bool ins[N];
vector<int> pos,d_pos[N];
int depth[N],maxd;
void dfs(int x){
maxd=max(maxd,depth[x]),d_pos[depth[x]].pb(x);
for(int i=head[x],y;i;i=tree[i].nxt){
if(depth[y=tree[i].v]||c[y]!=c[x]) continue;
depth[y]=depth[x]+1,dfs(y);
}
}
map<int,int> mp;
void get(int dep){
if(dep>maxd) return;
for(auto x:d_pos[dep]) ++mp[(depth[x]+k)%d],ans+=mp[depth[x]%d];
get(dep+1);
}
#define Init() for(int i=1;i<=maxd;++i) d_pos[i].clear()
void work(){
maxd=0,depth[pos[0]]=1,dfs(pos[0]);
d=0;
for(auto x:pos)
for(int i=head[x],y;i;i=tree[i].nxt)
if(c[y=tree[i].v]==c[x]){
if(!d) d=abs(depth[tree[i].v]-depth[x]-1);
else d=__gcd(d,abs(depth[tree[i].v]-depth[x]-1));
}
if(!d){ Init(); return; }
k=K%d;
if(k&&k*2!=d){ Init(); return; }
mp.clear(),get(1);
Init();
}
void tarjan(int x){
dfn[x]=low[x]=++tot,ins[x]=true,stk[++top]=x;
for(int i=head[x],y;i;i=tree[i].nxt){
if(!dfn[y=tree[i].v]) tarjan(y),low[x]=min(low[x],low[y]);
else if(ins[y]) low[x]=min(low[x],dfn[y]);
}
if(dfn[x]==low[x]){
int y; ++scc,pos.clear();
do y=stk[top--],ins[y]=false,c[y]=scc,pos.pb(y);
while(y!=x);
work();
}
}
int main(){
scanf("%d%d%lld",&n,&m,&K);
for(int i=1,u,v;i<=m;++i) scanf("%d%d",&u,&v),add(u,v);
for(int i=1;i<=n;++i) if(!dfn[i]) tarjan(i);
printf("%lld",ans);
return 0;
}
CF1835D Doctor's Brown Hypothesis的更多相关文章
- CF 84D Doctor(二分)
题目链接: 传送门 Doctor time limit per test:1 second memory limit per test:256 megabytes Description Th ...
- 2076 Problem F Quick Brown Fox
题目描述 A pangram is a phrase that includes at least one occurrence of each of the 26 letters, ‘a’. . . ...
- [杂谈] There is a Doctor in My Computer.
(p.s. 附带手写翻译,有错轻喷) Admin: Hi. (嗨) Doctor: How do you do? What brings you to see me? ...
- Bar Chart of Frequency of modals in different sections of the Brown Corpus
Natural Language Processing with Python Chapter 4.8 colors = 'rgbcmyk' # red, green, blue, cyan, mag ...
- the quick brown fox jumps over the lazy dog
THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
- Quick Brown Fox
The quick brown fox jumps over the lazy dog
- 假设检验(Hypothesis Testing)
假设检验(Hypothesis Testing) 1. 什么是假设检验呢? 假设检验又称为统计假设检验,是数理统计中根据一定假设条件由样本推断总体的一种方法. 什么意思呢,举个生活中的例子:买橘子(借 ...
- 新概念英语(1-63)Thank you, doctor.
新概念英语(1-63)Thank you, doctor. Who else is in bed today? why? A:How's Jimmy today? B:Better. Thank yo ...
- An Apple a day keeps the doctor away
An apple a day keeps the doctor away. 一天一苹果,不用请医生. 活学活用:apple as like as an apple to an oyster 毫无相同之 ...
- Null hypothesis TypeⅠerror Type Ⅱ error
Null hypothesis usually express the phenomenon of no effect or no difference. TypeⅠerror is the inco ...
随机推荐
- 实现领域驱动设计 - 使用ABP框架 - 更新操作实体
用例演示 - 更新 / 操作实体 一旦一个实体被创建,它将被用例更新/操作,直到它从系统中删除.可以有不同类型的用例直接或间接地更改实体 在本节中,我们将讨论更改 Issue 的多个属性的典型更新操作 ...
- Pydantic异步校验器深:构建高并发验证系统
title: Pydantic异步校验器深:构建高并发验证系统 date: 2025/3/25 updated: 2025/3/25 author: cmdragon excerpt: Pydanti ...
- halcon 深度学习教程(三) 目标检测之水果分类
原文作者:aircraft 原文链接:halcon 深度学习教程(三) 目标检测之水果分类 - aircraft - 博客园 深度学习教程目录如下,还在继续更新完善中 深度学习系列教程目录 有兴趣可以 ...
- frameset frame 实例和用法
转
看这个比较好
- 解决 Maven 打包项目中 Excel 文件乱码问题
在 Java 项目开发过程中,我们常常会使用 Maven 来管理项目依赖和进行项目打包.当涉及到使用 Freemarker 导出 Excel 文件时,不少开发者可能会遇到一个让人头疼的问题 --Exc ...
- Lua 的os.date()
Lua os.date() os.date## 原型:os.date ([format [, time]]) 解释:返回一个按format格式化日期.时间的字串或表. usage## 参数格式: 由原 ...
- spring cloud alibaba的小坑:Caused by: com.alibaba.nacos.api.exception.NacosException: endpoint is blank报错问题,
一.是因为加添spring cloud alibaba配置中心依赖和bootstrap.xml又不使用的问题
- DPDI Online在线kettle调度工具
1. DPDI简介 DPDI Online 是一款基于Kettle的强大在线任务调度平台,凭借其高效与灵活性,专为调度和监控Kettle客户端生成的ETL任务而设计 2. DPDI使用 2.1 DPD ...
- Git错误,Updates were rejected because the tip of your current branch is behind
问题:Updates were rejected because the tip of your current branch is behind 在push代码时,遇到这种问题Updates wer ...
- kali安装charles
00X01 kali安装charles wget -q -O - http://www.charlesproxy.com/packages/apt/PublicKey | sudo apt-key a ...