Warm up

hdu4612:http://acm.hdu.edu.cn/showproblem.php?pid=4612

题意:给你一个无向连通图,问加上一条边后得到的图的最少的割边数;

题解:首先对原图求割边数,然后缩点之后建树,然后求树的直径。因为加上一条边,能消耗最大的割边就是树的直径。一道很好的模板题目。

 1 #pragma comment(linker,"/STACK:100000000,100000000")//阔栈的语句
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <vector>
 5 #define pb push_back
 6 using namespace std;
 7 const int maxn = 200002;
 8 const int maxm = 1000002;
 9 struct EDGE{
10     int next, to, vis;
11 }edge[maxm*2];//记录边,vis表示这一条边是否被访问
12 struct GEEDGE {
13     int u, to;
14 }geedge[maxm];//记录割边
15 int n,m,time,top,type,getot,cnt;//time是时间戳,type标记连通块,getot割边的数量,cnt边数
16 int head[maxn],st[maxn],dfn[maxn],low[maxn],belo[maxn];//belo[i],表示i属于哪个连通块
17 int start,ans1,ans,u,v;
18 void init(){
19     time=top=type=getot=cnt=0;
20     memset(head,-1,sizeof(head));
21     memset(dfn,0,sizeof(dfn));
22     memset(low,0,sizeof(low));
23     memset(belo,0,sizeof(belo));
24 }
25 void add(int u,int v){
26    edge[cnt].to=v;
27    edge[cnt].next=head[u];
28    edge[cnt].vis=0;
29    head[u]=cnt++;
30 }
31 void dfs(int u) {
32     low[u] = dfn[u] = ++time;
33     st[++top] = u;
34     for(int i = head[u];i != -1;i = edge[i].next) {
35         if(edge[i].vis)    continue;
36         edge[i].vis = edge[i^1].vis = 1;
37         int to = edge[i].to;
38         if(!dfn[to]) {
39             dfs(to);
40             low[u] = min(low[u], low[to]);
41             if(low[to] > dfn[u]) {//表示找到一个连通块
42                 type++;
43                 int v;
44                 do {
45                     v = st[top--];
46                     belo[v] = type;//表示v属于第type个连通块
47                 } while(v != to);
48                 geedge[++getot].u = u;//记录割边的起点
49                 geedge[getot].to = to;//记录割边的另一个点
50             }
51         }
52         else
53             low[u] = min(low[u], low[to]);
54     }
55 }
56 void DFS(int len,int fa,int now){//求树的直径
57     if(len>ans){
58         ans=len;
59         start=now;
60     }
61     for(int i=head[now];i!=-1;i=edge[i].next){
62         int v=edge[i].to;
63         if(v!=fa) DFS(len+1,now,v);
64     }
65 }
66 int main(){
67      while(~scanf("%d%d",&n,&m)&&n){
68          init();
69          for(int i=1;i<=m;i++){
70             scanf("%d%d",&u,&v);
71             add(u,v);
72             add(v,u);
73          }
74          for(int i=1;i<=n;i++)
75             if(!dfn[i])
76               dfs(i);
77          ans1=getot;
78          memset(head,-1,sizeof(head));cnt=0;//刷新边,接下来从新加边
79          for(int i=1;i<=ans1;i++){
80             add(belo[geedge[i].to],belo[geedge[i].u]);
81             add(belo[geedge[i].u],belo[geedge[i].to]);
82          }
83          ans = 0;
84          DFS(0,-1,1);//
85          ans=0;
86          DFS(0,-1,start);//两遍DFS求树的直径
87         printf("%d\n", getot-ans);
88      }
89 }

Warm up,布布扣,bubuko.com

时间: 2024-11-14 08:34:32

Warm up的相关文章

HDU 4612——Warm up——————【边双连通分量、树的直径】

Warm up Time Limit:5000MS     Memory Limit:65535KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4612 Description N planets are connected by M bidirectional channels that allow instant transportation. It's always possible to travel bet

Warm up 2

hdu4619:http://acm.hdu.edu.cn/showproblem.php?pid=4619 题意:题目大意:给你两种纸牌 ,一种水平放置共有n张 ,一种竖直放置共有m张.水平放置的纸牌占据点(x, y)和(x + 1 , y) , 竖直放置的纸牌占据点(x , y) 和 (x , y + 1).水平放置的牌之间不会重叠,竖直放置的牌之间也不会重叠,但是水平放置的牌和竖直放置的牌之间可能会重叠.让你拿走一些牌,使剩下的牌之间不会重叠并且数量最多,输出剩余的最大牌数. 题解:对于这

Hdu 4612 Warm up (双连通分支+数的直径)

题目链接: Hdu 4612 Warm up 题目描述: 给一个无向连通图,问加上一条边后,桥的数目最少会有几个? 解题思路: 题目描述很清楚,题目也很裸,就是一眼看穿怎么做的,先求出来双连通分量,然后缩点重新建图,用bfs求树的直径,直径的长度就是减去桥的数目. 这个题目需要手动扩展,而且手动扩展的话要用C++提交,G++re哭了. 1 #include <cstdio> 2 #include <queue> 3 #include <cstring> 4 #inclu

hdu 4612 Warm up 双连通缩点+树的直径

首先双连通缩点建立新图(顺带求原图的总的桥数,其实由于原图是一个强连通图,所以桥就等于缩点后的边) 此时得到的图类似树结构,对于新图求一次直径,也就是最长链. 我们新建的边就一定是连接这条最长链的首尾,这样就将原图的桥减少了直径个. #include<iostream> #include<cstring> #include<cstdio> #include<vector> #include<algorithm> #include<map&g

hdu 4612 Warm up (带有重边的无向图Tarjan+树的直径)

Warm up Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 3947    Accepted Submission(s): 892 Problem Description N planets are connected by M bidirectional channels that allow instant transport

hdu 4619 Warm up 2 (二分图)

Warm up 2 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 1754    Accepted Submission(s): 781 Problem Description Some 1×2 dominoes are placed on a plane. Each dominoe is placed either horizont

hdoj 4612 Warm up【双连通分量求桥&amp;&amp;缩点建新图求树的直径】

Warm up Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 5093    Accepted Submission(s): 1131 Problem Description N planets are connected by M bidirectional channels that allow instant transport

Warm neutral wind , GIU GIU 2014 autumn and winter clothing style atlas released

2013 for the evolution of men's is without a doubt a year of magnificent significance , not just by dark lines derived stamp out religious lines got a huge response , followed by neutralization of the garments out of the boom has also been an influx

HDU 4619 Warm up 2(最大流或二分匹配)

Warm up 2 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 1895    Accepted Submission(s): 862 Problem Description Some 1×2 dominoes are placed on a plane. Each dominoe is placed either horizont