poj1523 割顶

简介:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int cut[1011], pre[1011];
vector<int> g[1011];
int ans, dfs_clock, son;
int dfs(int u, int fa){
	int lowu = pre[u] = ++dfs_clock;
	int child = 0;
	for (int i = 0; i < g[u].size(); i++){
		int v = g[u][i];
		if (!pre[v]){
			child ++;
			int lowv = dfs(v, u);
			if (pre[u] <= lowv){
				cut[u] ++;
			}
		
			lowu = min(lowu, lowv);
		}
		else if (pre[u] > pre[v]) lowu = min(lowu, pre[v]);
	}
	if (fa < 0)
		cut[u] = child - 1;
	return lowu;
}
int main(){
	int t = 0, u, v, n = -1;
	while (cin>>u && u){
		dfs_clock = ans = 0;
		for (int i = 1; i <=1000; i++) g[i].clear();
		memset(cut, 0, sizeof(cut));
		memset(pre, 0, sizeof(pre));
		while (u){
			cin>>v;
			n = max(n, max(u, v));
			g[u].push_back(v); g[v].push_back(u);
			cin>>u;
		}
		for (int i = 1; i <= n; i++)
		    if (!pre[i]) dfs(i, -1);
		printf("Network #%d\n", ++t);
		bool flag = 0;
		for (int i = 1; i <= n; i++)
		    if (cut[i] > 0) {
			    printf("  SPF node %d leaves %d subnets\n", i, cut[i] + 1);
			    flag = 1;}
        if (!flag) printf("  No SPF nodes\n");
        printf("\n");
	}
}

相关文章
|
4月前
|
算法
Highways(POJ—2485)
Highways(POJ—2485)
|
算法 数据建模 机器学习/深度学习
|
机器学习/深度学习
POJ 2487 Stamps
Description Background Everybody hates Raymond. He’s the largest stamp collector on planet earth and because of that he always makes fun of all the others at the stamp collector parties.
1056 0
POJ 2262 Goldbach&#39;s Conjecture
Problem Description In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in which he made the foll...
999 0
|
算法 机器人 编译器
POJ-2632
#include int main() { int k,a,b,n,m,i,j,num,rep,rect[100][100],robot[100][3]; int flag; char c; for(scanf("%d...
915 0
|
算法 存储
POJ 1014 Dividing 解答
题目详见http://poj.org/problem?id=1014 看到这道题第一反应便知道它是一道类似背包问题的题,  解法我自然而然得从背包问题的解法入手,  网上查了查,  背包问题的基本题型是01背包, 即每种...
1038 0