sicily 有向图边的分类

http://soj.sysu.edu.cn/show_problem.php?pid=1005&cid=1779

 1 #include 
 2 
 3 using namespace std;
 4 
 5 int graph[101][101];
 6 int kind[101][101]; 
 7 int vis[101];
 8 int pre[101];
 9 int post[101];
10 
11 int n, m;
12 int ans=0;
13 int flag;
14 int tag;
15 
16 void dfs(int x)
17 {
18     pre[x] = ++tag;
19     for(int i=1; i<=n; i++)
20     {
21         if(graph[x][i])
22         {
23             if(pre[i] == 0)
24             {
25                 kind[x][i] = 1;
26                 dfs(i);
27             }            
28             else
29             {
30                 if(post[i] == 0)
31                 {
32                     kind[x][i] = 3;
33                     flag=0;
34                 }
35                     
36                 else if(pre[i] > pre[x])
37                     kind[x][i] = 2;
38                 else
39                     kind[x][i] = 4;
40             }
41         }
42     }
43     post[x] = ++tag;
44 }
45 
46 int main()
47 {
48     int ca;
49     while(cin >> n >> m)
50     {
51         tag=0;
52         flag=1;
53         memset(graph, 0, sizeof(graph));
54         memset(vis, 0, sizeof(vis));
55         memset(kind, 0, sizeof(kind));
56         for(int i=0; i)
57         {
58             int u, v;
59             cin >> u >> v;
60             graph[u][v]=1;
61         }
62         dfs(1);
63         int q;
64         cin >> q;
65         for(int i=0; i)
66         {
67             int u, v;
68             cin >> u >> v;
69             cout << "edge (" << u << "," << v << ") is ";
70             if(kind[u][v] == 1)
71                 cout << "Tree Edge" << endl;
72             if(kind[u][v] == 2)
73                 cout << "Down Edge" << endl;
74             if(kind[u][v] == 3)
75                 cout << "Back Edge" << endl; 
76             if(kind[u][v] == 4)
77                 cout << "Cross Edge" << endl;
78         } 
79     }
80     return 0;
81 }

 

转载于:https://www.cnblogs.com/dominjune/p/4458074.html

你可能感兴趣的:(sicily 有向图边的分类)