結果

問題 No.1382 Travel in Mitaru city
ユーザー Example0911
提出日時 2021-02-11 17:33:09
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 144 ms / 2,000 ms
コード長 1,834 bytes
コンパイル時間 2,358 ms
コンパイル使用メモリ 214,080 KB
最終ジャッジ日時 2025-01-18 17:35:38
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 68
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using ll = long long;
using P = pair<ll, ll>;
const ll INF = (1LL << 61);
ll mod = (ll)1e9 + 7;
struct UnionFind {
	vector<int>par, vsize, esize;
	UnionFind(int sz_) : par(sz_), vsize(sz_, 1), esize(sz_, 0) {
		for (int i = 0; i < sz_; i++)par[i] = i;
	}
	void init(int sz_) {
		par.resize(sz_);
		vsize.resize(sz_, 1);
		esize.resize(sz_, 0);
		for (int i = 0; i < sz_; i++)par[i] = i;
	}
	int root(int x) {
		if (par[x] == x)return x;
		else return par[x] = root(par[x]);
	}
	bool merge(int x, int y) {
		x = root(x), y = root(y);
		if (x == y) {
			esize[x]++;
			return false;
		}
		if (vsize[x] < vsize[y])swap(x, y);
		vsize[x] += vsize[y];
		esize[x] += esize[y] + 1;
		par[y] = x;
		return true;
	}
	bool issame(int x, int y) {
		return root(x) == root(y);
	}
	int vs(int x) {
		return vsize[root(x)];
	}
	int es(int x) {
		return esize[root(x)];
	}
};
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int N, M, S, T; cin >> N >> M >> S >> T; S--; T--;
	vector<int>p(N); for (int i = 0; i < N; i++)cin >> p[i];
	vector<vector<int>>G(N);
	for (int i = 0; i < M; i++) {
		int a, b; cin >> a >> b; a--; b--;
		G[a].push_back(b);
		G[b].push_back(a);
	}
	map<int, vector<int>>mp;
	for (int i = 0; i < N; i++) {
		mp[p[i]].push_back(i);
	}
	vector<vector<int>>g;
	for (auto x : mp) {
		g.push_back(x.second);
	}
	// p_iが大きい淳に見ていく
	reverse(g.begin(), g.end());
	UnionFind uf(N);
	vector<bool>used(N);
	int ans = 0;
	for (auto x : g) {
		for (auto v : x) {
			used[v] = true;
			for (auto nv : G[v]) {
				if (used[nv]) {
					if (!uf.issame(v, nv)) {
						uf.merge(v, nv);
					}
				}
			}
		}
		bool ok = false;
		for (auto v : x) {
			if (uf.issame(v, S) && p[v] < p[S]) {
				ok = true;
			}
		}
		if (ok)ans++;
	}
	cout << ans << endl;
	return 0;
}
0