QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#569178#906. 强连通分量hlyWA 0ms3628kbC++111.2kb2024-09-16 21:09:102024-09-16 21:09:10

Judging History

你现在查看的是最新测评结果

  • [2024-09-16 21:09:10]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3628kb
  • [2024-09-16 21:09:10]
  • 提交

answer

#include <iostream>
#include <stack>
#include <vector>

using namespace std;

vector<vector<int>> edges;
vector<int> dfn;
vector<int> low;
int dfn_count=0;
stack<int> s;
vector<bool> in_stack;
vector<vector<int>> SCCs;

void Tarjan(int v)
{
	dfn[v]=++dfn_count;
	low[v]=dfn_count;
	in_stack[v]=true;
	s.push(v);
	for(auto i:edges[v])
	{
		if(dfn[i]!=0)
		{
			Tarjan(i);
			low[v]=min(low[v],low[i]);
		}
		else if(in_stack[i]==true)
			low[v]=min(low[v],low[i]);
	}
	if(dfn[v]==low[v])
	{
		vector<int> t;
		//t.push_back(v);
		while(s.top()!=v)
		{
			int s_top=s.top();
			s.pop();
			t.push_back(s_top);
			in_stack[s_top]=false;
		}
		int s_top=s.top();
		s.pop();
		t.push_back(s_top);
		in_stack[s_top]=false;
	}
}

int main()
{
	int N,M;
	cin>>N>>M;
	edges.resize(N);
	dfn.resize(N,0);
	low.resize(N);
	in_stack.resize(N,false);
	
	while(M-->0)
	{
		int a,b;
		cin>>a>>b;
		edges[a].push_back(b);
	}
	for(int i=0;i<N;i++)
		if(dfn[i]!=0)
			Tarjan(i);
		
	cout<<SCCs.size()<<endl;
	for(auto i:SCCs)
	{
		int n=i.size();
		cout<<n;
		for(int j=n-1;j>=0;j--)
		{
			cout<<" "<<i[j];
		}
		cout<<endl;
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 3628kb

input:

6 7
1 4
5 2
3 0
5 5
4 1
0 3
4 2

output:

0

result:

wrong answer Integer 0 violates the range [1, 6]