QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#469240 | #906. 强连通分量 | Purslane# | WA | 223ms | 58000kb | C++14 | 989b | 2024-07-09 16:39:27 | 2024-07-09 16:39:27 |
Judging History
answer
#include<bits/stdc++.h>
#define ffor(i,a,b) for(int i=(a);i<=(b);i++)
#define roff(i,a,b) for(int i=(a);i>=(b);i--)
using namespace std;
const int MAXN=500000+10;
int n,m,tot,in[MAXN],dfn[MAXN],low[MAXN];
stack<int> st;
vector<vector<int>> scc;
vector<int> G[MAXN];
void tarjan(int u) {
dfn[u]=low[u]=++tot,in[u]=1,st.push(u);
for(auto v:G[u]) {
if(!dfn[v]) tarjan(v);
else if(in[v]) low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u]) {
vector<int> vc;
while(st.top()!=u) vc.push_back(st.top()),in[st.top()]=0,st.pop();
vc.push_back(u),st.pop(),scc.push_back(vc),in[u]=0;
}
return ;
}
int main() {
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>n>>m;
ffor(i,1,m) {
int u,v;
cin>>u>>v,u++,v++;
G[u].push_back(v);
}
ffor(i,1,n) if(!dfn[i]) tarjan(i);
reverse(scc.begin(),scc.end());
cout<<scc.size()<<'\n';
for(auto vc:scc) {
cout<<vc.size()<<' ';
for(auto id:vc) cout<<id-1<<' '; cout<<'\n';
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 19068kb
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: 223ms
memory: 58000kb
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:
499995 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 199689 is later than 287603, but there is an edge (199689, 287603)