Untitled
#include <iostream> #include <vector> #include <queue> using namespace std; const int maxn = 1e5 + 10; const int INF = 2e9; int n, b1, b2, money; vector<int> graph[maxn]; int main() { ios_base::sync_with_stdio(false); vector<bool> visited(n + 1, false); queue<int> q; cin >> n >> b1 >> b2 >> money; int org_money = money; for(int i = 0; i < b1; i++) { int x; cin >> x; x--; visited[x] = true; q.push(x); } for(int i = 0; i < b2; i++) { int x; cin >> x; x--; visited[x] = true; } int m; cin >> m; for(int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--; b--; graph[a].push_back(b); graph[b].push_back(a); } while(!q.empty()) { int node = q.front(); q.pop(); for(int i = 0; i < (int) graph[node].size(); i++) { int neighbour = graph[node][i]; if(!visited[neighbour]) { visited[neighbour] = true; if(money > 0) { money--; q.push(neighbour); } } } } if(money == 0) { cout << org_money << endl; } else { cout << org_money - money << endl; } return 0; }
Leave a Comment