Advertisement
Stuffbyliang

Untitled

Feb 17th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.69 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <set>
  4.  
  5. using namespace std;
  6.  
  7. // matrix is 1 based.
  8.  
  9. bool matrix[50][50]={false};
  10.  
  11. //add edge
  12. void addfriend(int a, int b) {
  13.     matrix[a][b] = true;
  14.     matrix[b][a] = true;  
  15. }
  16.  
  17. //remove edge
  18. void unfriend(int a, int b) {
  19.     matrix[a][b] = false;
  20.     matrix[b][a] = false;  
  21. }
  22.  
  23. //count friends
  24. void n(int x) {
  25.     int count=0;
  26.     //through the row of x, count how many times there is an edge. = num of friends
  27.  
  28.     for(int i=1; i<50; i++) {
  29.         if(matrix[x][i]) {
  30.             count++;
  31.         }
  32.     }
  33.     cout << count << "\n";
  34. }
  35.  
  36. //count friends of friends
  37. void f(int x) {
  38.     set<int> friends; //this set will contain direct friends and x himself
  39.     set<int> foff; //this set will contain friends of friends
  40.    
  41.     friends.insert(x); //insert themself into the set
  42.  
  43.     //add to set friends
  44.     for(int i=1; i<50; i++) {
  45.         //if there is an edge
  46.         if(matrix[x][i]) {
  47.             //if not in set already
  48.             if(friends.count(i) == 0) {
  49.                 friends.insert(i);
  50.             }
  51.         }
  52.     }
  53.    
  54.     //loop through friends of friends
  55.     for(int i=1; i<50; i++) {
  56.         if(matrix[x][i]) {
  57.             for(int j=1; j<50; j++) {
  58.                 if(matrix[i][j]) {
  59.                     //if the friend is not in direct friends
  60.                     if(friends.count(j) == 0) {
  61.                         //if the friend has not been seen before in friends of freinds
  62.                         if(foff.count(j) == 0) {
  63.                             foff.insert(j); //insert the friend into friends of friends set
  64.                         }
  65.                     }
  66.                 }
  67.             }
  68.         }
  69.     }
  70.     cout << foff.size() << "\n";
  71. }
  72.  
  73.  
  74. //Floyd–Warshall algorithm
  75. void s(int x, int y) {
  76.     int dist[50][50];
  77.     for(int i=1; i<50; i++) {
  78.         for(int j=1; j<50; j++) {
  79.             if(i==j) {
  80.                 dist[i][j] = 0;
  81.             } else if(matrix[i][j]) {
  82.                 dist[i][j] = 1;
  83.             } else {
  84.                 dist[i][j] = 1000; //infinity
  85.             }
  86.         }
  87.     }
  88.     for(int k=1; k<50; k++) {
  89.         for(int i=1; i<50; i++) {
  90.             for(int j=1; j<50; j++) {
  91.                 if(dist[i][j] > dist[i][k] + dist[k][j]) {
  92.                     dist[i][j] = dist[i][k] + dist[k][j];
  93.                 }
  94.             }
  95.         }
  96.     }
  97.    
  98.     if(dist[x][y]==1000) {
  99.         cout << "Not connected\n";
  100.     } else {
  101.         cout << dist[x][y] << "\n";
  102.     }
  103. }
  104.  
  105. int main() {
  106.     cin.sync_with_stdio(0);
  107.     cin.tie(0);
  108.    
  109.     addfriend(1, 6);
  110.     addfriend(2, 6);
  111.     addfriend(3, 6);
  112.     addfriend(3, 4);
  113.     addfriend(3, 5);
  114.     addfriend(3, 15);
  115.     addfriend(4, 6);
  116.     addfriend(5, 6);
  117.     addfriend(6, 7);
  118.     addfriend(7, 8);
  119.     addfriend(8, 9);
  120.     addfriend(9, 10);
  121.     addfriend(9, 12);
  122.     addfriend(10, 11);
  123.     addfriend(11, 12);
  124.     addfriend(12, 13);
  125.     addfriend(13, 15);
  126.     addfriend(13, 14);
  127.     addfriend(16, 17);
  128.     addfriend(17, 18);
  129.     addfriend(16, 18);
  130.    
  131.     char command;
  132.     cin >> command;
  133.     while(command != 'q') {
  134.         int x, y;
  135.        
  136.         switch(command) {
  137.             case 'i':
  138.                 cin >> x >> y;
  139.                 addfriend(x, y);
  140.                 break;
  141.             case 'd':
  142.                 cin >> x >> y;
  143.                 unfriend(x, y);
  144.                 break;
  145.             case 'n':
  146.                 cin >> x;
  147.                 n(x);
  148.                 break;
  149.             case 'f':
  150.                 cin >> x;
  151.                 f(x);
  152.                 break;
  153.             case 's':
  154.                 cin >> x >> y;
  155.                 s(x, y);
  156.         }
  157.        
  158.         cin >> command;
  159.     }
  160.  
  161.     return 0;
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement