Poj_1523 SPF (Tarjan cut point)

Source: Internet
Author: User

Question: Find the cut point and find the remaining number of connected blocks after the cut point is deleted.

Slag code:

View code

 1 #include <iostream>
2 #include <cstring>
3 #include <cstdio>
4
5 using namespace std;
6
7 const int N = 1005;
8 const int M = 100000;
9 const int inf = ~0u>>2;
10
11 struct node {
12 int to;
13 int next;
14 } g[M];
15
16 int head[N], cut[N];
17 int dfn[N], low[N];
18 int t, ind, rot, r_son;
19 bool vis[N];
20
21 void init() {
22 memset(head, 0, sizeof(head));
23 memset(cut, 0, sizeof(cut));
24 memset(dfn, 0, sizeof(dfn));
25 memset(low, 0, sizeof(low));
26 memset(vis, 0, sizeof(vis));
27 t = 1; r_son = ind = 0;
28 }
29
30 void add(int u, int v) {
31 g[t].to = v; g[t].next = head[u]; head[u] = t++;
32
33 }
34
35 void tarjan(int u) {
36 int v, i;
37 dfn[u] = low[u] = ++ind;
38 for(i = head[u]; i; i = g[i].next) {
39 v = g[i].to;
40 if(!dfn[v]) {
41 tarjan(v);
42 //printf("%d %d\n", u, v);
43 if(u == rot) r_son++;
44 else {
45 low[u] = min(low[u], low[v]);
46 if(low[v] >= dfn[u]) cut[u]++;
47 }
48 }
49 else {
50 low[u] = min(low[u], dfn[v]);
51 }
52 }
53 }
54
55 int main() {
56 //freopen("data.in", "r", stdin);
57
58 int u, v, cas = 0;
59 int MIN, MAX, i;
60 bool f;
61 while(scanf("%d", &u), u) {
62 init();
63 MIN = inf; MAX = -1;
64 scanf("%d", &v);
65 add(u, v); add(v, u);
66
67 MIN = min(MIN, min(u, v));
68 MAX = max(MAX, max(u, v));
69
70 while(scanf("%d", &u), u) {
71 scanf("%d", &v);
72 add(u, v); add(v, u);
73
74 MIN = min(MIN, min(u, v));
75 MAX = max(MAX, max(u, v));
76 }
77 rot = MIN;
78 tarjan(rot);
79 cut[rot] = r_son - 1;
80 printf("Network #%d\n", ++cas);
81
82 for(f = false, i = MIN; i <= MAX; i++) {
83 if(cut[i]) {
84 f = true;
85 printf(" SPF node %d leaves %d subnets\n", i, cut[i] + 1);
86 }
87 }
88 if(!f) printf(" No SPF nodes\n");
89 cout << endl;
90 }
91 return 0;
92 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.