2018-03-19 Problem Solving►UVa UVa 11518 - Dominos 2 Contents 1. Problem2. Solution3. Code Problem題目網址中文網址 Solution直接把能推的都先放進列隊裡,接著做 BFS 就行了。 CodeUVa 1151812345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182#include <cstdio>#include <cstring>#include <queue>#define N 10001struct node{ int v, next;};inline int read(){ int n = 0; char ch; while ((ch = getchar()) >= '0' && ch <= '9') n = n * 10 + ch - '0'; return n;}int main(){ //freopen("test.out", "w", stdout); int adj[N];//adjacent list node edge[N]; bool visited[N]; int T; scanf("%d ", &T); while (T--) { int n = read(), m = read(), l = read(); //scanf("%d%d%d ", &n, &m, &l); memset(visited, false, sizeof visited); memset(adj, -1, sizeof adj); int idx = 0; int u, v; for (int i = 0; i < m; ++i) { //scanf("%d%d", &u, &v); u = read(); v = read(); edge[idx] = (node){v, adj[u]}; adj[u] = idx++; } std::queue<int> Q; for (int i = 0; i < l; ++i) { //scanf("%d", &u); u = read(); if (!visited[u]) //防止推一樣的 { visited[u] = true; Q.push(u); } } int ans = 0; while (!Q.empty()) { ++ans; int now = Q.front(); Q.pop(); for (int i = adj[now]; i != -1; i = edge[i].next) { int v = edge[i].v; if (!visited[v]) { visited[v] = true; Q.push(v); } } } printf("%d\n", ans); } return 0;} Newer UVa 11792 - Krochanska is Here Older UVa 195 - Anagram