Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <queue>
- using namespace std;
- vector<int> graph[1000010];
- bool visited[1000010];
- int main() {
- int n;
- cin >> n;
- int b1,b2,p;
- cin >> b1 >> b2 >> p;
- int a[100010];
- int b[100010];
- for(int i=0;i<b1;i++){
- cin >> a[i];
- }
- for(int i =0;i<b2;i++){
- cin >> b[i];
- }
- int m;
- cin >> m;
- for(int i = 0;i<m;i++) {
- int r, l;
- cin >> r >> l;
- graph[r].push_back(l);
- graph[l].push_back(r);
- }
- for (int i = 0; i <=n ; i++) {
- visited[i] = false;
- }
- for(int i=0;i<b1;i++){
- visited[a[i]] = true;
- }
- for(int i =0;i<b2;i++){
- visited[b[i]] = true;
- }
- queue<int> q;
- int gradovi = 0;
- for(int i=0;i<b1;i++){
- q.push(a[i]);
- while(!q.empty()){
- int tocka = q.front();
- q.pop();
- for(int j = 0;j<graph[tocka].size();j++){
- int sosed = graph [tocka][j];
- if(!visited[sosed]){
- visited[sosed] = true;
- q.push(sosed);
- gradovi++;
- }
- }
- }
- }
- if(gradovi > p) {
- gradovi = p;
- }
- cout << gradovi << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement