首页 > 其他 > 详细

POJ-1144 Tarjan求割点,双连通模板题

时间:2017-01-25 19:35:49      阅读:360      评论:0      收藏:0      [点我收藏+]

POJ 1144

题意:给出一个无向图求割点数目。题目读入有点特殊,每一行开头给出ai,后面的bi都与ai连通。

总结:板子题。

技术分享
// POJ-1144 
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<bitset>
#include<vector>
#include<set>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define F(i,a,b)  for (int i=a;i<b;i++)
#define FF(i,a,b) for (int i=a;i<=b;i++)
#define mes(a,b)  memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
typedef long long ll;
const int N = 110;

int n, tot, root;
int head[N], cut[N], vis[N], dfn[N], low[N];
struct Edge{int to, next; }edge[N<<1];

void Init()
{
    tot=0;
    mes(head, -1);
    mes(cut, 0);
    mes(vis, 0);
}
void Addedge(int u, int v)
{
    edge[tot].to=v;
    edge[tot].next=head[u];
    head[u]=tot++;
}
void Tarjan(int u, int fa)
{
    int son=0;
    vis[u]=1;
    dfn[u]=low[u]=++tot;
    for(int e=head[u]; e!=-1; e=edge[e].next) {
        int v=edge[e].to;
        if(vis[v]==0) {
            Tarjan(v, u);
            son++;
            low[u]=min(low[u], low[v]);
            if((u==root && son>1) || (u!=root && dfn[u]<=low[v]))   //如果u为树根且子树大于1;或者u不为树根,(u,v)为树枝边
                cut[u]=1;
        }
        else if(vis[v]==1 && v!=fa) {
            low[u]=min(low[u], dfn[v]);
        }
    }
    vis[u]=2;
}
int main()
{
    while(scanf("%d", &n) && n) {
        Init();
        int u, v;
        while(scanf("%d", &u) && u) {
            while(getchar()!=\n) {    //每一行以回车结束读入
                scanf("%d", &v);
                Addedge(u, v);
                Addedge(v, u);
            }
        }
        root=1;
        Tarjan(root, -1);
        int ans=0;
        FF(i,1,n) if(cut[i]) ans++;
        printf("%d\n", ans);
    }

    return 0;
}
View Code

POJ-1144 Tarjan求割点,双连通模板题

原文:http://www.cnblogs.com/sbfhy/p/6349893.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!