秋祭 | 静岡高校工学部



卬高杯


提出詳細

提出id提出時刻ユーザー名問題言語判定状況判定実行時間
1148702023-10-24 09:27:01n_o_n_o_nJcpp11/11AC25

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

ll N,M,X,P,B[100],T[1<<17];
vector<ll> G[1<<17];
bool check[1<<17],vis[1<<17];

int main(){
    cin>>N>>M>>X>>P;
    for(int i=0; i<N; i++)cin>>B[i];
    for(int j=0; j<M; j++)
    {
        cin>>T[j];
        for(int i=0; i<N; i++)
        {
            if(abs(T[j]-B[i])<=P)check[j]=1;
        }
    }
    for(;X--;)
    {
        ll u,v;
        cin>>u>>v;
        u--,v--;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    ll ans=0;
    for(ll i=0; i<M; i++)if(check[i]&&!vis[i])
    {
        ans++;
        vis[i]=1;
        queue<ll>q;
        q.push(i);
        while(!q.empty())
        {
            ll u=q.front();
            q.pop();
            for(ll v:G[u])
            {
                if(vis[v])continue;
                vis[v]=1;
                q.push(v);
            }
        }
    }
    cout << ans << endl;
}

case1.txt AC 8
case2.txt AC 10
case3.txt AC 17
case4.txt AC 23
case5.txt AC 23
case6.txt AC 24
case9.txt AC 23
case10.txt AC 23
case11.txt AC 25
case13.txt AC 25
sample1.txt AC 24
25 AC25 AC