【CF241E】Flights
【CF241E】Flights
题面
题解
对于原来的图,如果一条边不出现在\(1\)到\(n\)的路径上面,直接\(ban\)掉即可。
那么考虑一条边\(u\rightarrow v\),一定满足\(1\leq dis_v-dis_u\leq 2\),其中\(dis_u,dis_v\)表示\(1\)到\(u,v\)的最短路。直接根据这个性质跑差分约束即可,一条边的答案即为\(dis_v-dis_u\)。
代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
inline int gi() {
register int data = 0, w = 1;
register char ch = 0;
while (!isdigit(ch) && ch != '-') ch = getchar();
if (ch == '-') w = -1, ch = getchar();
while (isdigit(ch)) data = 10 * data + ch - '0', ch = getchar();
return w * data;
}
const int INF = 1e9;
const int MAX_N = 1e3 + 5, MAX_M = 5e3 + 5;
struct Edge { int u, v; } a[MAX_M];
struct Graph { int to, cost; } ;
vector<Graph> G[MAX_N];
vector<int> E[MAX_N];
int N, M, vis[MAX_N];
void bfs(int s, int op) {
queue<int> que;
que.push(s), ++vis[s];
while (!que.empty()) {
int x = que.front(); que.pop();
for (auto v : E[x])
if (vis[v] == op) ++vis[v], que.push(v);
}
}
int dis[MAX_N];
bool inq[MAX_N];
bool spfa() {
static int cnt[MAX_N];
queue<int> que; que.push(1), inq[1] = 1, ++cnt[1];
for (int i = 2; i <= N; i++) dis[i] = INF;
while (!que.empty()) {
int x = que.front(); que.pop();
for (auto e : G[x]) {
int v = e.to, w = e.cost;
if (dis[x] + w < dis[v]) {
dis[v] = dis[x] + w;
if (!inq[v]) ++cnt[v], inq[v] = 1, que.push(v);
if (cnt[v] >= N) return 0;
}
}
inq[x] = 0;
}
return 1;
}
int main () {
#ifndef ONLINE_JUDGE
freopen("cpp.in", "r", stdin);
#endif
N = gi(), M = gi();
for (int i = 1; i <= M; i++) {
a[i].u = gi(), a[i].v = gi();
E[a[i].u].push_back(a[i].v);
}
bfs(1, 0);
for (int i = 1; i <= N; i++) E[i].clear();
for (int i = 1; i <= M; i++) E[a[i].v].push_back(a[i].u);
bfs(N, 1);
for (int i = 1; i <= M; i++) {
int u = a[i].u, v = a[i].v;
if (vis[u] != 2 || vis[v] != 2) continue;
G[u].push_back((Graph){v, 2});
G[v].push_back((Graph){u, -1});
}
if (spfa()) puts("Yes");
else return puts("No") & 0;
for (int i = 1; i <= M; i++) {
int u = a[i].u, v = a[i].v;
if (vis[u] != 2 || vis[v] != 2) puts("1");
else printf("%d\n", dis[v] - dis[u]);
}
return 0;
}
【CF241E】Flights的更多相关文章
- 【CF241E】Flights(差分约束)
[CF241E]Flights(差分约束) 题面 CF 有\(n\)个点\(m\)条边,要求给每条边赋一个\(1\)或\(2\)的边权,判断能否使得每一条\(1\)到\(n\)的路径的权值和都相等,如 ...
- 【CodeForces】576 D. Flights for Regular Customers
[题目]D. Flights for Regular Customers [题意]给定n个点m条边的有向图,每条边有di表示在经过该边前必须先经过di条边,边可重复经过,求1到n的最小经过边数.n,m ...
- 【BZOJ3831】[Poi2014]Little Bird 单调队列
[BZOJ3831][Poi2014]Little Bird Description In the Byteotian Line Forest there are trees in a row. ...
- 【LoadRunner】loadrunner常见问题汇总
LoadRunner常见问题1.LR 脚本为空的解决方法: 1.去掉ie设置中的第三方支持取消掉 2.在系统属性-高级-性能-数据执行保护中,添加loadrunner安装目录中的vugen.exe文件 ...
- 【BZOJ2625】[Neerc2009]Inspection 最小流
[BZOJ2625][Neerc2009]Inspection Description You are in charge of a team that inspects a new ski reso ...
- 【LeetCode】堆 heap(共31题)
链接:https://leetcode.com/tag/heap/ [23] Merge k Sorted Lists [215] Kth Largest Element in an Array (无 ...
- 【LeetCode】动态规划(下篇共39题)
[600] Non-negative Integers without Consecutive Ones [629] K Inverse Pairs Array [638] Shopping Offe ...
- 【LeetCode】BFS(共43题)
[101]Symmetric Tree 判断一棵树是不是对称. 题解:直接递归判断了,感觉和bfs没有什么强联系,当然如果你一定要用queue改写的话,勉强也能算bfs. // 这个题目的重点是 比较 ...
- Python高手之路【六】python基础之字符串格式化
Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...
随机推荐
- 能写数据后台,需要掌握哪些进阶的sql语句?
国庆假期花了一些时间,首次尝试并玩转 grafana,这几天继续不断优化和完善,如今看着自己的成果,相当满意.--逐步接近我想要的理想后台啦. 需求是不停歇的.今天我又给自己发掘了一些新需求,比如变量 ...
- vs2015下编译免费开源的jpeg库,ijg的jpeg.lib
vs2015下编译免费开源的jpeg库,ijg的jpeg.lib 1. 去Independent JPEG Group官网www.ijg.org下载jpegsrc,我下载的版本是jpegsrc9c.z ...
- jq动画插件,自制基于vue的圆形时钟
首先附上jq插件库,里面的东西太炫了,建议学前端的可以看看学习下:http://www.jq22.com/ 里面有个“超个性动画版本的个人简历”,通过屏幕上不断打印内容,改变相应样式来实现动画简历,我 ...
- node_exporte新版本指标名称变化说明
changelog如下 Breaking changes This release contains major breaking changes to metric names. Many metr ...
- 并发编程--一堆锁,GIL,同步异步,Event事件
目录 一堆锁 死锁现象(*****) 递归锁 RLock (了解) 信号量 (了解) GIL(*****) 什么时GIL锁 为什么需要GIL锁 Cpython解释器与GC的问题 GIL锁带来的问题 多 ...
- 谈谈游戏服务端SDK接入
“接sdk其实本质上就是一个对着接口文档写adaptor的工作,重复和无味.” 团队减员,身负多职,上一次调SDK已经可以回溯到游戏测试前夕了... 一般SDK只包含验证和支付功能,绝少部分SDK包含 ...
- js两个不同类型值比较Boolean(0=='')
写js遇到的问题 本以为 Boolean(0=='') 结果为true 可是在控制台执行 Boolean(0==' ')trueBoolean(0==null)false 百度得知,两个不同类型值比较 ...
- Vue Nginx反向代理配置 解决生产环境跨域
Vue本地代理举例: module.exports = { publicPath: './', devServer: { proxy: { '/api': { target: 'https://mov ...
- Java 数组实例——将阿拉伯数字转换为最大写
题目:将阿拉伯数字转换为最大写,比如1234转换为壹仟贰佰叁拾肆. package my_package; public class Transform { private String[] arr1 ...
- Django 配置mysql遇到问题(一)
问题一: django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have ...