hdu 4612 Warm up 桥缩点
题目:给出一个图,添加一条边之后,问能够在新图中得到的最少的桥的数量。
分析:我们可以双联通分量进行缩点,原图变成了一棵树。问题变成了:求树中添加一条边之后,使得不在圈的边最少。显然求一边直径,用总边数减掉最长路上的边数就是答案。注意数据存在重边的情况。
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; typedef long long ll;
typedef unsigned long long ull; #define debug puts("here")
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define foreach(i,vec) for(unsigned i=0;i<vec.size();i++)
#define pb push_back
#define RD(n) scanf("%d",&n)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define RD4(x,y,z,w) scanf("%d%d%d%d",&x,&y,&z,&w)
#define All(vec) vec.begin(),vec.end()
#define MP make_pair
#define PII pair<int,int>
#define PQ priority_queue
#define cmax(x,y) x = max(x,y)
#define cmin(x,y) x = min(x,y)
#define Clear(x) memset(x,0,sizeof(x))
/* #pragma comment(linker, "/STACK:1024000000,1024000000") int size = 256 << 20; // 256MB
char *p = (char*)malloc(size) + size;
__asm__("movl %0, %%esp\n" :: "r"(p) ); */ char IN;
bool NEG;
inline void Int(int &x){
NEG = 0;
while(!isdigit(IN=getchar()))
if(IN=='-')NEG = 1;
x = IN-'0';
while(isdigit(IN=getchar()))
x = x*10+IN-'0';
if(NEG)x = -x;
}
inline void LL(ll &x){
NEG = 0;
while(!isdigit(IN=getchar()))
if(IN=='-')NEG = 1;
x = IN-'0';
while(isdigit(IN=getchar()))
x = x*10+IN-'0';
if(NEG)x = -x;
} /******** program ********************/ const int MAXN = 200005;
const int MAXM = 1000005; vector<int> adj[MAXN];
int po[MAXN],tol;
int low[MAXN],dfn[MAXN],tim;
bool use[MAXN];
int fa[MAXN];
int id[MAXN],ha[MAXN]; struct Edge{
int y,id,next;
}edge[MAXM<<1]; inline void add(int x,int y,int i){
edge[++tol].y = y;
edge[tol].id = i;
edge[tol].next = po[x];
po[x] = tol;
} int findSet(int x){
if(x!=fa[x])
fa[x] = findSet(fa[x]);
return fa[x];
} void dfs(int x,int fid){
low[x] = dfn[x] = ++ tim;
for(int i=po[x];i;i=edge[i].next){
int y = edge[i].y;
int id = edge[i].id;
if(id==fid)continue;
if(!dfn[y]){
dfs(y,id);
cmin( low[x],low[y] );
if(low[y]<=dfn[x]){
int px = findSet(x);
int py = findSet(y);
if(px!=py)fa[px] = py;
}
}else
cmin( low[x],dfn[y] );
}
} int MAX,root; void dfsR(int x,int sz){
use[x] = true;
if(sz>MAX){
MAX = sz;
root = x;
}
foreach(i,adj[x])
if(!use[adj[x][i]])
dfsR(adj[x][i],sz+1);
} int main(){ #ifndef ONLINE_JUDGE
freopen("1002.in","r",stdin);
//freopen("sum.out","w",stdout);
#endif int size = 256 << 20; // 256MB
char *p = (char*)malloc(size) + size;
__asm__("movl %0, %%esp\n" :: "r"(p) ); int x,y,n,m;
while(1){
Int(n);Int(m);
if(!n&&!m)break; Clear(po);
tol = 0;
rep1(i,m){
Int(x);Int(y);
add(x,y,i);
add(y,x,i);
} rep1(i,n){
adj[i].clear();
fa[i] = i;
}
Clear(dfn);
tim = 0;
rep1(i,n)
if(!dfn[i])
dfs(i,0); Clear(id);
int tot = 0;
rep1(i,n){
int px = findSet(i);
if(!id[px])id[px] = ++tot;
ha[i] = id[px];
}
rep1(x,n){
int px = ha[x];
for(int i=po[x];i;i=edge[i].next){
int py = ha[ edge[i].y ];
if(px==py)continue;
adj[px].pb(py);
adj[py].pb(px);
}
} Clear(use);
MAX = 0;
dfsR(1,1); Clear(use);
MAX = 0;
dfsR(root,1); printf("%d\n",tot-MAX);
} return 0;
}
hdu 4612 Warm up 桥缩点的更多相关文章
- HDU 4612 Warm up tarjan缩环+求最长链
Warm up Problem Description N planets are connected by M bidirectional channels that allow instant ...
- hdu 4612 Warm up(缩点+树上最长链)
本来就是自己负责图论,结果水了= = 题目其实很裸,就是求桥的数量,只是要新加上一条边罢了.做法:先缩点.再在树上搜最长链(第一场多校的hdu 4607Park Visit就考了最长链,小样,套个马甲 ...
- HDU 4612 Warm up 连通图缩点
题目大意:给出一个连通图,求再一个边后,剩余的最少桥数. 题目思路:首先进行缩点得到重构后的图,求出重构后树的直径(通过两次BFS求出相距最远的两点间的距离),ans=重构图边数-树的直径 //#pr ...
- HDU 4612 Warm up —— (缩点 + 求树的直径)
题意:一个无向图,问建立一条新边以后桥的最小数量. 分析:缩点以后,找出新图的树的直径,将这两点连接即可. 但是题目有个note:两点之间可能有重边!而用普通的vector保存边的话,用v!=fa的话 ...
- Hdu 4612 Warm up (双连通分支+树的直径)
题目链接: Hdu 4612 Warm up 题目描述: 给一个无向连通图,问加上一条边后,桥的数目最少会有几个? 解题思路: 题目描述很清楚,题目也很裸,就是一眼看穿怎么做的,先求出来双连通分量,然 ...
- HDU 4612——Warm up——————【边双连通分量、树的直径】
Warm up Time Limit:5000MS Memory Limit:65535KB 64bit IO Format:%I64d & %I64u Submit Stat ...
- hdu 4612 Warm up
http://acm.hdu.edu.cn/showproblem.php?pid=4612 将原图进行缩点 变成一个树 树上每条边都是一个桥 然后加一条边要加在树的直径两端才最优 代码: #incl ...
- hdu 4612 Warm up 有重边缩点+树的直径
题目链接 Warm up Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Tot ...
- hdu 4612 Warm up 双连通缩点+树的直径
首先双连通缩点建立新图(顺带求原图的总的桥数,事实上因为原图是一个强连通图,所以桥就等于缩点后的边) 此时得到的图类似树结构,对于新图求一次直径,也就是最长链. 我们新建的边就一定是连接这条最长链的首 ...
随机推荐
- Redis本地环境搭建
Windows 下环境搭建 1. 设置hosts set duapphosts=127.0.0.1 sqld.duapp.com set redisduapphosts=127.0.0.1 redis ...
- Java浮点值拒绝服务漏洞危害分析
By 空虚浪子心 http://www.inbreak.net/ JAVA出了漏洞,CVE-2010-4476,会导致拒绝服务攻击.大家能从公告上,看到这样一段代码,挺长的.意思是只有开发人员写出这样 ...
- IOS 7 Study - Displaying an Image on a Navigation Bar
ProblemYou want to display an image instead of text as the title of the current view controlleron th ...
- CodeForces 164 B. Ancient Berland Hieroglyphs 单调队列
B. Ancient Berland Hieroglyphs 题目连接: http://codeforces.com/problemset/problem/164/B Descriptionww.co ...
- hdu 5277 YJC counts stars 暴力
YJC counts stars Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php? ...
- 分享 Java微信开发SDK
分享 Java微信开发SDK •发布于 4周前 •作者 朋也 •432 次浏览 •最后一次编辑是 2周前 •来自 分享 给大家分享两个java开发微信公众号的sdk jfinal-weixin ...
- TP复习13
## ThinkPHP 3.1.2 模板的使用技巧#讲师:赵桐正微博:http://weibo.com/zhaotongzheng 本节课大纲:一.模板包含 <include file=&quo ...
- document.createElement("A");
搞了一天,终于把A里面的属性弄出来 代码1: <BODY></BODY><SCRIPT LANGUAGE="JavaScript"><!- ...
- 翻译学python---《Learn Python the hard Way》---第一章 绪论
打算学习python,但是又不想单纯地看书或是写个小项目,干脆引入很流行的翻译学习法来学习吧- 在论坛上看到了国外的一本<Learn Python the hard Way> ...
- Spring MVC 3.0 深入
核心原理: . 用户发送请求给服务器.url:user.do . 服务器收到请求.发现DispatchServlet可以处理.于是调用DispatchServlet. . DispatchServle ...