CodeForces - 1000E :We Need More Bosses(无向图缩点+树的直径)
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 (2≤n≤3⋅1052≤n≤3⋅105, n−1≤m≤3⋅105n−1≤m≤3⋅105) — the number of locations and passages, respectively.
Then mm lines follow, each containing two integers xx and yy (1≤x,y≤n1≤x,y≤n, x≠yx≠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.
Examples
5 5
1 2
2 3
3 1
4 1
5 2
2
4 3
1 2
4 3
3 2
3
题意:一条路径上必经的边为关键边,现在让你找一条路径,使得其关键边最多,输出最多的数量。
思路:如果一条路径上面有环,那么这个环的任意一条边都不是关键边。所以我们缩点,那么就算在一棵树上找最多的关键边,显然就算求直径。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=;
int From[maxn],Laxt[maxn],To[maxn<<],Next[maxn<<],cnt;
int low[maxn],dfn[maxn],times,q[maxn],head,scc_cnt,scc[maxn];
vector<int>G[maxn];
int dis[maxn],S,T,ans;
void add(int u,int v)
{
Next[++cnt]=Laxt[u]; From[cnt]=u;
Laxt[u]=cnt; To[cnt]=v;
}
void tarjan(int u,int fa)
{
dfn[u]=low[u]=++times;
q[++head]=u;
for(int i=Laxt[u];i;i=Next[i]){
if(To[i]==fa) continue;
if(!dfn[To[i]]) {
tarjan(To[i],u);
low[u]=min(low[u],low[To[i]]);
}
else low[u]=min(low[u],dfn[To[i]]);
}
if(low[u]==dfn[u]){
scc_cnt++;
while(true){
int x=q[head--];
scc[x]=scc_cnt;
if(x==u) break;
}
}
}
void dfs(int u,int f)
{
dis[u]=dis[f]+;
for(int i=;i<G[u].size();i++){
if(G[u][i]!=f) dfs(G[u][i],u);
}
}
int main()
{
int N,M,u,v,i,j;
scanf("%d%d",&N,&M);
for(i=;i<=M;i++){
scanf("%d%d",&u,&v);
add(u,v); add(v,u);
}
tarjan(,);
for(i=;i<=N;i++){
for(j=Laxt[i];j;j=Next[j]){
if(scc[i]!=scc[To[j]])
G[scc[i]].push_back(scc[To[j]]);
}
}
dfs(,);
for(i=;i<=scc_cnt;i++) if(dis[i]>dis[S]) S=i;
dfs(S,);
for(i=;i<=scc_cnt;i++) ans=max(ans,dis[i]-);
printf("%d\n",ans);
return ;
}
对于无向图的缩点:
void tarjan(int u,int fa)
{
dfn[u]=low[u]=++times;
q[++head]=u;
for(int i=Laxt[u];i;i=Next[i]){
if(To[i]==fa) continue;
if(!dfn[To[i]]) {
tarjan(To[i],u);
low[u]=min(low[u],low[To[i]]);
}
else low[u]=min(low[u],dfn[To[i]]);
}
if(low[u]==dfn[u]){
scc_cnt++;
while(true){
int x=q[head--];
scc[x]=scc_cnt;
if(x==u) break;
}
}
}
对于有向图的缩点:二者唯一的区别就算有向图考虑横边,所以有个instack的判断。
void tarjan(int u)
{
instk[u]=;
q[++head]=u;
dfn[u]=low[u]=++times;
for(int i=Laxt[u];i;i=Next[i]){
int v=To[i];
if(!dfn[v]) {
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(instk[v])low[u]=min(low[u],dfn[v]);//无向图与有向图的区别
}
if(dfn[u]==low[u]){
scc_cnt++;
while(true){
int x=q[head--];
scc[x]=scc_cnt;
V[scc_cnt]+=w[x];
instk[x]=;
if(x==u) break;
}
}
}
CodeForces - 1000E :We Need More Bosses(无向图缩点+树的直径)的更多相关文章
- hdu-4612(无向图缩点+树的直径)
题意:给你n个点和m条边的无向图,问你如果多加一条边的话,那么这个图最少的桥是什么 解题思路:无向图缩点和树的直径,用并查集缩点: #include<iostream> #include& ...
- cf1000E We Need More Bosses (tarjan缩点+树的直径)
题意:无向联通图,求一条最长的路径,路径长度定义为u到v必须经过的边的个数 如果把强联通分量都缩成一个点以后,每个点内部的边都是可替代的:而又因为这是个无向图,缩完点以后就是棵树,跑两遍dfs求直径即 ...
- Codeforces 1000E We Need More Bosses (边双连通+最长链)
<题目链接> 题目大意:给定一个$n$个节点$m$条边的无向图,问你对任意两点,最多有多少条特殊边,特殊边指删除这条边后,这两个点不能够到达. 解题分析: 特殊变其实就是指割边,题意就是问 ...
- codeforces GYM 100114 J. Computer Network 无相图缩点+树的直径
题目链接: http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” ...
- Codeforces 734E Anton and Tree(缩点+树的直径)
题目链接: Anton and Tree 题意:给出一棵树由0和1构成,一次操作可以将树上一块相同的数字转换为另一个(0->1 , 1->0),求最少几次操作可以把这棵数转化为只有一个数字 ...
- HDU4612 Warm up 边双(重边)缩点+树的直径
题意:一个连通无向图,问你增加一条边后,让原图桥边最少 分析:先边双缩点,因为连通,所以消环变树,每一个树边都是桥,现在让你增加一条边,让桥变少(即形成环) 所以我们选择一条树上最长的路径,连接两端, ...
- hdu4612 Warm up 缩点+树的直径
题意抽象后为:给定一个无向图 问添加一条边的情况下最少能有多少个桥. 桥的定义:删除该边后原图变为多个连通块. 数据规模:点数N(2<=N<=200000),边数M(1<=M< ...
- F - Warm up HDU - 4612 tarjan缩点 + 树的直径 + 对tajan的再次理解
题目链接:https://vjudge.net/contest/67418#problem/F 题目大意:给你一个图,让你加一条边,使得原图中的桥尽可能的小.(谢谢梁学长的帮忙) 我对重边,tarja ...
- HDU4612:Warm up(缩点+树的直径)
Warm up Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Su ...
随机推荐
- .NET面试题汇总
目录 本次给大家介绍的是我收集以及自己个人保存一些.NET面试题 简介 1.C# 值类型和引用类型的区别 2.如何使得一个类型可以在foreach 语句中使用 3.sealed修饰的类有什么特点 4. ...
- linux配置nfs步骤及心得
这节我们介绍NFS的相关概念,以及怎样配置NFS和在client中查看NFS. NFS的配置过程非常easy. 在server端中编辑/etc/exports文件,加入例如以下内容: ...
- scrapy递归抓取网页数据
scrapy spider的parse方法能够返回两种值:BaseItem.或者Request.通过Request能够实现递归抓取. 假设要抓取的数据在当前页,能够直接解析返回item(代码中带**凝 ...
- HDFS源码分析数据块复制监控线程ReplicationMonitor(一)
ReplicationMonitor是HDFS中关于数据块复制的监控线程,它的主要作用就是计算DataNode工作,并将复制请求超时的块重新加入到待调度队列.其定义及作为线程核心的run()方法如下: ...
- 32.10 使用模板更改控件的UI
32.10 使用模板更改控件的UI 样式是改变WPF控件基本外形的非常好(且非常简单)的方式,它通过为窗口部件的特性设置建立一组默认的值,从而改变WPF控件的基本外形.但是,即使样式允许我们改变各种 ...
- python 基础 4.0 函数的一般形式及传参
#/usr/bin/python #coding=utf-8 #@Time :2017/10/23 15:58 #@Auther :liuzhenchuan #@File :函数的一般形式.p ...
- XShell 连接 vm虚拟机中的redhat Linux系统
选择的是nat链接,因为nat链接是没有网络的情况下,也是可以链接操作的,当然bridge也可以,那我就从第一步开始; 因为有的人可能改过电脑上的虚拟适配器的ip地址,导致和虚拟机默认的不一样了.如果 ...
- 九度OJ 1166:迭代求立方根 (迭代)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3695 解决:1700 题目描述: 立方根的逼近迭代方程是 y(n+1) = y(n)*2/3 + x/(3*y(n)*y(n)),其中y0 ...
- Kubernetes TensorFlow 默认 特定 集群管理器
Our goal is to foster an ecosystem of components and tools that relieve the burden of running applic ...
- Android笔记之ViewModel的使用示例
依赖 implementation 'android.arch.lifecycle:extensions:1.1.1' implementation 'com.squareup.retrofit2:r ...