[JZOJ5465]道路重建--边双缩点+树的直径
题目链接
lueluelue
分析
这鬼题卡了我10发提交,之前做过一道类似的题目:https://rye-catcher.github.io/2018/07/09/luogu题解P2860-USACO冗杂路径-缩点-桥/
危险的边就是桥边,Tarjan求出边双后缩点整个图变成树,树边都是危险的边,我们需要加一条边构成一个新的ecc使危险的边最小
于是一开始naiive的以为求出所有叶子节点判一判就好了,于是WA*1
发现这个SB思路一看就是错的,又想到APIO 巡逻很像这道题,发现我们只要将树的直径两端点连起来一定是最优的,因为直径上的边都成为联通分量上的了就不是危险的边
于是求个树的直径就好了
结果发现求树的直径过程中会遍历一个环wtf?!虽然不知道为什么会有个环但加上个vis数组就没事了 WA*2
接着交一发95 最后一点 RE 了,这时候才发现边的范围1e6
改了一下又交了一发结果5分只过了最后一个点wtf?! 又是fread的锅 (NOIP都不敢用了)
然后终于A了这题...
代码
/*
code by RyeCatcher
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <utility>
#include <queue>
#include <vector>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <iostream>
#define DEBUG freopen("dat.in","r",stdin);freopen("wa.out","w",stdout);
#define FO(x) {freopen(#x".in","r",stdin);freopen(#x".out","w",stdout);}
#define ri register int
#define ll long long
#define ull unsigned long long
#define SIZE 1<<22
using std::min;
using std::max;
using std::priority_queue;
using std::queue;
using std::vector;
using std::pair;
using namespace __gnu_pbds;
inline char gc(){
static char buf[SIZE],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,SIZE,stdin),p1==p2)?EOF:*p1++;
}
template <class T>inline void read(T &x){
x=0;int ne=0;char c;
while((c=gc())>'9'||c<'0')ne=c=='-';x=c-48;
while((c=gc())>='0'&&c<='9')x=(x<<3)+(x<<1)+c-48;x=ne?-x:x;return ;
}
const int maxn=400005;
const int M=4000005;
const int inf=0x7fffffff;
struct Edge{
int ne,to;
}edge[M<<1];
int h[maxn],num_edge=1;
inline void add_edge(int f,int to){
edge[++num_edge].ne=h[f];
edge[num_edge].to=to;
h[f]=num_edge;
}
struct QAQ{
int ne,to;
}se[M<<1];
int sh[maxn],num_se=1;
inline void add_se(int f,int to){
se[++num_se].ne=sh[f];
se[num_se].to=to;
sh[f]=num_se;
}
int n,m;
int dfn[maxn],low[maxn],tot=0;
bool bri[M<<1];
int in_ecc[maxn],cnt=0;
void tarjan(int now,int id){
int v;
dfn[now]=low[now]=++tot;
for(ri i=h[now];i;i=edge[i].ne){
v=edge[i].to;
if(!dfn[v]){
tarjan(v,i);
low[now]=min(low[now],low[v]);
if(dfn[now]<low[v]){
bri[i]=bri[i^1]=1;
}
}
else if(id!=(i^1)){
low[now]=min(low[now],dfn[v]);
}
}
return;
}
bool vis[maxn];
void color(int now,int fa){
int v;
in_ecc[now]=cnt;
for(ri i=h[now];i;i=edge[i].ne){
v=edge[i].to;
if(bri[i]||in_ecc[v])continue;
color(v,now);
}
}
int rt,tmp;
void dfs1(int now,int fa,int dis){
int v;
vis[now]=1;
if(dis>tmp){
rt=now,tmp=dis;
}
for(ri i=sh[now];i;i=se[i].ne){
v=se[i].to;
if(v==fa||vis[v])continue;
dfs1(v,now,dis+1);
}
return ;
}
int main(){
int x,y;
FO(rebuild);
//freopen("rebuild01.in","r",stdin);
//freopen("rebuild01.ans","w",stdout);
while(scanf("%d %d",&n,&m)!=EOF&&(n+m)){
//printf("--%d %d--\n",n,m);
int S1=sizeof(bool)*(n+3),S2=sizeof(bool)*(m*2+3);
for(ri i=1;i<=n;i++){
h[i]=dfn[i]=sh[i]=in_ecc[i]=vis[i]=0;
}
memset(bri,0,S2);//清空桥边标记!!!
num_edge=num_se=1;
tot=cnt=0;
for(ri i=1;i<=m;i++){
read(x),read(y);
add_edge(x,y),add_edge(y,x);
}
for(ri i=1;i<=n;i++)if(!dfn[i])tarjan(i,0);
for(ri i=1;i<=n;i++)if(!in_ecc[i]){
cnt++;
color(i,0);
}
//printf("%d\n",cnt);
for(ri i=1;i<=n;i++){
x=in_ecc[i];
for(ri j=h[i];j;j=edge[j].ne){
y=in_ecc[edge[j].to];
if(x!=y){
add_se(x,y);
add_se(y,x);
}
}
}
rt=1,tmp=-1;
dfs1(1,0,0);
memset(vis,0,S1);
dfs1(rt,0,0);
//printf("%d\n",lef);
printf("%d\n",cnt-1-tmp);
//puts("wtf");
}
return 0;
}
[JZOJ5465]道路重建--边双缩点+树的直径的更多相关文章
- [JZOJ 5465] [NOIP2017提高A组冲刺11.9] 道路重建 解题报告 (e-dcc+树的直径)
题目链接: http://172.16.0.132/senior/#main/show/5465 题目: 小X所居住的X国共有n个城市,有m条无向道路将其连接.作为一个统一的国家,X 城的任意两个城市 ...
- HDU4612 Warm up 边双(重边)缩点+树的直径
题意:一个连通无向图,问你增加一条边后,让原图桥边最少 分析:先边双缩点,因为连通,所以消环变树,每一个树边都是桥,现在让你增加一条边,让桥变少(即形成环) 所以我们选择一条树上最长的路径,连接两端, ...
- HDU 4612 Warm up (边双连通分量+缩点+树的直径)
<题目链接> 题目大意:给出一个连通图,问你在这个连通图上加一条边,使该连通图的桥的数量最小,输出最少的桥的数量. 解题分析: 首先,通过Tarjan缩点,将该图缩成一颗树,树上的每个节点 ...
- hdu4612(双连通缩点+树的直径)
传送门:Warm up 题意:询问如何加一条边,使得剩下的桥的数目最少,输出数目. 分析:tarjan缩点后,重新建图得到一棵树,树上所有边都为桥,那么找出树的直径两个端点连上,必定减少的桥数量最多, ...
- Gym - 100676H H. Capital City (边双连通分量缩点+树的直径)
https://vjudge.net/problem/Gym-100676H 题意: 给出一个n个城市,城市之间有距离为w的边,现在要选一个中心城市,使得该城市到其余城市的最大距离最短.如果有一些城市 ...
- codeforces GYM 100114 J. Computer Network 无相图缩点+树的直径
题目链接: http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” ...
- [J]computer network tarjan边双联通分量+树的直径
https://odzkskevi.qnssl.com/b660f16d70db1969261cd8b11235ec99?v=1537580031 [2012-2013 ACM Central Reg ...
- HDU 4612——Warm up——————【边双连通分量、树的直径】
Warm up Time Limit:5000MS Memory Limit:65535KB 64bit IO Format:%I64d & %I64u Submit Stat ...
- hdu 4612 Warm up 有重边缩点+树的直径
题目链接 Warm up Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Tot ...
随机推荐
- MySQL实时监听——EPX 原创: 奕X 贝壳产品技术 今天
MySQL实时监听——EPX 原创: 奕X 贝壳产品技术 今天
- JAVA_SWT 事件的四种写法
一:匿名内部类写法 在一个组件下加入以下语句 text.addMouseListener(new MouseAdapter(){ public void mouseDoubleClich(MouseE ...
- VS中卸载Visual Assist X
Tools=>Extensions and updates=>找到Visual Assist X 卸载:
- VMware与宿主机同一网段
将VMware做为一个物理的虚拟机,设置网段与宿主机在同一子网.
- DBUtil内部实现过程解读
python数据库连接工具DBUtils DBUtils是一个允许在多线程python应用和数据库之间安全及高效连接的python模块套件. 模块 DBUtils套件包含两个模块子集,一个适用于兼容D ...
- 【JavaScript】使用定时器实现Js的延期执行或重复执行setTimeout,setInterval
使用定时器实现JavaScript的延期执行或重复执行 window对象提供了两个方法来实现定时器的效果,分别是window.setTimeout()和window.setInterval.其中前者可 ...
- 基于JAVA JWT 实现OATUH TOKEN验证
什么是jwt? 最详细的是官网:https://jwt.io/ 这里以java的ssm框架为例,集成jwt. 1.pom.xml 导入jwt的包 <!-- jwt --> <!-- ...
- Java使用Apache Commons Exec运行本地命令行命令
首先在pom.xml中添加Apache Commons Exec的Maven坐标: <!-- https://mvnrepository.com/artifact/org.apache.comm ...
- 优化apk的odex处理
前文讲到了apk的反编译流程,但在有的情况下会出现apk的资源文件和代码文件分开的情况,这种情况会出现如下目录结构,并且在apk文件里面并没有dex文件. 所需工具 smali.jar和baksmal ...
- Swagger API文档
Swagger API文档集中化注册管理 接口文档是前后端开发对接时很重要的一个组件.手动编写接口文档既费时,又存在文档不能随代码及时更新的问题,因此产生了像swagger这样的自动生成接口文档的 ...