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

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

class ArtPoint {
public:
  int nv, ne;
  vector<bool> visited;
  vector<vector<int>> adj;
  vector<int> lowest;
  vector<int> prenum;
  vector<int> parent;
  ArtPoint() {}
  ArtPoint(int v, int e)
      : nv(v), ne(e), visited(v, false), adj(v), lowest(v), prenum(v),
        parent(v) {}
  void setAdj();
  void solve();
  int dfs(int u, int pa, int &pn);
};

void ArtPoint::setAdj() {
  for (auto i = 0; i < ne; ++i) {
    int s, t;
    cin >> s >> t;

    adj[s].push_back(t);
    adj[t].push_back(s);
  }
}

int ArtPoint::dfs(int u, int pa, int &pn) {
  visited[u] = true;
  parent[u] = pa;
  prenum[u] = pn++;
  lowest[u] = prenum[u];
  int n = adj[u].size();

  for (auto i = 0; i < n; ++i) {
    int v = adj[u][i];
    if (!visited[v]) {
      int cl = dfs(v, u, pn);
      lowest[u] = min(lowest[u], cl);
    } else if (v != parent[u])
      lowest[u] = min(lowest[u], prenum[v]);
  }
  return lowest[u];
}

void ArtPoint::solve() {

  set<int> ret;
  int pn = 0;
  int root = 0;

  setAdj();

  dfs(root, -1, pn);

  int pc = 0;

  for (auto i = 0; i < nv; ++i) {
    if (i == root)
      continue;
    if (parent[i] == root)
      ++pc;
    else {
      if (prenum[parent[i]] <= lowest[i])
        ret.insert(parent[i]);
    }
  }

  if (pc > 1)
    ret.insert(root);

  for (auto it = ret.begin(); it != ret.end(); ++it)
    cout << *it << endl;
}

int main() {
  int nv, ne;
  cin >> nv >> ne;

  if (nv == 1)
    return 0;

  ArtPoint ap(nv, ne);

  ap.solve();

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

class ArtPoint {
public:
  int solve();
  void dfs(vector<vector<int>> &adj, vector<bool> &visited, int u);
};

void ArtPoint::dfs(vector<vector<int>> &adj, vector<bool> &visited, int u) {
  visited[u] = true;

  int n = adj[u].size();

  for (auto i = 0; i < n; ++i) {
    if (!visited[adj[u][i]]) {
      dfs(adj, visited, adj[u][i]);
    }
  }
}

int ArtPoint::solve() {
  int nv, ne;
  cin >> nv >> ne;

  if (nv == 1)
    return 0;

  vector<vector<int>> adj(nv);

  for (auto i = 0; i < ne; ++i) {
    int s, t;
    cin >> s >> t;

    adj[s].push_back(t);
    adj[t].push_back(s);
  }

  vector<int> ret;

  for (auto i = 0; i < nv; ++i) {
    int n = adj[i].size();
    if (n == 1)
      continue;

    vector<bool> visited(nv, false);
    visited[i] = true;

    dfs(adj, visited, adj[i][0]);

    for (int j = 1; j < n; ++j) {
      if (!visited[adj[i][j]]) {
        ret.push_back(i);
        break;
      }
    }
  }

  for (auto it = ret.begin(); it != ret.end(); ++it)
    cout << *it << endl;

  return 0;
}

int main() {
  ArtPoint ap;

  ap.solve();

  return 0;
}