【模板】其他图论

LCA(暴力):

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int MAXN = 50000 + 50;
int n, tot = 0, m;
int first[MAXN], nxt[MAXN << 1], dis[MAXN], deep[MAXN], fa[MAXN];
bool vis[MAXN];
struct edge{
    int from, to, cost;
}es[MAXN << 1];
void build(int ff, int tt, int dd)
{
    es[++tot] = (edge){ff,tt,dd};
    nxt[tot] = first[ff];
    first[ff] = tot;
}
void dfs(int x)
{
    for(int i = first[x]; i != -1; i = nxt[i])
    {
        int v = es[i].to;
        if(!vis[v])
        {
            vis[v] = 1;
            dis[v] = es[i].cost;
            deep[v] = deep[x] + 1;
            fa[v] = x;
            dfs(v);
        }
    }
}
int lca(int x, int y)
{
    int ans = 0;
    if(deep[x] > deep[y])
        swap(x,y);
    while(deep[y] != deep[x])
    {
        ans += dis[y];
        y = fa[y];
    }
    while(x != y)
    {
        ans += dis[x];
        ans += dis[y];
        x = fa[x];
        y = fa[y];
    }
    return ans;
}
int main()
{
    scanf("%d",&n);
    memset(first,-1,sizeof(first));
    memset(dis,0x3f3f3f3f,sizeof(dis));
    for(int i = 1; i < n; i ++)
    {
        fa[i] = i;
        int f, t, d;
        scanf("%d%d%d", &f, &t, &d);
        build(f,t,d);
        build(t,f,d);
    }
    deep[0] = 1;
    vis[0] = 1;
    dfs(0); 
    scanf("%d",&m);
    while(m --)
    {
        scanf("%d%d", &f, &t);
        printf("%d\n",lca(f,t));
    }
    return 0;
}

LCA(倍增):

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 233333;
struct edge{
    int from,to,cost;
}es[maxn << 1];
int tot,fst[maxn],nxt[maxn],d[maxn];
int deep[maxn],anc[maxn][30],n,m;
bool vis[maxn];
void build(int ff,int tt,int dd)
{
    es[++tot] = (edge){ff,tt,dd};
    nxt[tot] = fst[ff];
    fst[ff] = tot;
}
void dfs(int x)
{
    vis[x] = 1;
    if(x == 0) deep[x] = 1;
    for(int i = 1;i <= 16;i ++)
        anc[x][i] = anc[anc[x][i-1]][i-1];
    for(int i = fst[x];i;i = nxt[i])
    {
        int v = es[i].to;
        if(!vis[v])
        {
            anc[v][0] = x;
            d[v] = d[x] + es[i].cost;
            deep[v] = deep[x] + 1;
            dfs(v);
        }
    }
}
int lca(int x,int y)
{
    if(deep[x] < deep[y]) swap(x,y);
    int t = deep[x] - deep[y];
    for(int i = 0;i <= 20;i ++)
    if(t & (1 << i)) x = anc[x][i];
    for(int i = 20;i >= 0; i--)
    if(anc[x][i] != anc[y][i])
    {
        x = anc[x][i];
        y = anc[y][i];
    }
    if(x == y) return x;
    return anc[x][0];
}

int main()
{
    scanf("%d",&n);
    for(int i = 1;i < n;i ++)
    {
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        build(a,b,c);
        build(b,a,c);
    }
    dfs(0);
    scanf("%d",&m);
    for(int i = 1;i <= m;i ++)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        printf("%d\n",d[a] + d[b] - 2 * d[lca(a,b)]);
    }
    return 0;
}

Tarjan:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
using namespace std;
const int maxn=100010;

int scc[maxn],dfn[maxn];//强连通分量、第一次发现 
int low[maxn];//某点及其后代能连到的最早值 
int head[maxn],tot,cnt,ans[maxn];//scc总数 和 访问次数
stack<int>s;

struct Edge
{
    int to;
    int fff;
    int d;
    int next;
}edge[maxn];

void build(int fff,int tt) 
{
    edge[++tot].to=tt;
    edge[tot].next=head[fff];
    head[fff]=tot;
}

