现在博客更新比较少了,就当我还活着吧
Rikka with Graph
题目传送:HDU - 5422 - Rikka with Graph
AC代码:
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <sstream>
#include <utility>
#include <iostream>
#include <algorithm>
#include <functional>
#define LL long long
#define INF 0x7fffffff
using namespace std;
int n, m;
int main() {
while(scanf("%d %d", &n, &m) != EOF) {
int u, v;
int flag = 0;
for(int i = 0; i < m; i ++) {
scanf("%d %d", &u, &v);
if((u == 1 && v == n) || (u == n && v == 1)) flag = 1;
}
if(flag == 1) printf("1 %d\n", n * (n - 1) / 2);
else printf("1 1\n");
}
return 0;
}
Rikka with Tree
题目传送:HDU - 5423 - Rikka with Tree
AC代码:
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <sstream>
#include <utility>
#include <iostream>
#include <algorithm>
#include <functional>
#define LL long long
#define INF 0x7fffffff
using namespace std;
const int maxn = 1005;
int n;
vector<int> G[maxn];
bool judge1() {
if(G[1].size() <= 0 || G[1].size() >= 2) {
return false;
}
int pre = 1;
int v = G[1][0];
while(G[v].size() == 2) {
for(int i = 0; i < 2; i ++) {
if(G[v][i] != pre) {
pre = v;
v = G[v][i];
if(G[v].size() == 1) return true;
break;
}
}
}
return false;
}
bool judge2() {
if(G[1].size() != n - 1) return false;
return true;
}
bool judge3() {
if(G[1].size() != 1) {
return false;
}
int cnt = 1;
int pre = 1;
int v = G[1][0];
while(G[v].size() == 2) {
for(int i = 0; i < 2; i ++) {
if(G[v][i] != pre) {
pre = v;
v = G[v][i];
cnt ++;
break;
}
}
if(G[v].size() != 2) break;
}
//cout << n << " " << cnt << endl;
if(G[v].size() == n - cnt) return true;
return false;
}
int main() {
while(scanf("%d", &n) != EOF) {
for(int i = 0; i < maxn; i ++) G[i].clear();
int u, v;
for(int i = 1; i < n; i ++) {
scanf("%d %d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
if(judge1() || judge2() || judge3()) {
printf("YES\n");
}
else printf("NO\n");
}
return 0;
}
Rikka with Graph II
题目传送:HDU - 5424 - Rikka with Graph II
官方题解:
如果图是连通的,可以发现如果存在哈密顿路径,一定有一条哈密顿路径的一端是度数最小的点,从那个点开始直接DFS搜索哈密顿路径复杂度是O(n)的。要注意先判掉图不连通的情况。
AC代码:
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <sstream>
#include <utility>
#include <iostream>
#include <algorithm>
#include <functional>
#define LL long long
#define INF 0x7fffffff
using namespace std;
const int maxn = 1005;
int n;
vector<int> G[maxn];
map<pair<int, int>, int> mp;
int deg[maxn];
int vis[maxn];
bool dfs(int u, int cnt) {
vis[u] = 1;
if(cnt == n) return true;
int d = G[u].size();
for(int i = 0; i < d; i ++) {
int v = G[u][i];
if(!vis[v]) {
if(dfs(v, cnt + 1)) return true;
vis[v] = 0;
}
}
return false;
}
int cnt;
void judge(int u) {
vis[u] = 1;
cnt ++;
int d = G[u].size();
for(int i = 0; i < d; i ++) {
int v = G[u][i];
if(!vis[v]) {
judge(v);
}
}
}
int main() {
while(scanf("%d", &n) != EOF) {
for(int i = 0; i <= n; i ++) G[i].clear();
mp.clear();
memset(deg, 0, sizeof(deg));
int u, v;
for(int i = 0; i < n; i ++) {
scanf("%d %d", &u, &v);
if(u == v) continue;
if(mp.find(make_pair(u, v)) != mp.end() || mp.find(make_pair(v, u)) != mp.end() ) {
continue;
}
G[u].push_back(v);
G[v].push_back(u);
deg[u] ++;
deg[v] ++;
mp[make_pair(u, v)] = 1;
}
int mi = INF;
int id;
for(int i = 1; i <= n; i ++) {
if(deg[i] < mi) {
mi = deg[i];
id = i;
}
}
memset(vis, 0, sizeof(vis));
cnt = 0;
judge(1);//特判不连通的情况,因为如果不连通直接dfs会因为回溯太多次而超时
if(cnt != n) {
printf("NO\n");
continue;
}
memset(vis, 0, sizeof(vis));
//cout<< id << endl;
if(dfs(id, 1)) {
printf("YES\n");
}
else printf("NO\n");
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-15 13:14:46