题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=251

求割点,除了输入用strtok和sscanf处理输入以外,对于求割点的tarjan算法有了进一步理解。

特别注意88行,如果u是根并且至少两个儿子,那它一定是割点无误,还有第二个情况用如图代表:

这个例子里显然:low[4]=2,dfn[4]=4,dfn[3]=3。现dfs到3点位置了,4是3的儿子,假如3是割点,那删掉3后4和2依然连通,因此3不是割点。判断依据可以是low[4]<dfn[3]。

假如low[4]>=dfn[3]的话,那3就是割点了。

  1 /*
  2 ━━━━━┒ギリギリ♂ eye!
  3 ┓┏┓┏┓┃キリキリ♂ mind!
  4 ┛┗┛┗┛┃\○/
  5 ┓┏┓┏┓┃ /
  6 ┛┗┛┗┛┃ノ)
  7 ┓┏┓┏┓┃
  8 ┛┗┛┗┛┃
  9 ┓┏┓┏┓┃
 10 ┛┗┛┗┛┃
 11 ┓┏┓┏┓┃
 12 ┛┗┛┗┛┃
 13 ┓┏┓┏┓┃
 14 ┃┃┃┃┃┃
 15 ┻┻┻┻┻┻
 16 */
 17 #include <algorithm>
 18 #include <iostream>
 19 #include <iomanip>
 20 #include <cstring>
 21 #include <climits>
 22 #include <complex>
 23 #include <fstream>
 24 #include <cassert>
 25 #include <cstdio>
 26 #include <bitset>
 27 #include <vector>
 28 #include <deque>
 29 #include <queue>
 30 #include <stack>
 31 #include <ctime>
 32 #include <set>
 33 #include <map>
 34 #include <cmath>
 35
 36 using namespace std;
 37
 38 #define fr first
 39 #define sc second
 40 #define cl clear
 41 #define BUG puts("here!!!")
 42 #define W(a) while(a--)
 43 #define pb(a) push_back(a)
 44 #define Rint(a) scanf("%d", &a)
 45 #define Rll(a) scanf("%lld", &a)
 46 #define Rs(a) scanf("%s", a)
 47 #define Cin(a) cin >> a
 48 #define FRead() freopen("in", "r", stdin)
 49 #define FWrite() freopen("out", "w", stdout)
 50 #define Rep(i, len) for(int i = 0; i < (len); i++)
 51 #define For(i, a, len) for(int i = (a); i < (len); i++)
 52 #define Cls(a) memset((a), 0, sizeof(a))
 53 #define Clr(a, x) memset((a), (x), sizeof(a))
 54 #define Full(a) memset((a), 0x7f7f, sizeof(a))
 55 #define lrt rt << 1
 56 #define rrt rt << 1 | 1
 57 #define pi 3.14159265359
 58 #define RT return
 59 typedef long long LL;
 60 typedef long double LD;
 61 typedef unsigned long long ULL;
 62 typedef pair<int, int> pii;
 63 typedef pair<string, int> psi;
 64 typedef map<string, int> msi;
 65 typedef vector<int> vi;
 66 typedef vector<LL> vl;
 67 typedef vector<vl> vvl;
 68 typedef vector<bool> vb;
 69
 70 const int maxn = 220;
 71 char str[66666];
 72 int n, m, rt, bcnt;
 73 int G[maxn][maxn];
 74 int dfn[maxn], low[maxn], vis[maxn];
 75 int in[maxn];
 76 bool cut[maxn];
 77 vi e[maxn];
 78 int bridge[maxn][3];
 79
 80 void dfs(int u, int d) {
 81     int son = 0;
 82     vis[u] = 1; dfn[u] = low[u] = d;
 83     Rep(i, e[u].size()) {
 84         int v = e[u][i];
 85         if(!vis[v]) {
 86             dfs(v, d+1); son++;
 87             low[u] = min(low[u], low[v]);
 88             if((u==rt&&son>1)||(u!=rt&&low[v]>=dfn[u])) cut[u] = 1;
 89         }
 90         else low[u] = min(low[u], dfn[v]);
 91     }
 92 }
 93
 94 int main() {
 95     // FRead();
 96     int u, v;
 97     while(~Rint(n) && n) {
 98         Cls(G); Cls(dfn); Cls(low); Cls(in);
 99         Cls(vis); Cls(bridge); Cls(cut); bcnt = 0;
100         Rep(i, n+5) e[i].cl();
101         getchar();
102         while(gets(str) && strcmp("0", str)) {
103             char* p = strtok(str, " ");
104             sscanf(p, "%d", &u);
105             p = strtok(NULL, " ");
106             while(p) {
107                 sscanf(p, "%d", &v);
108                 p = strtok(NULL, " ");
109                 G[u][v] = G[v][u] = 1;
110             }
111         }
112         For(i, 1, n+1) {
113             For(j, i+1, n+1) {
114                 if(G[i][j]) {
115                     e[i].push_back(j);
116                     e[j].push_back(i);
117                 }
118             }
119         }
120         rt = 1;
121         int ret = 0;
122         dfs(1, 0);
123         For(i, 1, n+1) if(cut[i]) ret++;
124         printf("%d\n", ret);
125     }
126     RT 0;
127 }