int tim = 0;
void tarjan(int x)
{
    dfn[x] = low[x] = ++tim;
    s.push(x);
    for(int i = fst[x];i!=-1;i = nxt[i])
    {
        int v = es[i].to;
        if(!dfn[v])
        {
            tarjan(v);
            low[x] = min(low[v],low[x]);
        }
        else if(!scc[v])
        low[x] = min(low[x],dfn[v]);
    }
    if(low[x] == dfn[x])
    {
        cnt ++;
        int cnt2 = 0;
        while(1)
        {
            int u = s.top();
            s.pop();
            cnt2 ++;
            scc[u] = cnt;
            if(u == x)
            {
                if(cnt2 != 1) 
                ans = min(ans,cnt2);
                return ;
            }
        }
    }
}

SPFA判负环:

ll spfa(ll s)
{
    memset(dis,0x3f,sizeof(dis));
    memset(vis,0,sizeof(vis));
    memset(use,0,sizeof(use));
    dis[s] = 0;
    q.push(s);
    vis[s] = 1;
    use[s] = true;
    while(!q.empty())
    {
        ll u = q.front();
        q.pop();
        use[u] = false;
        if(vis[u] > n + 1) return -1;
        for(ll i = first[u];i != -1;i = next[i])
        {
            ll v = l[i].t;
            if(dis[v] > dis[u] + l[i].v)
            {
                dis[v] = dis[u] + l[i].v;
                vis[v] = vis[u] + 1;
                if(!use[v])
                {
                    q.push(v);
                    use[v] = true;
                }
            }
        }
    }
    return 0;
}
int main()
{
    init(); 
    ll s,f,t,v;
    scanf("%lld %lld %lld",&n,&m,&s);
    for(ll i = 1;i <= m;i ++)
    {
        scanf("%lld %lld %lld",&f,&t,&v);
        build(f,t,v);
    }
    for(ll i = 0;i <= n;i ++)
    {
        if(spfa(i) == -1)
        {
            puts("-1");
            return 0;
        }
    }
    spfa(s);
    for(ll i = 1;i <= n;i ++)
    if(dis[i] >= INF) puts("NoPath");
    else printf("%lld\n",dis[i]);
    return 0;
}

严格次短路:

#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 2000000;
struct Edge{
    int from, to, cost;
}es[maxn];
int first[maxn], nxt[maxn] , tot = 0;
long long zd[maxn], cd[maxn];
bool used[maxn];
queue < int > q;
void build(int f, int t, int d)
{
    es[++tot] = (Edge){f,t,d};
    nxt[tot] = first[f];
    first[f] = tot;
}
void spfa(int s)
{
    zd[s] = 0;
    q.push(s);
    used[s] = 1;
    while(!q.empty())
    {
        int u = q.front();
        q.pop();
        used[u] = 0;
        for(int i = first[u]; i != -1; i = nxt[i])
        {
            int v = es[i].to;
            if(zd[v] > zd[u] + es[i].cost)
            {
                cd[v] = zd[v];
                zd[v] = zd[u] + es[i].cost;
                if(!used[v])
                {
                    q.push(v);
                    used[v] = 1;
                }
            }
            else if(cd[v] > zd[u] + es[i].cost && zd[v] < zd[u] + es[i].cost)
            {
                cd[v] = zd[u] + es[i].cost;
                if(!used[v])
                {
                    q.push(v);
                    used[v] = 1;
                }
            }
            else if(cd[v] > cd[u] + es[i].cost )
            {
                cd[v] = cd[u] + es[i].cost;
                if(!used[v])
                {
                    q.push(v);
                    used[v] = 1;
                }
            }
        }
    }
}

树的直径:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int SZ=100000+10;

int first[SZ],nxt[SZ<<1];
struct edge{
    int from,to;
}es[SZ<<1];
int tot=0,n;

void build(int ff,int tt)
{
    es[++tot]=(edge){ff,tt};
    nxt[tot]=first[ff];
    first[ff]=tot;
}

int ans=0,pos;
void dfs(int u,int f,int val)
{
    if(val>ans)
    {
        ans=val;
        pos=u;
    }
    for(int i=first[u];i!=-1;i=nxt[i])
    {
        int v=es[i].to;
        if(v==f) continue;
        dfs(v,u,val+1);
    }
}

int main()
{
     memset(first,-1,sizeof(first));
     int n;
     scanf("%d",&n);
     for(int i=1;i<=n;i++)
     {
        int l,r;
        scanf("%d%d",&l,&r);
        if(l) build(l,i),build(i,l);
        if(r) build(r,i),build(i,r);
     }
     dfs(1,1,0);
     ans=0;
     dfs(pos,1,0);
     cout<<ans<<endl;
     return 0;
} 
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值