首页 > 其他分享 >E. We Need More Bosses

E. We Need More Bosses

时间:2024-05-15 19:08:16浏览次数:9  
标签:int lst next depth Need Bosses now 节点 More

原题链接

题解

1.已知如果两个点之间有两条边不重合的路径,那么这两个点就在一个边强连通分量里,所以我们可以把处于同一个边强连通分量的点缩起来
在这里,我忘记了怎么求边强连通分量,所以我再提醒一下自己
已知树结构是不存在强连通分量的,它的特性是深度大的节点只有一条回到深度小的节点的边,所以我们深度搜索,对遇到的节点进行深搜顺序标记,记录每个节点能去的深序最小节点
假如遍历到当前节点 \(u\),已知其子节点能去的最小的深序节点比它还小,那说明它不是这个边强连通分量的深序最小点;
如果其子节点的最小深序节点等于它,那么代表它是这个边强连通分量的深序最小点

2.把边强连通分量缩点之后,由于保证联通的无向图,所以剩余节点一定是树
树中求两点最大距离等价于求树的直径,在这里我又忘记怎么求了,所以我再提醒一下自己
求树的直径:双dfs法,两次搜索深度最大的点
这里我证明为什么随便找一个点然后深搜深度最大的点一定是直径的端点
反证法:

code

// LUOGU_RID: 159070334
#include<bits/stdc++.h>
using namespace std;
int vis[300005]={0},lst[300005]={0};
int cnt=0;
stack<int> st;
vector<int> G[300005];
int belong[300005];
int tot=0;
void dfs(int now,int fa)
{
    vis[now]=++cnt;
    lst[now]=cnt;
    st.push(now);
    for(auto next:G[now])
    {
        if(next==fa) continue;
        if(vis[next]) lst[now]=min(vis[next],lst[now]);
        else
        {
            dfs(next,now);
            lst[now]=min(lst[now],lst[next]);
        }
    }
    if(vis[now]==lst[now])
    {
        int x;
        tot++;
        do
        {
            x=st.top();
            st.pop();
            belong[x]=tot;
        }while(x!=now);
    }
}


vector<int> E[300005];
int depth[300006]={0};
int root,maxd=-1;
void dfs1(int now,int fa)
{
    if(depth[now]>maxd)
    {
        maxd=depth[now];
        root=now;
    }
    for(auto next:E[now])
    {
        if(next==fa||depth[next]) continue;
        depth[next]=depth[now]+1;
        dfs1(next,now);
    }
}

int dfs2(int now,int fa)
{
    int ans=depth[now];
    for(auto next:E[now])
    {
        if(next==fa||depth[next]) continue;//由于建边的时候重复建边了
        depth[next]=depth[now]+1;
        ans=max(ans,dfs2(next,now));
    }
    return ans;
}
int main()
{

        int n,m;
        cin>>n>>m;
        for(int i=1;i<=m;i++)
        {
            int x,y;
            cin>>x>>y;
            G[x].push_back(y);
            G[y].push_back(x);
        }

        dfs(1,1);

        for(int i=1;i<=n;i++)
        {
            for(auto next:G[i])
            {
                if(belong[next]!=belong[i])
                {
                    E[belong[i]].push_back(belong[next]);
                    E[belong[next]].push_back(belong[i]);
                }
            }
        }


        dfs1(1,-1);
        memset(depth,0,sizeof depth);
        cout<<dfs2(root,-1);
    return 0;
}

标签:int,lst,next,depth,Need,Bosses,now,节点,More
From: https://www.cnblogs.com/pure4knowledge/p/18194542

相关文章

  • LeetCode 1287. Element Appearing More Than 25% In Sorted Array
    原题链接在这里:https://leetcode.com/problems/element-appearing-more-than-25-in-sorted-array/description/题目:Givenanintegerarray sorted innon-decreasingorder,thereisexactlyoneintegerinthearraythatoccursmorethan25%ofthetime,returnthat......
  • 经典译文:Transformer--Attention Is All You Need
    经典译文:Transformer--AttentionIsAllYouNeed来源  https://zhuanlan.zhihu.com/p/689083488 本文为Transformer经典论文《AttentionIsAllYouNeed》的中文翻译:https://arxiv.org/pdf/1706.03762.pdf注意力满足一切[email protected]......
  • SciTech-Mathmatics-ProbabilitiesAndStatistics-Distribution-is-all-you-need: 概率
    Distribution-is-all-you-need概率统计到深度学习,四大技术路线图谱,都在这里!https://github.com/graykode/distribution-is-all-you-need自然语言处理路线图:数学基础->语言基础->模型和算法项目作者:Tae-HwanJung,Github:graykode,2019-09-3013:35,选自Github自然......
  • openGauss 备机处于need-repair_WAL_状态问题
    备机处于needrepair(WAL)状态问题问题现象openGauss备机出现StandbyNeedrepair(WAL)故障。原因分析因网络故障、磁盘满等原因造成主备实例连接断开,主备日志不同步,导致数据库在启动时异常。处理分析通过gs_ctlbuild-D命令对故障节点进行重建,具体的操作方法请参见《工具......
  • linux6-touch&cat&more
    linux6-touch&cat&moretouch创建文件在/tmp目录下创建test.txt文件touch/tmp/test.txt填写多个参数创建多个文件touchtest1.txttest2.txttest3.txtcatcat,concatnate,查看文件内容查看/etc/目录下的service文件内容cat/etc/servicemore查看文件内容cat......
  • The "TypeScript Vue Plugin (Volar)" extension is no longer needed since v2. Plea
    这个报错信息表明你正在使用的是VisualStudioCode或者其他支持Volar的编辑器,而Volar是一个为Vue3应用提供TypeScript支持的工具。这个报错指出自从Volar版本2开始,"TypeScriptVue插件(Volar)"这个扩展就不再需要了。解决方法:如果你在使用的是VisualStudioCode编辑器,并且安装......
  • [BJDCTF 2020]YDSneedGirlfriend
    [BJDCTF2020]YDSneedGirlfriendUAF|所谓UAF漏洞是指程序在运行时通过悬空指针(悬空指针是指仍然指向已被释放内存空间的指针)访问已经被释放的内存.bamuwe@bamuwe:~/YDSneedGirlfriend$lddgirlfriendlinux-vdso.so.1(0x00007ffd09fec000)/home/bamuwe/pw......
  • json反序列化 JsonConvert.DeserializeObject 报错 One or more errors occurred. (U
    接口返回的字符串肉眼看起来正常,也是标准json,反序列化时候报错,字符串添加了UTF8-BOM头(windows记事本默认编码),可以通过以下代码移除标头//模拟json字符串对象varjsonStr="{}";byte[]buffer=Encoding.UTF8.GetBytes(jsonStr);varsResult=Encoding.UTF8.GetString......
  • 250 Stylized Mountain Cave Textures - Cliff Rock Crystal Gravel More
    250多种风格化的水晶、岩石、悬崖、砾石、矿石、熔岩和其他岩石纹理的集合,用于山地和洞穴风格化/幻想/rpg风格的游戏环境。在这个系列中,你会在风格化/幻想/rpg风格的游戏中找到大量适合山区和洞穴环境的纹理——水晶、洞穴地板/墙壁、岩石、悬崖、砾石、熔岩、岩石土、岩石地......
  • Randomness Is All You Need: Semantic Traversal of Problem-Solution Spaces with L
    本文是LLM系列文章,针对《RandomnessIsAllYouNeed:SemanticTraversalofProblem-SolutionSpaceswithLargeLanguageModels》的翻译。随机性就是你所需要的:具有大型语言模型的问题解决空间的语义遍历摘要1引言2相关工作3模型4算法5评估6实现7结论摘......