結果
| 問題 | 
                            No.1382 Travel in Mitaru city
                             | 
                    
| コンテスト | |
| ユーザー | 
                             sakasu1
                         | 
                    
| 提出日時 | 2021-02-25 17:53:13 | 
| 言語 | C++11(廃止可能性あり)  (gcc 13.3.0)  | 
                    
| 結果 | 
                             
                                WA
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 2,041 bytes | 
| コンパイル時間 | 1,478 ms | 
| コンパイル使用メモリ | 174,140 KB | 
| 実行使用メモリ | 21,220 KB | 
| 最終ジャッジ日時 | 2024-10-01 08:09:21 | 
| 合計ジャッジ時間 | 9,663 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge3 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 27 WA * 41 | 
ソースコード
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef pair<pll,ll> P;
struct unionfind{
  int par[400010]; // 親ノード  必要に応じて要素数を変えよう
  int rank[400010]; // ランク 必要に応じて要素数を変えよう
  int w[400010];
  unionfind(int n){
    init(n);
  }
  void init(int n){
    for(int i=0;i<=n;i++){
      par[i]=i;
      rank[i]=1;
      w[i]=0;
    }
  }
  int root(int x) {
    if (par[x] == x) {
      return x;
    }
    else {
      return par[x] = root(par[x]);
    }
  }
  int size(int x){
    return rank[root(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){
      w[x]++;
      return;
    }
    if(rank[x]<rank[y]){
      swap(x,y);
    }
    rank[x]=rank[x]+rank[y];
    par[y]=x;
    w[x]+=w[y];
    w[x]++;
  }
  int wsize(int x){
    return w[root(x)];
  }
};
int main(){
  const ll INF=1000000000;
  ll i,n,m,s,t;
  cin >> n >> m >> s >> t;
  s--,t--;
  unionfind tree(n);
  vector<ll> p(n);
  for(i=0;i<n;i++){
    cin >> p[i];
  }
  vector<vector<ll> > g(n);
  for(i=0;i<m;i++){
    ll a,b;
    cin >> a >> b;
    a--,b--;
    tree.unite(a,b);
    g[a].push_back(b);
    g[b].push_back(a);
  }
  vector<ll> ds(n,-INF),ns(n,0);
  ds[s]=0,ns[s]=1;
  priority_queue<P> que;
  que.push(P(pll(p[s],0),s));
  while(!que.empty()){
    P p1=que.top();
    que.pop();
    ll u=p1.first.first,v=p1.first.second;
    ll r=p1.second;
    if(ds[r]!=v){
      continue;
    }
    for(i=0;i<g[r].size();i++){
      ll us=g[r][i];
      if(u>p[us]){
        if(ds[us]<ds[r]+1 && ns[us]==0){
          ds[us]=ds[r]+1;
          que.push(P(pll(p[us],ds[us]),us));
        }
      }
      else{
        if(ds[us]<ds[r] && ns[us]==0){
          ds[us]=ds[r];
          que.push(P(pll(u,ds[us]),us));
        }
      }
    }
    ns[r]=1;
  }
  ll ans=0;
  for(i=0;i<n;i++){
    if(tree.same(i,t)){
      ans=max(ans,ds[i]);
    }
  }
  cout << ans << endl;
}
            
            
            
        
            
sakasu1