Advertisement
a53

autostrada

a53
Jan 28th, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. #include <fstream>
  2. #include <cstring>
  3. #include<algorithm>
  4. #include<vector>
  5. #define MAXN 101
  6. using namespace std;
  7. ifstream fin("autostrada.in");
  8. ofstream fout("autostrada.out");
  9. struct lista
  10. {
  11. int nod;
  12. int c;
  13. lista *urm;
  14. };
  15. lista *g[MAXN];
  16. struct Punct
  17. {
  18. int x, y ;
  19. friend bool operator <(Punct p1, Punct p2)
  20. {
  21. if (p1.x != p2.x) return p1.x < p2.x ;
  22. else return p1.y < p2.y ;
  23. }
  24. };
  25.  
  26. vector <Punct> v ;
  27. int t[101],niv[101],l[101],viz[101],n,m;
  28.  
  29. void ad(int x,int y)
  30. {
  31. lista *p;
  32. p=new lista;
  33. p->nod=y;
  34. p->c=0;
  35. p->urm=g[x];
  36. g[x]=p;
  37. }
  38.  
  39. void citgraf()
  40. {
  41. int x,y,k;
  42. fin>>n>>m;
  43. for(k=1; k<=m; k++)
  44. {
  45. fin>>x>>y;
  46. ad(x,y);
  47. ad(y,x);
  48. }
  49. }
  50. void df(int nod)
  51. {
  52. lista *p;
  53. int x;
  54. viz[nod]=1;
  55. l[nod]=niv[nod];
  56. for(p=g[nod]; p; p=p->urm)
  57. {
  58. x=p->nod;
  59. if(!viz[x])
  60. {
  61. niv[x]=niv[nod]+1;
  62. t[x]=nod;
  63. df(x);
  64. if(l[nod]>l[x])
  65. l[nod]=l[x];
  66. if(l[x]>niv[nod])
  67. p->c=1;
  68. }
  69. else if(x!=t[nod]&&l[nod]>niv[x])
  70. l[nod]=niv[x];
  71.  
  72. }
  73.  
  74. }
  75.  
  76. int main()
  77. {
  78. int i,k=0;
  79. lista *p;
  80. Punct q ;
  81. citgraf();
  82. for(i=1; i<=n; i++)
  83. if(!viz[i])
  84. {
  85. niv[i]=1;
  86. df(i);
  87. }
  88.  
  89. for(i=1; i<=n; i++)
  90. for(p=g[i]; p; p=p->urm)
  91. if(p->c==1)
  92. k++;
  93. fout<<k<<'\n';
  94. for(i=1; i<=n; i++)
  95. for(p=g[i]; p; p=p->urm)
  96. if(p->c==1)
  97. {
  98. if(i>p->nod) swap(i,p->nod);
  99. q.x=i;
  100. q.y=p->nod;
  101. v.push_back(q) ;
  102. }
  103. sort( v.begin() , v.end() , less<Punct>() ) ;
  104. for (i=0 ; i<k ; i++)
  105. fout<<v[i].x<<" "<<v[i].y<<"\n" ;
  106. return 0;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement