QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#341529 | #906. 强连通分量 | WRuperD# | WA | 0ms | 29604kb | C++14 | 1.8kb | 2024-02-29 19:37:29 | 2024-02-29 19:37:43 |
Judging History
answer
// Problem: 13. 强连通分量
// URL: https://qoj.ac/contest/1536/problem/906
// Writer: WRuperD
// Powered by CP Editor (https://cpeditor.org)
#include<bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
const int mininf = 1e9 + 7;
#define int long long
#define pb emplace_back
inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return x*f;}
inline void write(int x){if(x<0){x=~(x-1);putchar('-');}if(x>9)write(x/10);putchar(x%10+'0');}
#define put() putchar(' ')
#define endl puts("")
const int MAX = 5e5 + 10;
int dfn[MAX], clk, low[MAX];
int stc[MAX], topp;
int col[MAX];
bool ins[MAX];
int sccnum;
vector <int> g[MAX];
vector <int> ret[MAX];
void dfs(int u){
dfn[u] = low[u] = ++clk;
stc[++topp] = u;
ins[u] = 1;
// if(u == 6);
for(int v : g[u]){
if(!dfn[v]){
dfs(v);
low[u] = min(low[u], low[v]);
}else if(ins[v]) low[u] = min(low[u], dfn[v]);
}
// if(u == 6) write(low[u]), put(), write(dfn[u]), endl;
if(low[u] == dfn[u]){
// write(u), endl;
sccnum++;
while(stc[topp] != u){
col[stc[topp]] = u;
ret[sccnum].pb(stc[topp]);
ins[stc[topp]] = 0;
topp--;
}
ret[sccnum].pb(stc[topp]);
ins[stc[topp]] = 0;
col[u] = u;
topp--;
}
return ;
}
void solve(){
int n = read(), m = read();
for(int i = 1; i <= m; i++){
int u = read() + 1, v = read() + 1;
g[u].pb(v);
}
for(int i = 1; i <= n; i++) {
if(!dfn[i]){
dfs(i);
// write(i), put(), write(topp), endl;
}
}
// write(col[6]), endl;
write(sccnum), endl;
for(int i = 1; i <= sccnum; i++){
write(ret[i].size()), put();
for(int u : ret[i]) write(u - 1), put();
endl;
}
}
signed main(){
int t = 1;
while(t--) solve();
return 0;
}
详细
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 29604kb
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)