Advertisement
madmaxhasan

UVa 11504.cpp

May 23rd, 2018
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.20 KB | None | 0 0
  1. /* ====================================================== */
  2. #include <bits/stdc++.h>
  3. #define FileIn(file) freopen(file".txt", "r", stdin)
  4. #define FileOut(file) freopen(file".txt", "w", stdout)
  5. #define FOR(i, a, b) for(int i=a; i<=b; i++)
  6. #define REP(i, n) for(int i=0; i<n; i++)
  7. #define Fill(arr, val) memset(arr, val, sizeof arr)
  8. #define All(arr) arr.begin(), arr.end()
  9. #define PI 3.1415926535897932385
  10. #define ull unsigned long long
  11. #define ll long long
  12. #define pb push_back
  13. #define mkp make_pair
  14. #define MAX 10055
  15. #define INF 500000000
  16. #define debug cout << "DEBUG" << endl;
  17. using namespace std;
  18. typedef   pair <int, int>  ii;
  19. typedef   pair <char, int> ci;
  20. typedef   pair <int, char> ic;
  21. typedef vector <ii>       vii;
  22. typedef vector <ci>       vci;
  23. typedef vector <ic>       vic;
  24. typedef vector <int>       vi;
  25. typedef vector <bool>      vb;
  26. /* ====================================================== */
  27. bool sortbyFrst(const ii &a, const ii &b){return (a.first > b.first);}
  28. bool sortbyScnd(const ii &a, const ii &b){return (a.second > b.second);}
  29. bool perfectDec(const ic &a, const ic &b){return ((a.first != b.first) ? a.first > b.first : a.second < b.second);}
  30. int drX[] = {1, 1, 0, -1, -1, -1, 0, 1};
  31. int drY[] = {0, 1, 1, 1, 0, -1, -1, -1};
  32. /* ====================================================== */
  33.  
  34. int V, E;
  35. vector <vi> AdjList;
  36. bool visited[MAX];
  37. vii OutDegree;
  38. vi Degree;
  39.  
  40. void clr()
  41. {
  42.     AdjList.assign(V+1, vi());
  43.     Fill(visited, false);
  44.     OutDegree.clear();
  45.     Degree.assign(V+1, 0);
  46. }
  47.  
  48. void DFS(int u)
  49. {
  50.     visited[u] = true;
  51.     REP(i, AdjList[u].size())
  52.     {
  53.         int v = AdjList[u][i];
  54.         if(!visited[v])
  55.             DFS(v);
  56.     }
  57. }
  58.  
  59. int main()
  60. {
  61. #ifndef ONLINE_JUDGE
  62.     FileIn("in");
  63.     FileOut("out");
  64. #endif //ONLINE_JUDGE
  65.  
  66.     int TC;
  67.     scanf("%d",&TC);
  68.     while(TC--)
  69.     {
  70.         scanf("%d %d",&V,&E);
  71.         clr();
  72.         while(E--)
  73.         {
  74.             int x, y;
  75.             scanf("%d %d",&x,&y);
  76.             AdjList[x].push_back(y);
  77.             Degree[x]++;
  78.         }
  79.         FOR(i, 1, V)OutDegree.pb(mkp(i, Degree[i]));
  80.         sort(All(OutDegree), sortbyScnd);
  81.         int counter = 0;
  82.         REP(i, OutDegree.size())
  83.         {
  84.             int u = OutDegree[i].first;
  85.             if(!visited[u])
  86.             {
  87.                 ++counter;
  88.                 DFS(u);
  89.             }
  90.         }
  91.         printf("%d\n", counter);
  92.     }
  93.     return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement