fork download
  1. #include <iostream>
  2. #include <list>
  3. #include <cstring>
  4. using namespace std;
  5. int main()
  6. {
  7. int lmiast, ldrog;
  8. cin >> lmiast;
  9. cin >> ldrog;
  10. int lpowiazan=0, zmiasta, domiasta;
  11. short** ttwod = new short*[lmiast+1]; // tablica wskaźników
  12. for (int i=0; i<lmiast+1; i++)
  13. {
  14. ttwod[i]=new short[lmiast+1];
  15. for (int j=0; j<lmiast+1; j++)
  16. ttwod[i][j]=0;
  17. }
  18. for (int i=0; i<ldrog; i++)
  19. {
  20. cin >> zmiasta;
  21. cin >> domiasta;
  22. ttwod[zmiasta][domiasta]++;
  23. }
  24. /* KONIEC TABLICY. CO DALEJ? */
  25. /* WYPISANIE TYCH, KTÓRE ŁĄCZĄ SIĘ Z 2 */
  26. list<int>listawarstw;
  27. for (int i=0; i<lmiast+1; i++)
  28. {
  29. if (ttwod[i][2]!=0)
  30. {
  31. for (int temp=ttwod[i][2]; temp!=0; temp=temp-1)
  32. {
  33. listawarstw.push_back(i);
  34. }
  35. if (i==1)
  36. {
  37. for (int temp=ttwod[i][2]; temp!=0; temp=temp-1)
  38. {
  39. listawarstw.pop_back();
  40. lpowiazan++;
  41. }
  42. }
  43. }
  44. }
  45. /*DODAWANIE KOLEJNYCH WARSTW, SPRAWDZANIE CZY ŁĄCZĄ SIĘ Z 1*/
  46. while (listawarstw.size()!=0)
  47. {
  48. int i=listawarstw.back();
  49. for(int j=0; j!=lmiast+1; j++)
  50. {
  51. if (ttwod[j][i]!=0)
  52. {
  53. if (j==1)
  54. {
  55. for (int temp=ttwod[j][i]; temp!=0; temp=temp-1)
  56. {
  57. lpowiazan++;
  58. }
  59. }
  60. else
  61. {
  62. for (int temp=ttwod[j][i]; temp!=0; temp=temp-1)
  63. {
  64. listawarstw.push_front(j);
  65. }
  66. }
  67. }
  68. }
  69. if (listawarstw.size()==0)
  70. break;
  71. listawarstw.pop_back();
  72. }
  73. cout<<"liczba powiazan: "<<lpowiazan<<endl;
  74. }
  75.  
Time limit exceeded #stdin #stdout -1s 3516KB
stdin
31 60
1 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 2
1 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 2
stdout
Standard output is empty