Advertisement
_no0B

Untitled

Nov 29th, 2021
650
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.46 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define LL long long
  3. #define N ((int)6e4 + 5)
  4. #define MOD ((int)998244353 + 0)
  5. #define MAX ((int)1e9 + 7)
  6. #define MAXL ((ll)1e18 + 7)
  7. #define MAXP ((int)1e3 + 7)
  8. #define thr 1e-8
  9. #define pi acos(-1)  /// pi = acos ( -1 )
  10. #define fastio ios_base::sync_with_stdio(false),cin.tie(NULL)
  11. #define endl "\n"
  12.  
  13. using namespace std;
  14.  
  15.  
  16.  
  17. vector < int > edg[1005]; /// edg[0] , edg[1] , edg[2]
  18.  
  19. /// edg[i] = nodes connected to node 'i'
  20.  
  21. void AddEdge(int u , int v)
  22. {
  23.     edg[u].push_back(v);
  24. }
  25.  
  26.  
  27.  
  28. void bfs(int source){
  29.     queue < int > que;
  30.     que.push(source);
  31.     memset(dis,-1,sizeof dis);
  32.     dis[source] = 0;
  33.     while(!que.empty()){
  34.         int node = que.front();
  35.         que.pop();
  36.         for(int u:edg[node]){
  37.             if(vis[u] == 0){
  38.                 que.push(u);
  39.                 dis[u] = dep[node] + 1;
  40.             }
  41.         }
  42.     }
  43. }
  44.  
  45.  
  46.  
  47.  
  48. void BuildGraph()
  49. {
  50.     vector < int > prime = Sieve(1000);
  51.     for(int i = 4 ; i <= 1000 ; i++){
  52.         for(int p:prime){
  53.             if(p < i && i % p == 0){
  54.                 AddEdge(i , i + p);
  55.             }
  56.         }
  57.     }
  58. }
  59.  
  60.  
  61.  
  62. int main()
  63. {
  64.  
  65.     /// problem: https://lightoj.com/problem/number-transformation
  66.     int t;
  67.     cin>>t;
  68.  
  69.     BuildGraph();
  70.  
  71.     while(t--){
  72.         int source , des;
  73.         cin>>source >> des;
  74.         bfs(source); /// O(n + m)
  75.         cout<<dis[des]<<endl;
  76.     }
  77.  
  78.     /// O(n*n + t*(n+m));
  79.  
  80.  
  81.     return 0;
  82. }
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement