codeforces 241 E Flights 【dfs】【差分约束】

E. Flights

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

LiLand is a country, consisting of n cities. The cities are numbered from 1 to n. The country is well known because it has a very strange transportation system. There are many one-way flights that make it possible to travel between the cities, but the flights are arranged in a way that once you leave a city you will never be able to return to that city again.

Previously each flight took exactly one hour, but recently Lily has become the new manager of transportation system and she wants to change the duration of some flights. Specifically, she wants to change the duration of some flights to exactly 2 hours in such a way that all trips from city 1 to city n take the same time regardless of their path.

Your task is to help Lily to change the duration of flights.

Input

First line of the input contains two integer numbers n and m (2 ≤ n ≤ 1000; 1 ≤ m ≤ 5000) specifying the number of cities and the number of flights.

Each of the next m lines contains two integers ai and bi (1 ≤ ai < bi ≤ n) specifying a one-directional flight from city ai to city bi. It is guaranteed that there exists a way to travel from city number 1 to city number n using the given flights. It is guaranteed that there is no sequence of flights that forms a cyclical path and no two flights are between the same pair of cities.

Output

If it is impossible for Lily to do her task, print "No" (without quotes) on the only line of the output.

Otherwise print "Yes" (without quotes) on the first line of output, then print an integer ansi (1 ≤ ansi ≤ 2) to each of the next m lines being the duration of flights in new transportation system. You should print these numbers in the order that flights are given in the input.

If there are multiple solutions for the input, output any of them.

Examples

input

Copy

3 3
1 2
2 3
1 3

output

Copy

Yes
1
1
2

input

Copy

4 4
1 2
2 3
3 4
1 4

output

Copy

No

input

Copy

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

output

Copy

Yes
1
1
1
2
1
2
#include<bits/stdc++.h>
using namespace std;
const int MAX = 1e4 + 7;
const int INF = 0x3f3f3f3f;
struct node{
    int to, c, next;
} e[MAX];
int cnt = 0;
int head[MAX];
int x[MAX], y[MAX];
int dis[MAX], vis[MAX];
bool judge[MAX];
void add(int x, int y, int z){
    e[cnt] = {y, z, head[x]};
    head[x] = cnt++;
}
int n, m;
int t[MAX];
void dfs(int x){
    vis[x] = 1;
    if(x == n){
        judge[x] = 1;
        return;
    }
    for(int i = head[x]; i != -1; i = e[i].next){
        if(e[i].c == 2){
            if(!vis[e[i].to]) dfs(e[i].to);
            if(judge[e[i].to]) judge[x] = 1;
        }
    }
}
void spfa(){
    queue <int> q;
    memset(vis, 0, sizeof vis);
    memset(dis, INF, sizeof dis);
    memset(t, 0, sizeof t);
    vis[1] = 1;
    dis[1] = 0;
    q.push(1);
    t[1]++;
    while(!q.empty()){
        int u = q.front();
        vis[u] = 0;
        q.pop();
        for(int i = head[u]; i != -1; i = e[i].next){
            int to = e[i].to;
            int c = e[i].c;
            if(!judge[to]) continue;
            if(dis[to] > dis[u] + c){
                dis[to] = dis[u] +c;
                if(!vis[to]){
                    vis[to] = 1;
                    q.push(to);
                    t[to]++;
                    if(t[to] >= n){
                        puts("No");
                        return;
                    }
                }
            }
        }
    }
    puts("Yes");
    for(int i = 1; i <= m; i++){
        if(dis[y[i]] - dis[x[i]] == 1) puts("1");
        else puts("2");
    }
}
int main(){
    memset(head, -1, sizeof head);
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= m; i++){
        scanf("%d%d", &x[i], &y[i]);
        add(y[i], x[i], -1), add(x[i], y[i], 2);
    }
    dfs(1);
    spfa();
//    for(int i = 1; i <= n; i++){
//        printf("%d ", dis[i]);
//    }
//    cout << endl;
    return 0;
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值