zoukankan      html  css  js  c++  java
  • bzoj1051 [HAOI2006]受欢迎的牛

    [HAOI2006]受欢迎的牛

    Time Limit: 10 Sec Memory Limit: 162 MB

    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

    <br >

    大概就是又想起tarjan了。。。。顺手写了一发,1A还是很开心的啦~~~

    
    #include<bits/stdc++.h>
    using namespace std;
    const int maxn = 1e4 + 5;
    struct lpl{
    	int kind, pre, low;
    }ld[maxn];
    vector<int> point[maxn];
    stack<int> s;
    int n, m, cnt, knt, num[maxn];
    bool flag[maxn];
    
    inline void putit()
    {
    	int a, b;
    	scanf("%d%d", &n, &m);
    	for(int i = 1; i <= m; ++i){
    		scanf("%d%d", &a, &b);
    		point[a].push_back(b);
    	}
    }
    
    void tarjan(int t)
    {
    	s.push(t);
    	ld[t].pre = ld[t].low = ++cnt;
    	for(int i = point[t].size() - 1; i >= 0; --i){
    		int now = point[t][i];
    		if(ld[now].pre == 0){
    			tarjan(now);
    			ld[t].low = min(ld[t].low, ld[now].low);
    		}
    		else if(ld[now].kind == 0){
    			ld[t].low = min(ld[t].low, ld[now].low);	
    		}	
    	}
    	if(ld[t].low == ld[t].pre){
    		int now; knt++;
    		for(;;){
    			now = s.top(); s.pop();
    			ld[now].kind = knt;
    			num[knt]++;
    			if(now == t) break;
    		}
    	}
    }
    
    inline void workk()
    {
    	for(int i = 1; i <= n; ++i){
    		for(int j = point[i].size() - 1; j >= 0; --j){
    			if(ld[i].kind != ld[point[i][j]].kind){
    				flag[ld[i].kind] = true;
    			}
    		}
    	}
    	for(int i = 1; i <= n; ++i) 
    		if(flag[i] == false){
    			printf("%d", num[i]);
    			return;
    		}
    }
    
    int main()
    {
    	putit();
    	for(int i = 1; i <= n; ++i) if(ld[i].kind == 0) tarjan(i);
    	workk();
    	return 0;
    }
    
    
    心如花木,向阳而生。
  • 相关阅读:
    hdu 1195 Open the Lock
    hdu 1180 诡异的楼梯
    hdu 1175 连连看
    背包(采药)
    LCS模板,求长度,并记录子串
    hdu 1159 Common Subsequence
    hdu 1231 最大连续子序列
    DP的简单应用
    hdu 1728 逃离迷宫
    点击页面其他地方,关闭弹框
  • 原文地址:https://www.cnblogs.com/LLppdd/p/9040337.html
Copyright © 2011-2022 走看看