Advertisement
Josif_tepe

Untitled

Dec 26th, 2022
984
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <set>
  4. #include <cstring>
  5. #include <queue>
  6. #include <cmath>
  7. #include <algorithm>
  8. using namespace std;
  9. typedef long long ll;
  10. const int maxn = 200;
  11. ll gcd(ll a, ll b) {
  12.     if(a < b) {
  13.         swap(a, b);
  14.     }
  15.     while(b != 0) {
  16.         ll tmp = a;
  17.         a = b;
  18.         b = tmp % b;
  19.     }
  20.     return a;
  21. }
  22. bool perfect_square(ll a, ll b) {
  23.     ll c = (a * a) + (b * b);
  24.     ll sq = sqrt(c);
  25.     if(sq * sq == c) {
  26.         return true;
  27.     }
  28.     return false;
  29. }
  30. int graph[maxn][maxn];
  31. bool visited[maxn];
  32. int match[maxn];
  33. bool dfs(int node) {
  34.     for(int i = 0; i < maxn; i++) {
  35.         if(graph[node][i] == 1 and !visited[i]) {
  36.             visited[i] = true;
  37.             if(match[i] == -1 or dfs(match[i])) {
  38.                 match[i] = node;
  39.                 return true;
  40.             }
  41.         }
  42.     }
  43.     return false;
  44. }
  45. int maximum_bipartite_matching() {
  46.     memset(match, -1, sizeof match);
  47.     int result = 0;
  48.     for(int i = 0; i < maxn; i++) {
  49.         memset(visited, false, sizeof visited);
  50.         if(dfs(i)) {
  51.             result++;
  52.         }
  53.     }
  54.     return result;
  55. }
  56. int main() {
  57.     memset(graph, 0, sizeof graph);
  58.     int n;
  59.     cin >> n;
  60.     vector<ll> v(n);
  61.    
  62.     for(int i = 0; i < n; i++) {
  63.         cin >> v[i];
  64.     }
  65.    
  66.     for(int i = 0; i < n; i++) {
  67.         for(int j = 0; j < n; j++) {
  68.             if(gcd(v[i], v[j]) == 1 and perfect_square(v[i], v[j])) {
  69.                 graph[i][j] = 1;
  70.             }
  71.         }
  72.     }
  73.     cout << maximum_bipartite_matching() << endl;
  74.     return 0;
  75. }
  76.  
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement