#define MaxVertices 10 /* maximum number of vertices */ typedefint Vertex; /* vertices are numbered from 0 to MaxVertices-1 */ typedefstructVNode *PtrToVNode; structVNode { Vertex Vert; PtrToVNode Next; }; typedefstructGNode *Graph; structGNode { int NumOfVertices; int NumOfEdges; PtrToVNode *Array; };
Graph ReadG() { /* details omitted */ int a, b; Graph G = (Graph)malloc(sizeof(GNode)); scanf("%d%d", &G->NumOfVertices, &G->NumOfEdges); G->Array = (PtrToVNode *)malloc(sizeof(PtrToVNode) * G->NumOfVertices); for (int i = 0; i < G->NumOfVertices; i++) { G->Array[i] = NULL; } for (int i = 0; i < G->NumOfEdges; i++) { scanf("%d%d", &a, &b); PtrToVNode p = (PtrToVNode)malloc(sizeof(VNode)); p->Vert = b; p->Next = G->Array[a]; G->Array[a] = p; } return G; }
/* Your function will be put here */ voidStronglyConnectedComponents(Graph G, void (*visit)(Vertex V)) { int mp[MaxVertices][MaxVertices] = {0}, num = G->NumOfVertices; int vis[MaxVertices] = {0}; for (int i = 0; i < num; i++) { PtrToVNode p = G->Array[i]; while (p) { mp[i][p->Vert] = 1; p = p->Next; } } //利用到floyd算法,得到两个点是否联通的关系。 for (int k = 0; k < num; k++) { for (int i = 0; i < num; i++) { for (int j = 0; j < num; j++) { if (mp[i][k] && mp[k][j]) mp[i][j] = 1; //for(int l = 0;l < num;l ++) mp[i][j] |= mp[i][l] * mp[l][j]; //上一句可替换为这一句 可达矩阵的n - 1次方可以求出任意一点到另一点是否可达 } } } for (int i = 0; i < num; i++) { if (vis[i]) continue; visit(i); vis[i] = 1; for (int j = 0; j < num; j++) { if (!vis[j] && mp[i][j] && mp[j][i]) { vis[j] = 1; visit(j); } } putchar('\n'); } }
int t = 0; voiddfs(int v) { if (!t) cout << v; else cout << " " << v; t = 1; for (int i = 1; i <= n; i++) { if (!vis[i] && N[v][i]) { vis[i] = 1; dfs(i); cout << " " << v; } } }
intmain() { for (int i = 0; i < 1111; ++i) for (int j = 0; j < 1111; ++j) N[i][j] = 0; int m, S; cin >> n >> m >> S; for (int i = 0; i < m; ++i) { int a, b; cin >> a >> b; N[a][b] = N[b][a] = 1; } vis[S] = 1; dfs(S); for (int i = 1; i <= n; ++i) { if (!vis[i]) { cout << " 0" << endl; break; } } return0; }
int Graph[1111][1111]; int n; int vis[1111]; int Sum;
voidBFS(int S) { vis[S] = 1; queue<int> q; q.push(S); int start = S, tail, step = 0; while (!q.empty()) { int tmp = q.front(); q.pop(); for (int i = 1; i <= n; ++i) { if (!vis[i] && Graph[tmp][i]) { vis[i] = 1; q.push(i); Sum++; tail = i; } } if (start == tmp) { start = tail; step++; } if (step == 6) break; } }
intmain() { for (int i = 0; i < 1111; ++i) for (int j = 0; j < 1111; ++j) Graph[i][j] = 0; memset(vis, 0, sizeof(int) * 1111); int m; cin >> n >> m; while (m--) { int a, b; cin >> a >> b; Graph[a][b] = Graph[b][a] = 1; } for (int i = 1; i <= n; ++i) { Sum = 1; BFS(i); float ans = (float)Sum / n * 100; printf("%d: %.2f%%\n", i, ans); memset(vis, 0, sizeof(int) * 1111); } return0; }