zoukankan      html  css  js  c++  java
  • POJ-1523-SPF(求割点)

    链接:

    https://vjudge.net/problem/POJ-1523#author=0

    题意:

    Consider the two networks shown below. Assuming that data moves around these networks only between directly connected nodes on a peer-to-peer basis, a failure of a single node, 3, in the network on the left would prevent some of the still available nodes from communicating with each other. Nodes 1 and 2 could still communicate with each other as could nodes 4 and 5, but communication between any other pairs of nodes would no longer be possible.

    Node 3 is therefore a Single Point of Failure (SPF) for this network. Strictly, an SPF will be defined as any node that, if unavailable, would prevent at least one pair of available nodes from being able to communicate on what was previously a fully connected network. Note that the network on the right has no such node; there is no SPF in the network. At least two machines must fail before there are any pairs of available nodes which cannot communicate.

    思路:

    dfs求割点。每从一个点扩展出去一个割点,Num就加1,记录一个联通快。
    最后答案加1,记录父亲的联通快。

    代码:

    #include <iostream>
    #include <cstdio>
    #include <vector>
    #include <memory.h>
    #include <queue>
    #include <set>
    #include <map>
    #include <algorithm>
    #include <math.h>
    #include <stack>
    using namespace std;
    typedef long long LL;
    const int MAXN = 1e3+10;
    
    int Num[MAXN];
    int First[MAXN];
    int To[MAXN], Next[MAXN];
    int Dfn[MAXN], Low[MAXN];
    int s = 1e3+10, e = 0;
    int times, line;
    
    void Dfs(int u, int v)
    {
        Dfn[v] = Low[v] = ++times;
        int son = 0;
        for (int i = First[v];i != 0;i = Next[i])
        {
            int node = To[i];
            if (node == u)
                continue;
            if (Dfn[node] == 0)
            {
                son++;
                Dfs(v, node);
                Low[v] = min(Low[v], Low[node]);
                if ((v == s && son >= 2) || (v != s && Low[v] >= Dfn[v]))
                    Num[v]++;
            }
            else
                Low[v] = min(Low[v], Dfn[node]);
        }
    }
    
    void Init()
    {
        memset(Dfn, 0, sizeof(Dfn));
        memset(First, 0, sizeof(First));
        memset(To, 0, sizeof(To));
        memset(Next, 0, sizeof(Next));
        memset(Num, 0, sizeof(Num));
        times = line = 0;
        s = 1e3+10;
    }
    
    void AddEdge(int u, int v)
    {
        To[++line] = v;
        Next[line] = First[u];
        First[u] = line;
        To[++line] = u;
        Next[line] = First[v];
        First[v] = line;
    }
    
    int main()
    {
        int l, r;
        int cnt = 0;
        while (~scanf("%d", &l)&& l)
        {
            scanf("%d", &r);
            s = min(s, l);
            s = min(s, r);
            AddEdge(l, r);
            while (~scanf("%d", &l)&& l)
            {
                scanf("%d", &r);
                s = min(s, l);
                s = min(s, r);
                AddEdge(l, r);
            }
            Dfs(0, s);
            printf("Network #%d
    ", ++cnt);
            bool flag = true;
            for (int i = 1;i <= 1000;i++)
            {
                if (Num[i])
                {
                    printf("  SPF node %d leaves %d subnets
    ", i, Num[i]+1);
                    flag = false;
                }
            }
            if (flag)
                printf("  No SPF nodes
    ");
            printf("
    ");
            Init();
        }
    
        return 0;
    }
    
  • 相关阅读:
    笔试题
    js在IE和FF下的兼容性问题
    Textarea高度随内容自适应地增长,无滚动条
    请让页面中的一个元素(10px*10px)围绕坐标(200, 300) 做圆周运动
    一次点击两次触发addEventListener
    html 1.0 鼠标放上去会亮 onmouseover onmouseout 以及this标签的使用
    提示框一段时间以后消失setTimeout
    两种定时器 setInterval(一直执行) setTimeout(只执行一次)
    其他标签a实现提交功能
    创建标签的两种方法insertAdjacentHTML 和 createElement 创建标签 setAttribute 赋予标签类型 appendChild 插入标签
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11188289.html
Copyright © 2011-2022 走看看