点双连通分量

#include 
#include 
#include 
#include 
#include 

using namespace std;
const int N = 1e6+10;
const int NN = 1e3+10;
struct node{
    int to,next;
}edge[N];
int head[NN],top;
int dfn[NN],low[NN],time,num;
stacksta;
vectorvec[N];
void init(){
    memset(head,-1,sizeof(head));
    memset(dfn,0,sizeof(dfn));
    top = time = 0;
}
void tarjan(int u,int father){
    dfn[u] = ++time;
    low[u] = time;
    for(int i=head[u]; ~i; i=edge[i].next){
        int v = edge[i].to;
        if(!dfn[v]){
            sta.push(i);//存边
            tarjan(v,u);
            low[u] = min(low[u],low[v]);
            if(low[v] >= dfn[u]){
                ++num;
                while(!sta.empty()){
                    int j = sta.top();
                    sta.pop();
                    vec[num].push_back(j);//存的边
                    if(j == i)break;
                }
            }
        }
        else if(dfn[v] < dfn[u]){
            sta.push(i);
            low[u] = min(low[u],dfn[v]);
        }
    }
}
int main()
{
    int n;
    cin>>n;
    for(int i = 1; i <= n; ++i){
        if(!dfn[i])tarjan(i,-1);
    }
    return 0;
}

#include 
#include 
#include 
#include 
#include 

using namespace std;
const int N = 1e6+10;
const int NN = 1e3+10;
struct node{
    int to,next;
}edge[N];
int head[NN],top;
int dfn[NN],low[NN],time,num;
stacksta;
vectorvec[N];
void init(){
    memset(head,-1,sizeof(head));
    memset(dfn,0,sizeof(dfn));
    top = time = 0;
}
void tarjan(int u,int father){
    dfn[u] = ++time;
    low[u] = time;
    for(int i=head[u]; ~i; i=edge[i].next){
        int v = edge[i].to;
        if(!dfn[v]){
            sta.push(u);//存点
            tarjan(v,u);
            low[u] = min(low[u],low[v]);
            if(low[v] >= dfn[u]){
                ++num;
                while(!sta.empty()){
                    int j = sta.top();
                    sta.pop();
                    vec[num].push_back(j);//存的点
                    if(j == u)break;
                }
            }
        }
        else if(dfn[v] < dfn[u]){
            sta.push(u);
            low[u] = min(low[u],dfn[v]);
        }
    }
}
int main()
{
    int n;
    cin>>n;
    for(int i = 1; i <= n; ++i){
        if(!dfn[i])tarjan(i,-1);
    }
    return 0;
}

你可能感兴趣的:(acm)