#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
using namespace std;
const int N = 100 + 5;
int n;
int g[N][N];
int ancestor[N], d[N];
bool cut[N];
int a[N];
int vis[N];
void dfs(int cur, int father, int depth);
int main()
{
string s;
#ifndef ONLINE_JUDGE
ifstream cin("d:\\OJ\\uva_in.txt");
#endif // ONLINE_JUDGE
while (cin >> n, n) {
getline(cin, s);
fill(cut, cut + N, false);
fill(&g[0][0], &g[N][0], 0);
fill(vis, vis + N, 0);
while (getline(cin, s) && s != "0") {
istringstream line(s);
int num = 0;
while (line >> a[num]) num++;
for (int i = 1; i < num; i++) {
g[a[0]][a[i]] = g[a[i]][a[0]] = 1;
}
}
dfs(1, 0, 0);
int sum = 0;
for (int i = 1; i <= n; i++) {
if (cut[i])
sum++;
}
cout << sum << endl;
}
return 0;
}
void dfs(int cur, int father, int depth)
{
int tot = 0;
vis[cur] = -1;
ancestor[cur] = d[cur] = depth;
for (int i = 1; i <= n; i++) {
if (!g[cur][i])
continue;
if (i != father && vis[i] == -1) {
ancestor[cur] = min(ancestor[cur], d[i]);
}
if (!vis[i]) {
dfs(i, cur, depth + 1);
tot++;
ancestor[cur] = min(ancestor[cur], ancestor[i]);
if ((cur == 1 && tot > 1) || (cur != 1 && ancestor[i] >= d[cur]))
cut[cur] = true;
}
}
vis[cur] = 1;
}
UVa315 - Network
最新推荐文章于 2020-04-24 16:38:02 发布