欧拉路径笔记
欧拉路径定义
欧拉路径有两种,一种是普通的欧拉路径,即可以不重复通过每一条边且每条边恰好经过一次的路径。
另一种则是欧拉回路,就是可以在欧拉路径的基础上,还可以最后回到出发点的路径。
欧拉路径的判定
- 欧拉路径
要想使得一个无向图中含有欧拉路径,那么所有节点中,其度数为奇数的点的个数要么是
- 欧拉回路
要想使得一个无向图中含有欧拉回路,那么所有节点中,其度数为奇数的点的个数必须是
代码实现(寻找无向图中的欧拉路径(或回路)):
::::info[Code]
#include <bits/stdc++.h>
using namespace std;
const int N = 5e4 + 5;
int n, m, s, cnt, ans[N * 2], in[N];
char g[10001][10001];
void dfs(int x) {
for (int i = 1; i <= n; i++) {
if (g[x][i] >= 1) {
g[x][i]--;
dfs(i);
}
}
ans[++cnt] = x;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int x, y;
cin >> x >> y;
g[x][y]++, g[y][x]++;
}
s = 1;
cnt = 0;
dfs(s);
for (int i = cnt; i >= 1; i--) cout << ans[i] << '\n';
return 0;
}
::::
课前三连击:
A - Watchcow S
有趣的是,在本题中使用 int 定义数组是会 MLE 的,所以需要使用 char 存储,最终 60pts。
#include <bits/stdc++.h>
using namespace std;
const int N = 5e4 + 5;
int n, m, s, cnt, ans[N * 2], in[N];
char g[10001][10001];
void dfs(int x) {
for (int i = 1; i <= n; i++) {
if (g[x][i] >= 1) {
g[x][i]--;
dfs(i);
}
}
ans[++cnt] = x;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int x, y;
cin >> x >> y;
g[x][y]++, g[y][x]++;
}
s = 1;
cnt = 0;
dfs(s);
for (int i = cnt; i >= 1; i--) cout << ans[i] << '\n';
return 0;
}
B - Ant Trip
有趣的是,本题对小奥的考察非常有趣,但是我们可以一些方法,类如一条边增加的度数一定是偶数来判断等,最终 100pts。
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, m, in[N], dfn[N], tot;
vector<int> g[N], f[N];
void dfs(int x) {
dfn[x] = 1;
f[tot].push_back(x);
for (auto it : g[x]) {
if (dfn[it] == 1) continue;
dfs(it);
}
}
int main() {
cin.tie(0)->sync_with_stdio(false);
while (cin >> n >> m) {
memset(in, 0, sizeof(in));
memset(dfn, 0, sizeof(dfn));
for (int i = 1; i <= n; i++) g[i].clear(), f[i].clear();
tot = 0;
for (int i = 1; i <= m; i++) {
int x, y;
cin >> x >> y;
g[x].push_back(y), g[y].push_back(x);
in[x]++, in[y]++;
}
for (int i = 1; i <= n; i++)
if (!dfn[i]) tot++, dfs(i);
int ans = 0;
for (int i = 1; i <= tot; i++) {
if (f[i].size() == 1) continue;
int cnt = 0;
for (auto it : f[i]) cnt += (in[it] & 1);
if (!cnt)
ans++;
else
ans += cnt / 2; // 总度数为偶数的原因,考场上没有理解透彻
}
cout << ans << '\n';
}
return 0;
}
C - Weird journey
有趣的是,这道题考场上没一人做出来。
思路:
其实我们并不要将它想象为一个使得两边经过一次,其他的经过两次的题目,我们不妨假设有
所以来分析几种情况,首先如果是自环,那么如果删除这个自环的边,就可以直接减少
还有一种就是删除一个节点自己的两条边,也就是其度数
所以对于总的答案
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 5;
int n, m, fa[N], tot, in[N], vis[N];
int find(int x) {
if (fa[x] != x) fa[x] = find(fa[x]);
return fa[x];
}
void unionn(int x, int y) {
int u = find(x), v = find(y);
if (u != v) fa[u] = v;
}
signed main() {
cin.tie(0)->sync_with_stdio(false);
cin >> n >> m;
for (int i = 1; i <= n; i++) fa[i] = i;
for (int i = 1; i <= m; i++) {
int x, y;
cin >> x >> y;
unionn(x, y); // 并查集判联通快
vis[x] = vis[y] = 1;
if (x == y) {
tot++;
continue;
}
in[x]++, in[y]++;
}
int cnt = 0;
for (int i = 1; i <= n; i++)
if (fa[i] == i && vis[i]) cnt++;
if (cnt > 1) {
cout << 0 << '\n';
return 0;
}
int ans = tot * (tot - 1) / 2;
ans += tot * (m - tot);
for (int i = 1; i <= n; i++) ans += in[i] * (in[i] - 1) / 2;
cout << ans;
return 0;
}