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

#include <bits/stdc++.h>
#define f fafa
#define s second
#define mp make_pair
using namespace std;
typedef pair<int, int> pii;
int n, m, k, a, b, minim;
int adj[3005][3005];
int deg[3005], ndeg[3005];
vector<int> v[3005];
bool flag[3005];
bool relax[3005];
int constructt(bool first) {
  int res = 0, id2 = -1;
  bool fafa = first;
  for (int i = 1; i <= n; i++)
    deg[i] = ndeg[i];
  for (int i = 1; i <= n + 1; i++) {
    int maxd = -1;
    int id = -1;
    for (int j = 1; j <= n; j++) {
      if (deg[j] >= maxd && j != id2) {
        maxd = deg[j];
        id = j;
      }
    }
    if (maxd <= 0)
      break;
    if (fafa) {
      id2 = id;
      fafa = false;
    } else {
      // printf("cacad\\n");
      res += 1;
      deg[id] = 0;
      for (int j = 1; j <= n; j++)
        if (adj[id][j] && deg[j] > 0)
          deg[j] -= 1;
    }
  }
  return res;
}
int constructt2(bool first) {
  int res = 0, id2 = -1;
  bool fafa = first;
  for (int i = 1; i <= n; i++)
    deg[i] = ndeg[i];
  for (int i = 1; i <= n + 1; i++) {
    int maxd = -1;
    int id = -1;
    for (int j = 1; j <= n; j++) {
      if (deg[j] >= maxd && j != id2) {
        maxd = deg[j];
        id = j;
      }
    }
    if (maxd <= 0)
      break;
    if (fafa) {
      id2 = id;
      fafa = false;
    } else {
      // printf("cacad2\\n");
      id2 = -1;
      res += 1;
      deg[id] = 0;
      for (int j = 1; j <= n; j++)
        if (adj[id][j] && deg[j] > 0)
          deg[j] -= 1;
    }
  }
  return res;
}
int constructt3(bool first) {
  int res = 0, id2 = -1;
  bool fafa = first;
  for (int i = 1; i <= n; i++)
    deg[i] = ndeg[i];
  for (int i = 1; i <= n + 1; i++) {
    int maxd = -1;
    int id = -1;
    for (int j = 1; j <= n; j++) {
      if (deg[j] >= maxd) {
        maxd = deg[j];
        id = j;
      }
    }
    if (maxd <= 0)
      break;
    if (fafa) {
      for (int j = 1; j <= n; j++) {
        if (adj[id][j]) {
          deg[j] = 0;
          res++;
          for (int p = 1; p <= n; p++) {
            if (adj[j][p] && deg[p] > 0)
              deg[p] -= 1;
          }
        }
      }
      fafa = false;
    } else {
      // printf("cacad3\\n");
      res += 1;
      deg[id] = 0;
      for (int j = 1; j <= n; j++)
        if (adj[id][j] && deg[j] > 0)
          deg[j] -= 1;
    }
  }
  return res;
}
int constructt4(bool first) {
  int res = 0, id2 = -1;
  bool fafa = first;
  for (int i = 1; i <= n; i++)
    deg[i] = ndeg[i];
  for (int i = 1; i <= n + 1; i++) {
    int maxd = -1;
    int id = -1;
    for (int j = 1; j <= n; j++) {
      if (deg[j] >= maxd) {
        maxd = deg[j];
        id = j;
      }
    }
    if (maxd <= 0)
      break;
    if (fafa) {
      for (int j = 1; j <= n; j++) {
        if (adj[id][j]) {
          deg[j] = 0;
          res++;
          for (int p = 1; p <= n; p++) {
            if (adj[j][p] && deg[p] > 0)
              deg[p] -= 1;
          }
        }
      }
      fafa = false;
    } else {
      // printf("cacad3\\n");
      res += 1;
      if (res == 1)
        fafa = true;
      deg[id] = 0;
      for (int j = 1; j <= n; j++)
        if (adj[id][j] && deg[j] > 0)
          deg[j] -= 1;
    }
  }
  return res;
}
int constructt5(bool first) {
  int res = 0, id2 = -1;
  bool fafa = first;
  for (int i = 1; i <= n; i++)
    deg[i] = ndeg[i];
  for (int i = 1; i <= n + 1; i++) {
    int maxd = -1;
    int id = -1;
    for (int j = 1; j <= n; j++) {
      if (deg[j] > maxd && j != id2) {
        maxd = deg[j];
        id = j;
      }
    }
    if (maxd <= 0)
      break;
    if (fafa) {
      id2 = id;
      fafa = false;
    } else {
      // printf("cacad\\n");
      res += 1;
      deg[id] = 0;
      for (int j = 1; j <= n; j++)
        if (adj[id][j] && deg[j] > 0)
          deg[j] -= 1;
    }
  }
  return res;
}
int constructt6(bool first) {
  int res = 0, id2 = -1;
  bool fafa = first;
  for (int i = 1; i <= n; i++)
    deg[i] = ndeg[i];
  for (int i = 1; i <= n + 1; i++) {
    int maxd = -1;
    int id = -1;
    for (int j = 1; j <= n; j++) {
      if (deg[j] > maxd && j != id2) {
        maxd = deg[j];
        id = j;
      }
    }
    if (maxd <= 0)
      break;
    if (fafa) {
      id2 = id;
      fafa = false;
    } else {
      // printf("cacad2\\n");
      id2 = -1;
      res += 1;
      deg[id] = 0;
      for (int j = 1; j <= n; j++)
        if (adj[id][j] && deg[j] > 0)
          deg[j] -= 1;
    }
  }
  return res;
}
int constructt7(bool first) {
  int res = 0, id2 = -1;
  bool fafa = first;
  for (int i = 1; i <= n; i++)
    deg[i] = ndeg[i];
  for (int i = 1; i <= n + 1; i++) {
    int maxd = -1;
    int id = -1;
    for (int j = 1; j <= n; j++) {
      if (deg[j] >= maxd) {
        maxd = deg[j];
        id = j;
      }
    }
    if (maxd <= 0)
      break;
    if (fafa) {
      for (int j = 1; j <= n; j++) {
        if (adj[id][j]) {
          deg[j] = 0;
          res++;
          for (int p = 1; p <= n; p++) {
            if (adj[j][p] && deg[p] > 0)
              deg[p] -= 1;
          }
        }
      }
      fafa = false;
    } else {
      // printf("cacad3\\n");
      res += 1;
      deg[id] = 0;
      for (int j = 1; j <= n; j++)
        if (adj[id][j] && deg[j] > 0)
          deg[j] -= 1;
    }
  }
  return res;
}
int constructt8(bool first) {
  int res = 0, id2 = -1;
  bool fafa = first;
  for (int i = 1; i <= n; i++)
    deg[i] = ndeg[i];
  for (int i = 1; i <= n + 1; i++) {
    int maxd = -1;
    int id = -1;
    for (int j = 1; j <= n; j++) {
      if (deg[j] > maxd) {
        maxd = deg[j];
        id = j;
      }
    }
    if (maxd <= 0)
      break;
    if (fafa) {
      for (int j = 1; j <= n; j++) {
        if (adj[id][j]) {
          deg[j] = 0;
          res++;
          for (int p = 1; p <= n; p++) {
            if (adj[j][p] && deg[p] > 0)
              deg[p] -= 1;
          }
        }
      }
      fafa = false;
    } else {
      // printf("cacad3\\n");
      res += 1;
      if (res == 1)
        fafa = true;
      deg[id] = 0;
      for (int j = 1; j <= n; j++)
        if (adj[id][j] && deg[j] > 0)
          deg[j] -= 1;
    }
  }
  return res;
}
int num = 0;
int nabrak = 0;
void dfs(int now) {
  int sz = v[now].size();
  flag[now] = true;
  ndeg[now] = sz;
  num++;
  bool masuk = false;
  for (int i = 0; i < sz; i++) {
    if (!flag[v[now][i]]) {
      masuk = true;
      dfs(v[now][i]);
    }
  }
  if (sz > 1 && masuk == false)
    nabrak++;
  if (sz == 1 || sz > 2)
    nabrak += 10;
}
int main() {
  scanf("%d %d %d", &n, &m, &k);
  for (int i = 1; i <= m; i++) {
    scanf("%d %d", &a, &b);
    v[a].push_back(b);
    v[b].push_back(a);
    adj[a][b] = 1;
    adj[b][a] = 1;
  }
  memset(flag, false, sizeof(flag));
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (!flag[i]) {
      memset(ndeg, 0, sizeof(ndeg));
      num = nabrak = minim = 0;
      dfs(i);
      if (nabrak == 1) {
        // printf("sad %d %d\\n",i,num);
        ans += (num + 1) / 2;
        continue;
      }
      for (int now = 1; now <= n; now++) {
        if (v[now].size() > k && ndeg[now] > k) {
          minim += 1;
          ndeg[now] = 0;
          for (int i = 1; i <= n; i++) {
            if (adj[now][i] && ndeg[i] > 0) {
              ndeg[i]--;
            }
          }
        }
      }
      for (int now = 1; now <= n; now++) {
        if (v[now].size() == 1 && ndeg[now] > 0) {
          minim += 1;
          // printf("%d now %d\\n",i,now);
          ndeg[v[now][0]] = 0;
          for (int i = 1; i <= n; i++) {
            if (adj[v[now][0]][i] && ndeg[i] > 0) {
              ndeg[i]--;
            }
          }
        }
      }
      // printf("%d\\n",minim);
      int gg = min(constructt(false), constructt(true));
      gg = min(gg, constructt2(true));
      gg = min(gg, constructt3(true));
      gg = min(gg, constructt4(true));
      gg = min(gg, constructt5(false));
      gg = min(gg, constructt5(true));
      gg = min(gg, constructt6(true));
      gg = min(gg, constructt7(true));
      gg = min(gg, constructt8(true));
      // printf("%d %d %d\\n",i,minim, gg);
      ans += minim + gg;
    }
  }
  // printf("%d\\n",ans);
  if (ans > k)
    printf("Impossible\\n");
  else
    printf("%d\\n", ans);
}

