题目链接: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 小雀和他的王国的更多相关文章

  1. 牛客练习赛56 B 小琛和他的学校

    题目链接:https://ac.nowcoder.com/acm/contest/3566/B 思路:一条路可把图分为左右两部分. l_ci, l_peo, r_ci, r_peo, w 分别为左边城 ...

  2. 牛客练习赛48 C 小w的糖果 (数学,多项式,差分)

    牛客练习赛48 C 小w的糖果 (数学,多项式) 链接:https://ac.nowcoder.com/acm/contest/923/C来源:牛客网 题目描述 小w和他的两位队友teito.toki ...

  3. 牛客练习赛48 A· 小w的a+b问题 (贪心,构造,二进制)

    牛客练习赛48 A· 小w的a+b问题 链接:https://ac.nowcoder.com/acm/contest/923/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C ...

  4. 牛客练习赛44 C 小y的质数 (数论,容斥定理)

    链接:https://ac.nowcoder.com/acm/contest/634/C 来源:牛客网 题目描述 给出一个区间[L,R],求出[L,R]中孪生质数有多少对. 由于这是一个区间筛质数的模 ...

  5. 牛客练习赛44 B 小y的线段 (思维)

    链接:https://ac.nowcoder.com/acm/contest/634/B 来源:牛客网 题目描述 给出n条线段,第i条线段的长度为a_ia i ​ ,每次可以从第i条线段的j位置跳到第 ...

  6. 牛客练习赛44 A 小y的序列 (模拟,细节)

    链接:https://ac.nowcoder.com/acm/contest/634/A 来源:牛客网 小y的序列 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语 ...

  7. 牛客练习赛48 D 小w的基站网络

    链接:https://ac.nowcoder.com/acm/contest/923/D来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语言52428 ...

  8. 牛客练习赛40 A 小D的剧场 (思维dp)

    链接:https://ac.nowcoder.com/acm/contest/369/A 题目描述 若你摘得小的星星 你将得到小的幸福  若你摘得大的星星 你将得到大的财富  若两者都能摘得 你将得到 ...

  9. 牛客练习赛40 C 小A与欧拉路(树的直径)

    链接:https://ac.nowcoder.com/acm/contest/369/C 题目描述 小A给你了一棵树,对于这棵树上的每一条边,你都可以将它复制任意(可以为0)次(即在这条边连接的两个点 ...

随机推荐

  1. C指针右左法则

    摘录的别人的:  C语言所有复杂的指针声明,都是由各种声明嵌套构成的.如何解读复杂指针声明呢?右左法则是一个既著名又常用的方法.不过,右左法则其实并不是C标准里面的内容,它是从C标准的声明规定中归纳出 ...

  2. data structure test

    1.设计算法,对带头结点的单链表实现就地逆置.并给出单链表的存储结构(数据类型)的定义. #include <iostream> #include <cstdlib> #inc ...

  3. Java使用反射实现根据字符串类名及参数创建对象

    要根据字符串创建对象,可以使用 Class.forName(String) 方法: 而要新建一个可以指定初始值参数的对象,就必须得使用 getConstructor(Class<T>... ...

  4. python 使用记录

    #print输出后不换行 #python2.x 中末尾加逗号,表示不换行,print 'contents', #python3.x 中默认print('contents',end='\n'),想要输出 ...

  5. ARTS Week 5

    Nov 25, 2019 ~ Dec 1, 2019 Algorithm 深度优先搜索--书籍分配 题目描述:有b1-b5五本书,要分配给五个学生,分别是a1-a5.但每个学生都有其喜欢的书,要检查是 ...

  6. ELK:收集Docker容器日志

    简介 之前写过一篇博客 ELK:日志收集分析平台,介绍了在Centos7系统上部署配置使用ELK的方法,随着容器化时代的到来,容器化部署成为一种很方便的部署方式,收集容器日志也成为刚需.本篇文档从 容 ...

  7. 【Java并发工具类】StampedLock:比读写锁更快的锁

    前言 ReadWriteLock适用于读多写少的场景,允许多个线程同时读取共享变量.但在读多写少的场景中,还有更快的技术方案.在Java 1.8中, 提供了StampedLock锁,它的性能就比读写锁 ...

  8. node基础 day1

    js为什么能在浏览器中运行 浏览器内部存在一个js解析器,解析ECMAscript 把引擎从浏览器中抽离出来,不再依赖浏览器,作为一个软件安装在电脑上,在命令行里面, 这个软件就是node node ...

  9. javascript原生js轮播图

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. Debian 10 安装无线网卡驱动 (rtl8822be)

    apt install firmware-realtek