| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 9767 | Accepted: 4587 |
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
Source
给一个无向连通图,求割点数量。前两天离散老师讲了tarjan算法,暑假集训时根本无法理解,连连通图,强连通分量云云的概念都看得稀里糊涂,现在想想,自己的接受能力还真是有限,必须慢慢地接触一段时间才能有印象,不知道这算不算智力硬伤。。。
| 1144 | Accepted | 560K | 16MS | G++ | 1219B |
代码:
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
const int MAX = 105;
vector<int> G[MAX];
int dfn[MAX], low[MAX];
bool vis[MAX];
int n, ans;
int num;
void dfs(int u) {
vis[u] = true;
dfn[u] = low[u] = ++num;
int son = 0;
bool flag = false;
for (int i = 0; i < G[u].size(); ++i) {
int v = G[u][i];
if (vis[v]) {
if (low[u] > dfn[v]) low[u] = dfn[v];
continue;
}
dfs(v);
++son;
if (low[u] > low[v]) low[u] = low[v];
if (low[v] >= dfn[u]) flag = true;//这里的判断开始搞错了,初始化flag=true,出现low[v]<dfn[u]设为false,其实导致的问题是:即使第一次有孩子可以被“割”开,也可能被判为“不是割点”
}
if (u == 1) {
if (son >= 2) {
//printf("&&%d\n", u);
++ans;
}
} else if (flag && son >= 1) {
//printf("**%d\n", u);
++ans;
}
}
int tarjan() {
ans = num = 0;
memset(vis, false, sizeof(bool) * MAX);
fill(low, low + MAX, MAX);
//for (int i = 1; i < MAX; ++i) printf("%d ", low[i]);
dfs(1);
return ans;
}
int main() {
while (~scanf(" %d", &n) && n) {
for (int i = 0; i < MAX; ++i) G[i].clear();
int u;
while (~scanf(" %d", &u) && u) {
int v;
while ((getchar()) == ' ') {
scanf(" %d", &v);
G[u].push_back(v);
G[v].push_back(u);
}
}
printf("%d\n", tarjan());
}
return 0;
}
936



被折叠的 条评论
为什么被折叠?



