D. Dispute
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.
Input
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.
Output
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.
Examples
input
5 5 2 3 4 1 1 5 5 3 2 1 1 1 2 0 2output
2 1 2input
4 2 1 2 3 4 0 0 0 0output
3 1 3 4
题意:
1张无向图,每个点有一权值,且每个顶点可以摁一次,然后该点和和它直接相邻的点权值+1,
问最后能不能使得所有点权值均不等于原来的。
思路:
一开始想确实是想遇到不合法的点就摁,但考虑后面的点可能会影响前面摁的点,
其实完全不用考虑,恩过的点是不会再次不合法的,就是说没有后效性没,所以,用个队列
维护下要嗯的点即可。
最后注意图的联通性问题。
代码实现:
#include<iostream>
#include<cstring>
#include<cmath>
#include<queue>
#include<cstdio>
#include<algorithm>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int N=2e5+100;
const int M=4e5+100;
int head[N],ver[M],Next[M];
int tot=0;
int arr[N],vis[N];
void add(int x,int y) {
ver[++tot]=y;
Next[tot]=head[x];
head[x]=tot;
}
int sol(int n){
queue<int>Q;
int res=0;
while(Q.size())Q.pop();
for(int i=1;i<=n;i++){
if(arr[i]==0){
Q.push(i);
}
}
while(Q.size()){
int x=Q.front();
Q.pop();
if(arr[x]!=0)continue;
vis[x]=1;
res++;
arr[x]--;
for(int i=head[x];i;i=Next[i]){
int y=ver[i];
arr[y]--;
if(arr[y]==0){
Q.push(y);
}
}
}
return res;
}
int main() {
int n,m,cnt;
while(cin>>n>>m) {
tot=cnt=0;
memset(head,0,sizeof(head));
memset(Next,0,sizeof(Next));
memset(ver,0,sizeof(ver));
memset(vis,0,sizeof(vis));
for(int i=1; i<=m; i++) {
int x,y;
cin>>x>>y;
add(x,y);
add(y,x);
}
for(int i=1; i<=n; i++) {
cin>>arr[i];
}
int tmp=sol(n);
if(tmp){
cout<<tmp<<endl;
for(int i=1;i<=n;i++){
if(vis[i]){
cout<<i<<" ";
}
}
cout<<endl;
}else{
cout<<0<<endl;
}
}
return 0;
}
THE END;