Saleh127

UVA 10199/Articulation point

Aug 10th, 2021 (edited)
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define test int tt; cin>>tt; for(int cs=1;cs<=tt;cs++)
  5. ll a[205];
  6. vector<ll>g[205];
  7. bool v[205];
  8. map<ll,string>x;
  9. map<string,ll>y;
  10. ll timer;
  11. ll low[205],in[205];
  12.  
  13.  
  14. void dfs(ll vertex,ll parent)
  15. {
  16. v[vertex]=1;
  17.  
  18. low[vertex] = in[vertex] = ++timer;
  19.  
  20. ll edge=0;
  21.  
  22. for(auto child:g[vertex])
  23. {
  24. if(child==parent) continue;
  25.  
  26. if(v[child])
  27. {
  28. low[vertex]=min(low[vertex],in[child]);
  29. }
  30. else
  31. {
  32. dfs(child,vertex);
  33.  
  34. if(low[child]>=in[vertex] && parent!=-1)
  35. {
  36. a[vertex]=1;
  37. }
  38. low[vertex]=min(low[vertex],low[child]);
  39.  
  40. edge++;
  41. }
  42. }
  43.  
  44. if(parent==-1 && edge>1)
  45. {
  46. a[vertex]=1;
  47. }
  48. }
  49.  
  50.  
  51. void clr(ll n)
  52. {
  53. for(ll i=0; i<n+4; i++)
  54. {
  55. g[i].clear();
  56. v[i]=0;
  57. low[i]=-1;
  58. in[i]=-1;
  59. a[i]=0;
  60. }
  61. x.clear();
  62. y.clear();
  63. timer=0;
  64. }
  65.  
  66. int main()
  67. {
  68. ios_base::sync_with_stdio(0);
  69. cin.tie(0);
  70. cout.tie(0);
  71.  
  72. ll n,m,i,j,k,l,rr=1;
  73. string c,d;
  74.  
  75. while(cin>>n && n)
  76. {
  77. clr(n);
  78.  
  79. for(i=1; i<=n; i++)
  80. {
  81. cin>>c;
  82. x[i]=c;
  83. y[c]=i;
  84. }
  85.  
  86. cin>>m;
  87.  
  88. while(m--)
  89. {
  90. cin>>c>>d;
  91. l=y[c];
  92. k=y[d];
  93. g[l].push_back(k);
  94. g[k].push_back(l);
  95. }
  96.  
  97. for(i=1; i<=n; i++)
  98. {
  99. if(v[i]==0)
  100. {
  101. dfs(i,-1);
  102. }
  103. }
  104.  
  105. set<string>ans;
  106.  
  107. for(i=1; i<=n; i++)
  108. {
  109. if(a[i]==1)
  110. {
  111. ans.insert(x[i]);
  112. }
  113. }
  114.  
  115.  
  116. if(rr>1) cout<<endl;
  117.  
  118. cout<<"City map #"<< rr++ <<": "<<ans.size()<<" camera(s) found"<<endl;
  119.  
  120. for(auto ss:ans)
  121. {
  122. cout<<ss<<endl;
  123. }
  124. }
  125.  
  126.  
  127. return 0;
  128. }
  129.  
Add Comment
Please, Sign In to add comment