zoukankan      html  css  js  c++  java
  • BZOJ1023:[SHOI2008]仙人掌图——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=1023

    Description

      如果某个无向连通图的任意一条边至多只出现在一条简单回路(simple cycle)里,我们就称这张图为仙人掌
    图(cactus)。所谓简单回路就是指在图上不重复经过任何一个顶点的回路。

     

      举例来说,上面的第一个例子是一张仙人图,而第二个不是——注意到它有三条简单回路:(4,3,2,1,6,5,4)、(7,8,9,10,2,3,7)以及(4,3,7,8,9,10,2,1,6,5,4),而(2,3)同时出现在前两个的简单回路里。另外,第三张图也不是仙人图,因为它并不是连通图。显然,仙人图上的每条边,或者是这张仙人图的桥(bridge),或者在且仅在一个简单回路里,两者必居其一。定义在图上两点之间的距离为这两点之间最短路径的距离。定义一个图的直径为这张图相距最远的两个点的距离。现在我们假定仙人图的每条边的权值都是1,你的任务是求出给定的仙人图的直径。

    Input

      输入的第一行包括两个整数n和m(1≤n≤50000以及0≤m≤10000)。其中n代表顶点个数,我们约定图中的顶点将从1到n编号。接下来一共有m行。代表m条路径。每行的开始有一个整数k(2≤k≤1000),代表在这条路径上的顶点个数。接下来是k个1到n之间的整数,分别对应了一个顶点,相邻的顶点表示存在一条连接这两个顶点的边。一条路径上可能通过一个顶点好几次,比如对于第一个样例,第一条路径从3经过8,又从8返回到了3,但是我们保证所有的边都会出现在某条路径上,而且不会重复出现在两条路径上,或者在一条路径上出现两次。

    Output

      只需输出一个数,这个数表示仙人图的直径长度。

    Sample Input

    15 3
    9 1 2 3 4 5 6 7 8 3
    7 2 9 10 11 12 13 10
    5 2 14 9 15 10 8

    10 1
    10 1 2 3 4 5 6 7 8 9 10

    Sample Output

    8
    9

    ——————————————————————————

    看hzwer的博客吧:http://hzwer.com/4645.html

    (以及虽然我知道怎么做了但是不会单调队列的我决定什么时候会了单调队列什么时候再补题解)

    #include<cstdio>
    #include<iostream>
    #include<cmath>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    const int N=50005;
    const int M=20000005;
    inline int read(){
        int X=0,w=0;char ch=0;
        while(!isdigit(ch)){w|=ch=='-';ch=getchar();}
        while(isdigit(ch))X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
        return w?-X:X;
    }
    struct node{
        int to;
        int nxt;
    }edge[M];
    int head[N],val[N],num[N],cnt=0;
    int n,m,low[N],dfn[N],fa[N],t=0;
    int ans=0,dep[N],f[N],a[N*2];
    int q[N*2],l,r;
    bool vis[N];
    inline void add(int u,int v){
        cnt++;
        edge[cnt].to=v;
        edge[cnt].nxt=head[u];
        head[u]=cnt;
        return;
    }
    void dp(int rt,int u){
        int delta=dep[u]-dep[rt]+1;
        for(int i=u;i!=rt;i=fa[i])a[delta--]=f[i];
        a[delta]=f[rt];
        delta=dep[u]-dep[rt]+1;
        for(int i=1;i<=delta;i++)a[i+delta]=a[i];
        q[1]=1;l=r=1;
        for(int i=2;i<=2*delta;i++){
        while(l<=r&&i-q[l]>delta/2)l++;
        ans=max(ans,a[i]+i+a[q[l]]-q[l]);
        while(l<=r&&a[q[r]]-q[r]<=a[i]-i)r--;
        q[++r]=i;
        }
        for(int i=2;i<=delta;i++){
        f[rt]=max(f[rt],a[i]+min(i-1,delta-i+1));
        }
        return;
    }
    void tarjan(int u){
        dfn[u]=low[u]=++t;
        for(int i=head[u];i;i=edge[i].nxt){
        int v=edge[i].to;
        if(v==fa[u])continue;
        if(!dfn[v]){
            fa[v]=u;
            dep[v]=dep[u]+1;
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }else{
            low[u]=min(low[u],dfn[v]);
        }
        if(dfn[u]<low[v]){//
            ans=max(ans,f[u]+f[v]+1);
            f[u]=max(f[u],f[v]+1);
        }
        }
        for(int i=head[u];i;i=edge[i].nxt){
        int v=edge[i].to;
        if(fa[v]!=u&&dfn[u]<dfn[v]){//
            dp(u,v);
        }
        }
        return;
    }
    int main(){
        n=read();m=read();
        for(int i=1;i<=m;i++){
        int k=read(),u=read();
        for(int j=2;j<=k;j++){
            int v=read();
            add(u,v);add(v,u);
            u=v;
        }
        }
        tarjan(1);
        printf("%d
    ",ans);
        return 0;
    }
  • 相关阅读:
    关于jabber协议
    xmpp相关链接,
    Implementation of the Server Dialback method as defined by the RFC3920
    好吧,隐藏的文件,
    Vue之methods watch和compute的区别和联系
    面向过程编程&面向对象编程
    JS高阶---线程与事件机制(小结)
    Vuex简介
    Vuex操作步骤
    vue单页面应用刷新网页后vuex的state数据丢失的解决方案
  • 原文地址:https://www.cnblogs.com/luyouqi233/p/8241274.html
Copyright © 2011-2022 走看看