1 #include <stdio.h> 2 #include <string.h> 3 4 const int maxv = 1000; 5 const int maxe = 5000; 6 const int maxn = 1000; 7 /* 8 * 邻接矩阵 9 * 10 */ 11 struct adjMetrix { 12 int G[maxn+10][maxn+10]; 13 int visit[maxn]; //标记点访问与否 14 int n; 15 void addedge(int u, int v) { 16 G[u][v] = 1; 17 return ; 18 } 19 void read() { 20 memset(G, 0, sizeof(G)); 21 memset(visit, 0, sizeof(visit)); 22 int u, v, w; 23 scanf("%d", &n); 24 for (int i=0; i<n; i++) { 25 scanf("%d %d", &u, &v); 26 addedge(u, v); 27 } 28 return ; 29 } 30 void dfs(int i) { 31 for (int j=0; j<=n; j++) { 32 if (G[i][j]!=0 && visit[j]==0) { 33 printf("%d %d\n", i, j); 34 visit[j] = 1; 35 dfs(j); 36 } 37 } 38 } 39 }; 40 41 /* 42 * 邻接链表 43 * 44 */ 45 struct Edge { 46 int to, w, next; 47 }; 48 49 struct adjTable { 50 int node[maxv]; 51 int visit[maxe]; 52 int cnt; 53 struct Edge e[maxe]; 54 void init() { 55 memset(node, -1, sizeof(node)); 56 memset(visit, 0, sizeof(visit)); 57 cnt = 0; 58 } 59 void addedge(int u,int v) { 60 e[cnt].to = v; 61 e[cnt].next = node[u]; 62 node[u] = cnt++; 63 } 64 void read() { 65 int n, u, v, w; 66 scanf("%d", &n); 67 for (int i=1; i<=n; i++) { 68 scanf("%d %d", &u, &v); 69 addedge(u, v); 70 } 71 return ; 72 } 73 void dfs(int p) { 74 int i; 75 for (i=node[p]; i!=-1; i=e[i].next) { 76 if (visit[ e[i].to ] == 0) { //如果下一条边的指向点未被访问过 77 printf("%d %d\n", p, e[i].to); 78 visit[ p ] = 1; //标记当前点已被访问 79 dfs( e[i].to ); 80 } 81 } 82 } 83 };
时间: 2024-11-13 08:35:55