#include <cstdlib> #include <iostream> #include <vector>
using namespace std;
int n, m, Bcnt, Stop, Dindex, DFN[210], LOW[210], instack[210], stap[210], Belong[210]; vector<int> v_adj[210];
void init() { int i; for(i=0; i<210; i++) v_adj[i].clear(); memset(DFN, 0, sizeof(DFN)); memset(LOW, 0, sizeof(LOW)); memset(instack, 0, sizeof(instack)); memset(Belong, 0, sizeof(Belong)); }
void tarjan(int i) { int j; DFN[i] = LOW[i] = ++Dindex; instack[i] = true; stap[++Stop] = i; vector<int>::iterator begin, end; end = v_adj[i].end(); for(begin=v_adj[i].begin(); begin != end; begin++) { j = *begin; if(!DFN[j]) { tarjan(j); if(LOW[j] < LOW[i]) LOW[i] = LOW[j]; } else if(instack[j] && DFN[j] < LOW[i]) LOW[i] = DFN[j]; } if(DFN[i] == LOW[i]) { Bcnt++; do { j = stap[Stop--]; instack[j] = false; Belong[j] = Bcnt; }while(j != i); } }
void solve() { int i; Bcnt = Stop = Dindex = 0; for(i=1; i<=n; i++) { if(!DFN[i]) tarjan(i); } printf("%d\n", Bcnt); }
int main(int argc, char *argv[]) { int i, a, b; while(1) { cin >> n >> m; if(0 == n && 0 == m) break; init(); for(i=0; i<m; i++) { cin >> a >> b; v_adj[a].push_back(b); } solve(); } system("PAUSE"); return EXIT_SUCCESS; }
|