acwing算法提高之图论--欧拉回路和欧拉路径

news/2024/9/22 17:28:21/

目录

  • 1 介绍
  • 2 训练

1 介绍

本专题用来记录欧拉回路和欧拉路径相关的题目。

相关结论:
(1)对于无向图,所有边都是连通的。
(1.1)存在欧拉路径的充要条件:度数为奇数的结点只能是0个或者2个。
(1.2)存在欧拉回路的充要条件:度数为奇数的结点只能是0个。

(2)对于有向图,所有边都是连通的。
(2.1)存在欧拉路径的充要条件1:所有结点的出度均等于其入度。
(2.1)存在欧拉路径的充要条件2:除去两个结点外,其余所有结点的出度等于入度。且除去的那两个结点,其中一个结点的出度比入度多1(起点),另一个结点的入度比出度多1(终点)。
(2.2)存在欧拉回路的充要条件:所有结点的出度均等于其入度。

2 训练

题目1:1123铲雪车

C++代码如下,

#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>using namespace std;int main() {double x1, y1, x2, y2;cin >> x1 >> y1;double sum = 0;while (cin >> x1 >> y1 >> x2 >> y2) {double dx = x1 - x2;double dy = y1 - y2;sum += sqrt(dx * dx + dy * dy) * 2;}int minutes = round(sum / 1000 / 20 * 60);int hours = minutes / 60;minutes %= 60;printf("%d:%02d\n", hours, minutes);return 0;
}

题目2:1184欧拉回路

C++代码如下,

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>using namespace std;const int N = 100010, M = 400010;int type;
int n, m;
int h[N], e[M], ne[M], idx;
bool used[M];
int ans[M], cnt;
int din[N], dout[N];void add(int a, int b) {e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}void dfs(int u) {for (int &i = h[u]; ~i;) {if (used[i]) {i = ne[i];continue;}used[i] = true;if (type == 1) used[i ^ 1] = true;int t;if (type == 1) {t = i / 2 + 1;if (i & 1) t = -t;} else {t = i + 1;}int j = e[i];i = ne[i];dfs(j);ans[++cnt] = t;}
}int main() {scanf("%d", &type);scanf("%d%d", &n, &m);memset(h, -1, sizeof h);for (int i = 0; i < m; ++i) {int a, b;scanf("%d%d", &a, &b);add(a, b);if (type == 1) add(b, a);din[b]++, dout[a]++;}if (type == 1) {for (int i = 1; i <= n; ++i) {if (din[i] + dout[i] & 1) {puts("NO");return 0;}}} else {for (int i = 1; i <= n; ++i) {if (din[i] != dout[i]) {puts("NO");return 0;}}}for (int i = 1; i <= n; ++i) {if (h[i] != -1) {dfs(i);break;}}if (cnt < m) {puts("NO");return 0;}puts("YES");for (int i = cnt; i; --i) printf("%d ", ans[i]);puts("");return 0;
}

题目3:1124骑马修栅栏

C++代码如下,

#include <cstring>
#include <iostream>
#include <algorithm>using namespace std;const int N = 510;int n = 500, m;
int g[N][N];
int ans[1100], cnt;
int d[N];void dfs(int u) {for (int i = 1; i <= n; ++i) {if (g[u][i]) {g[u][i]--, g[i][u]--;dfs(i);}}ans[++cnt] = u;
}int main() {cin >> m;while (m--) {int a, b;cin >> a >> b;g[a][b]++, g[b][a]++;d[a]++, d[b]++;}int start = 1;while (!d[start]) start++;for (int i = 1; i <= n; ++i) {if (d[i] % 2) {start = i;break;}}dfs(start);for (int i = cnt; i; i--) printf("%d\n", ans[i]);return 0;
}

题目4:1185单词游戏

