hdu2460Network

链接:http://acm.hdu.edu.cn/showproblem.php?pid=2460

题意:给出一个n个点m条无向边,然后逐步加入一些边,求每次加入当前边后图内还剩多少条割边。

分析:先用tarjan缩点,将图缩成一颗树,然后在树上加个边权每次缩到lca就好了。详见代码。

代码:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=1e5+10;
const int M=2e5+10;
const int mod=1000000007;
const int MOD1=1000000007;
const int MOD2=1000000009;
const double EPS=0.00000001;
typedef long long ll;
const ll MOD=1000000007;
const int INF=1000000010;
const ll MAX=1ll<<55;
const double eps=1e-5;
const double inf=~0u>>1;
const double pi=acos(-1.0);
typedef double db;
typedef unsigned int uint;
typedef unsigned long long ull;
struct Edge {
    int u,v,id,nex;
    Edge() {}
    Edge(int u,int v,int id,int nex) {
        this->u=u;this->v=v;this->id=id;this->nex=nex;
    }
}edge[M<<1];
stackS;
int tot,first[N];
int sccn,dfs_clock,pre[N],dfn[N],sccno[N],vis[M];
void dfs_scc(int u) {
    pre[u]=dfn[u]=++dfs_clock;S.push(u);
    for (int i=first[u];~i;i=edge[i].nex)
    if (!vis[edge[i].id]) {
        vis[edge[i].id]=1;
        if (!pre[edge[i].v]) {
            dfs_scc(edge[i].v);
            dfn[u]=min(dfn[u],dfn[edge[i].v]);
        } else if (!sccno[edge[i].v]) dfn[u]=min(dfn[u],dfn[edge[i].v]);
    }
    if (pre[u]==dfn[u]) {
        sccn++;
        while (1) {
            int x=S.top();
            sccno[x]=sccn;S.pop();
            if (x==u) break ;
        }
    }
}
void find_scc(int n,int m) {
    dfs_clock=sccn=0;
    for (int i=1;i<=m;i++) vis[i]=0;
    for (int i=1;i<=n;i++) pre[i]=sccno[i]=0;
    for (int i=1;i<=n;i++)
    if (!pre[i]) dfs_scc(i);
}
int dep[N],fa[N];
vectorV[N];
void dfs(int u,int v) {
    dep[u]=dep[v]+1;fa[u]=v;
    for (int i=0;idep[b]) ret+=vis[a],a=fa[a];
    else ret+=vis[b],b=fa[b];
    a=u;b=v;
    while (a!=b)
    if (dep[a]>dep[b]) vis[a]=0,a=fa[a];
    else vis[b]=0,b=fa[b];
    return ret;
}
int main()
{
    int u,v,i,n,m,q,ca=0,ans;
    while (scanf("%d%d", &n, &m)&&(n+m)) {
        for (tot=0,i=1;i<=n;i++) first[i]=-1;
        for (i=1;i<=m;i++) {
            scanf("%d%d", &u, &v);
            edge[tot]=Edge(u,v,i,first[u]);first[u]=tot++;
            edge[tot]=Edge(v,u,i,first[v]);first[v]=tot++;
        }
        find_scc(n,m);
        for (i=1;i<=sccn;i++) V[i].clear();
        for (i=0;i


你可能感兴趣的:(online,judge,Hdu,图论,双连通分量)