Advertisement
Saleh127

UVA 610/Bridge Find

Aug 10th, 2021
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 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[2005];
  6. vector<ll>g[2005];
  7. bool v[2005];
  8. ll timer;
  9. ll low[2005],in[2005];
  10. vector<pair<ll,ll>>ans;
  11.  
  12. void dfs(ll vertex,ll parent)
  13. {
  14. v[vertex]=1;
  15.  
  16. low[vertex] = in[vertex] = ++timer;
  17.  
  18. ll edge=0;
  19.  
  20. for(auto child:g[vertex])
  21. {
  22. if(child==parent) continue;
  23.  
  24. if(v[child])
  25. {
  26. low[vertex]=min(low[vertex],in[child]);
  27.  
  28. ///child have a lower in time
  29. /// means have a back edge/cycle
  30. if(in[vertex]>in[child])
  31. {
  32. ans.push_back({vertex,child});
  33. }
  34. }
  35. else
  36. {
  37. dfs(child,vertex);
  38.  
  39. ///if not visited then there must be a edge
  40. ///if it's also a bridge then two way
  41.  
  42. if(low[child]>in[vertex])
  43. {
  44. ans.push_back({child,vertex});
  45. }
  46. ans.push_back({vertex,child});
  47. low[vertex]=min(low[vertex],low[child]);
  48. }
  49. }
  50. }
  51.  
  52.  
  53. void clr(ll n)
  54. {
  55. for(ll i=0; i<n+4; i++)
  56. {
  57. g[i].clear();
  58. v[i]=0;
  59. low[i]=-1;
  60. in[i]=-1;
  61. a[i]=0;
  62. }
  63. ans.clear();
  64. timer=0;
  65. }
  66.  
  67. int main()
  68. {
  69. ios_base::sync_with_stdio(0);
  70. cin.tie(0);
  71. cout.tie(0);
  72.  
  73. ll n,m,i,j,k,l,rr=1;
  74.  
  75. while(cin>>n>>m && (n+m)>0)
  76. {
  77. clr(n);
  78.  
  79. while(m--)
  80. {
  81. cin>>j>>k;
  82. g[j].push_back(k);
  83. g[k].push_back(j);
  84. }
  85.  
  86. for(i=1;i<=n;i++)
  87. {
  88. if(v[i]==0)
  89. {
  90. dfs(i,-1);
  91. }
  92. }
  93.  
  94. cout<<rr++<<endl<<endl;
  95.  
  96. for(auto ss:ans)
  97. {
  98. cout<<ss.first<<" "<<ss.second<<endl;
  99. }
  100.  
  101. cout<<"#"<<endl;
  102.  
  103. }
  104.  
  105. return 0;
  106. }
  107.  
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement