Josif_tepe

Untitled

Feb 11th, 2026
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <vector>
  4. #include <cmath>
  5. #include <queue>
  6. using namespace std;
  7. const int maxn = 1e5 + 10;
  8. vector<int> graph[maxn];
  9.  
  10. int main() {
  11.     ios_base::sync_with_stdio(false);
  12.     int n, B1, B2, P;
  13.     cin >> n >> B1 >> B2 >> P;
  14.    
  15.     vector<int> kosta(B1), kiril(B2);
  16.    
  17.     queue<int> q;
  18.    
  19.     vector<bool> visited(n + 1, false);
  20.     for(int i = 0; i < B1; i++) {
  21.         cin >> kosta[i];
  22.         q.push(kosta[i]);
  23.        
  24.         visited[kosta[i]] = true;
  25.     }
  26.    
  27.     for(int i = 0; i < B2; i++) {
  28.         cin >> kiril[i];
  29.         visited[kiril[i]] = true;
  30.     }
  31.    
  32.     int m;
  33.     cin >> m;
  34.    
  35.     for(int i = 0; i < m; i++) {
  36.         int a, b;
  37.         cin >> a >> b;
  38.        
  39.         graph[a].push_back(b);
  40.         graph[b].push_back(a);
  41.     }
  42.    
  43.     int res = 0;
  44.     while(!q.empty()) {
  45.         int node = q.front();
  46.         q.pop();
  47.        
  48.         for(int i = 0; i < (int) graph[node].size(); i++) {
  49.             int neighbour = graph[node][i];
  50.            
  51.             if(!visited[neighbour]) {
  52.                 q.push(neighbour);
  53.                 visited[neighbour] = true;
  54.                
  55.                 if(res + 1 <= P) {
  56.                     res++;
  57.                 }
  58.             }
  59.         }
  60.     }
  61.    
  62.     cout << res << endl;
  63.     return 0;
  64. }
  65.  
Advertisement
Add Comment
Please, Sign In to add comment