关闭

UVA315 && UVA 796 (求割点和桥,模版)

标签: 割点和桥
212人阅读 评论(0) 收藏 举报

两题的共同之处 输入都很麻烦但是也难不倒我QAQ

另外 第二题 有个trick点,要排序后输出


UVA 315

<span style="font-size:14px;">/* ***********************************************
Author        :angon

************************************************ */
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define showtime fprintf(stderr,"time = %.15f\n",clock() / (double)CLOCKS_PER_SEC)
#define lld %I64d
#define REP(i,k,n) for(int i=k;i<n;i++)
#define REPP(i,k,n) for(int i=k;i<=n;i++)
#define scan(d) scanf("%d",&d)
#define scanl(d) scanf("%I64d",&d)
#define scann(n,m) scanf("%d%d",&n,&m)
#define scannl(n,m) scanf("%I64d%I64d",&n,&m)
#define mst(a,k)  memset(a,k,sizeof(a))
#define LL long long
#define N 1005
#define mod 1000000007
inline int read()
{
    int s=0;
    char ch=getchar();
    for(; ch<'0'||ch>'9'; ch=getchar());
    for(; ch>='0'&&ch<='9'; ch=getchar())s=s*10+ch-'0';
    return s;
}


/* *  求 无向图的割点和桥
*  可以找出割点和桥,求删掉每个点后增加的连通块。
*  需要注意重边的处理,可以先用矩阵存,再转邻接表,或者进行判重 */
const int MAXN = 10010;
const int MAXM = 100010;
struct Edge
{
    int to,next;
    bool cut;//是否为桥的标记
} edge[MAXM];
int head[MAXN],tot;
int Low[MAXN],DFN[MAXN];
int Index,top,bridge;
bool Instack[MAXN];
bool cut[MAXN];
int add_block[MAXN];//删除一个点后增加的连通块 int bridge;
void addedge(int u,int v)
{
    edge[tot].to = v;
    edge[tot].next = head[u];
    edge[tot].cut = false;
    head[u] = tot++;
}
void Tarjan(int u,int pre)
{
    int v;
    Low[u] = DFN[u] = ++Index;
    Instack[u] = true;
    int son = 0;
    for(int i = head[u]; i != -1; i = edge[i].next)
    {
        v = edge[i].to;
        if(v == pre)continue;
        if( !DFN[v] )
        {
            son++;
            Tarjan(v,u);
            if(Low[u] > Low[v])Low[u] = Low[v];
            //桥
            //一条无向边(u,v)是桥,当且仅当(u,v)为树枝边,且满足DFS(u)<Low(v)。
            if(Low[v] > DFN[u])
            {
                bridge++;
                edge[i].cut = true;
                edge[i^1].cut = true;
            }
            //割点
            //一个顶点u是割点,当且仅当满足(1)或(2) (1) u为树根,且u有多于一个子树。
            //(2) u不为树根,且满足存在(u,v)为树枝边(或称父子边,
            //即u为v在搜索树中的父亲),使得DFS(u)<=Low(v)
            if(u != pre && Low[v] >= DFN[u])//不是树根
            {
                cut[u] = true;
                add_block[u]++;
            }
        }
        else if( Low[u] > DFN[v])              Low[u] = DFN[v];
    }     //树根,分支数大于1
    if(u == pre && son > 1)cut[u] = true;
    if(u == pre)add_block[u] = son - 1;
    Instack[u] = false;
}
void init()
{
    mst(DFN,0);
    mst(head,-1);
    mst(cut,0);
    mst(Instack,0);
    mst(add_block,0);
    tot = Index = bridge = 0;
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int n,u,v;
    while(~scan(n) && n)
    {
        init();
        while(~scan(u) && u)
        {
            while(getchar()!='\n')
            {
                scan(v);
                addedge(u,v);
                addedge(v,u);
            }
        }
        for(int i=1; i<=n; i++)
        {
            if(!DFN[i])
                Tarjan(i,i);
        }
        int ans=0;
        for(int i=1; i<=n; i++)
            if(cut[i])
                ans++;
        printf("%d\n",ans);

    }

    return 0;
}</span>

 UVA 796

/* ***********************************************
Author        :angon

************************************************ */
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define showtime fprintf(stderr,"time = %.15f\n",clock() / (double)CLOCKS_PER_SEC)
#define lld %I64d
#define REP(i,k,n) for(int i=k;i<n;i++)
#define REPP(i,k,n) for(int i=k;i<=n;i++)
#define scan(d) scanf("%d",&d)
#define scanl(d) scanf("%I64d",&d)
#define scann(n,m) scanf("%d%d",&n,&m)
#define scannl(n,m) scanf("%I64d%I64d",&n,&m)
#define mst(a,k)  memset(a,k,sizeof(a))
#define LL long long
#define N 1005
#define mod 1000000007
inline int read(){int s=0;char ch=getchar();for(; ch<'0'||ch>'9'; ch=getchar());for(; ch>='0'&&ch<='9'; ch=getchar())s=s*10+ch-'0';return s;}


const int MAXN = 100010;
const int MAXM = 1000010;
struct Edge
{
    int to,next;
    bool cut;//是否为桥的标记
} edge[MAXM];
int head[MAXN],tot;
int Low[MAXN],DFN[MAXN];
int Index,top,bridge;
bool Instack[MAXN];
bool cut[MAXN];
int add_block[MAXN];//删除一个点后增加的连通块 int bridge;
void addedge(int u,int v)
{
    edge[tot].to = v;
    edge[tot].next = head[u];
    edge[tot].cut = false;
    head[u] = tot++;
}
struct T
{
    int u, v;
}t[MAXM];
void Tarjan(int u,int pre)
{
    int v;
    Low[u] = DFN[u] = ++Index;
    Instack[u] = true;
    int son = 0;
    for(int i = head[u]; i != -1; i = edge[i].next)
    {
        v = edge[i].to;
        if(v == pre)continue;
        if( !DFN[v] )
        {
            son++;
            Tarjan(v,u);
            if(Low[u] > Low[v])Low[u] = Low[v];
            //桥
            //一条无向边(u,v)是桥,当且仅当(u,v)为树枝边,且满足DFS(u)<Low(v)。
            if(Low[v] > DFN[u])
            {
                t[++bridge].u=u;
                t[bridge].v=v;
                if(t[bridge].u>t[bridge].v) swap(t[bridge].u,t[bridge].v);
                //edge[i].cut = true;
                //edge[i^1].cut = true;
            }
        }
        else if( Low[u] > DFN[v])   Low[u] = DFN[v];
    }
    Instack[u] = false;
}
void init()
{
    mst(DFN,0);
    mst(head,-1);
    mst(cut,0);
    mst(Instack,0);
    mst(add_block,0);
    tot = Index = bridge = 0;
}
bool cmp(T a, T b)
{
    return a.u<b.u || a.u==b.u&&a.v<b.v;
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int n,u,v,k;
    while(~scan(n) )
    {
        init();
        for(int i=0;i<n;i++)
        {
            scan(u); u++;
            scanf(" (%d)",&k);
            while(k--)
            {
                scan(v); v++;
                addedge(u,v);
                addedge(v,u);
            }
        }
        for(int i=1; i<=n; i++)
        {
            if(!DFN[i])
                Tarjan(i,i);
        }
        printf("%d critical links\n",bridge);
        sort(t+1,t+bridge+1,cmp);
        for(int i=1;i<=bridge;i++)
            printf("%d - %d\n",t[i].u-1,t[i].v-1);
        printf("\n");
    }
    return 0;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:54000次
    • 积分:2046
    • 等级:
    • 排名:第18779名
    • 原创:152篇
    • 转载:4篇
    • 译文:2篇
    • 评论:41条
    个人简介
    前ACMer,现职位算法工程师,主要负责室内定位和SLAM相关算法研究
    最新评论