fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int a[3001][3001];
  4. bool v[3001];
  5. vector<int> g[3001];
  6. void dfs(int r,int d,int p)
  7. {
  8. v[r]=true;
  9.  
  10. if(d==2)
  11. {a[p][r]++;}
  12. // cout<<p<<r<<" ";}
  13. if(d==2)
  14. v[r]=false;
  15. //return ;}
  16. if(d<2)
  17. {
  18. for(int nn:g[r])
  19. {
  20. if(!v[nn])
  21. dfs(nn,d+1,p);
  22. }}
  23. }
  24. int main()
  25. {
  26. int n,m;
  27. cin>>n>>m;
  28. for(int i=0;i<m;i++)
  29. {
  30. int a,b;
  31. cin>>a>>b;
  32. g[a].push_back(b);
  33. }
  34. // int a[n+1][m+1];
  35. for(int i=0;i<3001;i++)
  36. {
  37. for(int j=0;j<3001;j++)
  38. a[i][j]=0;
  39. }
  40. for(int i=1;i<=n;i++)
  41. {dfs(i,0,i);
  42. // cout<<"-";
  43. // v={false};
  44. memset(v,false,sizeof(v));
  45. }
  46. int c=0;
  47. for(int i=1;i<=n;i++)
  48. {
  49. for(int j=1;j<=n;j++)
  50. {if(a[i][j]==3)
  51. c++;
  52. a[j][i]=0;}
  53. }
  54.  
  55. cout<<c<<"\n";
  56. return 0;
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63. }
Success #stdin #stdout 0.01s 38904KB
stdin
5 12
1 3
1 4
1 5
2 3
2 4
2 5
3 1
3 2
4 1
4 2
5 2
5 4
stdout
1