Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <queue>
- using namespace std;
- const int maxn = 1e5 + 10;
- vector<int> graph[maxn];
- int main() {
- int n, num_kiril, num_kosta, coins;
- cin >> n >> num_kosta >> num_kiril >> coins;
- vector<int> kiril(num_kiril);
- vector<int> kosta(num_kosta);
- vector<bool> visited(n, false);
- queue<int> q;
- for(int i = 0; i < num_kosta; i++) {
- cin >> kosta[i];
- kosta[i]--;
- q.push(kosta[i]);
- visited[kosta[i]] = true;
- }
- for(int i = 0; i < num_kiril; i++) {
- cin >> kiril[i];
- kiril[i]--;
- visited[kiril[i]] = true;
- }
- int m;
- cin >> m;
- int res = 0;
- 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] and coins > 0) {
- visited[neighbour] = true;
- q.push(neighbour);
- coins--;
- res++;
- }
- }
- }
- cout << res << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement