結果

問題 No.583 鉄道同好会
ユーザー ldsybldsyb
提出日時 2017-10-28 00:48:40
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 61 ms / 2,000 ms
コード長 1,756 bytes
コンパイル時間 2,041 ms
コンパイル使用メモリ 182,628 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-05-01 18:23:18
合計ジャッジ時間 3,161 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,948 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 16 ms
6,940 KB
testcase_12 AC 22 ms
6,940 KB
testcase_13 AC 22 ms
6,944 KB
testcase_14 AC 22 ms
6,944 KB
testcase_15 AC 28 ms
6,944 KB
testcase_16 AC 50 ms
6,944 KB
testcase_17 AC 60 ms
6,944 KB
testcase_18 AC 61 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template <typename T>
struct graph {
	
	struct edge {
		int from, to;
		T cost;
	};
	
	vector<edge> edges;
	vector<vector<int>> g;
	int n;
	
	graph(int n) : n(n) {
		g.resize(n);
	}
	
	virtual void add(int from, int to, T cost) = 0;
};

template <typename T>
struct undirected_graph : graph<T> {
	using graph<T>::edges;
	using graph<T>::g;
	using graph<T>::n;
	
	undirected_graph(int n) : graph<T>(n) {
	}
	
	void add(int from, int to, T cost = 1) {
		assert(0 <= from && from < n && 0 <= to && to < n);
		g[from].emplace_back(edges.size());
		g[to].emplace_back(edges.size());
		edges.push_back({from, to, cost});
	}
};

template <typename T>
vector<T> dijkstra(const graph<T> &g, int start) {
	assert(0 <= start && start < g.n);
	vector<T> dist(g.n, numeric_limits<T>::max());
	priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> s;
	dist[start] = 0;
	s.emplace(dist[start], start);
	while (!s.empty()) {
		T expected = s.top().first;
		int i = s.top().second;
		s.pop();
		if (expected != dist[i]) {
			continue;
		}
		for (int id : g.g[i]) {
			auto &e = g.edges[id];
			int to = e.from ^ e.to ^ i;
			if (dist[i] + e.cost < dist[to]) {
				dist[to] = dist[i] + e.cost;
				s.emplace(dist[to], to);
			}
		}
	}
	return dist;
}

int main() {
	int n, m;
	cin >> n >> m;
	undirected_graph<int> g(n);
	for (int i = 0; i < m; i++) {
		int from, to;
		cin >> from >> to;
		g.add(from, to);
	}
	auto d = dijkstra(g, g.edges[0].from);
	for (auto &e : g.edges) {
		if (d[e.from] == numeric_limits<int>::max()) {
			cout << "NO" << endl;
			return 0;
		}
	}
	int puni = 0;
	for (auto &i : g.g) {
		if (i.size() & 1) {
			puni++;
		}
	}
	cout << (puni <= 2 ? "YES" : "NO") << endl;
	return 0;
}
0