[HAOI2006]受欢迎的牛

首先我们要理解题意(一开始没想清楚)  后面才想明白

以下3点很重要!!!

1,如果明星牛羡慕其它牛,那么被它羡慕的牛也一定会羡慕它,然后构成一个环,所以我们可以考虑tarjan缩点

2,缩点以后出度为0的即为明星牛,反之出度不为0的肯定不是明星牛,因为to[x]不羡慕x

3,如果缩点后不止一个点出度为0的话就没有明星牛,因为这n个出度为0的点不能相互羡慕

搞懂了以上三点思路就清晰了

tarjan缩点,tarjan的过程中要记下一个缩掉的环中点的个数

最后有明星牛就输出个数,没有就输出0


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <stack>
#define maxn 100001
using namespace std;
int n,m,next[maxn],to[maxn],st[maxn],low[maxn],dfn[maxn],scc[maxn],out[maxn],topt,num,ans,nn[maxn];
bool f[maxn];
void add(int x,int y)
{
    to[++topt]=y;
    next[topt]=st[x];
    st[x]=topt;
}
stack<int>s;
void tarjan(int x)
{
    dfn[x]=low[x]=++topt;
    f[x]=1; s.push(x); int p=st[x];
    while (p)
    {
        if (!dfn[to[p]]) {tarjan(to[p]); low[x]=min(low[x],low[to[p]]);}
         else if (f[to[p]]) low[x]=min(low[x],dfn[to[p]]);
        p=next[p];
    }
    if (dfn[x]==low[x])
    {
        num++;
        while (s.top()!=x)
        {
            f[s.top()]=1;
            scc[s.top()]=num;
            s.pop();
            nn[num]++;
        }
        f[s.top()]=1;
        scc[s.top()]=num;
        s.pop();
        nn[num]++;
    }
}
int main()
{
    scanf("%d%d",&n,&m);
    for (int i=1;i<=m;i++) {int xx,yy; scanf("%d%d",&xx,&yy); add(xx,yy);}
    topt=0;
    for (int i=1;i<=n;i++) if (!dfn[i]) tarjan(i);
    for (int i=1;i<=n;i++)
    {
        int p=st[i];
        while (p)
        {
            if (scc[i]!=scc[to[p]]) out[scc[i]]++;
            p=next[p];
        }
    }
    int judge=0;
    for (int i=1;i<=num;i++)if (out[i]==0) judge++,ans=nn[i];
    if (judge!=1) printf("0");else 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、付费专栏及课程。

余额充值