rt, 只要把第28行的cnt++改为++cnt即可让程序从输出错误答案变成exe停止工作
大为不解, 希望有大佬帮助
以下为代码:
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <bitset>
using namespace std;
const int M = 1e5 + 10;
const int N = 1e4 + 10;
std::vector<int> graph[M];
std::vector<int> belong[N];
std::stack<int> st;
int n, m;
int dfn[M], low[M], cnt;
bitset<N> instack;
int p;
bitset<N> is_print;
void tarjan(int u, int fa)
{
dfn[u] = low[u] = cnt++;
st.push(u);
instack[u] = true;
for(int n : graph[u])
{
if(n == fa)
{
fa = -1;
continue;
}
if(dfn[n] == 0)
{
tarjan(n, u);
low[u] = std::min(low[u], low[n]);
}
else if(instack[n])
{
low[u] = std::min(low[u], dfn[n]);
}
if(dfn[u] == low[u])
{
++p;
while(st.top() != u)
{
belong[p].push_back(st.top());
instack[st.top()] = false;
st.pop();
}
belong[p].push_back(u);
instack[st.top()] = false;
st.pop();
std::sort(belong[p].begin(), belong[p].end());
}
}
}
int main()
{
scanf("%d %d", &n, &m);
for(int u, v, i = 1;i <= m;i ++)
{
scanf("%d %d", &u, &v);
graph[u].push_back(v);
}
tarjan(1, -1);
printf("%d\n", p);
for(int i = 1;i <= p;i ++)
{
for(int j : belong[i])
printf("%d ", j);
putchar('\n');
}
return 0;
}