牛客练习赛56 E 小雀和他的王国
题目链接:https://ac.nowcoder.com/acm/contest/3566/E
思路:tarjan缩点,桥重建图,dfs跑树的直径。
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll; const int N = (int)2e5+,mod = (int)1e9+;
struct node{
int to,nxt;
}e[N<<];
vector<pair<int,int> > cut;
int n,m,u,v,tot,tim,top,scc_num,bridge;
int head[N],dfn[N],low[N],s[N],scc_no[N],d[N];
bool vis[N]; ll quick(ll a,ll b){
ll ans=;
a=a%mod;
while(b!=){
if(b&) ans=(ans*a)%mod;
b>>=;
a=(a*a)%mod;
}
return ans;
} inline void add(int u,int v){
e[tot].to = v; e[tot].nxt = head[u]; head[u] = tot++;
e[tot].to = u; e[tot].nxt = head[v]; head[v] = tot++;
} void tarjan(int now,int pre){
dfn[now] = low[now] = ++tim;
s[top++] = now;
int pre_cnt = ;
for(int o = head[now]; ~o; o = e[o].nxt){
int to = e[o].to;
if(to == pre && pre_cnt == ){ pre_cnt = ; continue; }
if(!dfn[to]){
tarjan(to,now);
low[now] = min(low[now],low[to]);
if(dfn[now] < low[to]) cut.push_back(make_pair(now,to));
}else low[now] = min(low[now],dfn[to]);
} if(dfn[now] == low[now]){
++scc_num;
int tmp;
do{
tmp = s[--top];
scc_no[tmp] = scc_num;
}while(now != tmp);
}
} void dfs(int now,int pre){
vis[now] = ;
d[now] = d[pre]+;
for(int o = head[now]; ~o; o = e[o].nxt){
int to = e[o].to;
if(vis[to]) continue;
dfs(to,now);
}
} void rebuild(){
for(int i = ; i <= scc_num; ++i) head[i] = -; tot = ;
bridge = cut.size();
for(int i = ; i < bridge; ++i){
// printf("%d %d\n",scc_no[cut[i].first],scc_no[cut[i].second]);
add(scc_no[cut[i].first],scc_no[cut[i].second]);
}
/*
for(int i = 1; i <= scc_num; ++i){
printf("u = %d\t",i);
for(int o = head[i]; ~o; o = e[o].nxt) printf("%d ",e[o].to);
cout << endl;
}*/
} void show_info(){
for(int i = ; i <= n; ++i){
printf("loc = %d scc_no = %d\n",i,scc_no[i]);
}
} void solve(){ for(int i = ; i <= n; ++i) head[i] = -; tot = ;
for(int i = ; i <= m; ++i){
scanf("%d%d",&u,&v);
add(u,v);
}
tarjan(,);
rebuild();
int s = ;
for(int i = ; i <= scc_num; ++i){
d[i] = -; vis[i] = ;
}
dfs(s,);
for(int i = ; i <= scc_num; ++i){
if(d[s] < d[i]) s = i;
}
for(int i = ; i <= scc_num; ++i){
d[i] = -; vis[i] = ;
}
dfs(s,);
int max_len = ;
for(int i = ; i <= scc_num; ++i) max_len = max(max_len,d[i]);
// printf("max_len = %d\n",max_len);
//printf("%lf\n",(double)(bridge-max_len)/(m+1));
printf("%lld\n",1ll*(bridge-max_len)*quick(m+,mod-)%mod);
} int main(){ scanf("%d%d",&n,&m);
solve();
//show_info(); return ;
}
牛客练习赛56 E 小雀和他的王国的更多相关文章
- 牛客练习赛56 B 小琛和他的学校
题目链接:https://ac.nowcoder.com/acm/contest/3566/B 思路:一条路可把图分为左右两部分. l_ci, l_peo, r_ci, r_peo, w 分别为左边城 ...
- 牛客练习赛48 C 小w的糖果 (数学,多项式,差分)
牛客练习赛48 C 小w的糖果 (数学,多项式) 链接:https://ac.nowcoder.com/acm/contest/923/C来源:牛客网 题目描述 小w和他的两位队友teito.toki ...
- 牛客练习赛48 A· 小w的a+b问题 (贪心,构造,二进制)
牛客练习赛48 A· 小w的a+b问题 链接:https://ac.nowcoder.com/acm/contest/923/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C ...
- 牛客练习赛44 C 小y的质数 (数论,容斥定理)
链接:https://ac.nowcoder.com/acm/contest/634/C 来源:牛客网 题目描述 给出一个区间[L,R],求出[L,R]中孪生质数有多少对. 由于这是一个区间筛质数的模 ...
- 牛客练习赛44 B 小y的线段 (思维)
链接:https://ac.nowcoder.com/acm/contest/634/B 来源:牛客网 题目描述 给出n条线段,第i条线段的长度为a_ia i ,每次可以从第i条线段的j位置跳到第 ...
- 牛客练习赛44 A 小y的序列 (模拟,细节)
链接:https://ac.nowcoder.com/acm/contest/634/A 来源:牛客网 小y的序列 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语 ...
- 牛客练习赛48 D 小w的基站网络
链接:https://ac.nowcoder.com/acm/contest/923/D来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语言52428 ...
- 牛客练习赛40 A 小D的剧场 (思维dp)
链接:https://ac.nowcoder.com/acm/contest/369/A 题目描述 若你摘得小的星星 你将得到小的幸福 若你摘得大的星星 你将得到大的财富 若两者都能摘得 你将得到 ...
- 牛客练习赛40 C 小A与欧拉路(树的直径)
链接:https://ac.nowcoder.com/acm/contest/369/C 题目描述 小A给你了一棵树,对于这棵树上的每一条边,你都可以将它复制任意(可以为0)次(即在这条边连接的两个点 ...
随机推荐
- I Love GPLT
这道超级简单的题目没有任何输入. 你只需要把这句很重要的话 —— “I Love GPLT”——竖着输出就可以了. 所谓“竖着输出”,是指每个字符占一行(包括空格),即每行只能有1个字符和回车. 输入 ...
- 初识Redis,看这一篇就够了
环境的搭建和安装网上有很多教程,在这里就不再重复了. 1. Redis是什么? Redis(全称:Remote Dictionary Server 远程字典服务)是一个开源的使用ANSI C语言编写. ...
- 基于TensorFlow的MNIST手写数字识别-初级
一:MNIST数据集 下载地址 MNIST是一个包含很多手写数字图片的数据集,一共4个二进制压缩文件 分别是test set images,test set labels,training se ...
- BZOJ 3343 教主的魔法(分块)
题意: 有一个1e6的数组,t次操作:将[l,r]内的值增加w,或者查询[l,r]内的值大于等于add的 思路: 分块,块大小为sqrt(n),每次只需要暴力头尾两块,中间的整块打标记, 对于查询查操 ...
- HTML5与HTML4的区别-----文档结构
HTML5在结构和语法上做了大量的简化.当然,也提供了语义化的标签 结构上区别: 1.简化了文档声明语句 HTML5仅规定了一种: <!DOCTYPE html> 2. ...
- Shiro自动登录
Shiro RememberMe spring.xml <bean class="org.apache.shiro.web.mgt.DefaultWebSecurityManager& ...
- spring cloud微服务快速教程之(七) Spring Cloud Alibaba--nacos(一)、服务注册发现
0.前言 什么是Spring Cloud Alibaba? Spring Cloud Alibaba 是阿里开源的,致力于提供微服务开发的一站式解决方案.此项目包含开发分布式应用微服务的必需组件,方便 ...
- bootstrap4网格
Bootstrap 4 网格系统有以下 5 个类: .col- 针对所有设备 .col-sm- 平板 - 屏幕宽度等于或大于 576px .col-md- 桌面显示器 - 屏幕宽度等于或大于 768p ...
- 6.【Spring Cloud Alibaba】API网关-SpringCloudGateway
SpringCloud Gateway是什么?优缺点分析 springCloud Gateway优点 springCloud Gateway缺点 编写SpringCloundGateway pom.x ...
- C语言程序转汇编代码
最近在学着写bootloader,由于汇编太繁杂,希望可以使用C语言完成一部分,然后转成NASM汇编代码,经过摸索,最终找到了一个解决方案,记录于此,留作参考. 核心步骤 使用gcc编译得到.o文件 ...