bzoj 1718: [Usaco2006 Jan] Redundant Paths 分离的路径
题意
给你一个无向图
问你最少添加多少条边可以使得他变成边双图
题解
直接双连通缩点
得到一颗树
然后答案是叶子节点/2向上取整
取法是每一次找两个LCA深度最小的叶子,两个连边就可以了
然后不知道为什么,我的统计入度为1的节点的写法,一直WA
对拍也不出事。。真的是一定是OJ的问题
最后改成FYC的暴力dfs写法才AC
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int N=20010;
int n,m;
struct qq{int x,y,last;}e[N*2];int num,last[N];
void init (int x,int y)
{num++;e[num].x=x;e[num].y=y;e[num].last=last[x];last[x]=num;
}
int low[N],dfn[N],id;
int sta[N],top=0;
int belong[N],cnt;
void dfs (int x,int fa)
{low[x]=dfn[x]=++id;sta[++top]=x;for (int u=last[x];u!=-1;u=e[u].last){int y=e[u].y;if (y==fa) continue;if (dfn[y]==-1){dfs(y,x);low[x]=min(low[x],low[y]);}else low[x]=min(low[x],dfn[y]);}if (low[x]==dfn[x]){cnt++;int i;do{i=sta[top--];belong[i]=cnt;}while (i!=x);}
}
int du[N];
int ans=0;
void dfs1 (int x,int fa)
{int tot=0;for (int u=last[x];u!=-1;u=e[u].last){int y=e[u].y;if (y==fa) continue;dfs1(y,x);tot++;}if (tot==0) ans++;if (x==belong[1]&&tot==1) ans++;
}
int main()
{memset(dfn,-1,sizeof(dfn));num=0;memset(last,-1,sizeof(last));scanf("%d%d",&n,&m);for (int u=1;u<=m;u++){int x,y;scanf("%d%d",&x,&y);init(x,y);init(y,x);}dfs(1,0);num=0;memset(last,-1,sizeof(last));for(int i=1;i<=m*2;i+=2){int x=e[i].x,y=e[i].y;if(belong[x]!=belong[y]) init(belong[x],belong[y]),init(belong[y],belong[x]);}if (num==0) {printf("0\n");return 0;}dfs1(belong[1],0);printf("%d\n",(ans+1)/2);return 0;
}
bzoj 1718: [Usaco2006 Jan] Redundant Paths 分离的路径相关推荐
- [BZOJ1718]:[Usaco2006 Jan] Redundant Paths 分离的路径(塔尖)
题目传送门 题目描述 为了从F个草场中的一个走到另一个,贝茜和她的同伴们有时不得不路过一些她们讨厌的可怕的树.奶牛们已经厌倦了被迫走某一条路,所以她们想建一些新路,使每一对草场之间都会至少有两条相互分 ...
- Redundant Paths 分离的路径(边双连通分量)
题干: 为了从F(1≤F≤5000)个草场中的一个走到另一个,贝茜和她的同伴们有时不得不路过一些她们讨厌的可怕的树.奶牛们已经厌倦了被迫走某一条路,所以她们想建一些新路,使每一对草场之间都会至少有两条 ...
- bzoj 1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会 -- Tarjan
1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会 Time Limit: 5 Sec Memory Limit: 64 MB Description The N (2 & ...
- bzoj 1656: [Usaco2006 Jan] The Grove 树木(BFS)
1656: [Usaco2006 Jan] The Grove 树木 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 246 Solved: 158 [ ...
- bzoj 1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会(Trajan)
1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 380 Solved: 2 ...
- bzoj 1655: [Usaco2006 Jan] Dollar Dayz 奶牛商店(高精度完全背包)
1655: [Usaco2006 Jan] Dollar Dayz 奶牛商店 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 599 Solved: 3 ...
- POJ3177 Redundant Paths
POJ3177 Redundant Paths 文章目录 Description 题意: 题解: 代码: Time Limit: 1000MS Memory Limit: 65536K Total S ...
- BZOJ 1662: [Usaco2006 Nov]Round Numbers 圆环数(数位DP+恶心细节)
BZOJ 1662: [Usaco2006 Nov]Round Numbers 圆环数 Time Limit: 5 Sec Memory Limit: 64 MB Description 正如你所知 ...
- [POJ3177]Redundant Paths(双联通)
在看了春晚小彩旗的E技能(旋转)后就一直在lol--额抽点时间撸一题吧-- Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Tota ...
最新文章
- 傅里叶变换的参考文档
- 作为互联网流量入口,CDN日志大数据你该怎么玩?
- Android笔记 simpleAdapter demo
- android在搭建框架时要注意,Android开发搭建应用框架步骤和注意的问题
- JS 中对象的简单创建和继承
- python 获取英文人名翻译
- windows搭建共享文件服务器,利用Windows共享搭建文件服务器
- 关于笔记本WLAN无线上网
- QR code二维码简介及Qrencode库的移植与使用
- matlab之创建图像轮廓图函数imcontour
- 她只用一个方法,就把英语拿下了!
- 黑群晖DSM安装教程及详细配置
- vue中怎么获取元素
- Unity DOTS学习导航
- B树B+树的原理和操作
- 华硕F80cr笔记本装win7旗舰版后杂音解决办法
- 在VB6 处理pdf 和jpg文件
- 美团点评携手巴黎欧莱雅沙龙专属发起“城市新青年”计划
- 解决Jmeter在Mac下崩溃、闪退
- 天越公众号系统粉丝海报裂变用法及介绍