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");
	}
}

相关文章
|
2天前
Hopscotch(POJ-3050)
Hopscotch(POJ-3050)
|
7月前
|
人工智能
POJ 3104 Drying
POJ 3104 Drying
|
人工智能 机器学习/深度学习
POJ 1012 Joseph
Joseph Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 53862   Accepted: 20551 Description The Joseph's problem is notoriously known.
818 0
|
人工智能
POJ 2531
初学dfs参考别人代码,如有雷同,见怪不怪。#include using namespace std; int aa[25][25]; int maxa=0; int step[25]={0},n; void dfs(int a,int b) { int t=b; step...
676 0
POJ 1804
题目:http://poj.org/problem?id=1804 大意:给你一串数字,排序。求出最少的交换次数  \ 我用归并做的 #include #include using namespace std; int aa[500010],bb[500010]; long lon...
673 0
poj-1006-Biorhythms
Description 人生来就有三个生理周期,分别为体力、感情和智力周期,它们的周期长度为23天、28天和33天。每一个周期中有一天是高峰。在高峰这天,人会在相应的方面表现出色。例如,智力周期的高峰,人会思维敏捷,精力容易高度集中。
587 0

热门文章

最新文章