POJ 1144 Network(Tarjan)
2016-08-09 19:27
441 查看
Network
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 12405 Accepted: 5708
Description
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
Hint
You need to determine the end of one line.In order to make it’s easy to determine,there are no extra blank before the end of each line.
抄了一波模板,求割点数
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 12405 Accepted: 5708
Description
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
Hint
You need to determine the end of one line.In order to make it’s easy to determine,there are no extra blank before the end of each line.
抄了一波模板,求割点数
#include <map> #include <set> #include <queue> #include <stack> #include <vector> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int N = 100010; const int M = (N << 2); struct node { int next; int to; // int id; }edge[M]; struct BRIDGE { int u; int v; }bridge[M]; int head ; int DFN ; int low ; int st ; bool instack ; int tot, top, ord, sccnum, cnt; void init () { memset (head, -1, sizeof(head)); memset (DFN, -1, sizeof(DFN)); memset (low, 0, sizeof(low)); memset (instack, 0, sizeof(instack)); tot = top = cnt = sccnum = ord = 0; } void addedge (int from, int to) { edge[tot].to = to; // edge[tot].id = id; edge[tot].next = head[from]; head[from] = tot++; } int cmp (BRIDGE a, BRIDGE b) { if (a.u == b.u) { return a.v < b.v; } return a.u < b.u; } void tarjan (int u, int fa) { DFN[u] = low[u] = ++ord; instack[u] = 1; st[top++] = u; for (int i = head[u]; ~i; i = edge[i].next) { int v = edge[i].to; if (v == fa) { continue; } if (DFN[v] == -1) { tarjan (v, u); low[u] = min (low[v], low[u]); if (low[v] > DFN[u]) { bridge[++cnt].u = u; bridge[cnt].v = v; if (bridge[cnt].u > bridge[cnt].v) { swap (bridge[cnt].u, bridge[cnt].v); } } } else if (instack[v]) { low[u] = min (low[u], DFN[v]); } } if (low[u] == DFN[u]) { ++sccnum; int v; do { v = st[--top]; instack[v] = 0; }while (u != v); } } void solve (int n) { for (int i = 1; i <= n; ++i) { if (DFN[i] == -1) { tarjan(i, -1); } } sort (bridge + 1, bridge + cnt + 1, cmp); printf("%d critical links\n", cnt); for (int i = 1; i <= cnt; ++i) { printf("%d - %d\n", bridge[i].u - 1, bridge[i].v - 1); } printf("\n"); } int main() { int n; int u, v; int num; while (~scanf("%d", &n)) { if (n == 0) { printf("0 critical links\n\n"); continue; } init(); for (int i = 1; i <= n; ++i) { scanf("%d", &u); ++u; getchar(); getchar(); scanf("%d", &num); getchar(); while (num--) { scanf("%d", &v); ++v; addedge (u, v); } } solve (n); } return 0; }
相关文章推荐
- poj 1144 Network(模板题)(Tarjan 关节点的朴素算法)
- [POJ1144]Network(tarjan求割点)
- Poj 1144 Network【Tarjan求割点】
- POJ 1144 Network 无向图求割点Tarjan
- [poj1144 Network]tarjan求割点
- POJ1144 Network (Tarjan算法求无向图的割点)
- 【模板】【POJ1144】Network——tarjan求割点
- poj 1144 Network (tarjan割点模板)
- POJ 1144 Network Tarjan 求无向图的割点的个数 Tarjan 模板题
- 【POJ 1144】Network(Tarjan求割点)
- POJ 1144 Network ---tarjan算法求割点
- poj 1144 Network (tarjan求割点)
- poj 1144 Network tarjan求无向连通图的割点个数
- POJ 1144 Network 求割点(tarjan)
- POJ 1144 - Network 用tarjan求无向图的割点
- poj 1144 Network(Tarjan求割点)
- [poj 1144]Network[Tarjan求割点]
- [poj 1144]Network[Tarjan求割点]
- POJ1144--Network(Tarjan求割点)
- POJ 1144 Network(Tarjan)