Problem p00329 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

#define MAX_V 1000
int V;
int n;
vector<int> G[MAX_V];
int match[MAX_V];
int Match[MAX_V];
int used[MAX_V];
int L[MAX_V], R[MAX_V];
int C = 0;

void add_edge(int u, int v) {
  G[u].push_back(v);
  G[v].push_back(u);
}

bool dfs(int v) {
  used[v] = C;
  for (int i = 0; i < (int)G[v].size(); i++) {
    int u = G[v][i], w = match[u];
    int a = min(u, v), b = max(u, v) % n;
    if (b < L[a] || R[a] < b)
      continue;
    if (w < 0 || (used[w] != C && dfs(w))) {
      match[v] = u;
      match[u] = v;
      return true;
    }
  }
  return false;
}

int bipartite_matching() {
  C++;
  int res = 0;
  memset(match, -1, sizeof(match));
  for (int v = 0; v < n; v++) {
    C++;

    if (match[v] < 0) {
      if (dfs(v))
        res++;
      else
        return 0;
    }
  }
  return res;
}

int t[500][500];
int main() {
  cin >> n;
  n--;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      scanf("%d", &t[i][j]);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      if (t[j][i] == 1)
        add_edge(i, n + j);

  V = n + n;
  for (int i = 0; i < n; i++) {
    L[i] = 0;
    R[i] = n - 1;
  }
  int F = bipartite_matching();
  //  for(int i=0;i<V;i++)Match[i]=match[i];

  if (F == n) {
    cout << "yes" << endl;
    for (int i = 0; i < n; i++) {
      // cout<<i<<endl;
      int l = 0, r = match[i] - n, m;
      while (l < r) {
        m = (l + r) / 2;
        R[i] = m;
        if (bipartite_matching() == n) {
          r = m;
        } else {
          l = m + 1;
        }
      }
      L[i] = R[i] = l;
      bipartite_matching();
    }

    // cout<<bipartite_matching()<<endl;
    for (int i = 0; i < n; i++) {
      cout << match[i] + 1 - n << endl;
    }

  } else {
    cout << "no" << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define MAX_V 1000
int V;
int n;
vector<int> G[MAX_V];
int match[MAX_V];
bool used[MAX_V];
int L[MAX_V], R[MAX_V];

void add_edge(int u, int v) {
  G[u].push_back(v);
  G[v].push_back(u);
}

bool dfs(int v) {
  used[v] = true;
  for (int i = 0; i < (int)G[v].size(); i++) {
    int u = G[v][i], w = match[u];
    int a = min(u, v), b = max(u, v) % n;
    if (b < L[a] || R[a] < b)
      continue;
    if (w < 0 || (!used[w] && dfs(w))) {
      match[v] = u;
      match[u] = v;
      return true;
    }
  }
  return false;
}

int bipartite_matching() {
  int res = 0;
  memset(match, -1, sizeof(match));
  for (int v = 0; v < V; v++) {
    if (match[v] < 0) {
      memset(used, 0, sizeof(used));
      if (dfs(v))
        res++;
    }
  }
  return res;
}

int t[500][500];
int main() {
  cin >> n;
  n--;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      cin >> t[i][j];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      if (t[j][i] == 1)
        add_edge(i, n + j);

  V = n + n;
  for (int i = 0; i < n; i++) {
    L[i] = 0;
    R[i] = n - 1;
  }

  if (bipartite_matching() == n) {
    cout << "yes" << endl;
    for (int i = 0; i < n; i++) {
      int l = 0, r = n - 1, m;
      while (l < r) {
        m = (l + r) / 2;
        R[i] = m;
        if (bipartite_matching() == n) {
          r = m;
        } else {
          l = m + 1;
        }
      }
      L[i] = R[i] = l;
    }
    bipartite_matching();
    // cout<<bipartite_matching()<<endl;
    for (int i = 0; i < n; i++) {
      cout << match[i] + 1 - n << endl;
    }

  } else {
    cout << "no" << endl;
  }

  return 0;
}