Codeforces-1369-E. DeadLee(拓扑+贪心)

E. DeadLee

Lee bought some food for dinner time, but Lee’s friends eat dinner in a deadly way. Lee is so scared, he doesn’t want to die, at least not before seeing Online IOI 2020…

There are n different types of food and m Lee’s best friends. Lee has wi plates of the i-th type of food and each friend has two different favorite types of food: the i-th friend’s favorite types of food are xi and yi (xi≠yi).

Lee will start calling his friends one by one. Whoever is called will go to the kitchen and will try to eat one plate of each of his favorite food types. Each of the friends will go to the kitchen exactly once.

The only problem is the following: if a friend will eat at least one plate of food (in total) then he will be harmless. But if there is nothing left for him to eat (neither xi nor yi), he will eat Lee instead ×_×.

Lee can choose the order of friends to call, so he’d like to determine if he can survive dinner or not. Also, he’d like to know the order itself.

Input

The first line contains two integers n and m (2≤n≤105; 1≤m≤2⋅105) — the number of different food types and the number of Lee’s friends.

The second line contains n integers w1,w2,…,wn (0≤wi≤106) — the number of plates of each food type.

The i-th line of the next m lines contains two integers xi and yi (1≤xi,yi≤n; xi≠yi) — the favorite types of food of the i-th friend.

Output

If Lee can survive the dinner then print ALIVE (case insensitive), otherwise print DEAD (case insensitive).

Also, if he can survive the dinner, print the order Lee should call friends. If there are multiple valid orders, print any of them.

Examples

input

3 3
1 2 1
1 2
2 3
1 3

output

ALIVE
3 2 1 

input

3 2
1 1 0
1 2
1 3

output

ALIVE
2 1 

input

4 4
1 2 0 1
1 3
1 2
2 3
2 4

output

ALIVE
1 3 2 4 

input

5 5
1 1 1 2 1
3 4
1 2
2 3
4 5
4 5

output

ALIVE
5 4 1 3 2 

input

4 10
2 4 1 4
3 2
4 2
4 1
3 1
4 1
1 3
3 2
2 1
3 1
2 4

output

DEAD

Note

In the first example, any of the following orders of friends are correct : [1,3,2], [3,1,2], [2,3,1], [3,2,1].

In the second example, Lee should call the second friend first (the friend will eat a plate of food 1) and then call the first friend (the friend will eat a plate of food 2). If he calls the first friend sooner than the second one, then the first friend will eat one plate of food 1 and food 2 and there will be no food left for the second friend to eat.

解题思路:

比赛的时候答案都已经呼之欲出了。就差最后的输出没有倒序输出!!就差亿点点。

设一个盘菜x有i个人想吃,那么令x的入度为 i。
题目有解的起始条件显然是有至少一盘菜可以满足所有想吃的人要求,也就是a[x] >= in[x]。
那么就让这盘菜被吃吧。然后吃掉这盘菜的人,另一盘菜就大可不必去吃了。
所以把另一盘菜的入度减1。当另一盘才也满足a[x] >= in[x]时又入队。其实就是拓扑排序。
关键的一点是,要倒序输出。

AC代码:

#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;
#define sd(n) scanf("%d",&n)
#define sdd(n,m) scanf("%d%d",&n,&m)
#define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define pd(n) print f("%d\n", n)
#define pc(n) print f("%c", n)
#define pdd(n,m) print f("%d %d", n, m)
#define pld(n) print f("%int d\n", n)
#define pldd(n,m) print f("%int d %int d\n", n, m)
#define sld(n) scanf("%int d",&n)
#define sldd(n,m) scanf("%int d%int d",&n,&m)
#define slddd(n,m,k) scanf("%int d%int d%int d",&n,&m,&k)
#define sf(n) scanf("%lf",&n)
#define sc(n) scanf("%c",&n)
#define sff(n,m) scanf("%lf%lf",&n,&m)
#define sfff(n,m,k) scanf("%lf%lf%lf",&n,&m,&k)
#define ss(str) scanf("%s",str)
#define rep(i,a,n) for(int  i=a;i<=n;i++)
#define per(i,a,n) for(int  i=n;i>=a;i--)
#define mem(a,n) memset(a, n, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define aint (x) (x).begin(),(x).end()
#define fi first
#define se second
#define mod(x) ((x)%MOD)
#define gcd(a,b) __gcd(a,b)
#define lowbit(x) (x&-x)
#define pii map<int ,int >
#define mk make_pair
#define rtl rt<<1
#define rtr rt<<1|1
#define Max(x,y) (x)>(y)?(x):(y)
#define int long long


