結果

問題 No.3482 Quod Erat Demonstrandum
コンテスト
ユーザー cho435
提出日時 2026-03-27 22:13:13
言語 C++23(gnu拡張)
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=gnu++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 115 ms / 2,000 ms
コード長 1,579 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 6,437 ms
コンパイル使用メモリ 381,128 KB
実行使用メモリ 20,616 KB
最終ジャッジ日時 2026-03-27 22:13:45
合計ジャッジ時間 11,134 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 45
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
#include <atcoder/all>

using namespace std;
using ll = long long;
#define rep(i, s, t) for (ll i = s; i < (ll)(t); i++)
#define all(x) begin(x), end(x)
template <class T> bool chmin(T& x, T y) {
	return x > y ? (x = y, true) : false;
}
template <class T> bool chmax(T& x, T y) {
	return x < y ? (x = y, true) : false;
}

void solve() {
	int n, m;
	cin >> n >> m;
	vector<int> a(m), b(m), c(m);
	rep(i, 0, m) cin >> a[i] >> b[i] >> c[i];
	rep(i, 0, m) a[i]--, b[i]--;
	atcoder::dsu uf(n);
	vector<vector<int>> g(n);
	rep(i, 0, m) if (c[i] == 1) {
		uf.merge(a[i], b[i]);
		g[a[i]].push_back(b[i]);
		g[b[i]].push_back(a[i]);
	}
	vector<int> dist(n, 1e9);
	dist[0] = 0;
	int s = uf.leader(0), t = uf.leader(n - 1);
	vector<int> v = {0};
	rep(i, 0, v.size()) {
		int nw = v[i];
		for (int nx : g[nw]) {
			if (chmin(dist[nx], dist[nw] + 1)) v.push_back(nx);
		}
	}
	if (s == t) {
		cout << "Same\n";
		cout << dist[n - 1] << '\n';
		return;
	}
	v = {n - 1};
	dist[n - 1] = 0;
	rep(i, 0, v.size()) {
		int nw = v[i];
		for (int nx : g[nw]) {
			if (chmin(dist[nx], dist[nw] + 1)) v.push_back(nx);
		}
	}
	if (s > t) swap(s, t);
	int ans = 1e9;
	rep(i, 0, m) if (c[i] == 2) {
		auto [l, r] = minmax({uf.leader(a[i]), uf.leader(b[i])});
		if (l == s && r == t) {
			chmin(ans, dist[a[i]] + dist[b[i]] + 1);
		}
	}
	if (ans == 1e9) cout << "Unknown\n";
	else {
		cout << "Different\n";
		cout << ans << '\n';
	}
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout << fixed << setprecision(15);
	int t = 1;
	cin >> t;
	while (t--) solve();
}
0