QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#671030#906. 强连通分量QingyyxWA 2ms14476kbC++204.0kb2024-10-24 10:09:392024-10-24 10:09:40

Judging History

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

  • [2024-10-24 10:09:40]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:14476kb
  • [2024-10-24 10:09:39]
  • 提交

answer

#include <bits/stdc++.h>
#define ll long long
#define enl putchar('\n')
#define all(x) (x).begin(),(x).end()
#define debug(x) printf(" "#x":%d\n",x);
using namespace std;
const int MAXN = 1e5 + 5, MAXM = 1e5 + 5;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 998244353;
typedef pair<int, int> pii;
char buf[1 << 21], * p1 = buf, * p2 = buf, obuf[1 << 21], * o = obuf, of[35];
#define gc()(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
inline ll qpow(ll a, ll n) { ll res = 1; while (n) { if (n & 1)res = res * a % mod; n >>= 1; a = a * a % mod; }return res; }
template <class T = int>inline T read() { T s = 0, f = 1; char c = gc(); for (; !isdigit(c); c = gc())if (c == '-')f = -1; for (; isdigit(c); c = gc())s = s * 10 + c - '0'; return s * f; }
inline void read(int* a, int n) { for (int i = 1; i <= n; ++i)a[i] = read(); }
inline int inal(char* s) { int n = 0; for (s[0] = gc(); !isalpha(s[0]); s[0] = gc()); for (; isalpha(s[n]); s[++n] = gc()); return s[n] = 0, n; }
inline int indi(char* s) { int n = 0; for (s[0] = gc(); !isdigit(s[0]); s[0] = gc()); for (; isdigit(s[n]); s[++n] = gc()); return s[n] = 0, n; }
inline void outd(auto* a, int n) { for (int i = 1; i <= n; ++i)printf("%d ", a[i]); enl; }
int n, m, q;
struct EGDE {
    struct EG {
        int to, nxt;
    } e[MAXM << 1];
    int head[MAXN], tot;
    void clear(int n) { memset(head + 1, -1, sizeof(int) * n); tot = 0; }
    void add(int u, int v) { e[tot] = {v, head[u]}; head[u] = tot++; }
    EG operator[](const int& x)const { return e[x]; }
    int rep(int x) { return head[x]; }
} e, E;

struct SCC {
    int dfn[MAXN], low[MAXN], stk[MAXN], top, scc[MAXN];
    int tim, cntscc;
    bool vis[MAXN];
    void tarjan(int u) {
        dfn[u] = low[u] = ++tim;
        stk[++top] = u;
        vis[u] = 1;
        for (int i = e.rep(u); ~i; i = e[i].nxt) {
            int v = e[i].to;
            if (!dfn[v]) {
                tarjan(v);
                low[u] = min(low[u], low[v]);
            } else if (vis[v])low[u] = min(low[u], low[v]);
        }
        if (dfn[u] == low[u]) {
            scc[u] = ++cntscc;
            vis[u] = 0;
            while (stk[top] != u) {
                scc[stk[top]] = cntscc;
                vis[stk[top--]] = 0;
            }
            top--;
        }
    }
    vector<int> sc[MAXN];
    void work() {
        for (int i = 1; i <= n; ++i) if (!dfn[i]) tarjan(i);
        for (int i = 1; i <= n; ++i) sc[scc[i]].push_back(i);
        for (int i = 1; i <= n; ++i)
            for (int j = e.rep(i); ~j; j = e[j].nxt) {
                if (scc[i] == scc[e[j].to])continue;
                E.add(scc[i], scc[e[j].to]);
            }
    }
    void topsort() {
        static int deg[MAXN];
        queue<int> q;
        for (int i = 1; i <= cntscc; ++i)
            for (int j = E.rep(i); ~j; j = E[j].nxt)deg[E[j].to]++;
        for (int i = 1; i <= cntscc; ++i)if (!deg[i])q.push(i);
        while (!q.empty()) {
            int u = q.front(); q.pop();
            printf("%d ", sc[u].size());
            for (int i = 0; i < sc[u].size(); ++i)printf("%d ", sc[u][i] - 1); enl;
            for (int i = E.rep(u); ~i; i = E[i].nxt) {
                int v = E[i].to;
                if (!--deg[v])q.push(v);
            }
        }
    }
}s;

void solve() {
    n = read(), m = read();
    e.clear(n); E.clear(n);
    for (int i = 1; i <= m; ++i) {
        int u = read() + 1, v = read() + 1;
        e.add(u, v);
    }
    s.work();
    s.topsort();
}
signed main(signed argc, char const* argv[]) {
    clock_t c1 = clock();
#ifdef LOCAL
    freopen("in.in", "r", stdin);
    freopen("out.out", "w", stdout);
#endif
    //=============================================================
    int TxT = 1;
    // TxT = read();
    while (TxT--)
        solve();
    //=============================================================
#ifdef LOCAL
    end :
    cerr << "Time Used:" << clock() - c1 << "ms" << endl;
#endif
    return 0;
}

詳細信息

Test #1:

score: 0
Wrong Answer
time: 2ms
memory: 14476kb

input:

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

output:

2 0 3 
2 1 4 
1 5 
1 2 

result:

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