Advertisement
Ishmam_Rahman

1063 - Ant Hills (LightOJ)

Mar 28th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. vector<int>adj[10007];
  5. //bool visit[100007];
  6. int low[10007],AP[10007],discovery[10007];
  7. int Time=0;
  8.  
  9. int dfsAP(int src, int parent){
  10.     low[src]=discovery[src]= (++Time);
  11.  
  12.     int children=0;
  13.     for(int i=0;i<adj[src].size();i++){
  14.         int w=adj[src][i];
  15.  
  16.         if(w!=parent){ // don't wanna go back to the before node
  17.             if(discovery[w]==0){
  18.                 children++;
  19.                 dfsAP(w,src);
  20.  
  21.                 if(discovery[src]<=low[w]) AP[src]=1; //Condition 1
  22.                 low[src]=min(low[src],low[w]);
  23.             }
  24.  
  25.             else{
  26.                 low[src]=min(low[src],discovery[w]);
  27.             }
  28.         }
  29.     }
  30.     return children;
  31. }
  32.  
  33.  
  34. int main()
  35. {
  36.     int t; cin>>t;
  37.     for(int o=1;o<=t;o++){
  38.  
  39.             memset(low,0,sizeof(low));
  40.             memset(AP,0,sizeof(AP));
  41.             memset(discovery,0,sizeof(discovery));
  42.     Time=0;
  43.         int n,m,res=0;
  44.     cin>>n>>m;
  45.  
  46.     for(int i=0;i<m;i++) {
  47.         int a,b;
  48.         cin>>a>>b;
  49.         adj[a].push_back(b);
  50.         adj[b].push_back(a);
  51.     }
  52.  
  53.  
  54.  
  55.         AP[1]=dfsAP(1,1)>1; //Condition 2
  56.  
  57.  
  58.     for(int i=1;i<=n;i++) if(AP[i]==1) res++;
  59.  
  60.     cout<<"Case "<<o<<": "<<res<<endl;
  61.  
  62.     for(int i=0;i<10007;i++) {
  63.         adj[i].clear();
  64.     }
  65. }
  66. return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement