Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 9464 | Accepted: 4452 |
Description
possible to reach through lines every other place, however it need not be a direct connection, it can go through several exchanges. From time to time the power supply fails at a place and then the exchange does not operate. The officials from TLC realized that in such a case it can happen that besides the fact that the place with the failure is unreachable, this can also cause that some other places cannot connect to each other. In such a case we will say the place (where the failure
occured) is critical. Now the officials are trying to write a program for finding the number of all such critical places. Help them.
Input
by one space. Each block ends with a line containing just 0. The last block has only one line with N = 0;
Output
Sample Input
5 5 1 2 3 4 0 6 2 1 3 5 4 6 2 0 0
Sample Output
1 2
Hint
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int maxn = 105;
int dfn[maxn];
int low[maxn];
int flag[maxn];
vector<int>adj[maxn];
int root;
int lay;
void init(int n)
{
for (int i = 1; i <= n; i ++)
{
adj[i].clear();
}
}
void tarjan(int u)
{
dfn[u] = low[u] = ++lay;
for (int i = 0; i < (int)adj[u].size(); i ++) {
if (!dfn[adj[u][i]]) { //未訪問過
tarjan(adj[u][i]);
low[u] = min(low[u], low[adj[u][i]]);
if (low[adj[u][i]] >= dfn[u] && u != 1) { //不是根
flag[u] = 1; // 是割點
} else if (u == 1) { // 是根
root ++; // 根的分支個數
}
} else { //訪問過
low[u] = min(low[u], dfn[adj[u][i]]);
}
}
}
int main()
{
int n;
while (scanf("%d", &n)&&n) {
init(n);
int x, y;
while (scanf("%d", &x)&&x) {
while (getchar() != '\n') {
scanf("%d", &y);
adj[x].push_back(y);
adj[y].push_back(x);
}
}
int ans = 0;
lay = root = 0;
memset(dfn, 0, sizeof(dfn));
memset(low, 0, sizeof(low));
memset(flag, 0, sizeof(flag));
tarjan(1);
if (root > 1) ans ++;
for (int i = 2; i <= n; i ++) if (flag[i]){
ans ++;
}
printf("%d\n", ans);
}
return 0;
}