poj1523 SPF

SPF

题目背景:

poj1523     

分析:直接判定割点,然后针对每一个个点进行一次dfs之后就可以确定每一个割点会将图像分为几部分。详见代码。

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 = 5000 + 10; const int MAXM = 1000000 + 10; stack 
                
                  s; int first[MAXN << 1], low[MAXN << 1], num[MAXN << 1], scc[MAXN << 1]; int scnt[MAXN << 1]; int n, m, x, y, z, cnt, tot, ind; bool exist[MAXN << 1], cut[MAXN << 1], vis[MAXN << 1]; 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; } void dfs(int cur, int fa) { int child = 0; num[cur] = low[cur] = ++ind; for(int p = first[cur]; p; p = edge[p].next) { if(!num[edge[p].to]) { child++; dfs(edge[p].to, cur), low[cur] = min(low[cur], low[edge[p].to]); if(cur == 1) cut[1] = (child >= 2 ? true : false); else if(low[edge[p].to] >= num[cur]) cut[cur] = true; } else if(edge[p].to != fa) low[cur] = min(low[cur], num[edge[p].to]); } } void read() { R(y), create(x, y), create(y, x); while(true) { R(x); if(!x) break; R(y), create(x, y), create(y, x); } } void dfs2(int cur) { for(int p = first[cur]; p; p = edge[p].next) if(!vis[edge[p].to]) vis[edge[p].to] = true, dfs2(edge[p].to); } int dfs1(int cur) { int cnt = 0; vis[cur] = true; for(int p = first[cur]; p; p = edge[p].next) if(!vis[edge[p].to]) vis[edge[p].to] = true, cnt++, dfs2(edge[p].to); return cnt; } void work(int cas) { dfs(1, 1); bool flag = false; cout << "Network #" << cas <<'\n'; for(int i = 1; i <= 1000; ++i) { int cnt = 0; if(!cut[i]) continue; memset(vis, false, sizeof(vis)), cnt = dfs1(i); flag = true; cout << " SPF node" << " " << i << " " <<"leaves " << cnt << " subnets" << '\n'; } if(!flag) cout << " No SPF nodes" << "\n"; cout << '\n'; } void clear() { tot = 0; memset(first, 0, sizeof(first)); memset(num, 0 ,sizeof(num)); memset(cut, false, sizeof(cut)) ; } int main() { int cas = 0; while(true) { R(x); if(!x) break; read(); work(++cas); clear(); } return 0; } 
                 
                
               
              
             
            
          
         
         
        
        
       
       
      
      

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值