Algorithm
Valera has n counters numbered from 1 to n. Some of them are connected by wires, and each of the counters has a special button.
Initially, all the counters contain number 0. When you press a button on a certain counter, the value it has increases by one. Also, the values recorded in all the counters, directly connected to it by a wire, increase by one.
Valera and Ignat started having a dispute, the dispute is as follows. Ignat thought of a sequence of n integers a1, a2, ..., an. Valera should choose some set of distinct counters and press buttons on each of them exactly once (on other counters the buttons won't be pressed). If after that there is a counter with the number i, which has value ai, then Valera loses the dispute, otherwise he wins the dispute.
Help Valera to determine on which counters he needs to press a button to win the dispute.
The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 105), that denote the number of counters Valera has and the number of pairs of counters connected by wires.
Each of the following m lines contains two space-separated integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi), that mean that counters with numbers ui and vi are connected by a wire. It is guaranteed that each pair of connected counters occurs exactly once in the input.
The last line contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ 105), where ai is the value that Ignat choose for the i-th counter.
If Valera can't win the dispute print in the first line -1.
Otherwise, print in the first line integer k (0 ≤ k ≤ n). In the second line print k distinct space-separated integers — the numbers of the counters, where Valera should push buttons to win the dispute, in arbitrary order.
If there exists multiple answers, you are allowed to print any of them.
5 5
2 3
4 1
1 5
5 3
2 1
1 1 2 0 2
2
1 2
4 2
1 2
3 4
0 0 0 0
3
1 3 4
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <bits/stdc++.h>
using namespace std;
int const N = 1e5 + 1;
int n, m, a[N], b[N];
bool vis[N];
vector<vector<int> > g;
vector<int> sol;
queue<int> q;
int main() {
scanf("%d %d", &n, &m);
g.resize(n);
for(int i = 0, a, b; i < m; ++i) {
scanf("%d %d", &a, &b);
--a, --b;
g[a].push_back(b);
swap(a, b);
g[a].push_back(b);
}
for(int i = 0; i < n; ++i) {
scanf("%d", a + i);
if(a[i] == 0)
q.push(i);
}
if(q.empty()) {
puts("0");
return 0;
}
while(!q.empty()) {
int fr = q.front();
q.pop();
if(vis[fr] || b[fr] != a[fr])
continue;
vis[fr] = true;
sol.push_back(fr + 1);
++b[fr];
for(int i = 0; i < g[fr].size(); ++i)
if(++b[g[fr][i]] == a[g[fr][i]])
q.push(g[fr][i]);
}
for(int i = 0; i < n; ++i)
if(a[i] == b[i]) {
puts("-1");
return 0;
}
printf("%d\n", int(sol.size()));
for(int i = 0; i < sol.size(); ++i)
printf("%s%d", i == 0 ? "" : " ", sol[i]);
puts("");
return 0;
}
Copy The Code &
Try With Live Editor
Input
2 3
4 1
1 5
5 3
2 1
1 1 2 0 2
Output
1 2
Demonstration
Codeforces Solution-Dispute-Solution in C, C++, Java, Python