#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 10010, M = 100010;
int n, m;
int h[N], e[M], ne[M], idx;
int dfn[N], low[N], timestamp;
int stk[N], top;
bool in_stk[N];
int id[N], scc_cnt;
int ans ;
void add(int a, int b){
e[idx] = b, ne[idx] = h[a] , h[a] = idx ++;
}
void tarjan(int u){
dfn[u] = low[u] = ++ timestamp;
stk[++ top] = u, in_stk[u] = true;
for (int i = h[u]; ~i; i = ne[i]){
int j = e[i];
if (!dfn[j]){
tarjan(j);
low[u] = min(low[u], low[j]);
}else if (in_stk[j]) low[u] = min(low[u], dfn[j]);
}
if (dfn[u] == low[u]){
++ scc_cnt;
int y;
int cnt = 0;
do{
y = stk[top --];
in_stk[y] = false;
id[y] = scc_cnt;
cnt ++;
}while(y != u);
ans = ans + (cnt) * (cnt - 1) / 2;
}
}
int main(){
scanf("%d%d", &n, &m);
memset(h, -1, sizeof h);
while(m --){
int a, b;
scanf("%d%d", &a, &b);
add(a, b);
}
for (int i = 1; i <= n; i ++)
if (!dfn[i])
tarjan(i);
cout << ans << endl;
return 0;
}
201509-4-----------高速公路
最新推荐文章于 2021-04-05 10:54:04 发布