urmisaha

Uber Vacation - Finding k-core graphs

Nov 20th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.14 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define maxm 100001
  3. #define pb push_back
  4.  
  5. using namespace std;
  6.  
  7. // Similar to finding k-core graphs
  8. // Input:
  9. // 9 17 3
  10. // 1 2
  11. // 1 3
  12. // 2 3
  13. // 2 6
  14. // 3 4
  15. // 3 5
  16. // 3 6
  17. // 3 7
  18. // 4 5
  19. // 4 7
  20. // 4 8
  21. // 5 7
  22. // 5 8
  23. // 6 7
  24. // 6 9
  25. // 7 8
  26. // 7 9
  27. // Output:
  28. // 5
  29.  
  30. // Input:
  31. // 13 12 3
  32. // 1 2
  33. // 1 3
  34. // 1 4
  35. // 2 5
  36. // 2 6
  37. // 2 7
  38. // 3 8
  39. // 3 9
  40. // 3 10
  41. // 4 11
  42. // 4 12
  43. // 4 13
  44. // Output:
  45. // 0
  46.  
  47. int N, M, K, u, v, c;
  48. vector<int> deg(maxm), g[maxm];
  49.  
  50. int main(){
  51.     cin>>N>>M>>K;
  52.     for(int i=0; i<M; ++i){
  53.         cin>>u>>v;
  54.         g[u].pb(v);
  55.         g[v].pb(u);
  56.         deg[u]++;
  57.         deg[v]++;
  58.     }
  59.     queue<int> q;
  60.     for(int i=1; i<=N; ++i)
  61.         if(deg[i] > 0 && deg[i] < K)
  62.             q.push(i);
  63.     while(!q.empty()){
  64.         int x = q.front();
  65.         q.pop();
  66.         deg[x] = -1;
  67.         for(int i=0; i<g[x].size(); ++i){
  68.             deg[g[x][i]]--;
  69.             if(deg[g[x][i]] > 0 && deg[g[x][i]] < K)
  70.                 q.push(g[x][i]);
  71.         }
  72.     }
  73.     for(int i=1; i<=N; ++i)
  74.         if(deg[i] >= K)
  75.             c++;
  76.     cout<<c<<endl;
  77.     return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment