백준 알고리즘 14492번 : 부울행렬의 부울곱

Zoo Da·2021년 11월 23일
0

백준 알고리즘

목록 보기
262/337
post-thumbnail

링크

https://www.acmicpc.net/problem/14492

sol1) 구현

#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
#define fastio ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define int int64_t
using namespace std;

using matrix = vector<vector<int>>;

matrix mul(matrix a,matrix b){
  const int len = a.size();
  matrix res(len, vector<int>(len));
  for(int i = 0; i < len; i++){
    for(int j = 0; j < len; j++){
      for(int k = 0; k < len; k++){
        res[i][j] |= (a[i][k] & b[k][j]);
      }
    }
  }
  return res;
}

int32_t main() {
  fastio;
  int n,cnt = 0; cin >> n;
  matrix a(n, vector<int>(n)),b(n, vector<int>(n));
  for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) cin >> a[i][j];
  for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) cin >> b[i][j];
  auto ans = mul(a,b);
  for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) cnt += ans[i][j];
  cout << cnt << "\n";
}
profile
메모장 겸 블로그

0개의 댓글