typedef pair<int ,int > PII;
typedef long long ll;
typedef unsigned long long uint ;
typedef long double ld;
const int  MOD = 1e9 + 7;
const int  mod = 1e9 + 7;
const double eps = 1e-9;
const int  INF = 0x3f3f3f3f3f3f3f3f;
//const int  inf = 0x3f3f3f3f;
inline int  read(){int  ret = 0, sgn = 1;char ch = getchar();
while(ch < '0' || ch > '9'){if(ch == '-')sgn = -1;ch = getchar();}
while (ch >= '0' && ch <= '9'){ret = ret*10 + ch - '0';ch = getchar();}
return ret*sgn;}
inline void Out(int  a){if(a>9) Out(a/10);putchar(a%10+'0');}
int  qmul(int  a,int  b,int  mod){int  res=0;while(b){if(b&1)res=(res+a)%mod;a=(a+a)%mod;b>>=1;}return res;}
int  qpow(int  m,int  k,int  mod){int  res=1%mod,t=m%mod;while(k){if(k&1)res=qmul(res,t,mod);t=qmul(t,t,mod);k>>=1;}return res;}
int  gcd(int  a,int  b){if(b > a) swap(a,b); return b==0?a : gcd(b,a%b);}
int  lcm(int  a,int  b){return a/gcd(a,b)*b;}
int  inv(int  x,int  mod){return qpow(x,mod-2,mod)%mod;}
//const int  N = 3e3+15;
int  t = 1,cas = 1;
int  n,m;
const int  N = 2e6+7;
int dp[N];
int a[N],ans;
vector<int> G[N];
int in[N];
int vis[N];
int want[N][2];
queue<int> que;
int food[N];
int res[N];
int pos = 0;

signed  main(){
    scanf("%lld%lld",&n,&m);
    for(int i = 0 ; i < n ; i ++){
        scanf("%lld",&a[i]);
    }
    for(int i = 0 ; i < m ; i ++){
        food[i] = -1;
        int x,y;
        scanf("%lld%lld",&x,&y);
        x--;y--;
        in[x]++,in[y]++;
        G[x].pb(i);
        G[y].pb(i);
        want[i][0] =x;
        want[i][1] =y;
    }
    int flag = 0;
    for(int i = 0 ; i < n ; i ++){
        if(in[i] <= a[i]){
            que.push(i);
        }
    }
    //cout<<"-----debug-----"<<endl;
    while(!que.empty()){
        int tmp = que.front();que.pop();
        for(int i = 0 ; i < G[tmp].size() ; i ++){
            int per = G[tmp][i];
            if(food[per] == -1){
                food[per] = tmp;
                res[pos++] = per+1;
                for(int k = 0 ; k < 2 ; k ++){
                    int wt = want[per][k];
                    in[wt] --;
                    if(!vis[wt] && in[wt] <= a[wt]){
                        vis[wt] = 1;
                        que.push(wt);
                    }
                }
            }
        }
    }
    if(pos == m){
        cout<<"ALIVE"<<endl;
        for(int i = m-1 ; i >= 0 ; i  --)
            cout<<res[i]<<" ";
    }
    else{
        cout<<"DEAD"<<endl;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值