3560 Graph’s Cycle Component

http://acm.hdu.edu.cn/showproblem.php?pid=3560

Graph’s Cycle Component   使用广度优先判断是否连通,环的每个顶点的度均为2,判断连通分量的每个顶点的度是否为2来判断是否为环。使用深度优先栈溢出,亦可以使用并查集判断是否连通。

  
  
  
  
  1. #include<stdio.h>  
  2. #include<stdlib.h>  
  3. #define N 100005  
  4. typedef struct arc_node{  
  5.     int adjvex;  
  6.     struct arc_node *next;  
  7. }arc_node;  
  8. typedef struct vex_node{  
  9.     int color;  
  10.     int degree;  //度  
  11.     arc_node *first_arc;  
  12. }vex_node;  
  13. vex_node vertex[N];  
  14. int flag[N];   //标记第i个顶点是否被访问过  
  15. int queue[N];   //广度优先所需要的队列  
  16.  
  17. int init(int n)  
  18. {  
  19.     int i;  
  20.     for (i = 0; i < n; i++)  
  21.     {  
  22.         vertex[i].color = 0;  
  23.         vertex[i].degree = 0;  
  24.         vertex[i].first_arc = NULL;  
  25.     }  
  26.     return 1;  
  27. }  
  28. int insert(int u, int v)  
  29. {  
  30.     arc_node *p;  
  31.     p = (arc_node *)malloc(sizeof(arc_node));  
  32.     if (!p)  
  33.         exit(0);  
  34.     p->adjvex = v;  
  35.     p->next = vertex[u].first_arc;  
  36.     vertex[u].first_arc = p;  
  37.     vertex[u].degree++;  
  38.  
  39.     p = (arc_node *)malloc(sizeof(arc_node));  
  40.     if (!p)  
  41.         exit(0);  
  42.     p->adjvex = u;  
  43.     p->next = vertex[v].first_arc;  
  44.     vertex[v].first_arc = p;  
  45.     vertex[v].degree++;  
  46.     return 1;  
  47. }  
  48. int bfs(int u, int count)  
  49. {  
  50.     arc_node *p;  
  51.     int begin, end;  
  52.     int tag;   //tag为0表示该连通分量为环,为1表示该连通分量不是环  
  53.  
  54.     begin = 0;  
  55.     end = 0;  
  56.     queue[end++] = u;  
  57.     flag[u] = 1;  
  58.     if (vertex[u].degree == 2)  
  59.         tag = 0;  
  60.     else 
  61.         tag = 1;    
  62.     while (begin < end)  
  63.     {  
  64.         u = queue[begin++];  
  65.         p = vertex[u].first_arc;  
  66.         while (p)  
  67.         {  
  68.             if (flag[p->adjvex] == 0)  
  69.             {  
  70.                 queue[end++] = p->adjvex;  
  71.                 flag[p->adjvex] = 1;  
  72.                 if (vertex[p->adjvex].degree != 2)  
  73.                     tag = 1;   //  
  74.             }  
  75.             p = p->next;  
  76.         }  
  77.     }  
  78.     if (tag == 0)  
  79.         return 1;  
  80.     else 
  81.         return 0;  
  82. }  
  83. void main()  
  84. {  
  85.     int m, n;  
  86.     int u, v;  
  87.     int i;  
  88.     int count, num;  
  89. //  freopen("input.txt", "r", stdin);  
  90.     while (scanf("%d%d", &n, &m) != EOF && !(n == 0 && m == 0))  
  91.     {  
  92.         init(n);  
  93.         for (i = 0; i < m; i++)  
  94.         {  
  95.             scanf("%d%d", &u, &v);  
  96.             insert(u, v);  
  97.         }  
  98.         for (i = 0; i < n; i++)  
  99.         {  
  100.             flag[i] = 0;  
  101.         }  
  102.         count = 0;  //count用于标记是第几个连通分量  
  103.         num = 0;   //num 用于统计环的个数  
  104.         for (i = 0; i < n; i++)  
  105.         {  
  106.             if (flag[i] == 0)  
  107.             {  
  108.                   
  109.                 count++;  
  110.                 if (bfs(i, count) == 1)  
  111.                     num++;  
  112.             }  
  113.         }  
  114.         printf("%d %d\n", count, num);  
  115.     }  
  116. }  

 

你可能感兴趣的:(职场,ACM,休闲)