首页 > 其他分享 >SPOJ-EC_P Critical Edges

SPOJ-EC_P Critical Edges

时间:2022-08-26 23:44:18浏览次数:95  
标签:cute int Edges SPOJ Critical maxn low nex now

Critical Edges

tarjan 割边模板

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
#define pii pair<int, int>
const int maxn = 710;
int head[maxn], to[maxn * maxn], nexx[maxn * maxn], vp = 1;
int dfn[maxn], low[maxn], tp = 0;
vector<pii>cute;

inline void add(int u, int v)
{
    vp++;
    nexx[vp] = head[u];
    head[u] = vp;
    to[vp] = v;
}

void tarjan(int now, int pre)
{
    low[now] = dfn[now] = ++tp;
    for(int i=head[now]; i; i=nexx[i])
    {
        int nex = to[i];
        if(dfn[nex] == 0)
        {
            tarjan(nex, i);
            low[now] = min(low[now], low[nex]);
            if(low[nex] > dfn[now])
                cute.push_back({now, nex});
        }
        else if(i != (pre ^ 1))
            low[now] = min(low[now], dfn[nex]);
    }
}

void init(int n)
{
    for(int i=0; i<=n; i++) dfn[i] = low[i] = head[i] = 0;
    cute.clear();
    tp = 0;
    vp = 1;
}

int main()
{
    int t;
    scanf("%d", &t);
    for(int cas=1; cas<=t; cas++)
    {
        int n, m;
        scanf("%d%d", &n, &m);
        init(n);
        for(int i=0; i<m; i++)
        {
            int a, b;
            scanf("%d%d", &a, &b);
            add(a, b);
            add(b, a);
        }
        for(int i=1; i<=n; i++)
            if(dfn[i] == 0) tarjan(i, -1);
        for(auto &[x, y] : cute)
            if(x > y) swap(x, y);
        sort(cute.begin(), cute.end());

        printf("Caso #%d\n", cas);
        if(cute.size()) printf("%d\n", cute.size());
        else printf("Sin bloqueos\n");
        for(auto [x, y] : cute)
        {
            if(x > y) swap(x, y);
            printf("%d %d\n", x, y);
        }
    }
    return 0;
}

标签:cute,int,Edges,SPOJ,Critical,maxn,low,nex,now
From: https://www.cnblogs.com/dgsvygd/p/16629600.html

相关文章

  • CF576E Painting Edges
    传送门类比一下模板题,其实我们只需要把扩展域并查集再扩展成\(k\)个即可但有个问题,当改变一条边的颜色,导致不能构成二分图时,我们就不能操作;但在线段树上,我们的操作不......
  • AT ARC092F Two Faced Edges
    题意:给定一个有向图,保证无重边自环,求将图中的每条边反向后强联通分量的个数是否会改变。数据范围:$n$$≤$ $1000$,$m$$≤$$200000$。首先考虑一条边的影响。因为一条......