結果

問題 No.1382 Travel in Mitaru city
ユーザー nxterunxteru
提出日時 2021-02-07 21:02:38
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 117 ms / 2,000 ms
コード長 7,846 bytes
コンパイル時間 2,847 ms
コンパイル使用メモリ 216,192 KB
実行使用メモリ 15,176 KB
最終ジャッジ日時 2023-09-17 19:58:03
合計ジャッジ時間 9,478 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
8,044 KB
testcase_01 AC 4 ms
8,104 KB
testcase_02 AC 4 ms
8,040 KB
testcase_03 AC 4 ms
8,044 KB
testcase_04 AC 4 ms
7,996 KB
testcase_05 AC 4 ms
8,180 KB
testcase_06 AC 30 ms
9,772 KB
testcase_07 AC 25 ms
9,496 KB
testcase_08 AC 17 ms
8,972 KB
testcase_09 AC 33 ms
10,072 KB
testcase_10 AC 31 ms
9,860 KB
testcase_11 AC 65 ms
12,128 KB
testcase_12 AC 66 ms
12,180 KB
testcase_13 AC 68 ms
12,116 KB
testcase_14 AC 66 ms
11,996 KB
testcase_15 AC 65 ms
12,004 KB
testcase_16 AC 117 ms
14,920 KB
testcase_17 AC 108 ms
14,816 KB
testcase_18 AC 107 ms
14,912 KB
testcase_19 AC 114 ms
14,760 KB
testcase_20 AC 113 ms
14,912 KB
testcase_21 AC 109 ms
14,784 KB
testcase_22 AC 116 ms
14,832 KB
testcase_23 AC 112 ms
14,832 KB
testcase_24 AC 112 ms
14,744 KB
testcase_25 AC 108 ms
14,808 KB
testcase_26 AC 68 ms
12,280 KB
testcase_27 AC 67 ms
12,120 KB
testcase_28 AC 69 ms
12,140 KB
testcase_29 AC 67 ms
12,276 KB
testcase_30 AC 65 ms
12,408 KB
testcase_31 AC 57 ms
11,748 KB
testcase_32 AC 67 ms
12,536 KB
testcase_33 AC 63 ms
12,116 KB
testcase_34 AC 43 ms
10,832 KB
testcase_35 AC 29 ms
9,884 KB
testcase_36 AC 40 ms
11,748 KB
testcase_37 AC 10 ms
8,808 KB
testcase_38 AC 44 ms
12,192 KB
testcase_39 AC 15 ms
9,228 KB
testcase_40 AC 35 ms
11,148 KB
testcase_41 AC 38 ms
11,420 KB
testcase_42 AC 46 ms
12,108 KB
testcase_43 AC 39 ms
11,616 KB
testcase_44 AC 19 ms
9,732 KB
testcase_45 AC 36 ms
11,396 KB
testcase_46 AC 18 ms
9,456 KB
testcase_47 AC 55 ms
12,404 KB
testcase_48 AC 40 ms
11,120 KB
testcase_49 AC 60 ms
12,728 KB
testcase_50 AC 29 ms
10,016 KB
testcase_51 AC 89 ms
14,040 KB
testcase_52 AC 106 ms
15,176 KB
testcase_53 AC 23 ms
9,684 KB
testcase_54 AC 44 ms
11,112 KB
testcase_55 AC 103 ms
14,916 KB
testcase_56 AC 32 ms
10,352 KB
testcase_57 AC 66 ms
12,408 KB
testcase_58 AC 13 ms
8,984 KB
testcase_59 AC 31 ms
10,388 KB
testcase_60 AC 105 ms
14,660 KB
testcase_61 AC 6 ms
8,112 KB
testcase_62 AC 5 ms
8,064 KB
testcase_63 AC 10 ms
8,276 KB
testcase_64 AC 6 ms
8,208 KB
testcase_65 AC 5 ms
8,212 KB
testcase_66 AC 5 ms
8,076 KB
testcase_67 AC 6 ms
8,264 KB
testcase_68 AC 6 ms
8,352 KB
testcase_69 AC 6 ms
8,136 KB
testcase_70 AC 7 ms
8,304 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#include <algorithm>
#include <cassert>
#include <vector>

namespace atcoder {

// Implement (union by size) + (path compression)
// Reference:
// Zvi Galil and Giuseppe F. Italiano,
// Data structures and algorithms for disjoint set union problems
struct dsu {
  public:
    dsu() : _n(0) {}
    dsu(int n) : _n(n), parent_or_size(n, -1) {}

    int merge(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        int x = leader(a), y = leader(b);
        if (x == y) return x;
        if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);
        parent_or_size[x] += parent_or_size[y];
        parent_or_size[y] = x;
        return x;
    }

    bool same(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        return leader(a) == leader(b);
    }

    int leader(int a) {
        assert(0 <= a && a < _n);
        if (parent_or_size[a] < 0) return a;
        return parent_or_size[a] = leader(parent_or_size[a]);
    }

    int size(int a) {
        assert(0 <= a && a < _n);
        return -parent_or_size[leader(a)];
    }

    std::vector<std::vector<int>> groups() {
        std::vector<int> leader_buf(_n), group_size(_n);
        for (int i = 0; i < _n; i++) {
            leader_buf[i] = leader(i);
            group_size[leader_buf[i]]++;
        }
        std::vector<std::vector<int>> result(_n);
        for (int i = 0; i < _n; i++) {
            result[i].reserve(group_size[i]);
        }
        for (int i = 0; i < _n; i++) {
            result[leader_buf[i]].push_back(i);
        }
        result.erase(
            std::remove_if(result.begin(), result.end(),
                           [&](const std::vector<int>& v) { return v.empty(); }),
            result.end());
        return result;
    }

  private:
    int _n;
    // root node: -1 * component size
    // otherwise: parent
    std::vector<int> parent_or_size;
};

}  // namespace atcoder

using namespace std;
using namespace atcoder;

using ll=long long;
#define rng(i,l,r) for(int i=int(l);i<int(r);i++)
#define rep(i,r) rng(i,0,r)
#define rrng(i,l,r) for(int i=int(r)-1;i>=int(l);i--)
#define rrep(i,r) rrng(i,0,r)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
#define bg begin()
#define ed end()
#define all(x) x.bg,x.ed
#define si(x) int(x.size())
#define inf INT_MAX/2-100
#define infl LLONG_MAX/3
#ifdef LOCAL
#define dmp(x) cerr<<__LINE__<<' '<<#x<<' '<<x<<endl
#else
#define dmp(x) void(0)
#endif

template<class t,class u>bool chmax(t&a,u b){if(a<b)a=b;return a<b;}
template<class t,class u>bool chmin(t&a,u b){if(b<a)a=b;return b<a;}

template<class t>using vc=vector<t>;
template<class t>using vvc=vector<vector<t>>;

using pi=pair<int,int>;
using pl=pair<ll,ll>;
using vi=vc<int>;
using vl=vc<ll>;


ll readl(void){
	ll x;
	cin>>x;
	return x;
}
int readi(void){
	int x;
	cin>>x;
	return x;
}

string readstr(){
	string s;
	cin>>s;
	return s;
}
vi readvi(int n,int off=0){
	vi v(n);
	rep(i,n)v[i]=readi(),v[i]+=off;
	return v;
}

vl readvl(int n,int off=0){
	vl v(n);
	rep(i,n)v[i]=readl(),v[i]+=off;
	return v;
}

template<class t>
void print(t x,int suc=1){
	cout<<x;
	if(suc==1)cout<<"\n";
	if(suc==2)cout<<" ";
}

template<class t>
void print(const vc<t>&v,int suc=1){
	rep(i,si(v))print(v[i],i==int(si(v))-1?1:suc);
}

template<class t>
bool inc(t a,t b,t c){
	return !(c<b||b<a);
}

template<class t>
void compress(vc<t>&v){
	sort(all(v));
	v.erase(unique(all(v)),v.ed);
}

template<class t>
int lwb(const vc<t>&v,const t&a){
	return lower_bound(all(v),a)-v.bg;
}

template<class t>
struct Compress{
	vc<t>v;
	Compress()=default;
	Compress(const vc<t>&x){
		add(x);
	}
	Compress(const initializer_list<vc<t> >&x){
		for(auto &p:x)add(p);
	}
	void add(const t&x){
		v.eb(x);
	}
	void add(const vc<t>&x){
		copy(all(x),back_inserter(v));
	}
	void build(){
		compress(v);
	}
	int get(const t&x)const{
		return lwb(v,x);
	}
	vc<t>get(const vc<t>&x)const{
		vc<t>res(x);
		for(auto &p:res)p=get(p);
		return res;
	}
	const t &operator[](int x)const{
		return v[x];
	}
	int size(){
		return v.size();
	}
};
void Yes(bool ex=true){
	cout<<"Yes\n";
	if(ex)exit(0);
}
void YES(bool ex=true){
	cout<<"YES\n";
	if(ex)exit(0);
}
void No(bool ex=true){
	cout<<"No\n";
	if(ex)exit(0);
}
void NO(bool ex=true){
	cout<<"NO\n";
	if(ex)exit(0);
}
void orYes(bool x,bool ex=true){
	if(x)Yes(ex);
	else No(ex);
}
void orYES(bool x,bool ex=true){
	if(x)YES(ex);
	else NO(ex);
}
void Possible(bool ex=true){
	cout<<"Possible\n";
	if(ex)exit(0);
}
void POSSIBLE(bool ex=true){
	cout<<"POSSIBLE\n";
	if(ex)exit(0);
}
void Impossible(bool ex=true){
	cout<<"Impossible\n";
	if(ex)exit(0);
}
void IMPOSSIBLE(bool ex=true){
	cout<<"IMPOSSIBLE\n";
	if(ex)exit(0);
}
void orPossible(bool x,bool ex=true){
	if(x)Possible(ex);
	else Impossible(ex);
}
void orPOSSIBLE(bool x,bool ex=true){
	if(x)POSSIBLE(ex);
	else IMPOSSIBLE(ex);
}

