Advertisement
hieudoan

UVa 00315 Network

Aug 17th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.94 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef vector<int> vi;
  5.  
  6. int n;
  7. vector<vi> adjList;
  8. vi dfs_num,dfs_low, artiVertex, dfs_parent;
  9. int dfs_count,dfsRoot,rootChildren;
  10.  
  11. void ArticulationPoint(int u){
  12.     dfs_low[u]=dfs_num[u]=dfs_count++;
  13.     for(int j=0;j<(int)adjList[u].size();j++){
  14.         int v=adjList[u][j];
  15.         if(dfs_num[v]==-1){  //unvisited   
  16.             cerr<<"zo2\n";
  17.             dfs_parent[v]=u;
  18.             if(u==dfsRoot) rootChildren++;  //
  19.             ArticulationPoint(v);
  20.             //lúc này là nó gọi xog hết rồi, tức là tính xong low ở khu vực đó
  21.             //và u > v (u ancestor v)
  22.             if(dfs_low[v]>= dfs_num[u])  //chỉ có 1 path là phải đi qua u
  23.                 artiVertex[u]=1;
  24.             dfs_low[u]=min(dfs_low[u],dfs_low[v]); //TH u is AP thì k đổi nhưng nếu u ko là AP thì có nghĩ là low v < num u => có thể u đi theo path thông qua v sẽ ngắn hơn
  25.         }
  26.         else if(v!=dfs_parent[u])  //v ancestor u (ông trở lên) ~ back edge not circul
  27.                 dfs_low[u]=min(dfs_low[u],dfs_num[v]);
  28.        
  29.     }
  30. }
  31. int main()
  32. {
  33.     ios::sync_with_stdio(false);
  34.     cin.tie(0);
  35.     cout.tie(0);
  36.    
  37.     while(1){
  38.         cin>>n;
  39.         if(n==0) break;
  40.         stringstream ss;
  41.         string line;
  42.         adjList.assign(105,vi(0));
  43.         int v, next;
  44.         cin.ignore();  //lượt bỏ "\n" do cin trên kia nó ko get mà getline thì nó ôm trọn
  45.         while(1){
  46.             ss.clear();
  47.             getline(cin,line);
  48.             if(line=="0") break;
  49.             ss<<line;
  50.             ss>>v;
  51.             while(ss>>next){
  52.                 adjList[v].push_back(next);
  53.                 adjList[next].push_back(v);
  54.             }
  55.         }
  56.         //initial
  57.         dfs_num.assign(105,-1);
  58.         dfs_low.assign(105,0);
  59.         dfs_parent.assign(105,0);
  60.         artiVertex.assign(105,0);
  61.         dfs_count=0;
  62.        
  63.         for(int i=1;i<=n;i++){
  64.             if(dfs_num[i]==-1){
  65.                 dfsRoot=i;
  66.                 rootChildren=0;
  67.                 ArticulationPoint(i);
  68.                 artiVertex[dfsRoot]=(rootChildren>1);
  69.             }
  70.         }
  71.         // ArticulationPoint(1);
  72.         int ans=0;
  73.         for(int i=1;i<=n;i++)
  74.             ans+=artiVertex[i];
  75.         cout<<ans<<endl;
  76.     }
  77.  
  78.     return 0;
  79. }
  80. //hieudoanitus
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement