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 ...
随机推荐
- CompletableFuture你真的懂了么,我劝你在项目中慎用
1. 前言 在实际做项目中,我们经常使用多线程.异步的来帮我们做一些事情. 比如用户抽取奖品,异步的给他发一个push. 又比如一段前后不相关的业务逻辑,原本是顺序执行,耗时=(A + B + C), ...
- 探秘Transformer系列之(18)--- FlashAttention
探秘Transformer系列之(18)--- FlashAttention 目录 0x00 概述 0.1 问题 0.2 其它解决方案 0.3 Flash Attention 0x01 背景知识 1. ...
- 【Java】Math类的基本操作
Math类 Math 类是数学操作类,提供了一系列的数学操作方法,包括求绝对值.三角函数等,在 Math 类中提供的一切方法都是静态方法(类方法),所以直接由类名称调用即可. Math类的基本操作: ...
- Python合成多个视频为一个脚本
编写背景: 由于线上用户反馈媒体添加页加载时间很长,猜测是由于本地视频内存过大引起,于是编写此脚本以便快速生成内存很大的视频 代码如下: # coding=utf-8 from moviepy.edi ...
- 🎀腾讯云nodejs SDK打包体积过大吐槽事件
简介 2025年2月1日有位开发同学batchor在GitHub上提出了issue(你们是把***打包了吗?)对腾讯云Node.js的SDK打包体积过大进行吐槽(言语偏贴吧风格略显激进),SDK打包体 ...
- 使用Linux筛选文本-日志分析
用于简单的文本筛选和日志分析还是很方便的. 我这里用的kali **目的:**筛选出test文件中 状态码为500的url 命令: cat test |grep '500' >test1 或 g ...
- 『Plotly实战指南』--交互功能基础篇
在数据可视化领域,静态图表早已无法满足用户对深度分析与探索的需求. Plotly作为新一代交互式可视化工具,通过其强大的交互功能重新定义了"数据叙事"的边界. 通过精心设计的交互功 ...
- linux期末考试题(1)
linux期末考试题 一.选择题(共20分,每小题2分) 1.以下哪个环境变量表示shell搜索外部命令或程序路径(C) A.ENV B.PWD C.PATH D.ROOT 解答: ENV用于显示当前 ...
- 历数java虚拟机GC的种种缺点
Java通过垃圾收集器(Garbage Collection,简称GC)实现自动内存管理,这样可有效减轻Java应用开发人员的负担,也避免了更多内存泄露的风险. 如果你用过C++等需要手动管理内存的语 ...
- k8s之ingress反向代理pod
Ingress controller Nginx -->后来改造 Traefik -->也是用于微服务 Envoy -->微服务 Ingress资源 目前使用0.17.1版本ing ...