using uint=unsigned;
using ull=unsigned long long; 

template<uint const& MOD>
struct Modular{
	static constexpr uint const &mod=MOD;
	uint v;
	Modular(long long x=0){c(x%mod+mod);}
	Modular& c(uint x){
		v=x<mod?x:x-mod;
		return *this;
	}
	Modular pow(int k)const{
		Modular res(1),tmp(v);
		while(k){
			if(k&1)res*=tmp;
			tmp*=tmp;
			k>>=1;
		}
		return res;
	}

	Modular inv()const{return pow(mod-2);}
	Modular operator-()const{return Modular(mod-v);}

	Modular& operator+=(const Modular &x){return c(v+x.v);}
	Modular& operator-=(const Modular &x){return c(v+mod-x.v);}
	Modular& operator*=(const Modular &x){v=ull(v)*x.v%mod;return *this;}
	Modular& operator/=(const Modular &x){return *this*=x.inv();}

	Modular operator+(const Modular &x)const{return Modular(*this)+=x;}
	Modular operator-(const Modular &x)const{return Modular(*this)-=x;}
	Modular operator*(const Modular &x)const{return Modular(*this)*=x;}
	Modular operator/(const Modular &x)const{return Modular(*this)/=x;}

	friend Modular operator+(long long x,const Modular &y){return Modular(x)+y;}
	friend Modular operator-(long long x,const Modular &y){return Modular(x)-y;}
	friend Modular operator*(long long x,const Modular &y){return Modular(x)*y;}
	friend Modular operator/(long long x,const Modular &y){return Modular(x)/y;}

	friend ostream& operator<<(ostream&os,const Modular&x){
		return os<<x.v;
	}
	friend istream& operator>>(istream&is,Modular&x){
		long long p;
		is>>p;
		x=Modular(p);
		return is;
	}
	bool operator==(const Modular &x)const{return v==x.v;}
	bool operator!=(const Modular &x)const{return v!=x.v;}
	bool operator<(const Modular &x)const{return v<x.v;}
	explicit operator bool()const{return v;}
};

uint MODULAR=998244353;
//uint MODULAR=1000000007;
using Mint=Modular<MODULAR>; 

vector<Mint>fact,finv,invs;
void Initfact(int n=(1<<21)+10){
	fact.resize(n+1),finv.resize(n+1),invs.resize(n+1);
	fact[0]=1;
	for(int i=1;i<=n;i++){
		fact[i]=fact[i-1]*i;
	}
	finv[n]=fact[n].inv();
	for(int i=n-1;i>=0;i--){
		finv[i]=finv[i+1]*(i+1);
	}
	invs[0]=1;
	for(int i=0;i<=n;i++){
		invs[i]=finv[i]*fact[i-1];
	}
}

Mint comb(int n,int k){
	return fact[n]*finv[n-k]*finv[k];
}

ll gcd(ll a,ll b){
	if(a<b)swap(a,b);
	if(b==0)return a;
	return gcd(b,a%b);
}

int n,m,s,t,p[100005],ans;
vi g[100005];
vi q[100005];
int main(void){
	cin.tie(0);
	ios::sync_with_stdio(0);
	cin>>n>>m>>s>>t;
	s--,t--;
	vi cm;
	rep(i,n){
		cin>>p[i];
		cm.pb(p[i]);
	}
	sort(all(cm));
	cm.erase(unique(all(cm)),cm.end());
	rep(i,n){
		p[i]=lwb(cm,p[i]);
		q[p[i]].pb(i);
	}
	rep(i,m){
		int a,b;
		cin>>a>>b;
		g[--a].pb(--b);
		g[b].pb(a);
	}
	dsu uf(n);
	for(int i=si(cm)-1;i>=0;i--){
		for(auto v:q[i]){
			for(auto u:g[v]){
				if(p[u]>=i)uf.merge(v,u);
			}
		}
		if(i>=p[s])continue;
		bool ok=false;
		for(auto v:q[i]){
			if(uf.same(s,v))ok=true;
		}
		if(ok)ans++;
	}
	print(ans);
}
0