Hdu 1689 Alien's neck.pdf (minimum odd ring in bfs search)
Zookeeper
In an undirected graph, find the smallest odd ring of no less than three nodes in the undirected graph.
Bfs, but because the number of points on the ring is required to be an odd number, you cannot simply use a vis array to record whether the point has been accessed. You can change it to a two-dimensional one,
Vis [u] [0] indicates that a point exists in an even ring, and vis [u] [1] indicates that a point exists in an odd ring.
#include
#include
#include
#include
#include
#include #include
#include
#include
#include
#include
#include
#include
#define eps 1e-6 #define LL long long using namespace std; const int maxn = 1000 + 10;const int INF = 0x3f3f3f3f;int n, m, kase;int vis[maxn][2], dis[maxn];vector
G[maxn];void init() {for(int i = 1; i <= n; i++) G[i].clear();cin >> n >> m;int u, v;for(int i = 0; i < m; i++) {cin >> u >> v;G[u].push_back(v);G[v].push_back(u);}}int bfs(int u) {memset(vis, 0, sizeof(vis));queue
q; q.push(u); dis[u] = 1; vis[u][1] = 1;while(!q.empty()) {int v = q.front(); q.pop();for(int i = G[v].size() - 1; i >= 0; i--) {int tmp = G[v][i];dis[tmp] = dis[v] + 1;if(tmp == u && dis[v] >= 3 && dis[v]%2 == 1) return dis[v];if(vis[tmp][dis[tmp]%2]) continue;vis[tmp][dis[tmp]%2] = 1;q.push(G[v][i]);}}return INF;}void solve() {int ans = INF;for(int i = 1; i <= n; i++) ans = min(ans, bfs(i));if(ans != INF) printf("Case %d: JYY has to use %d balls.\n", ++kase, ans);else printf("Case %d: Poor JYY.\n", ++kase);}int main() {//freopen("input.txt", "r", stdin);int t; cin >> t;while(t--) {init();solve();}return 0;}