結果

問題 No.1382 Travel in Mitaru city
コンテスト
ユーザー zjsdut
提出日時 2026-03-05 11:53:35
言語 C++17
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 73 ms / 2,000 ms
コード長 1,576 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,530 ms
コンパイル使用メモリ 221,336 KB
実行使用メモリ 10,296 KB
最終ジャッジ日時 2026-03-05 11:53:49
合計ジャッジ時間 8,231 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 68
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif

struct dsu {
    vector<int> ps; // parent or size
    dsu(int n) : ps(n, -1) {}
    int leader(int x) {
        return ps[x] < 0 ? x : ps[x] = leader(ps[x]);
    }
    int merge(int x, int y) {
        x = leader(x);
        y = leader(y);
        if (x == y) return x;
        if (ps[x] < ps[y])
            swap(x, y);
        ps[y] += ps[x];
        ps[x] = y;
        return y;
    }
    bool same(int x, int y) {
        return leader(x) == leader(y);
    }
    int size(int x) {
        return -ps[leader(x)];
    }
};

int main() {
    ios::sync_with_stdio(0);
    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];

    dsu g(n);
    vector<vector<int>> adj(n);
    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;
        a--; b--;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    vector<int> l(n);
    iota(l.begin(), l.end(), 0);
    sort(l.begin(), l.end(), [&](int i, int j){return p[i] > p[j];});
    int minv = p[s];
    int ans = 0;
    for (int u : l) {
        if (p[u] < minv) {
            for (int v : adj[u]) {
                if (g.same(s, v)) {
                    ans++;
                    minv = p[u];
                    break;
                }
            }
        }
        for (int v : adj[u])
            if (p[v] >= p[u])
                g.merge(u, v);
    }
    cout << ans << '\n';
}
0