poj3177 Redundant Paths

Redundant Paths

题目背景:

poj3177

分析:题目本身不难,直接求取边双联通分量,然后计算出其中度数为1的DCC的个数即可,注意统计度数的时候是把无向边正反全部利用了的,所以统计的应该是度为2的DCC。

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 = 3000 + 10; const int MAXM = MAXN * MAXN; stack 
                
                  s; int n, m, x, y, tot, cnt, ind; int first[MAXN], scc[MAXN], num[MAXN], low[MAXN]; int scnt[MAXN]; bool exist[MAXN]; struct node { int next, to; } edge[MAXM]; inline void create(int x, int y) { edge[tot].next = first[x]; first[x] = tot; edge[tot].to = y; tot++; } inline void readin() { memset(first, -1, sizeof(first)); R(n), R(m); for (int i = 1; i <= m; ++i) { R(x), R(y); create(x, y), create(y, x); } } void dfs(int cur, int father) { ind++, num[cur] = ind, low[cur] = ind; exist[cur] = true, s.push(cur); for (int p = first[cur]; ~p; p = edge[p].next) { if (!num[edge[p].to]) dfs(edge[p].to, p), low[cur] = min(low[edge[p].to], low[cur]); else if (exist[edge[p].to] && father != (p ^ 1)) low[cur] = min(num[edge[p].to], low[cur]); } if (num[cur] == low[cur]) { int o = s.top(); cnt++; while (o != cur) scc[o] = cnt, exist[o] = false, s.pop(), o = s.top(); scc[o] = cnt, exist[o] = false, s.pop(); } } void work() { for (int i = 1; i <= n; ++i) if (!num[i]) dfs(i, first[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]) scnt[scc[i]]++, scnt[scc[edge[p].to]]++; int ccnt = 0; for (int i = 1; i <= cnt; ++i) if (scnt[i] == 2) ccnt++; W(((ccnt + 1) / 2)); } int main() { readin(); work(); flush(); return 0; } 
                 
                
               
              
             
            
          
         
         
        
        
       
       
      
      

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值