poj1144 割顶

简介:
如果节点没被访问过才child++
#include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<vector>
    #include<cstring>
    using namespace std;
    vector<int> g[211];
    int dfs_clock, n, pre[211], cut[211];
    int dfs(int u, int fa){
        int lowu = pre[u] = ++dfs_clock;
        int child = 0;
        for (int i = 0; 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]) cut[u] = 1;
    	    }else 
    	    if (v != fa && pre[u] > pre[v]) lowu = min(lowu, pre[v]);
        } 
        if (fa < 0) cut[u] = child > 1 ? 1 : 0;
        return lowu;
    }
    int main(){
    	while (cin>>n && n){
    		dfs_clock = 0;
    		for (int i = 1; i <= 100; i++) g[i].clear();
    		memset(pre, 0, sizeof(pre));
    		memset(cut, 0, sizeof(cut));
    		int u, v;
    		while (cin>>u && u){
    			while (getchar() != '\n'){
    				cin>>v;
    				g[u].push_back(v);g[v].push_back(u);
    			}
    		}
    		if (n < 3) {
    			cout<<"0\n";
    			continue;
    		}
    		dfs(1, -1);
    		int ans = 0;
    		for (int i = 1; i <= n; i++) ans += cut[i];
    		cout<<ans<<'\n';
    	}
    	return 0;
    }

相关文章
POJ 2487 Stamps
POJ 2487 Stamps
84 0
POJ 1936 All in All
POJ 1936 All in All
63 0
POJ 2027 No Brainer
POJ 2027 No Brainer
82 0
|
算法 数据建模 机器学习/深度学习
|
C语言
poj 2503 查字典
Description You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language.
838 0
|
并行计算 网络架构
poj-1005-l tanink i need a houseboat
Description Fred Mapper is considering purchasing some land in Louisiana to build his house on. In the process of investigating the land, he learned ...
940 0
|
消息中间件 人工智能 JavaScript