1051: [HAOI2006]受欢迎的牛

本文介绍了一个使用Tarjan算法进行强连通分量分析的C++实现。通过建立图的数据结构并利用深度优先搜索来查找图中的所有强连通分量,进而解决特定问题。文章详细展示了如何使用Tarjan算法重建图并进行后续处理。
#include<iostream>
#include<cstdio>
#define N 10001
using namespace std;
struct edge{
    int to,next;
}e[50001],d[50001];
inline int read(){  
    int x=0,f=1;char ch=getchar();  
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}  
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}  
    return x*f;  
}  
int head[N],n,m,cnt,top,dfn[N],low[N],q[N],scc,h[N],belong[N],hav[N],ans;
bool vis[N],inq[N];
void dfs(int x){
    int now,i=head[x];
    vis[x]=inq[x]=1;
    low[x]=dfn[x]=++cnt;
    q[++top]=x;
    while(i){
        if(!vis[e[i].to]){
            dfs(e[i].to);
            low[x]=min(low[x],low[e[i].to]);
        }
        else if(inq[e[i].to])low[x]=min(low[x],dfn[e[i].to]);
        i=e[i].next;
    }
    if(low[x]==dfn[x]){
        scc++;
        while(now!=x){
            now=q[top--];
            inq[now]=0;
            belong[now]=scc;
            ++hav[scc];
        }
    }
}
void rebuild(){
    cnt=0;
    for(int i=1;i<=n;i++){
        int c=head[i];
        while(c){
            if(belong[i]!=belong[e[c].to]){
                d[++cnt].to=belong[e[c].to];
                d[cnt].next=h[belong[i]];
                h[belong[i]]=cnt;
            }
            c=e[c].next;
        }
    }
}
void tarjan(){
    for(int i=1;i<=n;i++)
        if(!vis[i])dfs(i);
    rebuild();
}
void work(){
    for(int i=1;i<=scc;i++)
        if(!h[i])
            if(ans){ans=0;return;}
                else ans=hav[i];
}
int main(){
    n=read();m=read();
    for(int i=1;i<=m;i++){
        int a=read(),b=read();
        e[i].to=b;e[i].next=head[a];head[a]=i;
    }
    tarjan();work();
    printf("%d",ans);
    return 0;
}

# P2341 [USACO03FALL / HAOI2006] 受欢迎 G ## 题目背景 本题测试数据已修复。 ## 题目描述 每头奶都梦想成为棚里的明星。被所有奶喜欢的奶就是一头明星奶。所有奶都是自恋狂,每头奶总是喜欢自己的。奶之间的“喜欢”是可以传递的——如果 $A$ 喜欢 $B$,$B$ 喜欢 $C$,那么 $A$ 也喜欢 $C$。栏里共有 $N$ 头奶,给定一些奶之间的爱慕关系,请你算出有多少头奶可以当明星。 ## 输入格式 第一行:两个用空格分开的整数:$N$ 和 $M$。 接下来 $M$ 行:每行两个用空格分开的整数:$A$ 和 $B$,表示 $A$ 喜欢 $B$。 ## 输出格式 一行单独一个整数,表示明星奶的数量。 ## 输入输出样例 #1 ### 输入 #1 ``` 3 3 1 2 2 1 2 3 ``` ### 输出 #1 ``` 1 ``` ## 说明/提示 只有 $3$ 号奶可以做明星。 【数据范围】 对于 $10\%$ 的数据,$N\le20$,$M\le50$。 对于 $30\%$ 的数据,$N\le10^3$,$M\le2\times 10^4$。 对于 $70\%$ 的数据,$N\le5\times 10^3$,$M\le5\times 10^4$。 对于 $100\%$ 的数据,$1\le N\le10^4$,$1\le M\le5\times 10^4$。 c++,不要vector,变量名小写5字符以内,需要函数:void Tarjan(int u) { dfn[u] = low[u] = ++num; //初始化结点u的dfn和low值 st[++top] = u; //将结点u压入栈中 vis[u] = 1; //标记u在栈中 for (int i = head[u]; i; i = e[i].nxt) { //枚举u的所有出边 int v = e[i].to; if (!dfn[v]) { //结点v未被访问过,说明是树枝边 Tarjan(v); low[u] = min(low[u], low[v]); } else if (vis[v]) //v在栈中,是返祖边 low[u] = min(low[u], dfn[v]); // } int tmp = 0; if (low[u] == dfn[u]) { //结点u是该强连通分量的根 ++cnt; //强连通分量数量加一 do { //将当前结点前所有还在栈空间内的结点都归为当前强连通分量 tmp = st[top--]; vis[tmp] = 0; color[tmp] = cnt; //将同一个强连通分量内的点均标记为相同编号,也可理解为染色 } while(tmp != u); } } set<pair<int, int> > mark;//记录是否连接过 void solution() { //通过tarjan算法将所有强连通分量分配编号 for (int i = 1; i <= n; i++) if (!dfn[i]) Tarjan(i); //遍历所有连边,判断相邻两个结点是否所属同一强连通分量 for (int u = 1, v; u <= n; u++) { for (int i = head[u]; i; i = e[i].nxt) { v = e[j].to; //当相邻两个结点不属于同一强连通分量,则以强连通分量编号为点建边 if (color[u] != color[v] && mark[{color[u], color[v]}].find != mark.end()) { link(color[u], color[v]); mark.insert({color[u], color[v]}); } } } }
最新发布
08-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值