题目大意:给一张无向图,要求找一对$s$和$t$,使得其路径上的割边是最多的,输出其数量。

题解:把边双缩点以后求树的直径。

卡点:

C++ Code:

#include <cstdio>
#include <cstring>
#define maxn 300010
#define maxm 300010 #define ONLINE_JUDGE
#define read() R::READ()
#include <cctype>
namespace R {
int x;
#ifdef ONLINE_JUDGE
char *ch, op[1 << 26];
inline void init() {
fread(ch = op, 1, 1 << 26, stdin);
}
inline int READ() {
while (isspace(*ch)) ch++;
for (x = *ch & 15, ch++; isdigit(*ch); ch++) x = x * 10 + (*ch & 15);
return x;
}
#else
char ch;
inline int READ() {
ch = getchar();
while (isspace(ch)) ch = getchar();
for (x = ch & 15, ch = getchar(); isdigit(ch); ch = getchar()) x = x * 10 + (ch & 15);
return x;
}
#endif
} int n, m;
inline int min(int a, int b) {return a < b ? a : b;} namespace Tree {
int head[maxn], cnt;
struct Edge {
int to, nxt;
} e[maxm << 1];
void add(int a, int b) {
e[++cnt] = (Edge) {b, head[a]}; head[a] = cnt;
e[++cnt] = (Edge) {a, head[b]}; head[b] = cnt;
} int q[maxn], h = 1, t = 0;
int d[maxn];
int bfs(int rt) {
memset(d, 0, sizeof d);
d[q[h = t = 0] = rt] = 1;
while (h <= t) {
int u = q[h++];
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if (!d[v]) {
d[v] = d[u] + 1;
q[++t] = v;
}
}
}
return q[t];
}
int work() {
int x = bfs(1), y = bfs(x);
return d[y] - 1;
}
}
namespace Graph {
int head[maxn], cnt;
struct Edge {
int from, to, nxt;
} e[maxm << 1];
void add(int a, int b) {
e[++cnt] = (Edge) {a, b, head[a]}; head[a] = cnt;
e[++cnt] = (Edge) {b, a, head[b]}; head[b] = cnt;
} int res[maxn], CNT;
int DFN[maxn], low[maxn], idx, fa[maxn];
int S[maxn], top;
void Tarjan(int u) {
DFN[u] = low[u] = ++idx;
S[++top] = u;
int v;
for (int i = head[u]; i; i = e[i].nxt) {
v = e[i].to;
if (v == fa[u]) continue;
if (!DFN[v]) {
fa[v] = u;
Tarjan(v);
low[u] = min(low[u], low[v]);
if (low[v] > DFN[u]) {
CNT++;
do res[v = S[top--]] = CNT; while (v != e[i].to);
}
} else low[u] = min(low[u], DFN[v]);
}
}
inline void tarjan(int n) {
for (int i = 1; i <= n; i++) if (!DFN[i]) Tarjan(i);
}
inline void init() {
for (int i = 1; i <= cnt; i += 2) {
if (res[e[i].from] != res[e[i].to]) Tree::add(res[e[i].from], res[e[i].to]);
}
}
} int main() {
#ifdef ONLINE_JUDGE
R::init();
#endif
n = read(), m = read();
for (int i = 1; i <= m; i++) Graph::add(read(), read());
Graph::tarjan(n);
Graph::init();
printf("%d\n", Tree::work());
return 0;
}

