MaximCherchuk

Test2

Jan 22nd, 2018
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.09 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. typedef unsigned short us;
  6.  
  7. inline bool is_connected(pair<us, us> p, int first, int second) {
  8.     bool first_condition = (p.first == first && p.second == second);
  9.     bool second_condition = (p.first == second && p.second == first);
  10.     return first_condition || second_condition;
  11. }
  12.  
  13. void check_connectivity(us* indexTriples, int* connectivityOut, int vertices, pair<us, us> p, int index, int shift) {
  14.     if(p.first > p.second) {
  15.         swap(p.first, p.second);
  16.     }
  17.     for(int j = 0; j < vertices; j += 3) {
  18.         if(j == index) {
  19.             continue;
  20.         }
  21.  
  22.         int first = indexTriples[j];
  23.         int second = indexTriples[j + 1];
  24.         int third = indexTriples[j + 2];
  25.  
  26.         if(is_connected(p, first, second)) {
  27.             connectivityOut[index + shift] = j;
  28.             break;
  29.         }
  30.         if(is_connected(p, second, third)) {
  31.             connectivityOut[index + shift] = j + 1;
  32.             break;
  33.         }
  34.         if(is_connected(p, third, first)) {
  35.             connectivityOut[index + shift] = j + 2;
  36.         }
  37.     }
  38. }
  39.  
  40. void findConnectivity(us* indexTriples, int T, int* connectivityOut) {
  41.     const int vertices = 3 * T;
  42.     std::fill(connectivityOut, connectivityOut + vertices, -1);
  43.     for(int i = 0; i < vertices; i += 3) {
  44.         pair<us, us> p1 = make_pair(indexTriples[i], indexTriples[i + 1]);
  45.         pair<us, us> p2 = make_pair(indexTriples[i + 1], indexTriples[i + 2]);
  46.         pair<us, us> p3 = make_pair(indexTriples[i + 2], indexTriples[i]);
  47.  
  48.         check_connectivity(indexTriples, connectivityOut, vertices, p1, i, 0);
  49.         check_connectivity(indexTriples, connectivityOut, vertices, p2, i, 1);
  50.         check_connectivity(indexTriples, connectivityOut, vertices, p3, i, 2);
  51.     }
  52. }
  53.  
  54.  
  55. int main()
  56. {
  57.     int T = 3; // number of triangles
  58.     int* connectivityOut = new int[3 * T]; // edges
  59.     us *input = new us[9] {0,2,7,1,7,2,6,2,0};
  60.     findConnectivity(input, 3, connectivityOut);
  61.     for(int i = 0; i < 3 * T; ++i) {
  62.         cout << connectivityOut[i] + 1 << ' ';
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment