题目链接: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. 20190708三人开黑CF模拟赛

    7月8号晚上8点和两位巨佬开了一场虚拟cf: [Helvetic Coding Contest 2018 online mirror (teams allowed, unrated)] 我这么蔡,只A ...

  2. Magicodes.IE 2.0发布

    Magicodes.IE 2.0发布 Magicodes.IE是我们维护的开源的导入导出通用库,去年年底已加入NCC开源组织. Github地址:https://github.com/xin-lai/ ...

  3. qt客户端程序使用svg图片资源的几种方法

    直接使用svg格式文件资源的情况 1. 直接在UI控件属性面板中选择部分支持icon图标的控件的icon来源,这样图标可以显示 2.给toolbutton添加样式 qproperty-icon: ur ...

  4. Go操作MongoDB

    mongoDB是目前比较流行的一个基于分布式文件存储的数据库,它是一个介于关系数据库和非关系数据库(NoSQL)之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的. mongoDB介绍 mon ...

  5. Why all application lack a kind of most really charm ?

    Website and APP we used now are mostly web2.0 applications. While people practise in use, they can n ...

  6. css 浏览兼容问题及解决办法 (1)

    主流浏览器css兼容问题的总结 最近又搞了一波网站的兼容,由于要求ie浏览器还是要兼容到ie8,所以调起来还是各种蛋疼. 现在就post一些做兼容的总结,可能不够全面,但是可以告诉大家如何避过一些坑. ...

  7. 一起了解 .Net Foundation 项目 No.2

    .Net 基金会中包含有很多优秀的项目,今天就和笔者一起了解一下其中的一些优秀作品吧. 中文介绍 中文介绍内容翻译自英文介绍,主要采用意译.如与原文存在出入,请以原文为准. ASP.NET MVC, ...

  8. css3 3d api

    perspective 人看东西的距离 perspective: 500; 越小3d效果越好 perspective-origin观察视点.此处默认为视图的中心点 transform-origin:2 ...

  9. docker:搭建ELK 开源日志分析系统

    ELK 是由三部分组成的一套日志分析系统, Elasticsearch: 基于json分析搜索引擎,Elasticsearch是个开源分布式搜索引擎,它的特点有:分布式,零配置,自动发现,索引自动分片 ...

  10. CentOS7时区和时间设置

    [root@saltstack-master ~]# timedatectl set-timezone Asia/Shanghai [root@saltstack-master ~]# ln -sf ...