poj 1144 Network(无向图求割顶数)

  题目链接:poj 1144

  题意就是说有 n(标号为 1 ~ n)个网点连接成的一个网络,critical places 表示删去后使得图不连通的顶点,也就是割顶,求图中割顶的个数。

  直接上大白书上的模板即可,只是输入也有点卡人,我竟然傻傻的用手写的输入挂来处理,看了别人的博客才知道用 scanf("%s") 即可,因为 scanf("%s") 不会读入空格,再适当处理下即可。

  我的代码是:

 #include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
const int N = ; vector<int> G[N];
int pre[N], low[N], dfs_clock;
bool iscut[N]; inline void add_edge(int from , int to) {
G[from].push_back(to);
G[to].push_back(from);
} int dfs(int u, int fa) {
int lowu = pre[u] = ++dfs_clock;
int child = ;
for(int i = ; i < G[u].size(); ++i) {
int v = G[u][i];
if(!pre[v]) {
++child;
int lowv = dfs(v,u);
lowu = min(lowu, lowv);
if(lowv >= pre[u])
iscut[u] = ;
}
else if(pre[v] < pre[u] && v != fa)
lowu = min(lowu, pre[v]);
}
if(fa < && child == ) iscut[u] = ;
return low[u] = lowu;
} inline bool isline(const char &ch) {
return ch == '\n' || ch == '\r';
} #include<cctype>
bool eol;
inline void read(int &x) {
x = ;
eol = ;
char ch = getchar();
while(!isdigit(ch))
ch = getchar();
while(isdigit(ch)) {
x = x * + (ch - '');
ch = getchar();
}
if(isline(ch)) eol = ;
} int main() {
int n,x;
while(~scanf("%d",&n),n) {
for(int i = ; i < ; ++i)
G[i].clear();
memset(pre,,sizeof(pre));
memset(iscut,,sizeof(iscut));
dfs_clock = ; while() {
eol = ;
read(x);
if(!x) {
for(int i = ; i <= n; ++i)
if(!pre[i]) dfs(i, -);
int ans = ;
for(int i = ; i <= n; ++i)
if(iscut[i]) ++ans;
printf("%d\n",ans);
break;
}
else {
int u = x;
while(!eol) {
read(x);
add_edge(u,x);
}
}
}
}
return ;
}
上一篇:【转】分享两个基于MDK IDE的调试输出技巧


下一篇:hdu1267(递推)