Much more efficiently, please!

Write a program to computes the same as the program below, but much more
efficiently, both in space and in time. The read variables n and m are
such that 1 ≤ n ≤ 1000 and 0 ≤ m ≤ 10n. Every given pair of x and y is
such that x ≠ y.

+------------------------------------------------+-------------------------------------------------+---+---+---+---+
|                                                |                                                 |   |   |   |   |
|     #include <iostream>                        |     bool OK2(int i) {                           |   |   |   |   |
|     #include <vector>                          |       if (i == n) return true;                  |   |   |   |   |
|     #include <algorithm>                       |                                                 |   |   |   |   |
|     using namespace std;                       |       vector<bool> U(2, false);                 |   |   |   |   |
|                                                |       for (int j = 0; j < i; ++j)               |   |   |   |   |
|                                                |         if (G[i][j] == 1) U[C[j]] = true;       |   |   |   |   |
|     const int INF = 1000000000;                |                                                 |   |   |   |   |
|     typedef vector<int> VI;                    |       for (int k = 0; k < 2; ++k)               |   |   |   |   |
|     typedef vector<VI> VVI;                    |         if (not U[k]) {                         |   |   |   |   |
|                                                |           C[i] = k;                             |   |   |   |   |
|                                                |           if (OK2(i + 1)) return true;          |   |   |   |   |
|     int n, m;                                  |         }                                       |   |   |   |   |
|     VVI G;                                     |       return false;                             |   |   |   |   |
|     int C[1000];                               |     }                                           |   |   |   |   |
|                                                |                                                 |   |   |   |   |
|                                                |                                                 |   |   |   |   |
|     bool OK1(VVI A) {                          |     int main() {                                |   |   |   |   |
|       for (int x = 0; x < n; ++x) A[x][x] = 0; |       while (cin >> n >> m) {                   |   |   |   |   |
|                                                |         G = VVI(n, VI(n, INF));                 |   |   |   |   |
|       for (int k = 0; k < n; ++k)              |         while (m--) {                           |   |   |   |   |
|         for (int i = 0; i < n; ++i)            |           int x, y;                             |   |   |   |   |
|           for (int j = 0; j < n; ++j)          |           cin >> x >> y;                        |   |   |   |   |
|             A[i][j] = min(A[i][j],             |           G[x][y] = G[y][x] = 1;                |   |   |   |   |
|                           A[i][k] + A[k][j]);  |         }                                       |   |   |   |   |
|                                                |                                                 |   |   |   |   |
|       for (int i = 0; i < n; ++i)              |         if (not OK1(G)) cout << "NC" << endl;   |   |   |   |   |
|         for (int j = 0; j < n; ++j)            |         else if (OK2(0)) cout << "yes" << endl; |   |   |   |   |
|           if (A[i][j] == INF) return false;    |         else cout << "no" << endl;              |   |   |   |   |
|       return true;                             |       }                                         |   |   |   |   |
|     }                                          |     }                                           |   |   |   |   |
+:==============================================:+:===============================================:+:=:+:=:+:=:+:=:+

Problem information

Author: Salvador Roura

Generation: 2026-01-25T10:12:26.594Z

© Jutge.org, 2006–2026.
https://jutge.org
