QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#549440#906. 强连通分量foolnineWA 0ms3616kbC++201.5kb2024-09-06 15:34:502024-09-06 15:34:50

Judging History

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

  • [2024-09-06 15:34:50]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3616kb
  • [2024-09-06 15:34:50]
  • 提交

answer

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

using i64 = long long;

int main(void)
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m;
    cin >> n >> m;

    vector<vector<int>> e(n);
    for (int i = 0; i < m; i++) {
        int u, v;
        cin >> u >> v;
        e[u].push_back(v);
    }

    //f值相同的点属于同一个强连通分量
    int dfncnt = 0, flag = 0;
    vector<int> dfn(n + 1, 0), low(n + 1, 0), f(n + 1, 0);
    stack<int> st;
    auto tarjan = [&](auto self, int u) -> void {
        st.push(u);
        dfn[u] = low[u] = ++dfncnt;
        for (auto v : e[u]) {
            if (dfn[v] == 0) {
                self(self, v);
                low[u] = min(low[u], low[v]);
            } else if (f[v] == 0) {
                low[u] = min(low[u], low[v]);
            }
        }
        if (dfn[u] == low[u]) {
            ++flag;
            while (st.top() != u) {
                f[st.top()] = flag;
                st.pop();
            }
            st.pop();
            f[u] = flag;
        }
    };
    for (int i = 0; i < n; i++) {
        if (dfn[i] == 0) {
            tarjan(tarjan, i);
        }
    }


    vector<vector<int>> ans(flag);
    for (int i = 0; i < n; i++) {
        ans[f[i] - 1].push_back(i);
    }

    for (auto vec : ans) {
        cout << vec.size();
        for (auto x : vec) {
            cout << " " << x;
        }
        cout << endl;
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

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

output:

2 0 3
1 2
2 1 4
1 5

result:

wrong answer Integer 0 violates the range [1, 6]