tarjan:
const int N = 100010;
struct edge
{
int to, next;
}g[N];
int cnt, head[N];
int dfn[N], low[N], scc[N], scc_sz[N], st[N];//scc标记点属于哪个强连通分量,scc_sz记录某个强连通分量的点数
bool vis[N];
int index, top, num;
int n, m, x = 0;
void tarjan_init()
{
cnt = 0;
memset(head, -1, sizeof head);
memset(dfn, -1, sizeof dfn);
memset(vis, 0, sizeof vis);
memset(scc_sz, 0, sizeof scc_sz);
index = top = num = 0;
}
void add_edge(int v, int u)
{
g[cnt].to = u, g[cnt].next = head[v], head[v] = cnt++;
}
void tarjan(int v)//dfn[i]=-1调用tarjan函数
{
dfn[v] = low[v] = index++;
st[top++] = v;
vis[v] = true;
int u;
for(int i = head[v]; i != -1; i = g[i].next)
{
u = g[i].to;
if(dfn[u] == -1)
{
tarjan(u);
low[v] = min(low[v], low[u]);
}
else if(vis[u]) low[v] = min(low[v], dfn[u]);
}
if(dfn[v] == low[v])
{
num++;
do
{
u = st[--top];
vis[u] = false;
scc[u] = num;
scc_sz[num]++;
}while(u != v);
}
}
加多少边使图只有一个强连通分量:先强连通缩点,统计缩点后入度为0和出度为0的点的个数,两者中的较大值即是答案,注意特判图本来就是一个强连通分量的情况