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

本文介绍了一个运输系统的问题,即如何调整从城市1到城市n的所有路径的飞行时间,使其总时间相同,重点在于解决方法和算法实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}

 

### Codeforces 887E Problem Solution and Discussion The problem **887E - The Great Game** on Codeforces involves a strategic game between two players who take turns to perform operations under specific rules. To tackle this challenge effectively, understanding both dynamic programming (DP) techniques and bitwise manipulation is crucial. #### Dynamic Programming Approach One effective method to approach this problem utilizes DP with memoization. By defining `dp[i][j]` as the optimal result when starting from state `(i,j)` where `i` represents current position and `j` indicates some status flag related to previous moves: ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = ...; // Define based on constraints int dp[MAXN][2]; // Function to calculate minimum steps using top-down DP int minSteps(int pos, bool prevMoveType) { if (pos >= N) return 0; if (dp[pos][prevMoveType] != -1) return dp[pos][prevMoveType]; int res = INT_MAX; // Try all possible next positions and update 'res' for (...) { /* Logic here */ } dp[pos][prevMoveType] = res; return res; } ``` This code snippet outlines how one might structure a solution involving recursive calls combined with caching results through an array named `dp`. #### Bitwise Operations Insight Another critical aspect lies within efficiently handling large integers via bitwise operators instead of arithmetic ones whenever applicable. This optimization can significantly reduce computation time especially given tight limits often found in competitive coding challenges like those hosted by platforms such as Codeforces[^1]. For detailed discussions about similar problems or more insights into solving strategies specifically tailored towards contest preparation, visiting forums dedicated to algorithmic contests would be beneficial. Websites associated directly with Codeforces offer rich resources including editorials written after each round which provide comprehensive explanations alongside alternative approaches taken by successful contestants during live events. --related questions-- 1. What are common pitfalls encountered while implementing dynamic programming solutions? 2. How does bit manipulation improve performance in algorithms dealing with integer values? 3. Can you recommend any online communities focused on discussing competitive programming tactics? 4. Are there particular patterns that frequently appear across different levels of difficulty within Codeforces contests?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值