hdu4612(双连通缩点+树的直径)

传送门:Warm up

题意:询问如何加一条边,使得剩下的桥的数目最少,输出数目。

分析:tarjan缩点后,重新建图得到一棵树,树上所有边都为桥,那么找出树的直径两个端点连上,必定减少的桥数量最多,因此ans=树的边数-树的直径。

#pragma comment(linker,"/STACK:1024000000,1024000000")
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define mod 100000000
#define inf 0x3f3f3f3f
#define eps 1e-6
#define N 200010
#define FILL(a,b) (memset(a,b,sizeof(a)))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define PII pair<int,int>
using namespace std;
struct edge
{
    int v,next;
    edge(){}
    edge(int v,int next):v(v),next(next){}
}e[N*10],e2[N*10];
int n,step,scc,top,tot;
int head[N],dfn[N],low[N],belong[N],Stack[N];
bool instack[N],vis[N*10];
vector<int>g[N];
void init()
{
    tot=0;top=0;scc=0;
    FILL(head,-1);FILL(dfn,0);
    FILL(low,0);FILL(instack,false);
    FILL(vis,false);
}
void addedge(int u,int v)
{
    e[tot]=edge(v,head[u]);
    head[u]=tot++;
}
void tarjan(int u)
{
    int v;
    dfn[u]=low[u]=++step;
    Stack[top++]=u;
    instack[u]=true;
    int pre_num=0;
    for(int i=head[u];~i;i=e[i].next)
    {
        v=e[i].v;
        if(vis[i])continue;
        vis[i]=vis[i^1]=true;
        if(!dfn[v])
        {
            tarjan(v);
            if(low[u]>low[v])low[u]=low[v];
        }
        else if(instack[v])
        {
            if(low[u]>dfn[v])low[u]=dfn[v];
        }
    }
    if(dfn[u]==low[u])
    {
        scc++;
        do
        {
            v=Stack[--top];
            instack[v]=false;
            belong[v]=scc;
        }while(v!=u);
    }
}
int tree_len,x;
void dfs_tree(int u,int f,int d)
{
    if(d>=tree_len)tree_len=d,x=u;
    for(int i=0,sz=g[u].size();i<sz;i++)
    {
        int v=g[u][i];
        if(v==f)continue;
        dfs_tree(v,u,d+1);
    }
}
void solve()
{
    for(int i=1;i<=n;i++)
        if(!dfn[i])tarjan(i);
    for(int i=1;i<=n;i++)g[i].clear();
    for(int u=1;u<=n;u++)
    {
        for(int i=head[u];~i;i=e[i].next)
        {
            int v=e[i].v;
            if(belong[u]!=belong[v])
            {
                g[belong[u]].push_back(belong[v]);
            }
        }
    }
    tree_len=0;
    dfs_tree(1,-1,0);
    dfs_tree(x,-1,0);
    printf("%d\n",scc-1-tree_len);
}
int main()
{
    int m,u,v;
    while(scanf("%d%d",&n,&m)>0)
    {
        if(n+m==0)break;
        init();
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&u,&v);
            addedge(u,v);
            addedge(v,u);
        }
        solve();
    }
}

时间: 2024-11-05 02:21:51

hdu4612(双连通缩点+树的直径)的相关文章

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

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

poj3694--Network(双连通缩点+lca)

poj3694:题目链接 题目大意:给出n个点,m条无向边的图,图中存在割边,问每加入一条新的边后的割边的数量 首先,进行双连通缩点,缩点后的图变成一棵树,树上的每条边都是割边,然后没加入一条新的边后,会使这条边的两个点到这两个点的lca形成一个环,使原本的割边减少. 图学的不好,只能显式建树,后来发现建树后没什么用,等以后再修改了 #include <cstdio> #include <cstring> #include <algorithm> #include &l

HDU 5409 CRB and Graph 双连通缩点 + st表

HDU 5409 显然要先双连通缩成一棵树. 然后对于树上的边才有答案. 对于一条边来说, 两侧的最大值分为mx1 , mx2 , 那么 u 一定是min(mx1, mx2), v 一定是 u + 1. 这个经过仔细分析一下就能得到, 然后按dfs序建个st表就好啦. #include<bits/stdc++.h> #define LL long long #define LD long double #define ull unsigned long long #define fi firs

poj 2942 Knights of the Round Table 【双连通缩点+判奇圈】【经典】

题目:poj 2942 Knights of the Round Table 题意:n个骑士经常一起开会,其中有一些两两相互憎恨,他们不能同一桌,开会要表决一些事情,所以必须奇数个人,最少3个,求永远也参加不了会议的人的个数. 分析:这个题目两点 首先,建图求双连通缩点 建图的话,因为相互憎恨的不能再一块,所以要建补图,让能够在一起的所有的连接,这样的话,如果能存在环且环上的点是奇数个的话就可以参加会议,标记求不能参加的即可. 建好图之后用tarjan算法双连通缩点,把在一个环上的点保存起来.

HDU4612:Warm up(缩点+树的直径)

Warm up Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 9073    Accepted Submission(s): 2120 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4612 Description: N planets are connected by M bidire

codeforces GYM 100114 J. Computer Network 无相图缩点+树的直径

题目链接: http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” consists of n servers and m two-way communication links. Two servers can communicate either through a direct link, or through a chain of links, by relayi

【连通图|边双连通+缩点】POJ-3177 Redundant Paths

Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K       Description In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1..F) to another field, Bessie and the rest of the herd are forced to cross near the T

【连通图|边双连通+缩点】POJ-3352 Road Construction

Road Construction Time Limit: 2000MS Memory Limit: 65536K Description It's almost summer time, and that means that it's almost summer construction time! This year, the good people who are in charge of the roads on the tropical island paradise of Remo

poj 3352 Road Construction【边双连通求最少加多少条边使图双连通&amp;&amp;缩点】

Road Construction Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10141   Accepted: 5031 Description It's almost summer time, and that means that it's almost summer construction time! This year, the good people who are in charge of the r