fork download
  1. #define _CRT_SECURE_NO_WARNINGS // preprocessor property for Visual Studio
  2. #include <stdio.h>
  3. using namespace std;
  4.  
  5. inline void array_input(int **&arr, int &n) {
  6. for (int i = 0; i < n; ++i) {
  7. arr[i] = new int[n];
  8. for (int j = 0; j < n; ++j) {
  9. scanf("%d", &arr[i][j]);
  10. }
  11. }
  12. }
  13.  
  14. inline void array_delete(int **&arr, int &n) {
  15. for (int i = 0; i < n; ++i) {
  16. delete[]arr[i];
  17. }
  18. delete[]arr;
  19. }
  20.  
  21. int main()
  22. {
  23. int n; // processing 1 testcase at a time
  24. while (scanf("%d", &n) && n) {
  25. int **number_of_roads = new int *[n];
  26. array_input(number_of_roads, n);
  27. int **friend_answers = new int *[n];
  28. array_input(friend_answers, n);
  29. int correct_answer;
  30. bool is_correct = true;
  31. for (int i = 0; i < n; ++i) {
  32. for (int j = 0; j < n && is_correct; ++j) { // stop calculation when error detected
  33. correct_answer = 0;
  34. for (int k = 0; k < n; ++k) {
  35. correct_answer += number_of_roads[i][k] * number_of_roads[k][j];
  36. }
  37. if (friend_answers[i][j] != correct_answer) {
  38. is_correct = false;
  39. }
  40. }
  41. }
  42. printf(is_correct ? "YES\n" : "NO\n");
  43. array_delete(number_of_roads, n);
  44. array_delete(friend_answers, n);
  45. }
  46. return 0;
  47. }
  48.  
Success #stdin #stdout 0.01s 5600KB
stdin
5
1 2 7 8 9
4 5 8 7 3
1 0 2 5 6
1 0 0 5 4
1 7 2 5 9
33 75 55 142 170
42 54 90 157 154
14 44 23 73 95
10 30 15 53 65
45 100 85 137 143

5
1 2 7 8 9
4 5 8 7 3
1 0 2 5 6
1 0 0 5 4
1 7 2 5 9
33 75 55 142 170
42 4 90 157 154
14 44 23 73 95
10 30 15 53 65
45 100 85 137 143

0
stdout
YES
NO