poj 1523 SPF (求割点模板题,同时记录删点后连通块个数)

版权声明:本文为博主原创文章,转载请注明出处~~ https://blog.csdn.net/hxc2101/article/details/82528087

题目:http://poj.org/problem?id=1523

题意:给定一个有N个点的无向连通图,问图中有哪些点去掉后图将不再连通?同时输出去掉这个点后,图被分为几个连通块?

思路:在求割点的同时,可以用一个数组记录去掉该点后图中连通块的个数。

选一个点作为根结点。根结点 归根结点看 child 数量来判断割点;其他点 看是否满足low[v]>=dfn[u]来判断割点。

#include <iostream>
#include <cstdio>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <bitset>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <cstddef>
#include <memory>
#include <vector>
#include <cctype>
#include <string>
#include <cmath>
#include <queue>
#include <deque>
#include <list>
#include <ctime>
#include <stack>
#include <sstream>
#include <map>
#include <set>
//#include <unordered_map>
//#include <unordered_set>
//#pragma GCC optimize(3)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pb push_back
#define mkp(a,b) make_pair(a,b)
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define fi first
#define se second
#define lc (d<<1) //d*2
#define rc (d<<1|1) //d*2+1
#define eps 1e-9
#define dbg(x) cerr << #x << " = " << x << "\n";
#define mst(a,val) memset(a,val,sizeof(a))
#define stn(a) setprecision(a)//小数总有效位数
#define stfl setiosflags(ios::fixed)//点后位数:cout<<stfl<<stn(a);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double PI=3.1415926535897932;
const int MAXN=1e5+10;
const ll mod=1e9+7;
ll inline mpow(ll a,ll b){ll ans=1;a%=mod;while(b){if(b&1)ans=(ans*a)%mod;a=(a*a)%mod,b>>=1;}return ans;}
int inline sgn(double x){return (x>-eps)-(x<eps);} //a<b:sgn(a-b)<0
priority_queue<int,vector<int>,greater<int> > qu; //up
priority_queue<int,vector<int>,less<int> > qd; //dn
const int inf = 0x3f3f3f3f; //9
const ll inff = 0x3f3f3f3f3f3f3f3f; //18

vector<int>edg[1010];
int kase=0;
int dfn[1010],low[1010];
int dep=0;
int child=0;
int root;
int sub[1010];

void tarjan(int u,int fa)
{
    dfn[u]=low[u]=++dep;
    for(int i=0;i<edg[u].size();i++)
    {
        int v=edg[u][i];
        if(dfn[v]==-1)
        {
            tarjan(v,u);
            low[u]=min(low[u],low[v]);
            if(u==root) child++;
            else if(low[v]>=dfn[u]) sub[u]++;
        }
        else if(v!=fa) low[u]=min(low[u],dfn[v]);
    }
}

int main()
{
    fio;
    while(true)
    {
        dep=0,child=0;
        mst(edg,0);mst(dfn,-1);mst(sub,0);
        int a,b,flag=0;
        while(true)
        {
            cin>>a;
            if(a) {cin>>b;flag=1;edg[a].pb(b);edg[b].pb(a);}
            else break;
        }
        if(!flag) break;
        else kase++;
        cout<<"Network #"<<kase<<endl;
        root=b;
        tarjan(root,-1);

        flag=0;
        if(child) sub[root]=child-1;
        for(int i=0;i<1005;i++)
            if(sub[i]){flag=1;cout<<"  SPF node "<<i<<" leaves "<<sub[i]+1<<" subnets"<<endl;}
        if(flag==0) cout<<"  No SPF nodes"<<endl;
        cout<<endl;
    }
}

猜你喜欢

转载自blog.csdn.net/hxc2101/article/details/82528087