标签:
http://acm.hdu.edu.cn/showproblem.php?pid=5424
/* 对于一个连通图,有N个顶点,如果有N条边,那么肯定有环 因为N个点如果不成环最多N-1条边。。。然后找最小度的点DFS就行。。。 */ /************************************************ * Author :Powatr * Created Time :2015-8-31 17:29:13 * File Name :HDU5424.cpp ************************************************/ #include <cstdio> #include <algorithm> #include <iostream> #include <sstream> #include <cstring> #include <cmath> #include <string> #include <vector> #include <queue> #include <deque> #include <stack> #include <list> #include <map> #include <set> #include <bitset> #include <cstdlib> #include <ctime> using namespace std; #define lson l, mid, rt << 1 #define rson mid + 1, r, rt << 1 | 1 typedef long long ll; const int MAXN = 1e3 + 10; const int INF = 0x3f3f3f3f; const int MOD = 1e9 + 7; int deg[MAXN]; int n; int mp[MAXN][MAXN]; int vis[MAXN]; vector<int> G[MAXN]; int flag; void dfs(int u, int num) { if(flag == 1) return; if(num == n){ flag = 1; return ; } for(int i = 0; i < G[u].size(); i++){ int v = G[u][i]; if(!vis[v]){ vis[v] = 1; dfs(v, num+1); vis[v] = 0; } } } int main(){ int u, v; while(~scanf("%d", &n)){ memset(mp, 0, sizeof(mp)); memset(vis, 0, sizeof(vis)); memset(deg, 0, sizeof(deg)); for(int i = 1; i <= n; i++) G[i].clear(); for(int i = 1; i <= n; i++){ scanf("%d%d", &u, &v); if(mp[u][v] == 0 ){ mp[u][v] = mp[v][u] = 1; G[u].push_back(v); G[v].push_back(u); deg[u]++; deg[v]++; } } int tot = 0; int id = 1; for(int i = 1; i <= n; i++) if(deg[i] == 1){ id = i; tot++; } if(tot > 2){ puts("NO"); continue; } flag = 0; vis[id] = 1; dfs(id, 1); printf("%s\n", !flag ? "NO" : "YES"); } return 0; }
HDU5424——DFS——Rikka with Graph II
标签:
原文地址:http://www.cnblogs.com/zero-begin/p/4773910.html