Thứ Năm, 15 tháng 11, 2012

Probelm Dispute

Link: http://codeforces.com/problemset/problem/242/D

D. Dispute
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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 nintegers 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.
Sample test(s)
input
5 5
2 3
4 1
1 5
5 3
2 1
1 1 2 0 2
output
2
1 2
input
4 2
1 2
3 4
0 0 0 0
output
3
1 3 4


Thuật toán:
- Gọi b[] là dãy các giá trị của các counter.
- Ta sẽ nhấn counter thứ i nếu b[i] = a[i].
Và khi nhấn counter i thì ta xem các counter j kề i có giá trị b[j] sau khi tăng 1 có bằng a[j] không. Nếu bằng thì chắc chắn ta sẽ phải nhấn counter j, do đó ta bỏ counter j vào hàng đợi chờ đến lượt duyệt.

Code:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<set>
#include<vector>
#include<utility>
#include<map>
#include<list>
#include<queue>
#include<stack>
#include<cstring>
#include<string>
#include<cmath>

#define oo 100005
#define rep(i,s,e) for(int i = s; i < e; i++)
#define FOR(it,c) for(__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define SZ(x) ((int)(x).size())

using namespace std;

typedef pair<int,int> II;
typedef pair<II,int> d;
typedef long long LL;

vector<int> V[oo];
map<int,int> M;
vector<int> res;
set<int> S;
queue<int> Q;
int n,m,u,v;
int a[oo],b[oo];

int main()
{
#define Off  true
if(Off)
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
}
cin >> n >> m;
rep(i,1,m+1)
{
scanf("%d %d",&u,&v);
V[u].push_back(v);
V[v].push_back(u);
}
rep(i,1,n+1) scanf("%d",&a[i]);
rep(i,1,n+1)
if(a[i] == b[i]) Q.push(i);
while(!Q.empty())
{
int p = Q.front();
Q.pop();
if(a[p]!=b[p]) continue;
res.push_back(p);
FOR(it,V[p])
if(++b[*it] == a[*it]) Q.push(*it);
}
printf("%d\n",SZ(res));
rep(i,0,SZ(res))
printf("%d ",res[i]);
return 0;
}





Không có nhận xét nào:

Đăng nhận xét