[CF1000E]We Need More Bosses的更多相关文章

  1. cf1000E We Need More Bosses (tarjan缩点+树的直径)

    题意:无向联通图,求一条最长的路径,路径长度定义为u到v必须经过的边的个数 如果把强联通分量都缩成一个点以后,每个点内部的边都是可替代的:而又因为这是个无向图,缩完点以后就是棵树,跑两遍dfs求直径即 ...

  2. 题解 CF1000E 【We Need More Bosses】

    这道题绝不是紫题... 题目的意思其实是让你求一个无向无重边图的直径. 对于求直径的问题我们以前研究过树的直径,可以两遍dfs或者两边bfs解决. 对于图显然不能这样解决,因为图上两点之间的简单路径不 ...

  3. CodeForces - 1000E :We Need More Bosses(无向图缩点+树的直径)

    Your friend is developing a computer game. He has already decided how the game world should look lik ...

  4. E - We Need More Bosses CodeForces - 1000E (tarjan缩点,树的直径)

    E - We Need More Bosses CodeForces - 1000E Your friend is developing a computer game. He has already ...

  5. We Need More Bosses CodeForces - 1000E(缩点 建图 求桥 求直径)

    题意: 就是求桥最多的一条路 解析: 先求连通分量的个数 然后缩点建图  求直径即可 #include <bits/stdc++.h> #define mem(a, b) memset(a ...

  6. 每日英语:Bosses May Use Social Media to Discriminate Against Job Seekers

    Many companies regularly look up job applicants online as part of the hiring process. A new study su ...

  7. CodeForces - 1000E We Need More Bosses

    题面在这里! 依然一眼题,求出割边之后把图缩成一棵树,然后直接求最长链就行了2333 #include<bits/stdc++.h> #define ll long long using ...

  8. Educational Codeforces Round 46 (Rated for Div. 2) E. We Need More Bosses

    Bryce1010模板 http://codeforces.com/contest/1000/problem/E 题意: 给一个无向图,求图的最长直径. 思路:对无向图缩点以后,求图的最长直径 #in ...

  9. We Need More Bosses CodeForces - 1000E (无向图缩点)

    大意: 给定无向连通图, 定义两个点$s,t$个价值为切断一条边可以使$s,t$不连通的边数. 求最大价值. 显然只有桥会产生贡献. 先对边双连通分量缩点建树, 然后求直径即为答案. #include ...

随机推荐

  1. Linq to Entity 时间差作为筛选条件产生的问题

    前言 在使用 Linq to Entity 的時候,會把之前 Linq to SQL 的想法就帶進去,寫好之後編譯也都不會出錯,但是實際上在跑的時候就會出現錯誤訊息了,這點真的要注意了.這次我遇到問題 ...

  2. STL 之 set的应用

    关于set Set是STL中的一个容器,特点是其中包含的元素值是唯一的,set根据其底层实现机制分为hash存储和红黑树存储两种方式,这两种结构最本质的区别就是有序和无序,红黑树的存储是有序的而has ...

  3. linux数据库copy方法

    相信大多数程序员都会遇到数据库copy的问题,下面就总结几种常见的方法,针对有mysql基础的同学参考 方法一:利用sqlyog的copy database的功能,如图 这种最简单,速度比较慢: 方法 ...

  4. 【Mysql】给mysql配置远程登录

    grant all privileges on 库名.表名 to '用户名'@'IP地址' identified by '密码' with grant option; flush privileges ...

  5. 【转载】char*,const char*和string 三者转换

    本文转自 http://blog.csdn.net/perfumekristy/article/details/7027678 const char* 和string 转换 const char*转换 ...

  6. Linux中的代码编辑器vim

    Vim的三种工作模式 命令行模式 插入模式 底行模式 Vim 的命令行模式 命令行模式是进入vim后的初始模式,在该模式下主要是使用方向键来移动光标的位置,并通过相应的命令来进行文字的编辑. 切换方法 ...

  7. MySQL 获取物理表的主键字段

    参考代码: /** * 获取主键字段 * @param $table * @param $database * @return mixed */ public function get_primary ...

  8. 2019-04-11 python入门学习——配置机器及搭建开发环境

    # 在windows操作系统中搭建python 3.x版本的开发环境,开发工具为 Anaconda 3. # 1.1 下载及安装Anaconda 3 Anaconda的特点:集成性高,包含很多常用的开 ...

  9. Hacker Cups and Balls Gym - 101234A 二分+线段树

    题目:题目链接 题意:有编号从1到n的n个球和n个杯子. 每一个杯子里有一个球, 进行m次排序操作,每次操作给出l,r. 如果l<r,将[l,r]范围内的球按升序排序, 否则降序排, 问中间位置 ...

  10. PHP.35-TP框架商城应用实例-后台11-商品分类-删除分类(2种方法)、添加、修改

    删除分类 删除一个分类的同时,其所有子分类都删除 在控制器CategoryCtroller.class.php中添加删除函数(delete) 在分类模型中添加钩子函数_before_delete()[ ...