zoukankan      html  css  js  c++  java
  • poj1144 tarjan求割点

    poj1144 tarjan求割点

    额,算法没什么好说的,只是这道题的读入非常恶心。

    注意,当前点x是否是割点,与low[x]无关,只和low[son]和dfn[x]有关

    还有,默代码的时候记住分目标点是父亲还是孩子两种情况讨论。

    #include <cctype>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    
    const int maxn=100, maxm=5000;
    
    struct Graph{
        struct Edge{
            int to, next; Graph *bel;
            inline int operator *(){ return to; }
            Edge& operator ++(){
                return *this=bel->edge[next]; }
        };
        void addedge(int x, int y){
            Edge &e=edge[++cntedge];
            e.to=y; e.next=fir[x];
            fir[x]=cntedge; e.bel=this;
        }
        void reset(){
            cntedge=0; memset(fir, 0, sizeof(fir)); }
        Edge& getlink(int x){
            return edge[fir[x]]; }
        Edge edge[maxm*2];
        int cntedge, fir[maxn];
    }g;
    
    int n, x, y, len, cut[maxn];
    int time, dfn[maxn], low[maxn];
    char s[maxn*5];
    
    int getint(){
        char c; int flag=1, re=0;
        for (c=getchar(); !isdigit(c); c=getchar())
            if (c=='-') flag=-1;;
        for (re=c-48; c=getchar(), isdigit(c); re=re*10+c-48);
        return re*flag;
    }
    
    void tarjan(int now){
        dfn[now]=low[now]=++time;
        int cntc=0; Graph::Edge e=g.getlink(now);
        for (; *e; ++e){
            if (dfn[*e]) low[now]=min(low[now], dfn[*e]);
            else{
                ++cntc; //这里错了一次
                tarjan(*e);
                low[now]=min(low[now], low[*e]);
                if (low[*e]>=dfn[now]) cut[now]=1;
            }
        }
        if (now==1) cut[now]=cntc>1?1:0;  //处理根节点的特殊情况
    }
    
    int main(){
        while (n=getint()){
            g.reset();
            while (x=getint()){
                fgets(s, maxn*5, stdin);
                len=strlen(s); y=0;
                for (int i=0; i<=len; ++i){
                    if (!isdigit(s[i])&&y){
                        g.addedge(x, y); g.addedge(y, x);
                        y=0; }
                    if (isdigit(s[i])) y=y*10+s[i]-48;
                }
            }
            for (int i=1; i<=n; ++i)
                dfn[i]=low[i]=cut[i]=0;
            time=0; tarjan(1); int cnt=0;
            for (int i=1; i<=n; ++i) if (cut[i]) ++cnt;
            printf("%d
    ", cnt);
        }
        return 0;
    }
    
  • 相关阅读:
    linux 常用命令行
    二叉搜索树(BST)
    pytorch Gradient Clipping
    python 读写文件
    我终于可以毕业啦!
    为什么有可执行文件还需要库文件
    java常识
    *args、*kwargs
    win终端命令
    import_module
  • 原文地址:https://www.cnblogs.com/MyNameIsPc/p/7979697.html
Copyright © 2011-2022 走看看