例题6-16(uva-10129)

每条边遍历一次且经过所有顶点,从起点开始 到 起点结束 ,称为欧拉图(欧拉回路)。
从起点开始 到 其他点 结束 ,称为半欧拉图(欧拉通路)。

无向图

1.是欧拉图 当且仅当 连通 且没有 奇度顶点(相邻边的数目)
2.是半欧拉图 当且仅当 连通 且 恰有两个奇度顶点

有向图
1.是欧拉图 当且仅当 基图连通(看做无向图) 且每个顶点的出度 等于 入度
2.是半欧拉图 当且仅当 基图连通 且仅有 两个奇度顶点,其中一个入度比出度大1,另一个出度比入度大1.

#include <iostream>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <numeric>
#include <chrono>
#include <ctime>
#include <cmath>
#include <cctype>
#include <string>
#include <cstdio>
#include <iomanip>


#include <thread>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <iterator>
using namespace std;
int deg[26], use[26],p[26];

int GetFather(int u) {
	return u == p[u] ? u : p[u] = GetFather(p[u]);
}

int main()
{
	int n,m;
	string in;
	vector<int> remain;
	cin >> n;
	while (n--) {
		int total = 26;
		cin >> m; cin.ignore();

		remain.clear();
		memset(deg, 0, sizeof(deg));
		memset(use, 0, sizeof(use));
		for (int i = 0; i < 26; ++i) p[i] = i;

		for (int i = 0; i < m; ++i) {
			getline(cin, in);
			int a = in.front() - 'a', b = in.back() - 'a';
			deg[a]++;
			deg[b]--;
			use[a] = use[b] = 1;
			int ua = GetFather(a),ub = GetFather(b);
			if (ua != ub) {
				p[ua] = p[ub]; total--;
			}
		}
		
		for (int i = 0; i < 26; ++i) {
			if (use[i] == 0) --total;
			else if (deg[i] != 0) remain.emplace_back(deg[i]);
		}

		if (total == 1 && (remain.empty() || (remain.size() == 2 && (remain.front() == -1 || remain.front() == 1)))) {
			cout << "Ordering is possible." << endl;
		}
		else {
			cout << "The door cannot be opened." << endl;
		}
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值