【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 ...
随机推荐
- 女性长期没有"恩爱",会出现这4个后果?提醒:频率最好能在这个数
一直以来,很多人认为:男性性欲比女性强! 其实:因人而异! 但不管怎么说,“性”话题在如今社会中已经不再成为隐晦谈资. 越来越多的人,可以把此话题拿到桌面上各抒己见. 总归,“性”是我们探索自我的一种 ...
- angularJS中select元素的应用浅析
select array 数据: select ng-model 用法: 1.可以是一个对象形式,ng-model="test" $scope.test = {name: &quo ...
- js中函数的参数为函数的情况即回调函数
js中函数的参数可以是数组对象也可以是函数,当参数为函数时我们叫做回调函数 //定义回调函数function B() { console.log("函数B")setTimeout( ...
- CSS3 2D 变换
CSS2D transform 表示2D变换,目前获得了各主流浏览器的支持,是CSS3中具有颠覆性的特征之一,可以实现元素的位移.旋转.倾斜.缩放,甚至支持矩阵方式,可以取代大量之前只能靠Flash才 ...
- QT Graphics-View图元组使用
通过把一个item作为另一个item的孩子,你可以得到item组的大多数本质特性:这些items会一起移动,所有变换会从父到子传递.QGraphicsItem也可以为它的孩子处理所有的事件,这样就允许 ...
- CDA数据分析实务【第一章:营销决策分析概述】
一.营销概述 营销是关于企业如何发现.创造和交付价值以满足一定目标市场的需求,同时获取利润的学科.营销学用来辨识未被满足的需求,定义,度量目标市场的规模和利润潜力,找到最合适企业进入的细分市场和适合该 ...
- 使用Matplotlab画图
1.绘制折线图 #! /usr/bin/env python#encoding=utf-8 # 用于python2import sys reload(sys) sys.setdefaultencodi ...
- pip换源-换成国内的源
PyPI使用国内源 通过几次 pip 的使用,对于默认的 pip 源的速度实在无法忍受,于是便搜集了一些国内的pip源,如下: 阿里云 http://mirrors.aliyun.com/pypi ...
- 基于SCRUM方法实践的西油计科党建设计与实现-个人实践流程清单
基于SCRUM方法实践的西油计科党建设计与实现 个人实践流程清单 一.Alpha版本冲刺个人在SCRUM团队任务清单: 时间 我这个三天做了什么 实际解决燃尽图项目数量 我遇到了什么问题 我下一个三天 ...
- 伪分布式下Hadoop3.0打不开localhost:50070
伪分布式下Hadoop打不开localhost:50070,可以打开localhost:8088 1.对hdfs进行format hadoop namenode -format 在选择Y/N时输入大写 ...