poj2186 Popular cows

Popularcows

题目背景:

poj2186

分析:题目本身简单,从每头牛向他们所喜欢的牛连边,考虑这么一个问题,所有的满足题目受欢迎的条件,则这些牛都在同一个强连通分量中,并且,这个SCC一定是出度为0的,所以我们要求的就是那个SCC中的点的个数即可,但是注意,可能出现获得的图并不联通的情况,这个时候答案直接为NO,因为不可能有符合题目的牛。

Source:

#include 
      
      
       
       
#include 
       
       
        
        
#include 
        
        
         
         
#include 
         
         
          
          
#include 
          
          
            #include 
           
             #include 
            
              #include 
             
               using namespace std; inline char read() { static const int IN_LEN = 1024 * 1024; static char buf[IN_LEN], *s, *t; if (s == t) { t = (s = buf) + fread(buf, 1, IN_LEN, stdin); if (s == t) return -1; } return *s++; } template 
              
                inline bool R(T &x) { static char c; static bool iosig; for (c = read(), iosig = false; !isdigit(c); c = read()) { if (c == -1) return false; if (c == '-') iosig = true; } for (x = 0; isdigit(c); c = read()) x = (x << 3) + (x << 1) + (c ^ '0'); if (iosig) x = -x; return true; } const int OUT_LEN = 1024 * 1024; char obuf[OUT_LEN], *oh = obuf; inline void writechar(char c) { if (oh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf; *oh++ = c; } template 
               
                 inline void W(T x) { static int buf[30], cnt; if (!x) writechar(48); else { if (x < 0) writechar('-'), x = -x; for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48; while (cnt) writechar(buf[cnt--]); } } inline void flush() { fwrite(obuf, 1, oh - obuf, stdout); } const int MAXN = 100000 + 10; const int MAXM = 1000000 + 10; stack 
                
                  s; int n, m, x, y, tot, ind, ccnt; int first[MAXN], outdegree[MAXN], scc[MAXN], ods[MAXN], scnt[MAXN], num[MAXN]; int low[MAXN]; bool exist[MAXN]; struct node { int next, to; } edge[MAXM]; inline void create(int x, int y) { tot++; edge[tot].next = first[x]; first[x] = tot; edge[tot].to = y; } inline void readin() { R(n), R(m); for (int i = 1; i <= m; ++i) { R(x), R(y); outdegree[x]++; create(x, y); } } void dfs(int cur) { ind++; num[cur] = ind; low[cur] = ind; s.push(cur); exist[cur] = true; for (int p = first[cur]; p; p = edge[p].next) { if (num[edge[p].to] == 0) { dfs(edge[p].to); low[cur] = min(low[cur], low[edge[p].to]); } else { if (exist[edge[p].to]) low[cur] = min(low[cur], num[edge[p].to]); } } if (num[cur] == low[cur]) { int o = s.top(); ccnt++; while (o != cur) { scc[o] = ccnt; ods[ccnt] += outdegree[o]; exist[o] = false; s.pop(); o = s.top(); scnt[ccnt]++; } s.pop(); scc[o] = ccnt; ods[ccnt] += outdegree[o]; exist[o] = false; scnt[ccnt]++; } } inline void work() { for (int i = 1; i <= n; ++i) if (!num[i]) dfs(i); for (int i = 1; i <= n; ++i) for (int p = first[i]; p; p = edge[p].next) if (scc[i] == scc[edge[p].to]) ods[scc[i]]--; int ans = 0; bool flag = true; for (int i = 1; i <= ccnt; ++i) if (ods[i] == 0) { if (ans) flag = false; else ans = i; } if (flag) W(scnt[ans]); else writechar(48); } int main() { readin(); work(); flush(); return 0; } 
                 
                
               
              
             
            
          
         
         
        
        
       
       
      
      

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值