分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 前端开发

UVA315 Network —— 割点

发布时间:2023-09-06 01:18责任编辑:傅花花关键词:暂无标签

题目链接:https://vjudge.net/problem/UVA-315

A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting several places numbered by integers from 1 to N. No two places have the same number. The lines are bidirectional and always connect together two places and in each place the lines end in a telephone exchange. There is one telephone exchange in each place. From each place it is possible to reach through lines every other place, however it need not be a direct connection, it can go through several exchanges. From time to time the power supply fails at a place and then the exchange does not operate. The officials from TLC realized that in such a case it can happen that besides the fact that the place with the failure is unreachable, this can also cause that some other places cannot connect to each other. In such a case we will say the place (where the failure occured) is critical. Now the officials are trying to write a program for finding the number of all such critical places. Help them.

Input

The input file consists of several blocks of lines. Each block describes one network. In the first line of each block there is the number of places N < 100. Each of the next at most N lines contains the number of a place followed by the numbers of some places to which there is a direct line from this place. These at most N lines completely describe the network, i.e., each direct connection of two places in the network is contained at least in one row. All numbers in one line are separated by one space. Each block ends with a line containing just ‘0’. The last block has only one line with N = 0.

Output

The output contains for each block except the last in the input file one line containing the number of critical places. Sample Input 5 5 1 2 3 4 0 6 2 1 3 5 4 6 2 0 0 Sample Output 1 2

代码如下:

 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <vector> 7 #include <queue> 8 #include <stack> 9 #include <map>10 #include <string>11 #include <set>12 #define ms(a,b) memset((a),(b),sizeof((a)))13 using namespace std;14 typedef long long LL;15 const double EPS = 1e-8;16 const int INF = 2e9;17 const LL LNF = 2e18;18 const int MAXN = 1e2+10;19 20 struct Edge21 {22 ????int to, next;23 }edge[MAXN*MAXN*2];24 int tot, head[MAXN];25 26 int Index, DFN[MAXN], Low[MAXN];27 bool cut[MAXN];28 29 void addedge(int u, int v)30 {31 ????edge[tot].to = v;32 ????edge[tot].next = head[u];33 ????head[u] = tot++;34 }35 36 void Tarjan(int u, int pre)37 {38 ????DFN[u] = Low[u] = ++Index;39 ????int son = 0;40 ????for(int i = head[u]; i!=-1; i = edge[i].next)41 ????{42 ????????int v = edge[i].to;43 ????????if(v==pre) continue;44 ????????if(!DFN[v])45 ????????{46 ????????????son++;47 ????????????Tarjan(v, u);48 ????????????Low[u] = min(Low[u], Low[v]);49 ????????????if(u!=pre && Low[v]>=DFN[u])50 ????????????????cut[u] = true;51 ????????}52 ????????else53 ????????????Low[u] = min(Low[u], DFN[v]);54 ????}55 56 ????if(u==pre && son>1) cut[u] = true;57 }58 59 void init()60 {61 ????tot = 0;62 ????memset(head, -1, sizeof(head));63 64 ????Index = 0;65 ????memset(DFN, 0, sizeof(DFN));66 ????memset(Low, 0, sizeof(Low));67 ????memset(cut, false, sizeof(cut));68 }69 70 int main()71 {72 ????int n;73 ????while(scanf("%d", &n) &&n)74 ????{75 ????????init();76 ????????int u, v;77 ????????while(scanf("%d", &u) && u)78 ????????{79 ????????????while(getchar()!=‘\n‘)80 ????????????{81 ????????????????scanf("%d", &v);82 ????????????????addedge(u, v);83 ????????????????addedge(v, u);84 ????????????}85 ????????}86 87 ????????Tarjan(1, 1);88 ????????int ans = 0;89 ????????for(int i = 1; i<=n; i++)90 ????????????if(cut[i]) ans++;91 92 ????????printf("%d\n", ans);93 ????}94 }
View Code

UVA315 Network —— 割点

原文地址:http://www.cnblogs.com/DOLFAMINGO/p/7679809.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved