cf 1000E We Need More Bosses

一 原题

E. We Need More Bosses time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output

Your friend is developing a computer game. He has already decided how the game world should look like — it should consist of nn locations connected by mm two-way passages. The passages are designed in such a way that it should be possible to get from any location to any other location.

Of course, some passages should be guarded by the monsters (if you just can go everywhere without any difficulties, then it's not fun, right?). Some crucial passages will be guarded by really fearsome monsters, requiring the hero to prepare for battle and designing his own tactics of defeating them (commonly these kinds of monsters are called bosses). And your friend wants you to help him place these bosses.

The game will start in location ss and end in location tt, but these locations are not chosen yet. After choosing these locations, your friend will place a boss in each passage such that it is impossible to get from ss to ttwithout using this passage. Your friend wants to place as much bosses as possible (because more challenges means more fun, right?), so he asks you to help him determine the maximum possible number of bosses, considering that any location can be chosen as ss or as tt.


The first line contains two integers nn and mm (2n31052≤n≤3⋅105n1m3105n−1≤m≤3⋅105) — the number of locations and passages, respectively.

Then mm lines follow, each containing two integers xx and yy (1x,yn1≤x,y≤nxyx≠y) describing the endpoints of one of the passages.

It is guaranteed that there is no pair of locations directly connected by two or more passages, and that any location is reachable from any other location.


Print one integer — the maximum number of bosses your friend can place, considering all possible choices for ssand tt.

Examples input Copy
5 5
1 2
2 3
3 1
4 1
5 2
output Copy
input Copy
4 3
1 2
4 3
3 2
output Copy

二 分析



三 代码
AUTHOR: maxkibble
LANG: c++
PROB: cf 1000E

#include cstdio
#include stack
#include vector

const int maxn = 3e5 + 5;

#define pb push_back

int n, m;
std::vectorint g[maxn];

void init() {
    scanf("%d%d", n, m);
    while (m--) {
        int s, e;
        scanf("%d%d", s, e);

int clk, dfu[maxn], low[maxn], sz, belong[maxn];
bool inStack[maxn];
std::stackint st;
std::vectorstd::vectorint  comp;

void tarjan(int u, int fa) {
    dfu[u] = low[u] = ++clk;
    inStack[u] = true;
    for (int v: g[u]) {
        if (v == fa) continue;
        if (!dfu[v]) {
            tarjan(v, u);
            low[u] = std::min(low[u], low[v]);
        } else {
            low[u] = std::min(low[u], dfu[v]);
    if (dfu[u] == low[u]) {
        std::vectorint v;
        while (true) {
            int hd =;
            inStack[hd] = false;
            belong[hd] = sz;
            if (hd == u) break;

std::vectorint g1[maxn];

void buildGraph() {
    for (int i = 1; i = n; i++) {
        for (int d: g[i]) {
            int s = belong[i], e = belong[d];
            if (s == e) continue;

int dis[maxn];

void dfs(int u, int fa) {
    dis[u] = dis[fa] + 1;
    for (int v: g1[u]) {
        if (v != fa) dfs(v, u);

int calcDiag() {
    dfs(1, 0);
    int furthest = 1;
    for (int i = 1; i = sz; i++) {
        if (dis[i]  dis[furthest]) furthest = i;
    dfs(furthest, 0);
    int ans = 0;
    for (int i = 1; i = sz; i++) ans = std::max(ans, dis[i]);
    return ans - 1;

void solve() {
    for (int i = 1; i = n; i++) {
        if (!dfu[i]) tarjan(i, 0);
    printf("%d\n", calcDiag());

int main() {
    return 0;

8 简首页