QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#921480#906. 强连通分量SG95#WA 1ms14800kbC++201.2kb2025-02-28 23:32:422025-02-28 23:32:42

Judging History

This is the latest submission verdict.

  • [2025-02-28 23:32:42]
  • Judged
  • Verdict: WA
  • Time: 1ms
  • Memory: 14800kb
  • [2025-02-28 23:32:42]
  • Submitted

answer

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

const int MXN = 500005;
const int MXM = 500005;
int N, M;
int head[MXN], to[MXM], nxt[MXM];
int dfn[MXN], low[MXN], did;
int stk[MXN], top(-1);
bool instk[MXN];
int cnt;
vector<int> scc[MXN];

void dfs(int p) {
  low[p] = dfn[p] = did++;
  stk[++top] = p, instk[p] = true;
  for (int e(head[p]); ~e; e = nxt[e]) {
    int q(to[e]);
    if (dfn[q] == -1) {
      dfs(q);
      low[p] = min(low[p], low[q]);
    } else if (instk[q]) {
      low[p] = min(low[p], dfn[q]);
    }
  }
  if (low[p] == dfn[p]) {
    int cur(cnt++);
    while (~top) {
      int q(stk[top--]);
      instk[q] = false;
      scc[cur].push_back(q);
      if (q == p) break;
    }
  }
}

int main() {
  cin >> N >> M;
  memset(head, -1, sizeof head);
  for (int i(0), x(0), y(0); i != M; ++i) {
    cin >> x >> y;
    to[i] = y, nxt[i] = head[x], head[x] = i;
  }
  memset(dfn, -1, sizeof dfn);
  for (int i(0); i != N; ++i) if (dfn[i] == -1) dfs(i);
  cout << cnt << endl;
  for (int i(0); i != cnt; ++i) {
    cout << scc[i].size();
    for (int j : scc[i]) cout << ' ' << j;
    cout << endl;
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 14800kb

input:

6 7
1 4
5 2
3 0
5 5
4 1
0 3
4 2

output:

4
2 3 0
1 2
2 4 1
1 5

result:

wrong answer 5 is later than 2, but there is an edge (5, 2)