E. We Need More Bosses【无向图强连通】

E. We Need More Bosses

题意: 求一个无向图缩点后,求直径长度

注意无向图强连通和有向图强连通是有区别的,主要是无向图强连通不能回头,要求在tarjan算法里记录father

#include
#define PI acos(-1.0)
#define pb push_back
#define F first
#define S second
using namespace std;
typedef long long ll;
const int N=3e5+5;
const int MOD=1e9+7;
ll a[N],sum[N],dis[N];
int par[N];
int vis[N];
vector > edge[N];
vector bet;
vector maxsubtree;
vector diameter;
ll maxlen=0,k,st,ed;

int n,m,cur,cnt,top,mp[N],DFN[N],LOW[N],s[N];

void tarjan(int u,int f){
    DFN[u]=LOW[u]=++cur;
    s[++top]=u;
    for(int i=0;i<(int)edge[u].size();i++){
        int v=edge[u][i].F;
        if(v==f)    continue;
        if(!DFN[v]){
            tarjan(v,u);
            LOW[u]=min(LOW[u],LOW[v]);
        }
        else if(DFN[v] && !mp[v])   LOW[u]=min(LOW[u],DFN[v]);
    }
    if(DFN[u]==LOW[u]){
        int v=-1;
        cnt++;
        while(u!=v){
            v=s[top--];
            mp[v]=cnt;
        }
//        puts("************");
    }
}


void dfs(int u,int f){
    par[u]=f;
    for(auto t:edge[u]){
        int v=t.F;
        if(v==f)    continue;
        dis[v]=dis[u]+1;
        dfs(v,u);
    }
}
void FindDiameter(){
    memset(dis,0,sizeof dis);
    dfs(1,-1);
    ll mx=-1;
    for(int i=1;i<=n;i++)
        if(dis[i]>mx)   mx=dis[i],st=i;
    memset(dis,0,sizeof dis);
    dfs(st,-1);
    mx=-1;
    for(int i=1;i<=n;i++)
        if(dis[i]>mx)   mx=dis[i],ed=i;
    for(int i=ed;i!=-1;i=par[i])    diameter.pb(i);
    reverse(diameter.begin(),diameter.end());///find the diameter of the tree


    return ;
}
pair p[N];
int main(void){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);

    cin>>n>>m;
    for(int i=1;i<=m;i++){
        ll u,v,w;
        cin>>u>>v;
        w=1;
        p[i]={u,v};
        edge[u].pb({v,w});
        edge[v].pb({u,w});
    }
    for(int i=1;i<=n;i++)
        if(!DFN[i]) tarjan(i,-1);
//    cout << "cnt="<

 

你可能感兴趣的:(图论之强联通SCC,图论之树的直径)