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

#include <bits/stdc++.h>
using namespace std;
const int maxn = 50010;
struct node {
  int u, v, w;
  bool used, mark, del;
  node(int x = 0, int y = 0, int z = 0) {
    u = x;
    v = y;
    w = z;
    mark = del = used = false;
  }
  bool operator<(const node &tmp) const { return w < tmp.w; }
} e[maxn];
int uf[maxn], n, m, cnt;
bool flag[maxn];
int find(int x) { return uf[x] = uf[x] == x ? uf[x] : find(uf[x]); }
int kruskal(bool &Unique, bool first) {
  int ans = cnt = 0;
  for (int i = 0; i <= n; ++i)
    uf[i] = i;
  for (int i = 0; i < m; ++i) {
    if (!first && e[i].del)
      continue;
    int fx = find(e[i].u);
    int fy = find(e[i].v);
    if (fx != fy) {
      uf[fx] = fy;
      ans += e[i].w;
      if (first) {
        e[i].used = true;
        if (e[i].mark)
          Unique = false;
      }
      if (++cnt == n - 1)
        return ans;
    }
  }
  return ans;
}
int main() {
  int u, v, w;
  // freopen("data.in","r",stdin);
  while (~scanf("%d %d", &n, &m)) {
    for (int i = 0; i < m; ++i) {
      scanf("%d %d %d", &u, &v, &w);
      e[i] = node(u, v, w);
    }
    memset(flag, false, sizeof(flag));
    sort(e, e + m);
    for (int i = 1; i < m; ++i)
      if (e[i].w == e[i - 1].w)
        e[i].mark = e[i - 1].mark = true;
    bool Unique = true;
    int MST = kruskal(Unique, 1);
    if (Unique)
      printf("%d %d\\n", cnt, MST);
    else {
      int res1 = 0, res2 = 0;
      for (int i = 0; i < m; ++i) {
        if (e[i].used && e[i].mark) {
          e[i].del = true;
          int mst = kruskal(Unique, 0);
          if (mst == MST && cnt == n - 1)
            flag[i] = true;
          e[i].del = false;
        }
        if (e[i].used && !flag[i]) {
          res1++;
          res2 += e[i].w;
        }
      }
      printf("%d %d\\n", res1, res2);
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 50010;
struct node {
  int u, v, w;
  bool mark, del, use;
  node(int x = 0, int y = 0, int z = 0) {
    u = x;
    v = y;
    w = z;
    mark = del = use = false;
  }
  friend bool operator<(const node &a, const node &b) { return a.w < b.w; }
} e[maxn];

int uf[510], n, m, cnt;
bool flag[maxn];
bool Unique;

int find(int x) {
  if (uf[x] == x)
    return x;
  else
    return find(uf[x]);
}
int kruscal(bool &Unique, bool first) {
  int ans, x, y;
  ans = 0;
  cnt = 0;
  for (int i = 0; i < m; i++) {
    if (!first && e[i].del)
      continue;
    x = find(e[i].u);
    y = find(e[i].v);
    if (x != y) {
      uf[x] = y;
      cnt++;
      ans += e[i].w;
      if (first) {
        e[i].use = 1;
        if (e[i].mark)
          Unique = 0;
      }
    }

    if (cnt == n - 1)
      return ans;
  }
  return ans;
}

void init() {
  for (int i = 0; i < 510; i++)
    uf[i] = i;
}
int main() {
  // freopen("data.in","r",stdin);
  scanf("%d%d", &n, &m);
  int u, v, w, res1, res2;
  res1 = 0, res2 = 0;
  Unique = 1;
  memset(flag, 0, sizeof(flag));
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &u, &v, &w);
    e[i] = node(u, v, w);
  }
  sort(e, e + m);
  for (int i = 1; i < m; i++) {
    if (e[i].w == e[i - 1].w)
      e[i].mark = e[i - 1].mark = 1;
  }
  init();
  int MST = kruscal(Unique, true);
  if (Unique)
    printf("%d %d\\n", cnt, MST);
  else {
    for (int i = 0; i < m; i++) {
      if (e[i].mark && e[i].use) {
        e[i].del = 1;
        init();
        int mst = kruscal(Unique, false);
        if (mst == MST && cnt == n - 1)
          flag[i] = 1;
        e[i].del = 0;
      }

      if (!flag[i] && e[i].use) {
        res1++;
        res2 += e[i].w;
      }
    }
    printf("%d %d\\n", res1, res2);
  }
}