Sunday, June 7, 2015

UVa 315 - Network

// UVa 315 - Network
#include <iostream>
#include <sstream>
#include <string.h>
#include <limits.h>

using namespace std;

int lnk[101][101];
int counter, sol;
int L[101], O[101];

void add_link(int i, int j) {
	lnk[i][++lnk[i][0]] = j;
	lnk[j][++lnk[j][0]] = i;
}

void dfs(int i, int p) {
	O[i] = ++counter;
	L[i] = O[i];
	int children_count = 0;
	int biggest_children_L = 0;
	for (int k = 1; k <= lnk[i][0]; k++) {
		int j = lnk[i][k];
		if (O[j] == 0) {
			dfs(j, i);
			children_count++;
			if (L[j] > biggest_children_L || biggest_children_L == 0)
				biggest_children_L = L[j];
			if (L[j] < L[i])
				L[i] = L[j];
		} else if (j != p && j != i && O[j] < L[i])
			L[i] = O[j];
	}
	if (i != 1 && biggest_children_L >= O[i])
		sol++;
	else if (i == 1 && children_count > 1)
		sol++;
}

int main() {

	string line;
	int n;
	cin >> n;
	getline(cin, line);
	while (n) {
		for (int i = 1; i <= n; i++)
			lnk[i][0] = 0;
		getline(cin, line);
		istringstream strm(line);
		int i;
		strm >> i;
		while (i) {
			int j;
			while (strm >> j)
				add_link(i, j);
			getline(cin, line);
			strm.clear();
			strm.str(line);
			strm >> i;
		}

		counter = 0;
		sol = 0;
		memset(O, 0, sizeof(O));
		memset(L, 0, sizeof(L));
		dfs(1, 0);
		cout << sol << endl;

		cin >> n;
		getline(cin, line);
	}
	return 0;
}

No comments:

Post a Comment