天天看点

【暑训个人赛#2 F】 POJ - 3352 Tarjan模板题

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 Remote Island would like to repair and upgrade the various roads that lead between the various tourist attractions on the island.

The roads themselves are also rather interesting. Due to the strange customs of the island, the roads are arranged so that they never meet at intersections, but rather pass over or under each other using bridges and tunnels. In this way, each road runs between two specific tourist attractions, so that the tourists do not become irreparably lost.

Unfortunately, given the nature of the repairs and upgrades needed on each road, when the construction company works on a particular road, it is unusable in either direction. This could cause a problem if it becomes impossible to travel between two tourist attractions, even if the construction company works on only one road at any particular time.

So, the Road Department of Remote Island has decided to call upon your consulting services to help remedy this problem. It has been decided that new roads will have to be built between the various attractions in such a way that in the final configuration, if any one road is undergoing construction, it would still be possible to travel between any two tourist attractions using the remaining roads. Your task is to find the minimum number of new roads necessary.

Input

The first line of input will consist of positive integers n and r, separated by a space, where 3 ≤ n ≤ 1000 is the number of tourist attractions on the island, and 2 ≤ r ≤ 1000 is the number of roads. The tourist attractions are conveniently labelled from 1 to n. Each of the following r lines will consist of two integers, v and w, separated by a space, indicating that a road exists between the attractions labelled v and w. Note that you may travel in either direction down each road, and any pair of tourist attractions will have at most one road directly between them. Also, you are assured that in the current configuration, it is possible to travel between any two tourist attractions.

Output

One line, consisting of an integer, which gives the minimum number of roads that we need to add.

Sample Input

Sample Input 1

10 12

1 2

1 3

1 4

2 5

2 6

5 6

3 7

3 8

7 8

4 9

4 10

9 10

Sample Input 2

3 3

1 2

2 3

1 3

Sample Output

Output for Sample Input 1

2

Output for Sample Input 2

题意:将原来给的图最少加上多少条边可以使得图中没有割点

思路:

先将强联通分量变成缩点,这样原来的图就变成了一个树结构。这个时候看一下每个缩点的出度,统计出度为1的即为叶结点(双向边,出度最小就是1)。我们要把这颗树加边取消割点,要加的边就是(叶结点的个数+1)/2。(构建的话就是每次最近公共祖先最远的两个叶结点结点相连收缩在一起)

AC代码:

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e4+500;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };

vector<vector<ll> > D(maxn);
ll dfn[maxn], low[maxn], vis[maxn], s[maxn], cnt[maxn], sum=0, idx = 0 , color[maxn];
ll up = 0;
ll n,m;
ll du[maxn];

void init()     //初始化
{
    mem(dfn,0);mem(low,0);mem(vis,0);mem(s,0);
    mem(cnt,0);mem(color,0);mem(du,0);
    for(int i=1;i<=n;i++) D[i].clear();
    sum = idx = up = 0;
}

void Tarjan(ll u,ll fa)       //Tarjan算法
{
    dfn[u] = low[u] = ++idx;        //时序
    vis[u] = 1; s[++up] = u;        //进栈并标记
    for(int i=0;i<D[u].size();i++)      //找子树
    {
        int v = D[u][i];
        if(!dfn[v])     //如果没有遍历过
        {
            Tarjan(v,u);      //往下搜索
            low[u] = min(low[u],low[v]);  //看看这个点下去能不能祖先,回得到的话low[v] < low[u]
        }
        else        //若已经遍历过
        {
            //而且在栈中,同样,这整个为一个强联通分量
            if(fa!=v) low[u] = min(low[u],low[v]);
        }
    }
    if(dfn[u]==low[u])  //如果往下没有回到祖先的
    {
        color[u] = ++sum;       //那么这一整块(可能是强联通分量的根或者单一点)染色
        vis[u] = 0;     //相当于出栈
        while(s[up] != u)       //u之前的点出栈,这些都是一个强联通分量
        {
            color[s[up]] = sum;     //染上相同颜色
            vis[s[up--]] = 0;       //清除标记
        }
        up--;       //别忘了当前这个点,去掉了后up--
    }
}

int main()
{
    while(~scanf("%lld%lld",&n,&m))
    {
        init();
        rep(i,1,m)      //读入
        {
            ll x,y; x = read(); y = read();
            D[x].pb(y); D[y].pb(x);
        }
        rep(i,1,n) if(dfn[i]==0) Tarjan(i,-1);     //因为有可能整个图不连通,对每个没遍历过的试探

        rep(i,1,n)      //缩点操作
        {
            for(int j=0;j<D[i].size();j++)
            {
                ll v = D[i][j];
                if(color[v]!=color[i]) du[color[i]] ++;         //如果它这个点和子树不在同一个强联通分量内,就将i所属联通分量看作一个点(缩点)。连向v所属联通分量。出度++
            }
            cnt[color[i]] ++;   //记录这个强联通分量含有的点的个数
        }

        ll obj = 0;ll ans=0;
        rep(i,1,sum)
        if(du[i]==1) ans++;
        cout<<(ans+1)/2<<endl;
    }
    return 0;
}