Error: TLE

Fixed submission:

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

#define ll long long
#define llu unsigned long long

vector<int> adj[3005];
int ans, flag[3005], n;

vector<int> tmp[40];

inline int deg(int v) {
  int ret = 0;
  for (int i = 0; i < (int)adj[v].size(); i++)
    ret += (flag[adj[v][i]] == 0);
  return ret;
}

void dfs(int cur) {
  if (cur >= ans)
    return;

  // cek udah selesai atau belum
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (flag[i]) {
      cnt++;
      continue;
    }

    if (deg(i) == 0)
      cnt++;
    else
      break;
  }

  if (cnt == n) {
    ans = min(ans, cur);
    return;
  }

  for (int v = 1; v <= n; v++) {
    if (flag[v])
      continue;

    int degree = deg(v);
    if (degree == 0)
      continue;

    // coba di flag
    if (degree > 1) {
      flag[v] = 1;
      dfs(cur + 1);
    }

    // coba nggak diflag
    tmp[cur].clear();

    for (int i = 0; i < (int)adj[v].size(); i++) {
      int cand = adj[v][i];
      if (flag[cand])
        continue;

      flag[cand] = 1;
      tmp[cur].push_back(cand);
    }

    dfs(cur + degree);

    flag[v] = 0;
    for (int i = 0; i < (int)tmp[cur].size(); i++)
      flag[tmp[cur][i]] = 0;
  }
}

int main() {
  int m, k, u, v;
  scanf("%d %d %d", &n, &m, &k);

  for (int i = 0; i < m; i++) {
    scanf("%d %d", &u, &v);

    adj[u].push_back(v), adj[v].push_back(u);
  }

  ans = k + 1;
  dfs(0);

  if (ans > k)
    printf("Impossible\\n");
  else
    printf("%d\\n", ans);
}