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;
    }

相关文章
|
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.
856 0
POJ 2487 Stamps
Description Background Everybody hates Raymond. He’s the largest stamp collector on planet earth and because of that he always makes fun of all the others at the stamp collector parties.
1056 0
|
机器学习/深度学习 算法
|
存储 索引
|
机器学习/深度学习
poj 2912 Rochambeau
点击打开链接poj 2912 思路: 带权并查集 分析: 1 有n个小孩玩游戏,里面有一个入是裁判,剩下的人分为三组。现在我们既不知道裁判是谁也不知道分组的情况。
945 0