codeforce 1000 E. We Need More Bosses

本文介绍了一种在游戏世界中优化Boss布局的方法,通过强连通分量缩点找到树的直径来确定最大数量的Boss位置,确保从起点到终点路径上的挑战最大化。
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 tt without 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.

Input

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.

Output

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


思路:
强连通分量缩点,找树的直径。
代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn=3e5+10;
vector<int>G[maxn],mp[maxn];
int dfn[maxn],low[maxn],fa[maxn],d[maxn];
int tol=0,top=0;
bool vis[maxn];
stack<int>P;
void tarjan(int v,int Fa)
{
    dfn[v]=low[v]=++tol;
    P.push(v);
    for(int i=0;i<G[v].size();i++)
    {
        int to=G[v][i];
        if(to==Fa)continue;
        if(!dfn[to])
        {
            tarjan(to,v);
            low[v]=min(low[v],low[to]);
        }
        else low[v]=min(low[v],dfn[to]);
    }
    if(low[v]==dfn[v])
    {
        top++;int s;
        do
        {
            s=P.top();P.pop();
            fa[s]=top;
        }
        while(s!=v);
    }
}
void bfs(int st)
{
    memset(vis,0,sizeof(vis));
    queue<int>P;
    P.push(st);vis[st]=1; d[st]=0;
    while(!P.empty())
    {
        int v=P.front();P.pop();
        for(int i=0;i<mp[v].size();i++)
        {
            int to=mp[v][i];
            if(vis[to])continue;
            d[to]=d[v]+1;
            P.push(to);
            vis[to]=1;
        }
    }
}
int main()
{
    int n,m;scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
    {
        int x,y;scanf("%d%d",&x,&y);
        G[x].push_back(y);
        G[y].push_back(x);
    }
    tarjan(1,-1);
    for(int i=1;i<=n;i++)
    {
        for(int j=0;j<G[i].size();j++)
        {
            int to=G[i][j];
            if(fa[i]!=fa[to])
                mp[fa[i]].push_back(fa[to]);
        }
    }
    bfs(1);
    int id=1;
    for(int i=2;i<=top;i++)
        if(d[i]>d[id])id=i;
    bfs(id);
    int ans=0;
    for(int i=1;i<=top;i++)
        ans=max(ans,d[i]);
    printf("%d\n",ans);
    return 0;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值