zoukankan      html  css  js  c++  java
  • poj 1144 Network(无向图求割顶数)

      题目链接:poj 1144

      题意就是说有 n(标号为 1 ~ n)个网点连接成的一个网络,critical places 表示删去后使得图不连通的顶点,也就是割顶,求图中割顶的个数。

      直接上大白书上的模板即可,只是输入也有点卡人,我竟然傻傻的用手写的输入挂来处理,看了别人的博客才知道用 scanf("%s") 即可,因为 scanf("%s") 不会读入空格,再适当处理下即可。

      我的代码是:

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 #include<cmath>
     5 #include<vector>
     6 using namespace std;
     7 const int N = 103;
     8 
     9 vector<int> G[N];
    10 int pre[N], low[N], dfs_clock;
    11 bool iscut[N];
    12 
    13 inline void add_edge(int from , int to) {
    14     G[from].push_back(to);
    15     G[to].push_back(from);
    16 }
    17 
    18 int dfs(int u, int fa) {
    19     int lowu = pre[u] = ++dfs_clock;
    20     int child = 0;
    21     for(int i = 0; i < G[u].size(); ++i) {
    22         int v = G[u][i];
    23         if(!pre[v]) {
    24             ++child;
    25             int lowv = dfs(v,u);
    26             lowu = min(lowu, lowv);
    27             if(lowv >= pre[u])
    28                 iscut[u] = 1;
    29         }
    30         else if(pre[v] < pre[u] && v != fa)
    31             lowu = min(lowu, pre[v]);
    32     }
    33     if(fa < 0 && child == 1)   iscut[u] = 0;
    34     return low[u] = lowu;
    35 }
    36 
    37 inline bool isline(const char &ch) {
    38     return ch == '
    ' || ch == '
    ';
    39 }
    40 
    41 #include<cctype>
    42 bool eol;
    43 inline void read(int &x) {
    44     x = 0;
    45     eol = 0;
    46     char ch = getchar();
    47     while(!isdigit(ch))
    48         ch = getchar();
    49     while(isdigit(ch)) {
    50         x = x * 10 + (ch - '0');
    51         ch = getchar();
    52     }
    53     if(isline(ch))   eol = 1;
    54 }
    55 
    56 int main() {
    57     int n,x;
    58     while(~scanf("%d",&n),n) {
    59         for(int i = 0; i < 101; ++i)
    60             G[i].clear();
    61         memset(pre,0,sizeof(pre));
    62         memset(iscut,0,sizeof(iscut));
    63         dfs_clock = 0;
    64 
    65         while(1) {
    66             eol = 0;
    67             read(x);
    68             if(!x) {
    69                 for(int i = 1; i <= n; ++i)
    70                     if(!pre[i])   dfs(i, -1);
    71                 int ans = 0;
    72                 for(int i = 1; i <= n; ++i)
    73                     if(iscut[i])   ++ans;
    74                 printf("%d
    ",ans);
    75                 break;
    76             }
    77             else {
    78                 int u = x;
    79                 while(!eol) {
    80                     read(x);
    81                     add_edge(u,x);
    82                 }
    83             }
    84         }
    85     }
    86     return 0;
    87 }
  • 相关阅读:
    简述at和crontab命令
    自建yum仓库,分别为网络源和本地源
    简述rpm与yum命令的常见选项
    用户目录权限管理.手动添加用户.截取用户信息
    总结描述用户和组管理类命令的使用方法,系统用户相关信息,取出主机IP地址
    Android独立交叉编译环境搭建
    Python编程总结之常用三方模块
    GDB常用命令简介
    linux内核中task_struct与thread_info及stack三者的关系
    在Linux-PC上建立kdump调试环境
  • 原文地址:https://www.cnblogs.com/Newdawn/p/4725034.html
Copyright © 2011-2022 走看看