poj 2117 Electricity

import java.util.*;
import java.io.*;
import java.math.BigInteger;
import java.util.logging.Level;
import java.util.logging.Logger;
 
class edge{
    int from, to, next;
    public edge(int a, int b, int c){
        from=a;to=b;next=c;
    }
}
 
class main_problem implements Runnable{
    ArrayList edges;
    int header[], vis[], depth[], low[], cut[];
    int index = 0, p=0, c=0, idx=0;
    void tarjan(int u, int pre, int root){  
        int sons = 0;
        vis[u]=1;
        depth[u]=low[u]=idx++;
        for(int i=header[u];i!=-1;i=edges.get(i).next){
            int v = edges.get(i).to;
            if(vis[v]==1&&v!=pre){
                low[u] = Math.min(low[u], depth[v]);
            }
            if(vis[v]==0){
                tarjan(v,u,root);
                sons++;
                low[u]=Math.min(low[u], low[v]);
                if((u==root&&sons>1)||(u!=root&&depth[u]<=low[v]))
                    cut[u]++;
            }
        }
        vis[u]=2;
    }

    void initial() throws IOException{
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        //BufferedReader reader = new BufferedReader(new FileReader("e.in"));
        while(true){           
            StringTokenizer st = new StringTokenizer(reader.readLine());
            p=Integer.valueOf(st.nextToken());
            c=Integer.valueOf(st.nextToken());
            index = 0;
            if(p==0&&c==0) break;
            if(c==0){
                System.out.println(p-1);
                continue;
            }
            edges = new ArrayList();
            vis = new int[p+1];
            depth = new int[p+1];
            low = new int[p+1];  
            header = new int[p+1];
            cut = new int[p+1];
            Arrays.fill(header, -1);
            for(int i=0;i

题意是很裸的,基本裸的不能再裸,直接问你了去掉一个点把图分成几个部分

就是求割点了

其实图论的太监算法我是不会的,于是现学一下。

最大的问题是java dfs太深爆堆栈了,然后google发现要改-Xss参数,但是OJ是不会吊你的,你没法改参数

然后CF上看了一个blog提供的牛逼solution. 

well, isn't it beautiful?!

你可能感兴趣的:(POJ)