[bzoj1051]Popular Cows

刚刚被ysy在联考里虐了,差点爆tan(pi/4),只好来bzoj寻求安慰再被虐一次233

(tarjan是什么智障东西不想打我好弱啊,tarjan都不会打)

Description

  每一头牛的愿望就是变成一头最受欢迎的牛。现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎。 这

种关系是具有传递性的,如果A认为B受欢迎,B认为C受欢迎,那么牛A也认为牛C受欢迎。你的任务是求出有多少头

牛被所有的牛认为是受欢迎的。

Input

  第一行两个数N,M。 接下来M行,每行两个数A,B,意思是A认为B是受欢迎的(给出的信息有可能重复,即有可

能出现多个A,B)

Output

  一个数,即有多少头牛被所有的牛认为是受欢迎的。

Sample Input

3 3

1 2

2 1

2 3

Sample Output

1

HINT

100%的数据N<=10000,M<=50000

这道题很裸的SCC吧,鉴于网上tarjan的code太多了,我还是打一个丑丑的dfs凑个数吧(tarjan异端中的一股清流)

要注意图不联通的情况,这时候没有牛受欢迎。

代码:

#include<vector>
#include<cstdio>
#include<cstring>
using std::vector;
const int MAX_V=10000;
int V,M;
vector<int>G[MAX_V],rG[MAX_V];
vector<int>vs;
bool used[MAX_V];
int cmp[MAX_V];
void add_edge(int s,int t){G[s].push_back(t); rG[t].push_back(s);}
void dfs(int v){
    used[v]=1;
    for(int i=0;i<G[v].size();i++)if(!used[G[v][i]])dfs(G[v][i]);
    vs.push_back(v);
}
void rdfs(int v,int k){
    used[v]=1;
    cmp[v]=k;
    for(int i=0;i<rG[v].size();i++)if(!used[rG[v][i]])rdfs(rG[v][i],k);
}
int scc(){
    memset(used,0,sizeof(used));
    for(int v=0;v<V;v++)if(!used[v])dfs(v);
    memset(used,0,sizeof(used));
    int k=0;
    for(int i=vs.size()-1;i>=0;i--)if(!used[vs[i]])rdfs(vs[i],k++);
    return k;
}
int main(){
    scanf("%d%d",&V,&M);
    for(int i=0;i<M;i++){
        int a,b;scanf("%d%d",&a,&b);add_edge(a-1,b-1);
    }
    int n=scc(),u=0,num=0;
    for(int v=0;v<V;v++)if(cmp[v]==n-1)u=v,num++;
    memset(used,0,sizeof(used));
    rdfs(u,0);
    for(int i=0;i<V;i++)
        if(!used[i]){
            num=0;
            break;
        }
    printf("%d\n",num);
    return 0;
}

时间: 2024-08-01 04:33:59

[bzoj1051]Popular Cows的相关文章

POJ 2186 Popular Cows(Targin缩点)

传送门 Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 31808   Accepted: 12921 Description Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10,000) cows, you are given up to M (1 <=

POJ2186 Popular Cows 【强连通分量】+【Kosaraju】+【Tarjan】+【Garbow】

Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 23445   Accepted: 9605 Description Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10,000) cows, you are given up to M (1 <= M &l

【图论】Popular Cows

[POJ2186]Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 34752   Accepted: 14155 Description Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10,000) cows, you are given up to M (1

Popular Cows

传送门(poj):http://poj.org/problem?id=2186 (bzoj):http://www.lydsy.com/JudgeOnline/problem.php?id=1051 Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 33482   Accepted: 13638 Description Every cow's dream is to become the most

poj2186 Popular Cows --- 强连通

给一个有向图,问有多少结点是其他所有结点都可以到达的. 等价于,在一个有向无环图上,找出度为0 的结点,如果出度为0的结点只有一个,那么这个就是答案,如果大于1个,则答案是0. 这题有环,所以先缩点.求唯一出度为0的强连通分量. #include<cstdio> #include<cstring> #include<vector> #include<queue> #include<iostream> #define inf 0x3f3f3f3f

POJ2186 Popular Cows【Kosaraju】【强连通分量】

Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24266Accepted: 9954 Description Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10,000) cows, you are given up to M (1 <= M <= 5

强连通分量tarjan缩点——POJ2186 Popular Cows

这里的Tarjan是基于DFS,用于求有向图的强联通分量. 运用了一个点dfn时间戳和low的关系巧妙地判断出一个强联通分量,从而实现一次DFS即可求出所有的强联通分量. §有向图中, u可达v不一定意味着v可达u.    相互可达则属于同一个强连通分量    (Strongly Connected Component, SCC) §有向图和它的转置的强连通分量相同 §所有SCC构成一个DAG(有向无环图) dfn[u]为节点u搜索的次序编号(时间戳),即首次访问u的时间 low[u]为u或u的

POJ - 2186 - Popular Cows (tarjan)

Popular Cows 题目传送:Popular Cows 思路:tarjan算法求强连通分量 AC代码: #include <map> #include <set> #include <cmath> #include <deque> #include <queue> #include <stack> #include <cstdio> #include <cctype> #include <strin

POJ 2186 Popular Cows

Popular Cows Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 2186 64-bit integer IO format: %lld      Java class name: Main Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <