QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#482067 | #906. 强连通分量 | ShiRoZeTsu | WA | 0ms | 23412kb | C++14 | 1.1kb | 2024-07-17 17:05:36 | 2024-07-17 17:05:37 |
Judging History
answer
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 5e5 + 5;
const int maxm = 5e5 + 5;
struct edge { int to, nxt; } e[maxm<<1];
int tot = 1, head[maxn];
inline void add(int u, int v) {
e[++tot].to = v;
e[tot].nxt = head[u];
head[u] = tot;
}
int n, m, cnt, top, dfncnt;
int low[maxn], dfn[maxn], stk[maxn];
bool vis[maxn];
vector<int> s[maxn];
void getscc(int u) {
low[u] = dfn[u] = ++dfncnt;
stk[++top] = u;
vis[u] = true;
for(int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if(!dfn[v]) { getscc(v); low[u] = min(low[u], low[v]); }
else if(vis[v]) low[u] = min(low[u], low[v]);
}
vis[u] = false;
if(dfn[u] == low[u]) {
int x; cnt++;
while((x = stk[top--])) {
s[cnt].push_back(x);
if(x == u) break;
}
}
}
int main() {
scanf("%d %d", &n, &m);
for(int i = 1, u, v; i <= m; i++) {
scanf("%d %d", &u, &v);
add(u+1, v+1);
}
for(int i = 1; i <= n; i++)
if(!dfn[i]) getscc(i);
printf("%d\n", cnt);
for(int i = 1; i <= cnt; i++) {
printf("%u", s[i].size());
for(int x : s[i]) printf(" %d", x-1);
putchar('\n');
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 23412kb
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)