Advertisement
Guest User

Untitled

a guest
May 21st, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct _grafo
  5. {
  6. int *adj;
  7. int num_archi;
  8. }grafo;
  9.  
  10.  
  11.  
  12. int dfs(grafo*, int n, int* color, int* parent);
  13.  
  14. int main()
  15. {
  16. int n = 0;
  17. int i;
  18. scanf("%d", &n);
  19.  
  20. /*----------------LETTURA GRAFO-----------------*/
  21. grafo *E = (grafo*)malloc(n * sizeof(grafo));
  22. int archi, j;
  23.  
  24. for(i = 0; i < n; i++)
  25. {
  26. scanf("%d", &archi);
  27. E[i].adj = (int*)malloc(archi * sizeof(int));
  28. E[i].num_archi = archi;
  29. for(j = 0; j < archi; j++)
  30. {
  31. scanf("%d", E[i].adj + j);
  32. }
  33. }
  34. //printf("Fine lettura\n");
  35. /*------------PRINT RISULTATO--------------*/
  36.  
  37. int *color = (int*)malloc(n * sizeof(int));
  38. int *parent = (int*)malloc(n * sizeof(int));
  39. for(i = 0 ; i < n; i++)
  40. {
  41. color[i] = 0;
  42. parent[i] = -1;
  43. }
  44. printf("%d\n", dfs(E, 0, color, parent));
  45.  
  46. return 0;
  47. }
  48.  
  49.  
  50. int dfs(grafo *E, int src, int *color, int *parent)
  51. {
  52. int dest, i;
  53. for(i = 0; i < E[src].num_archi; i++)
  54. {
  55. dest = E[src].adj[i];
  56. //printf("dest = %d\n src = %d\n", dest, src);
  57. if(!color[dest])
  58. {
  59. color[dest] = 1;
  60. parent[dest] = src;
  61. dfs(E, dest, color, parent);
  62. }
  63. else
  64. {
  65. if((parent[dest] != src) && (parent[dest] != -1))
  66. return 0;
  67. }
  68. }
  69. return 1;
  70.  
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement