[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 ...
随机推荐
- Tosca TestCases: Update all,Checkin all,Checkout,Checkout Tree
#记录一下TestCases Module 红框里面这几个button的功能 #Update all 从数据库里把别人最新checkin的东西给拽出来查看,也就是拿最新版本 #Checkin all ...
- 015-命令行下载安装brew
一.brew 1.安装Homebrew 安装命令: ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/inst ...
- word 2010中设置默认粘贴为 只保留文本粘贴【visio也适用于快捷键方式】
VISIO也适用如下方式: 3. 当然也可以直接是 Ctrl + Alt + V打开选择性粘贴选项卡 来选择其中某项来粘贴也是可以的 转: word 2010中设置默认粘贴为 只保留文本粘贴 2012 ...
- 算法习题---4-5IP网络(Uva1590)
一:题目 给出m(1到10000之间)个IP地址,求他们最小的网络号和子网掩码 (一)样例输入 3 表示要获取的IP地址个数 194.85.160.177 IP地址 194.85.160.183 19 ...
- Day9作业:socket之FTP工具
代码传的太累,直接发个github的链接吧! https://github.com/ccorzorz/Socketserver_FTP 上两张图给抛砖引玉下吧: 后台管理: FTP程序,包括客户端和s ...
- (五)Centos之目录处理命令
一.目录处理命令 linux中 关于目录 有几个重要概念:一个是 / 根目录 还有一个当前用户的家目录 比如 root用户的家目录是 /root ,普通用户的家目录是/home/xxx 下,如下图 ...
- spark报错:invalid token
启动spark报错,启动container失败,去看yarn的日志,显示invalid token, 经过排查是hadoop子节点的配置和主节点的配置不一致导致的,同步之后,问题解决.
- PostgreSQL学习笔记——窗口函数
在学习窗口函数之前,我们新建一个Product表并往其中插入一些数据: drop table if exists Product; create table Product ( product_id ...
- 【Leetcode_easy】788. Rotated Digits
problem 788. Rotated Digits solution1: class Solution { public: int rotatedDigits(int N) { ; ; i< ...
- 给.Net Core添加Swagger实现接口文档自动生成
1.添加Nuget相关引用 Swashbuckle.AspNetCore