結果

問題 No.1382 Travel in Mitaru city
コンテスト
ユーザー zjsdut
提出日時 2026-03-05 11:20:58
言語 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
結果
WA  
実行時間 -
コード長 1,526 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,557 ms
コンパイル使用メモリ 226,128 KB
実行使用メモリ 9,088 KB
最終ジャッジ日時 2026-03-05 11:21:07
合計ジャッジ時間 8,526 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 50 WA * 18
権限があれば一括ダウンロードができます

ソースコード

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--;
        if (p[a] == p[b])
            g.merge(a, b);
        else if (p[a] < p[b])
            adj[a].push_back(b);
        else
            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 ans = 0;
    for (int u : l) {
        for (int v : adj[u]) {
            if (!g.same(u, v)) {
                if (g.same(s, v))
                    ans++;
                g.merge(u, v);
            }
        }
    }
    cout << ans << '\n';
}
0