#include<iostream> #include<cstdio> #include<queue> #include<vector> #include<map> #define db(a) cout << #a << " = " << a << endl #define db2(a, b) cout << #a << " = " << a << " " #b << " = " << b << endl #define foreach(mapa, it) for (typeof(mapa.begin()) it = mapa.begin(); it != mapa.end(); it++) using namespace std; map<int, vector<int> > adyacencia; map<int, int> dist; void bfs(int a, int b) { queue<int> q; q.push(a); dist.clear(); dist[a] = 0; while (!q.empty()) { int u = q.front(); q.pop(); if (u == b) { printf("%2d to %2d: %d\n", a, b, dist[b]); break; } for (int i = 0; i < adyacencia[u].size(); i++) { if (dist.count(adyacencia[u][i])) continue; dist[adyacencia[u][i]] = dist[u] + 1; q.push(adyacencia[u][i]); } } } int main() { #ifdef dennisbot freopen("in.in", "r", stdin); freopen("ou.out", "w", stdout); #endif int n, var, ini, fin, cont = 1, ret; while (1) { adyacencia.clear(); dist.clear(); for (int i = 1; i < 20; i++) { ret = scanf("%d", &n); if (ret == EOF) return 0; for (int j = 0; j < n; j++) { scanf("%d", &var); adyacencia[i].push_back(var); adyacencia[var].push_back(i); } } scanf("%d", &n); printf("Test Set #%d\n", cont++); for (int i = 0; i < n; i++) { scanf("%d", &ini); scanf("%d", &fin); bfs(ini, fin); } puts(""); } }
sábado, 31 de marzo de 2012
567 - Risk, uva
Suscribirse a:
Enviar comentarios (Atom)
No hay comentarios:
Publicar un comentario