You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.6 KiB
C++
60 lines
1.6 KiB
C++
#include <cstdio>
|
|
#include <cctype>
|
|
#include <cstring>
|
|
#define clz(X) memset(X, 0, sizeof(X))
|
|
inline int max(int a, int b) { return a > b ? a : b; }
|
|
inline int min(int a, int b) { return a < b ? a : b; }
|
|
inline void read(int &x)
|
|
{
|
|
int ch = x = 0;
|
|
while (!isdigit(ch = getchar()));
|
|
for (; isdigit(ch); ch = getchar())
|
|
x = x * 10 + ch - '0';
|
|
}
|
|
int map[1010][1010], range;
|
|
int dfn[1010], low[1010], idx;
|
|
int son, subnet[1010];
|
|
void tarjan(int u)
|
|
{
|
|
dfn[u] = low[u] = ++idx;
|
|
for (int v = 1; v <= range; v++)
|
|
if (map[u][v])
|
|
if (!dfn[v])
|
|
{
|
|
tarjan(v);
|
|
low[u] = min(low[u], low[v]);
|
|
if (low[v] >= dfn[u])
|
|
(u == 1 ? son : subnet[u])++;
|
|
}
|
|
else
|
|
low[u] = min(low[u], dfn[v]);
|
|
}
|
|
int main()
|
|
{
|
|
int x, y, T = 0;
|
|
while (read(x), x)
|
|
{
|
|
clz(map), clz(dfn), clz(low), clz(subnet), son = idx = 0;
|
|
read(y);
|
|
map[x][y] = map[y][x] = 1;
|
|
range = max(x, y);
|
|
while (read(x), x)
|
|
{
|
|
read(y);
|
|
map[x][y] = map[y][x] = 1;
|
|
range = max(range, max(x, y));
|
|
}
|
|
printf("Network #%d\n", ++T);
|
|
tarjan(1);
|
|
bool flag = false;
|
|
if (son > 1) subnet[1] = son - 1;
|
|
for (int i = 1; i <= range; i++)
|
|
if (subnet[i])
|
|
printf(" SPF node %d leaves %d subnets\n", i, subnet[i] + 1),
|
|
flag = true;
|
|
if (!flag)
|
|
puts(" No SPF nodes");
|
|
putchar('\n');
|
|
}
|
|
return 0;
|
|
} |