QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#502711 | #906. 强连通分量 | gogo11 | WA | 1ms | 3536kb | C++20 | 2.2kb | 2024-08-03 12:17:40 | 2024-08-03 12:17:41 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define all(x) x.begin(),x.end()
constexpr int N = 5e5 + 10;
constexpr int M = 5e5 + 10;
constexpr ll Mod = 998244353;
constexpr int INF = 0x3f3f3f3f;
constexpr ll Base = 1277;
constexpr int MAXBIT = 30;
vector<int> bel[N];
struct SCC {
int n;
vector<vector<int>> adj;
vector<int> stk;
vector<int> dfn,low,col;
int cur,cnt;
SCC() {}
SCC(int n) {init(n);}
void init(int n) {
this->n=n;
adj.assign(n+1,{});
dfn.assign(n+1,-1);
low.resize(n+1);
col.assign(n+1,-1);
stk.clear();
cur=cnt=0;
}
void addEdge(int u,int v) {
adj[u].push_back(v);
}
void dfs(int x) {
dfn[x]=low[x]=cur++;
stk.push_back(x);
for (int y: adj[x]) {
if(dfn[y]==-1) {
dfs(y);
low[x]=min(low[x],low[y]);
} else if(col[y]==-1) {
low[x]=min(low[x],dfn[y]);
}
}
if(dfn[x]==low[x]) {
int y;
cnt+=1;
do {
y=stk.back();
bel[cnt].push_back(y);
col[y]=cnt;
stk.pop_back();
}while(y!=x);
}
}
vector<int> work() {
for (int i=1;i<=n;i++)
if(dfn[i]==-1)
dfs(i);
return col;
}
int sccnum() {
return cnt;
}
};
void solve()
{
int n,m;
cin>>n>>m;
SCC scc(n);
for(int i=1;i<=m;i++)
{
int u,v;
cin>>u>>v;
u++,v++;
scc.addEdge(u,v);
}
scc.work();
cout<<scc.sccnum()<<"\n";
for(int i=1;i<=scc.sccnum();i++)
{
cout<<bel[i].size()<<" ";
for(int v: bel[i])
cout<<v-1<<" ";
cout<<"\n";
}
}
int main()
{
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
// #ifndef ONLINE_JUDGE
// freopen("input.txt","r",stdin);
// freopen("out.txt","w",stdout);
// #endif
int _ = 1;
// cin >> _;
while (_--)
solve();
return 0;
}
详细
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 3536kb
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)