「CF241E」Flights
传送门
Luogu
解题思路
首先对于所有不属于任何一条路径上的边,它的权值是任意的。
对于所有在路径上的边 \((u,v)\) 满足 \(1\le dis_v-dis_u\le2\)
差分约束即可。
细节注意事项
- 用dfs判负环时注意一下时间效率
参考代码
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <vector>
#include <cmath>
#include <ctime>
#define rg register
using namespace std;
template < typename T > inline void read(T& s) {
s = 0; int f = 0; char c = getchar();
while (!isdigit(c)) f |= (c == '-'), c = getchar();
while (isdigit(c)) s = s * 10 + (c ^ 48), c = getchar();
s = f ? -s : s;
}
const int _ = 1010;
const int __ = 5010 * 2 + 1010;
int n, m, vis[_], dis[_], exi[_];
struct edge{ int x, y; }g[__];
vector < int > G1[_], G2[_];
int tot, head[_], nxt[__], ver[__], w[__];
inline void Add_edge(int u, int v, int d)
{ nxt[++tot] = head[u], head[u] = tot, ver[tot] = v, w[tot] = d; }
inline int check(int i) { return vis[g[i].x] == 3 && vis[g[i].y] == 3; }
inline int spfa(int u) {
exi[u] = 1;
for (rg int i = head[u]; i; i = nxt[i]) {
int v = ver[i];
if (dis[v] < dis[u] + w[i]) {
dis[v] = dis[u] + w[i];
if (exi[v]) return 0;
if (!spfa(v)) return 0;
}
}
exi[u] = 0;
return 1;
}
inline void dfs1(int u) { vis[u] |= 1; for (rg int v : G1[u]) if (!(vis[v] & 1)) dfs1(v); }
inline void dfs2(int u) { vis[u] |= 2; for (rg int v : G2[u]) if (!(vis[v] & 2)) dfs2(v); }
int main() {
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
#endif
read(n), read(m);
for (rg int u, v, i = 1; i <= m; ++i)
read(u), read(v), G1[u].push_back(v), G2[v].push_back(u), g[i] = (edge) { u, v };
dfs1(1), dfs2(n);
for (rg int i = 1; i <= m; ++i)
if (check(i)) Add_edge(g[i].x, g[i].y, 1), Add_edge(g[i].y, g[i].x, -2);
for (rg int i = 1; i <= n; ++i) Add_edge(0, i, 0), dis[i] = -0x3f3f3f3f;
if (!spfa(0)) return puts("No"), 0;
puts("Yes");
for (rg int i = 1; i <= m; ++i) {
if (check(i)) printf("%d\n", dis[g[i].y] - dis[g[i].x]);
else puts("1");
}
return 0;
}
完结撒花 \(qwq\)
「CF241E」Flights的更多相关文章
- 「CF576D」 Flights for Regular Customers
「CF576D」 Flights for Regular Customers 对不起我又想网络流去了 你看这长得多像啊,走过至少多少条边就是流量下界,然后没上界 但是这个题求的最少走多少条边啊...完 ...
- 「译」JUnit 5 系列:条件测试
原文地址:http://blog.codefx.org/libraries/junit-5-conditions/ 原文日期:08, May, 2016 译文首发:Linesh 的博客:「译」JUni ...
- 「译」JUnit 5 系列:扩展模型(Extension Model)
原文地址:http://blog.codefx.org/design/architecture/junit-5-extension-model/ 原文日期:11, Apr, 2016 译文首发:Lin ...
- JavaScript OOP 之「创建对象」
工厂模式 工厂模式是软件工程领域一种广为人知的设计模式,这种模式抽象了创建具体对象的过程.工厂模式虽然解决了创建多个相似对象的问题,但却没有解决对象识别的问题. function createPers ...
- 「C++」理解智能指针
维基百科上面对于「智能指针」是这样描述的: 智能指针(英语:Smart pointer)是一种抽象的数据类型.在程序设计中,它通常是经由类型模板(class template)来实做,借由模板(tem ...
- 「JavaScript」四种跨域方式详解
超详细并且带 Demo 的 JavaScript 跨域指南来了! 本文基于你了解 JavaScript 的同源策略,并且了解使用跨域跨域的理由. 1. JSONP 首先要介绍的跨域方法必然是 JSON ...
- 「2014-5-31」Z-Stack - Modification of Zigbee Device Object for better network access management
写一份赏心悦目的工程文档,是很困难的事情.若想写得完善,不仅得用对工具(use the right tools),注重文笔,还得投入大把时间,真心是一件难度颇高的事情.但,若是真写好了,也是善莫大焉: ...
- 「2014-3-18」multi-pattern string match using aho-corasick
我是擅(倾)长(向)把一篇文章写成杂文的.毕竟,写博客记录生活点滴,比不得发 paper,要求字斟句酌八股结构到位:风格偏杂文一点,也是没人拒稿的.这么说来,arxiv 就好比是 paper 世界的博 ...
- 「2014-3-17」C pointer again …
记录一个比较基础的东东-- C 语言的指针,一直让人又爱又恨,爱它的人觉得它既灵活又强大,恨它的人觉得它太过于灵活太过于强大以至于容易将人绕晕.最早接触 C 语言,还是在刚进入大学的时候,算起来有好些 ...
随机推荐
- scw——02错误initializationError(Runner:JUnit 4)
代码: 错误: 原因:版本不同,自动生成的代码不同 解决:
- totensor方法和normalize方法 数值映射和归一化
totensor方法和normalize方法 数值映射和归一化 待办 ToTensor是指把PIL.Image(RGB) 或者numpy.ndarray(H x W x C) 从0到255的值映射到0 ...
- MySQL数据库 | 数据表-查询命令详细记录
本篇专门记录数据库增删改查中最常用.花招最多的 查. [文章结构] 一.数据的准备 二.基本的查询功能 三.条件查询 四.查询排序 五.聚合函数 六.分组查询 七.分页查询 八.连接查询 九.子查询 ...
- C++如何输入单行和多行带空格的字符串并将字符串倒序输出
首先,我们知道在C++中,空格或者回车被认为是字符串的分割标志,使用cin输入string类的字符串时遇到会自动停止接收输入 例如,当如下程序时: #include <bits/stdc++.h ...
- idea配置checkstyle/findbugs/pmd插件
前提条件:工程已导入idea 一,Findbugs/PMD/CheckStyles插件安装 打开settings的plugins,点击查找FindBugs-IDEA/PMDPlugin/CheckSt ...
- java中一个类中的 this. 是什么作用
2)this代表的是本类的实例对象,不是什么调用本类的方法的对象.当你用new创建一个对象的时候,对象就已经在内存了.(具体的你的血jvm和反射).构造方法只是为了给对象里的属性赋值.在类里任何地方出 ...
- 动态设置微信小程序 navigationBarTitle 的值
wx.setNavigationBarTitle({ title:' 动态值 ' })
- bootstrap的字体设置
@font-face { font-family: 'Glyphicons Halflings'; src: url('../fonts/glyphicons-halflings-regular.eo ...
- python 让异常名称显示出来
一 try: pass except Exception as e: print(e) 二 import sys try: pass except: print(sys.exc_info()) 下面有 ...
- codeforces div2 603 C. Everyone is a Winner!(二分)
题目链接:https://codeforces.com/contest/1263/problem/C 题意:给你一个数字n,求n/k有多少个不同的数 思路:首先K大于n时,n/k是0.然后k取值在1到 ...