标签:form red ansi tac set 包含 accept size out
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 40746 | Accepted: 16574 |
Description
Input
Output
Sample Input
3 3 1 2 2 1 2 3
Sample Output
1
Hint
1 #include <cstdio> 2 #include <stack> 3 #include <vector> 4 #include <algorithm> 5 #include <cstring> 6 7 using namespace std; 8 9 stack<int> s; 10 const int MAX_V=11000; 11 bool instack[MAX_V]; 12 int dfn[MAX_V]; 13 int low[MAX_V]; 14 int ComponentNumber=0; 15 int index; 16 vector<int> edge[MAX_V]; 17 vector<int> redge[MAX_V]; 18 vector<int> Component[MAX_V]; 19 int inComponent[MAX_V]; 20 int N, M; 21 bool visited[MAX_V]; 22 23 void add_edge(int x, int y) 24 { 25 edge[x].push_back(y); 26 redge[y].push_back(x); 27 } 28 29 void tarjan(int i) 30 { 31 dfn[i]=low[i]=index++; 32 instack[i]=true; 33 s.push(i); 34 int j; 35 for (int e=0; e<edge[i].size(); e++) 36 { 37 j=edge[i][e]; 38 if (dfn[j]==-1) 39 { 40 tarjan(j); 41 low[i]=min(low[i], low[j]); 42 } 43 else 44 if (instack[j]) low[i]=min(low[i], dfn[j]); 45 } 46 if (dfn[i]==low[i]) 47 { 48 ComponentNumber++; 49 do 50 { 51 j=s.top(); 52 s.pop(); 53 instack[j]=false; 54 Component[ComponentNumber].push_back(j); 55 inComponent[j]=ComponentNumber; 56 } 57 while (j!=i); 58 } 59 } 60 61 void rdfs(int v) 62 { 63 visited[v]=true; 64 for (int i=0; i<redge[v].size(); i++) 65 { 66 if (!visited[redge[v][i]]) 67 { 68 rdfs(redge[v][i]); 69 } 70 } 71 } 72 73 int main() 74 { 75 memset(dfn, -1, sizeof(dfn)); 76 scanf("%d %d", &N, &M); 77 for (int i=0; i<M; i++) 78 { 79 int x, y; 80 scanf("%d %d", &x, &y); 81 add_edge(x, y); 82 } 83 for (int i=1; i<N+1; i++) 84 { 85 if (dfn[i]==-1) tarjan(i); 86 } 87 int v=Component[1][0]; 88 int num=Component[1].size(); 89 rdfs(v); 90 for (int i=1; i<=N; i++) 91 { 92 if (!visited[i]) 93 { 94 num=0; 95 break; 96 } 97 } 98 printf("%d\n", num); 99 }
标签:form red ansi tac set 包含 accept size out
原文地址:https://www.cnblogs.com/Ymir-TaoMee/p/9792105.html