题目链接: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. webpack入门系列1

    一.什么是webpack?为什么要使用它? Webpack 是一个前端资源加载/打包工具.它将根据模块的依赖关系进行静态分析,然后将这些模块按照指定的规则生成对应的静态资源. 为什么我们要使用它呢?因 ...

  2. Sqlite命令行基本操作

    SQLite是遵守ACID的关系数据库管理系统,它包含在一个相对小的C程序库中. 与许多其它数据库管理系统不同,SQLite不是一个客户端/服务器结构的数据库引擎,而是被集成在用户程序中. 1.进入命 ...

  3. 全局对象的构造函数会在main函数之前执行?

    #include <iostream> using namespace std; class CTest { public: CTest() { cout << "构 ...

  4. css实现渐变字体和流光字体

    这是段渐变文本 .text{ font-size: 30px; font-weight: bold; background-image: linear-gradient(#ed3f27, #9b099 ...

  5. 前端:CSS第四章第一节

    块级元素一行只有一个,比如P标签 CSS层叠样式表,意思就是样式是可以叠加的,比如下面的代码 <style> .ok{ color: aqua; } .blue{ color: #5283 ...

  6. 深度学习中的特征(feature)指的是什么?

    一般在machine learning意义上,我们常说的feature,是一种对数据的表达.当然,要衡量一种feature是否是合适的表达,要根据数据,应用,ML的模型,方法....很多方面来看.一般 ...

  7. 基于MXNet的im2rec.py的debug

    1.im2rec.py调试错误:multiprocessing not available, fall back to single threaded encoding imread 经过查找发现是程 ...

  8. Features for Multi-Target Multi-Camera Tracking and Re-identification论文解读

    解读一:Features for Multi-Target Multi-Camera Tracking and Re-identification Abstract MTMCT:从多个摄像头采集的视频 ...

  9. go net包简记

    TCP服务端 go语言中可以每次建立一次链接就创建一个goroutine去处理,使用goroutine实现并发非常方便和高效. TCP服务端程序的一般处理流程1.建立并绑定 Socket:首先服务端使 ...

  10. Go语言实现:【剑指offer】正则表达式匹配

    该题目来源于牛客网<剑指offer>专题. 请实现一个函数用来匹配包括 . 和 * 的正则表达式.模式中的字符.表示任意一个字符,而 * 表示它前面的字符可以出现任意次(包含0次). 在本 ...