fork download
  1. import java.util.*;
  2. import java.io.*;
  3. class ArticulationPoints
  4. {
  5. static ArrayList<ArrayList<Integer>> map = new ArrayList<ArrayList<Integer>>();
  6. static int parent[]=new int[10100];
  7. static int visited[]=new int[10100];
  8. static int disc[]=new int[10100];
  9. static int low[]=new int[10100];
  10. static int ap[]=new int[10100];
  11. static int time=0;
  12. public static void main(String[]args) throws IOException
  13. {
  14. String inLine[];
  15. StringBuilder op = new StringBuilder();
  16. for(int i=0;i<=10100;i++)
  17. map.add(new ArrayList<Integer>());
  18. while(true)
  19. {
  20. inLine = input.readLine().split(" ");
  21. int N = Integer.parseInt(inLine[0]);
  22. int M = Integer.parseInt(inLine[1]);
  23. if(N==0&&M==0)break;
  24. //Reset Everything!
  25. for(int i=0;i<=N;i++)
  26. { map.get(i).clear();parent[i]=-1;visited[i]=-1;ap[i]=-1;disc[i]=0;low[i]=0;}
  27. for(int i=0;i<M;i++)
  28. {
  29. inLine = input.readLine().split(" ");
  30. int u = Integer.parseInt(inLine[0]);
  31. int v = Integer.parseInt(inLine[1]);
  32. map.get(u).add(v);
  33. map.get(v).add(u);
  34. }
  35. time=0;
  36. dfs(1,true);
  37. int ans=0;
  38. for(int i=1;i<=N;i++)
  39. if(ap[i]==1)ans++;
  40. op.append(ans).append("\n");
  41. }
  42. System.out.println(op);
  43. }
  44.  
  45. static void dfs (int u , boolean isRoot)
  46. {
  47. int child=0;
  48. visited[u]=1;
  49. disc[u]=++time;
  50. low[u]=disc[u];
  51. int sz = map.get(u).size();
  52. for(int i=0;i<sz;i++)
  53. {
  54. int v = map.get(u).get(i);
  55. if(visited[v]!=1)
  56. {
  57. child++;
  58. parent[v]=u;
  59. dfs(v,false);
  60. // Check if the subtree rooted with v has a connection to one of the ancestors of u
  61. low[u] = Math.min(low[u],low[v]);
  62. // u is an articulation point in following cases
  63. // (1) u is root of DFS tree and has two or more chilren.
  64. if (isRoot&& child>=2)
  65. ap[u]=1;
  66. // (2) If u is not root and low value of one of its child is more
  67. // than discovery value of u.
  68. if ( !isRoot && low[v] >= disc[u])
  69. ap[u]=1;
  70. }
  71. else if (v != parent[u])
  72. low[u] = Math.min(low[u], disc[v]);
  73. }
  74. }
  75. }
Success #stdin #stdout 0.1s 320320KB
stdin
3 3
1 2
2 3
1 3
6 8
1 3
6 1
6 3
4 1
6 4
5 2
3 2
3 5
0 0
stdout
0
1