【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 ...
随机推荐
- vim:spell语法
先说结论,在vim配置文件加入: setlocal spell spelllang=en_us,cjk 1.spell指开启检查模式. 2.spelllang用于指定检查的种类. 3.cjk,指中国, ...
- 常用 Git 命令汇总
Git 命令汇总 1 Git 的一些通用术语 1.1 Git 的几个区 1.2 如何标识 Git 的某次提交 2 Git 配置 2.1 配置 2.2 读取配置 2.3 与 Beyond Compare ...
- rsync 排除指定目录
背景 将Server1上的数据同步到Server2: Server1目录结构: /us_data/yahoo └── qlib ├── calendars ├── dataset_cache ├── ...
- PHP面试题2019年腾讯工程师面试题和答案
一.单选题(共29题,每题5分) 1.PHP执行的时候有如下执行过程:Scanning(Lexing) - Compilation - Execution - Parsing,其含义分别为: A.将P ...
- hello world之vivado程序解决方法
体验米尔zynq系列Z-turn Board单板时,我开始用vivado.在安装vivad工程中出了一些问题,经过不懈的重新安装,终于成功了. 下面分享我用vivado设计hello world程序: ...
- Beego 学习笔记10:Easyui使用
EasyUI使用 1> 下载EasyUI.下载地址:http://www.jeasyui.com/download/index.php 根据自己使用的是jquery还是Angular进行 ...
- jQuery(function(){})和$(function(){ }) 和 $(document).ready(function(){ })关系
转自:https://www.jianshu.com/p/3b0fe5d07996 $(function(){ })和jQuery(function(){ })都是 $(document).ready ...
- springboot2.1.3 本地加载jar包+打包载入本地jar
项目已springboot为主,有时候我们需要引入的jar包并非maven公共库中存在(这里不谈私自搭建私库),那我们能否像普通的工程一样,导入自己手动添加的jar包文件呢? 答案是肯定的,来,一起往 ...
- 联想ideapad-330C 在Ubuntu18.04 上安装Realtek 8821CE无线网卡驱动
在新买的联想ideapad-330C笔记本上,安装Ubuntu 18.04后,悲催的发现,没有无线网络,幸好有线还能用,然后网上搜一波,发现不少人遇到这种问题,也有人给出解决方案 参考的链接: Thi ...
- Python 关于列表字典的键值修改
list (修改列表的索引值) 循环一个列表时,最好不要对原列表有改变大小的操作,这样会影响你的最终结果. #使用负索引进行修改列表 print('First') lis = [11, 22, 33, ...