forked from rflin/ccf-csp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path201509-4 高速公路 ccf .cpp
53 lines (53 loc) · 952 Bytes
/
201509-4 高速公路 ccf .cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
vector<int> v[10001];
stack<int> stk;
int DFN[10001],LOW[10001];
int index,ans;
bool vis[10001];
void TarJan(int u)
{
DFN[u]=LOW[u]=++index;
stk.push(u);
vis[u]=true;
for(unsigned int i=0;i<v[u].size();++i)
{
int w=v[u][i];
if(!DFN[w])
{
TarJan(w);
LOW[u]=min(LOW[u],LOW[w]);
}
else if(vis[w]) LOW[u]=min(LOW[u],DFN[w]);
}
if(DFN[u]==LOW[u])
{
int s,cnt=0;
do{
s=stk.top();
stk.pop();
vis[s]=false;
++cnt;
}while(u!=s);
if(cnt>1) ans+=cnt*(cnt-1)/2;
}
}
int main()
{
int n,m;
cin>>n>>m;
for(int i=0;i<m;++i)
{
int s,e;
cin>>s>>e;
v[s].push_back(e);
}
for(int i=1;i<=n;++i)
{
if(!DFN[i]) TarJan(i);
}
cout<<ans;
return 0;
}