确定图是否连通,和是否存在奇环
1.判断图是否连通。这里只要判断不用求几个连通分量,直接广搜。
2.判断奇环。mark[]数组中记录步数的奇偶性,如果相邻两点的奇偶性相同,则说明存在奇环。
#include <iostream> #include <cstring> #include <cstdio> #include <vector> #include <queue> #include <algorithm> using namespace std; vector<int> mp[100002]; int flag[100002]; int n, m, s, ca = 1; void init() { int i, a, b; scanf("%d %d %d", &n, &m, &s); for(i=0; i<n; ++i){ mp[i].clear(); flag[i] = -1; } for(i=0; i<m; ++i) { scanf("%d %d", &a, &b); mp[a].push_back(b); mp[b].push_back(a); } } void solve() { int i, j; int cnt = 0, p1, p2; bool ans = false; queue<int> Q; Q.push(s); flag[s] = 1; while(!Q.empty()) { p1 = Q.front(); Q.pop(); cnt ++; for(i=0; i<mp[p1].size(); ++i) { p2 = mp[p1][i]; if(flag[p2] == -1) { Q.push(p2); flag[p2] = flag[p1]^1; } else if(flag[p2] == flag[p1]) { ans = true; } } } if(ans && cnt == n) { printf("Case %d: YES\n", ca++); } else { printf("Case %d: NO\n", ca++); } } int main() { //freopen("c:/aaa.txt", "r", stdin); int T; scanf("%d", &T); while(T --) { init(); solve(); } return 0; }