C++代码如下,

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>using namespace std;const int N = 30;int n;
int din[N], dout[N], p[N];
bool st[N];int find(int x) {if (p[x] != x) p[x] = find(p[x]);return p[x];
}int main() {char str[1010];int T;scanf("%d", &T);while (T--) {scanf("%d", &n);memset(din, 0, sizeof din);memset(dout, 0, sizeof dout);memset(st, 0, sizeof st);for (int i = 0; i < 26; ++i) p[i] = i;for (int i = 0; i < n; ++i) {scanf("%s", str);int len = strlen(str);int a = str[0] - 'a', b = str[len - 1] - 'a';st[a] = st[b] = true;dout[a]++, din[b]++;p[find(a)] = find(b);}int start = 0, end = 0;bool success = true;for (int i = 0; i < 26; ++i) {if (din[i] != dout[i]) {if (din[i] == dout[i] + 1) end++;else if (din[i] + 1 == dout[i]) start++;else {success = false;break;}}}if (success && !(!start && !end || start == 1 && end == 1)) success = false;int rep = -1;for (int i = 0; i < 26; ++i) {if (st[i]) {if (rep == -1) rep = find(i);else if (rep != find(i)) {success = false;break;}}}if (success) puts("Ordering is possible.");else puts("The door cannot be opened.");}return 0;
}

http://www.ppmy.cn/news/1437694.html

相关文章

【银角大王——Django课程——创建项目+部门表的基本操作】

Django框架员工管理系统——创建项目部门表管理 员工管理系统创建项目命令行的形式创建Django项目——创建app注册app——在sttings中的INSTALLED_APPS [ ]数组中注册 设计表结构&#xff08;django&#xff09;连接数据库——在settings里面改写DATABASESDjango命令执行生成数…

网贷大数据黑名单要多久才能变正常?

网贷大数据黑名单是指个人在网贷平台申请贷款时&#xff0c;因为信用记录较差而被列入黑名单&#xff0c;无法获得贷款或者贷款额度受到限制的情况。网贷大数据黑名单的具体时间因个人信用状况、所属平台政策以及银行审核标准不同而异&#xff0c;一般来说&#xff0c;需要一定…

TDesign:腾讯的企业级前端框架,对标elementUI和ant-design

elementUI和ant-design在前端开发者中有了很高知名度了&#xff0c;组件和资源十分丰富了。本文介绍腾讯的一款B端框架&#xff1a;TDesign TDesign 是腾讯公司内部推出的企业级设计体系&#xff0c;旨在为腾讯旗下的各种产品提供一致、高效、优质的设计支持。这个设计体系是由…

DataGrip 禁用自动同步

DataGrip 是 JetBrains 出品的一款数据库管理工具 问题描述&#xff1a;默认设定&#xff0c;每次更新数据库结构时都会自动更新 Schemas 。不幸的是&#xff0c;DataGrip 的 introspect schemas 功能有严重的性能问题&#xff0c;数据库有一百多个表格的情况下&#xff0c;同步…

AJAX——Promise-链式调用

1.Promise链式调用 概念&#xff1a;依靠then()方法会返回一个新生成的Promise对象特性&#xff0c;继续串联下一环任务&#xff0c;知道结束 细节&#xff1a;then()回调函数中的返回值&#xff0c;会影响新生成的Promise对象最终状态和结果 好处&#xff1a;通过链式调用&…

xshell的基本命令

1. 创建虚拟环境: conda create -n 虚拟环境名称 python3.7 2. 激活进入虚拟环境 conda activate 虚拟环境名称 3. 退出虚拟环境 conda deactivate 4. 查看所有虚拟环境 conda env list 5. 卸载环境 conda remove -n 虚拟环境名称 --all 6. 执行py文件: python3.py文…

海外服务器被恶意攻击怎么办

如果您的海外服务器遭受了恶意攻击&#xff0c;以下是一些应对措施和步骤&#xff0c;立即隔离服务器。如果您察觉到服务器受到恶意攻击&#xff0c;立即隔离服务器&#xff0c;将其与网络隔离&#xff0c;以防止攻机进一步扩散。通知服务器提供商&#xff0c;以便他们能够提供…

CSS常用背景属性

CSS常用背景属性 背景属性背景色背景图背景图平铺方式背景图位置背景图缩放背景图片固定背景图复合属性 背景属性 描述属性背景色background-color背景图background-image背景图平铺方式background-repeat背景图位置background-position背景图缩放background-size背景图固定ba…