转载于:https://www.cnblogs.com/kirai/p/5515227.html

[UVA315]Network(tarjan, 求割点)相关推荐

  1. UVA - 315 Network(tarjan求割点)

    题目链接:点击查看 题目大意:给出一个由n台电脑互相连接而成的网络系统,其中有一些电脑如果一旦损坏,则会造成整个网络出现缺口,导致某些地方无法互相连通,我们称这种电脑为关键点,题目需要求出有多少个关键 ...

  2. [Codeforces 555E]Case of Computer Network(Tarjan求边-双连通分量+树上差分)

    [Codeforces 555E]Case of Computer Network(Tarjan求边-双连通分量+树上差分) 题面 给出一个无向图,以及q条有向路径.问是否存在一种给边定向的方案,使得 ...

  3. tarjan求割点和桥(割边)

    tarjan求割点和桥 参考博客:tarjan求割点和桥(割边) 例题:割点 代码(重要的地方在代码中都有注释): #include<bits/stdc++.h> #define ll l ...

  4. tarjan求割点和桥(割边)模板

    tanjan算法相关概念 为了与有向图尽可能保持一致,我们将无向图的一条无向边拆分成两条单向边.两条边互为反向边. 从图中一点作为起点,进行DFS搜索遍历图,这样会得到一棵树,我们称之为DFS搜索树, ...

  5. 图论 —— 图的连通性 —— Tarjan 求割点与桥

    [概念] 1.割点 1)割点:删除某点后,整个图变为不连通的两个部分的点 2)割点集合:在一个无向图中删除该集合中的所有点,能使原图变成互不相连的连通块的点的集合 3)点连通度:最小割点集合点数 如上 ...

  6. poj1144 - tarjan求割点

    何为割点?也就是题目中的关键点.在一个无向图中,去掉一个点,这个无向图会变成多个子图,那么这个点就叫做割点 同理,割边也是如此,如果去掉一条边,能让无向图变成多个子图,那么这条边叫做割边,所谓的桥. ...

  7. 【POJ - 1523】SPF(Tarjan求割点,求分割成的连通块数,模板题,tricks)

    题干: Consider the two networks shown below. Assuming that data moves around these networks only betwe ...

  8. Tarjan求割点桥

    概念 1.桥:是存在于无向图中的这样的一条边,如果去掉这一条边,那么整张无向图会分为两部分,这样的一条边称为桥无向连通图中,如果删除某边后,图变成不连通,则称该边为桥. 2.割点:无向连通图中,如果删 ...

  9. POJ 1144 Network (求割点)

    题意: 给定一幅无向图, 求出图的割点. 割点模板:http://www.cnblogs.com/Jadon97/p/8328750.html 分析: 输入有点麻烦, 用stringsteam 会比较 ...

最新文章

  1. 2022年了,PyTorch和TensorFlow选哪个?
  2. 五种开源协议(GPL,LGPL,BSD,MIT,Apache)介绍
  3. Window7新建文件夹后刷新才显示的解决办法
  4. Tomcat 启动提示未发现 APR 的解决方法
  5. Okhttp3-网络请求流程解析
  6. mfc在运行的时候为什么没有实例化_为什么不建议把数据库部署在Docker容器内?...
  7. 怎么样实现对一个对象的深拷贝
  8. 用百度AI的OCR文字识别结合PHP实现了图片的文字识别功能
  9. Tensorflow 卷积神经网络(三)池化与采样
  10. python map lambda表达式_Python的lambda表达式、filter、map、reduce等函数的用法
  11. Safari浏览器(有时没有图片时,提交会出现问题)。
  12. linux安装curl扩展
  13. redis操作之迭代器 hscan
  14. Photoshop实例视频教程
  15. 红外真空离心浓缩仪ZLNS-II
  16. C语言使用fgetc()函数
  17. 逆天改命,Java 反射的黑科技
  18. Three.js从入门到放弃
  19. python软件电脑配置要求-Python实现的读取电脑硬件信息功能示例
  20. 单引号和0的ASCII码

热门文章

  1. String StringBuilder StringBuffer三者之间的区别~~~
  2. php 编译原理,编译原理
  3. 用四位led数码管作显示器的篮球比赛24秒计时器求c语言代码,单片机编程控制LED七段数码管作显示的篮球赛计时计分系统...
  4. 物理实验数据处理(c语言)
  5. python 如何匹配列表中某个单词_Python中部分指定单词的最佳匹配项
  6. 0018计算机基础知识,0018 0019计算机应用基础上机试题
  7. 广东省2021高考2bi补录成绩查询,重磅!广东省2021年普通高考美术统考成绩1月8日起可查询!...
  8. mysql隔离级别和mvcc_数据库MVCC和隔离级别的关系是什么?
  9. C语言各种类型数据的输出显示
  10. python基础入门(6)之列表