結果

問題 No.1382 Travel in Mitaru city
ユーザー SSRSSSRS
提出日時 2021-02-07 20:53:27
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 217 ms / 2,000 ms
コード長 1,395 bytes
コンパイル時間 1,879 ms
コンパイル使用メモリ 186,128 KB
実行使用メモリ 25,820 KB
最終ジャッジ日時 2024-07-04 13:58:08
合計ジャッジ時間 10,966 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 68
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
struct unionfind{
	vector<int> p;
	unionfind(int N){
		p = vector<int>(N, -1);
	}
	int root(int x){
		if (p[x] < 0){
			return x;
		} else {
			p[x] = root(p[x]);
			return p[x];
		}
	}
	bool same(int x, int y){
		return root(x) == root(y);
	}
	void unite(int x, int y){
		x = root(x);
		y = root(y);
		if (x != y){
			if (p[x] < p[y]){
				swap(x, y);
			}
			p[y] += p[x];
			p[x] = y;
		}
	}
};
int main(){
  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>> E(N);
  for (int i = 0; i < M; i++){
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    E[a].push_back(b);
    E[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 P : mp){
    g.push_back(P.second);
  }
  reverse(g.begin(), g.end());
  int ans = 0;
  unionfind UF(N);
  vector<bool> used(N, false);
  for (auto gg : g){
    for (int v : gg){
      used[v] = true;
      for (int w : E[v]){
        if (used[w]){
          if (!UF.same(v, w)){
            UF.unite(v, w);
          }
        }
      }
    }
    bool ok = false;
    for (int v : gg){
      if (UF.same(v, S) && p[v] < p[S]){
        ok = true;
      }
    }
    if (ok){
      ans++;
    }
  }
  cout << ans << endl;
}
0