QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#482774#906. 强连通分量hcngWA 79ms48380kbC++142.2kb2024-07-17 21:28:122024-07-17 21:28:13

Judging History

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

  • [2024-07-17 21:28:13]
  • 评测
  • 测评结果:WA
  • 用时:79ms
  • 内存:48380kb
  • [2024-07-17 21:28:12]
  • 提交

answer

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

char *p1, *p2, buf[1000000];
#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++)
inline int read() {int x=0;char c=gc();while(!isdigit(c))c=gc();while(isdigit(c))x=x*10+c-'0',c=gc();return x;}
#define pc(c) putchar(c)
void write(int x) {if(x>9)write(x/10);pc('0'+x%10);}

int n, m;
int head[500010], nxt[500010], to[500010], tot;
int dfn[500010], low[500010], dfst;
int stk[500010], vis[500010], top;
int scc[500010];
vector<vector<int>> sccs; int cnt;
int indeg[500010], a[500010];

inline void add_edge(int u, int v) {
    nxt[++tot] = head[u], to[tot] = v, head[u] = tot;
}

void dfs(int u) {
    dfn[u] = ++dfst, low[u] = u;
    stk[++top] = u, vis[u] = 1;
    for (int i = head[u]; i; i = nxt[i]) {
        int v = to[i];
        if (!dfn[v]) {
            dfs(v);
            low[u] = min(low[u], low[v]);
        } else if (vis[v]) {
            low[u] = min(low[u], dfn[v]);
        }
    }
    if (low[u] == u) {
        cnt++;
        sccs.emplace_back();
        int v;
        do {
            v = stk[top--];
            vis[v] = 0;
            sccs[cnt].push_back(v);
        } while (v != u);
    }
}

int main() {
    n = read(), m = read();
    for (int i = 1; i <= m; i++) {
        int u = read() + 1, v = read() + 1;
        add_edge(u, v);
    }
    sccs.emplace_back();
    for (int i = 1; i <= n; i++) {
        if (!dfn[i]) dfs(i);
    }
    for (int u = 1; u <= n; u++) {
        for (int i = head[u]; i; i = nxt[i]) {
            int v = to[i];
            indeg[scc[v]]++;
        }
    }
    queue<int> Q;
    for (int i = 1; i <= cnt; i++) {
        if (indeg[i] == 0) Q.push(i);
    }
    while (!Q.empty()) {
        a[++top] = Q.front();
        for (int u : sccs[Q.front()]) {
            for (int i = head[u]; i; i = nxt[i]) {
                int v = to[i];
                if (--indeg[v] == 0) Q.push(v);
            }
        }
        Q.pop();
    }
    write(cnt); pc('\n');
    for (int i = cnt; i >= 1; i--) {
        write(sccs[a[i]].size()); pc(' ');
        for (int u : sccs[a[i]]) write(u - 1), pc(' ');
        pc('\n');
    }
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 2ms
memory: 13896kb

input:

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

output:

4
1 5 
2 4 1 
1 2 
2 3 0 

result:

ok OK

Test #2:

score: -100
Wrong Answer
time: 79ms
memory: 48380kb

input:

500000 500000
389812 410922
255712 339244
274009 248522
347288 199231
235313 301629
469588 84917
364188 449407
392348 436920
26220 198288
162188 468965
274222 92196
463222 408478
231663 467768
382681 38083
412497 160479
280851 268689
101149 25450
62271 9177
38892 268598
273853 250782
191939 89247
40...

output:

416987
1 499999 
1 499995 
1 499994 
1 499989 
1 499988 
1 499987 
1 499983 
1 499978 
1 499975 
1 499972 
1 499969 
1 499965 
1 499964 
1 499954 
1 499949 
1 499947 
1 499942 
1 499939 
1 499932 
1 499928 
1 499926 
1 499925 
1 499924 
1 499919 
1 499918 
1 499915 
1 499910 
1 499909 
1 499907 
1 4...

result:

wrong answer 161255 is later than 283572, but there is an edge (161255, 283572)