POJ1523 Targin求关节点
Targin求关节点
#include<queue>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N = 1010;
struct node{
int v, nxt;
}edge[2010];
int n, m, cnt, num, son;
int fir[N], low[N], dfn[N], s[N];
inline void add(int u, int v){
edge[++cnt] = (node){v, fir[u]}; fir[u] = cnt;
edge[++cnt] = (node){u, fir[v]}; fir[v] = cnt;
}
inline void dfs(int u){
dfn[u] = low[u] = ++num;
for(int i = fir[u]; i; i = edge[i].nxt){
int v = edge[i].v;
if(!dfn[v]){
dfs(v);
low[u] = min(low[u], low[v]);
if(low[v] >= dfn[u]){
if(u == m) son++;
else s[u]++;
}
}
else low[u] = min(low[u], dfn[v]);
}
}
int main(){
#ifdef ONLINE_JUDGE
#else
freopen("in.txt", "r", stdin);
#endif // ONLINE_JUDGE
int a, b, iCase = 1;
bool flag;
while(scanf("%d", &a), a){
scanf("%d", &b);
n = 0, m = 0x3f3f3f3f;
cnt = 0;
num = 0;
son = 0;
memset(fir, 0, sizeof(fir));
memset(dfn, 0, sizeof(dfn));
memset(s, 0, sizeof(s));
n = max(n, max(a, b));
m = min(m, min(a, b));
add(a, b);
while(scanf("%d", &a), a){
scanf("%d", &b);
n = max(n, max(a, b));
m = min(m, min(a, b));
add(a, b);
}
dfs(m);
printf("Network #%d
", iCase++);
s[m] = son - 1;
flag = false;
for(int i = 1; i <= n; i++){
if(s[i]){
printf(" SPF node %d leaves %d subnets
", i, s[i] + 1);
flag = true;
}
}
if(!flag){
printf(" No SPF nodes
");
}
printf("
");
}
return 0;
}