QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#480364#906. 强连通分量emsgerWA 0ms3580kbC++201.5kb2024-07-16 14:47:192024-07-16 14:47:19

Judging History

你现在查看的是最新测评结果

  • [2024-07-16 14:47:19]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3580kb
  • [2024-07-16 14:47:19]
  • 提交

answer

#include <algorithm>
#include <iostream>
#include <vector>

using i64 = long long;
using i128 = __int128_t;
using u32 = unsigned int;
using u64 = unsigned long long;
using u128 = __uint128_t;

int main()
{
  int n, m;
  std::cin >> n >> m;

  std::vector<std::vector<int>> adj(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    std::cin >> a >> b;
    adj[a].emplace_back(b);
  }

  std::vector<int> dfn(n, -1), low(n, -1);
  int cnt = 0;
  std::vector<int> stk;
  std::vector<bool> instk(n);
  int scc = 0;
  std::vector<int> belong(n, -1);
  auto dfs = [&](auto &&self, int u) -> void
  {
    dfn[u] = low[u] = cnt++;
    stk.emplace_back(u);
    instk[u] = true;
    for (auto v : adj[u]) {
      if (dfn[v] == -1) {
        self(self, v);
        low[u] = std::min(low[u], low[v]);
      } else if (instk[v]) {
        low[u] = std::min(low[u], dfn[v]);
      }
    }
    if (low[u] == dfn[u]) {
      while (true) {
        int x = stk.back();
        stk.pop_back();
        instk[x] = false;
        belong[x] = scc;
        if (x == u)  break;
      }
      scc++;
    }
  };

  for (int i = 0; i < n; i++) {
    if (dfn[i] == -1) dfs(dfs, i);
  }

  std::vector<std::vector<int>> group(scc);
  for (int i = 0; i < n; i++) {
    group[belong[i]].emplace_back(i);
  }

  std::cout << scc << std::endl;
  for (const auto &i : group) {
    std::cout << i.size() << " ";
    for (auto j : i) {
      std::cout << j << " ";
    }
    std::cout << std::endl;
  }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 3580kb

input:

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

output:

4
2 0 3 
1 2 
2 1 4 
1 5 

result:

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