QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#786130 | #906. 强连通分量 | zhulexuan | WA | 5ms | 21964kb | C++14 | 2.2kb | 2024-11-26 20:23:56 | 2024-11-26 20:23:56 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf INT_MAX
#define fr(i,l,r) for (i=(l); i<=(r); i++)
#define rfr(i,l,r) for (i=(l); i>=(r); i--)
template<typename T>inline void read(T &n){
T w=1; n=0; char ch=getchar();
while (!isdigit(ch) && ch!=EOF){ if (ch=='-') w=-1; ch=getchar(); }
while (isdigit(ch) && ch!=EOF){ n=(n<<3)+(n<<1)+(ch&15); ch=getchar(); }
n*=w;
}
template<typename T>inline void write(T x){
if (x==0){ putchar('0'); return ; }
T tmp;
if (x>0) tmp=x;
else tmp=-x;
if (x<0) putchar('-');
char F[105];
long long cnt=0;
while (tmp){
F[++cnt]=tmp%10+48;
tmp/=10;
}
while (cnt) putchar(F[cnt--]);
}
#define Min(x,y) x = min(x,y)
#define Max(x,y) x = max(x,y)
//基础配置=================================================================================
const ll N = 500005, M = 500005;
ll n,m;
struct edge{
ll x,y;
};
ll cnt = 1,head[N]; edge e[M];
void addedge(ll x,ll y){
cnt++;
e[cnt].x = head[x];
e[cnt].y = y;
head[x] = cnt;
}
bool ok[N];
ll tms,dfn[N],low[N];
bool vis[N]; ll top,q[N];
ll cs; vector<ll> c[N];
void tarjan(ll x){
ll i, go, son = 0; bool flag = false;
dfn[x] = low[x] = ++tms;
vis[x] = true, q[++top] = x;
for (i=head[x]; i; i=e[i].x){
ll go = e[i].y;
if (!dfn[go]){
tarjan(go);
Min( low[x] , low[go] );
}
else {
if (vis[go]) Min( low[x] , dfn[go] );
}
}
if (dfn[x]==low[x]){
cs++; c[cs].clear();
ll p;
do{
p = q[top--];
vis[p] = false;
c[cs].push_back(p);
}while (p!=x);
}
}
int main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
ll i,j;
read(n); read(m);
fr(i,1,m){
ll x,y; read(x); read(y);
addedge(x,y);
}
fr(i,0,n-1)
if (!dfn[i]) tarjan(i);
printf("%lld\n",cs);
fr(i,1,cs){
printf("%lld ",c[i].size());
for (j=0; j<c[i].size(); j++) printf("%lld ",c[i][j]);
putchar('\n');
}
return 0;
}
//g++ a.cpp -o a -Wall -Wl,--stack=512000000 -std=c++11 -O2
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 5ms
memory: 21964kb
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)