結果

問題 No.1382 Travel in Mitaru city
ユーザー ぷらぷら
提出日時 2021-02-07 21:54:39
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 151 ms / 2,000 ms
コード長 3,017 bytes
コンパイル時間 2,031 ms
コンパイル使用メモリ 183,036 KB
実行使用メモリ 11,204 KB
最終ジャッジ日時 2023-09-17 21:22:28
合計ジャッジ時間 10,789 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 58 ms
5,144 KB
testcase_07 AC 43 ms
4,504 KB
testcase_08 AC 23 ms
4,384 KB
testcase_09 AC 60 ms
4,984 KB
testcase_10 AC 59 ms
5,092 KB
testcase_11 AC 92 ms
7,480 KB
testcase_12 AC 94 ms
7,584 KB
testcase_13 AC 98 ms
7,536 KB
testcase_14 AC 89 ms
7,268 KB
testcase_15 AC 87 ms
7,300 KB
testcase_16 AC 150 ms
10,180 KB
testcase_17 AC 147 ms
10,188 KB
testcase_18 AC 150 ms
10,136 KB
testcase_19 AC 146 ms
9,868 KB
testcase_20 AC 147 ms
10,208 KB
testcase_21 AC 138 ms
9,536 KB
testcase_22 AC 133 ms
9,708 KB
testcase_23 AC 140 ms
10,016 KB
testcase_24 AC 151 ms
10,312 KB
testcase_25 AC 138 ms
9,756 KB
testcase_26 AC 134 ms
9,340 KB
testcase_27 AC 140 ms
10,120 KB
testcase_28 AC 133 ms
9,460 KB
testcase_29 AC 135 ms
9,316 KB
testcase_30 AC 140 ms
9,500 KB
testcase_31 AC 101 ms
8,584 KB
testcase_32 AC 122 ms
9,776 KB
testcase_33 AC 112 ms
9,168 KB
testcase_34 AC 76 ms
7,316 KB
testcase_35 AC 51 ms
5,956 KB
testcase_36 AC 88 ms
8,596 KB
testcase_37 AC 15 ms
4,380 KB
testcase_38 AC 97 ms
9,232 KB
testcase_39 AC 26 ms
4,680 KB
testcase_40 AC 74 ms
7,880 KB
testcase_41 AC 82 ms
8,340 KB
testcase_42 AC 99 ms
9,332 KB
testcase_43 AC 86 ms
8,528 KB
testcase_44 AC 35 ms
5,104 KB
testcase_45 AC 78 ms
8,364 KB
testcase_46 AC 31 ms
5,252 KB
testcase_47 AC 113 ms
10,536 KB
testcase_48 AC 83 ms
8,816 KB
testcase_49 AC 123 ms
11,204 KB
testcase_50 AC 60 ms
7,676 KB
testcase_51 AC 96 ms
8,608 KB
testcase_52 AC 116 ms
9,620 KB
testcase_53 AC 25 ms
4,712 KB
testcase_54 AC 51 ms
6,352 KB
testcase_55 AC 109 ms
9,496 KB
testcase_56 AC 35 ms
5,248 KB
testcase_57 AC 72 ms
7,280 KB
testcase_58 AC 12 ms
4,376 KB
testcase_59 AC 34 ms
5,164 KB
testcase_60 AC 105 ms
9,392 KB
testcase_61 AC 3 ms
4,380 KB
testcase_62 AC 1 ms
4,380 KB
testcase_63 AC 14 ms
4,376 KB
testcase_64 AC 5 ms
4,380 KB
testcase_65 AC 2 ms
4,376 KB
testcase_66 AC 3 ms
4,380 KB
testcase_67 AC 4 ms
4,376 KB
testcase_68 AC 6 ms
4,380 KB
testcase_69 AC 4 ms
4,376 KB
testcase_70 AC 8 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < int(n); i++)
#define rrep(i,n) for(int i = int(n)-1; i >= 0; i--)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define pb push_back
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
template<class T> bool chmax(T &a, const T &b) {if(a < b) {a = b; return true;} return false;}
template<class T> bool chmin(T &a, const T &b) {if(b < a) {a = b; return true;} return false;}
int dx[] = {1, 0,-1, 0, 1, 1,-1,-1};
int dy[] = {0, 1, 0,-1, 1,-1, 1,-1};
ll mod = 998244353;
ll MOD = 1000000007;
ll inf = 1001001001;
ll INF = 1001001001001001001;
struct UnionFind {
    vector<int>par;
    vector<int>size;
    UnionFind(int x) {
        par.resize(x);
        size.resize(x, 1);
        for(int i = 0; i < x; i++) {
            par[i] = i;
        }
    }
    int find(int x) {
        if(par[x] == x) {
            return x;
        }
        return par[x] = find(par[x]);
    }
    bool same(int x, int y) {
        return find(x) == find(y);
    }
    int consize(int x) {
        return size[find(x)];
    }
    void unite(int x, int y) {
        x = find(x);
        y = find(y);
        if(x == y) {
            return;
        }
        if(size[x] < size[y]) {
            par[x] = y;
            size[y] += size[x];
        }
        else {
            par[y] = x;
            size[x] += size[y];
        }
    }
};
int main() {
    int N,M,S,T;
    cin >> N >> M >> S >> T;
    S--;T--;
    vector<int>p(N);
    rep(i,N) {
        cin >> p[i];
    }
    vector<vector<int>>road(N);
    rep(i,M) {
        int a,b;
        cin >> a >> b;
        a--;b--;
        road[a].pb(b);
        road[b].pb(a);
    }
    queue<int>que;
    que.push(S);
    UnionFind uf(N);
    int res = p[S];
    int ans = 0;
    set<pair<int,int>>st;
    while(true) {
        while (!que.empty()) {
            int x = que.front();
            que.pop();
            rep(i,road[x].size()) {
                if(p[road[x][i]] >= res) {
                    if(!uf.same(x,road[x][i])) {
                        uf.unite(x,road[x][i]);
                        que.push(road[x][i]);
                    }
                }
                else {
                    if(!uf.same(x,road[x][i])) {
                        uf.unite(x,road[x][i]);
                        st.insert({p[road[x][i]],road[x][i]});
                    }
                }
            }
        }
        if(st.size() == 0) {
            break;
        }
        else {
            ans++;
            pair<int,int> x = *st.rbegin();
            auto it = st.end();
            it--;
            while(true) {
                if(it->first != x.first) {
                    break;
                }
                que.push(it->second);
                if(it == st.begin()){
                    st.erase(it);
                    break;
                }
                st.erase(it--);
            }
            chmin(res,x.first);
        }
    }
    cout << ans << endl;
}
0