F - Flow Control

给你一个有向图,要求你给每条边设置流量,使得所有点的流量符合题目给出的要求。

思路:只有在所有点的流量和为0时有解,因为增加一条边的值不会改变所有点的总流量和,

所以我们dfs回溯的时候构造就好了, 其他边设为0。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define pii pair<int, int> using namespace std; const int N = 2e5 + ;
const int M = 1e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 +; int head[N], s[N], ans[N], flow[N], tot, n, m, sum;
bool vis[N];
struct Edge {
int to, id, nx;
} edge[N << ]; void add(int u, int v, int id) {
edge[tot].to = v;
edge[tot].id = id;
edge[tot].nx = head[u];
head[u] = tot++;
} void dfs(int u, int p, int id) {
vis[u] = true;
for(int i = head[u]; ~i; i = edge[i].nx) {
int v = edge[i].to;
if(vis[v]) continue;
dfs(v, u, i);
} if(flow[u] != s[u]) {
int ret = abs(s[u] - flow[u]);
if(flow[u] < s[u]) {
flow[u] = ;
flow[p] -= ret;
if(id & ) {
ans[edge[id].id] = -ret;
} else {
ans[edge[id].id] = ret;
}
} else {
flow[u] = ;
flow[p] += ret;
if(id & ) {
ans[edge[id].id] = ret;
} else {
ans[edge[id].id] = -ret;
}
}
}
} int main() {
memset(head, -, sizeof(head));
scanf("%d", &n);
for(int i = ; i <= n; i++) {
scanf("%d", &s[i]);
sum += s[i];
} scanf("%d", &m);
for(int i = ; i <= m; i++) {
int u, v; scanf("%d%d", &u, &v);
add(u, v, i); add(v, u, i);
} if(!sum) {
dfs(, , -);
puts("Possible");
for(int i = ; i <= m; i++) {
printf("%d\n", ans[i]);
}
} else {
puts("Impossible");
}
return ;
}
/*
*/

Educational Codeforces Round 45 (Rated for Div. 2) F - Flow Control的更多相关文章

  1. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...

  2. Educational Codeforces Round 71 (Rated for Div. 2)-F. Remainder Problem-技巧分块

    Educational Codeforces Round 71 (Rated for Div. 2)-F. Remainder Problem-技巧分块 [Problem Description] ​ ...

  3. Educational Codeforces Round 45 (Rated for Div. 2) C、D

      C. Bracket Sequences Concatenation Problem time limit per test 2 seconds memory limit per test 256 ...

  4. Educational Codeforces Round 45 (Rated for Div. 2) E - Post Lamps

    E - Post Lamps 思路:一开始看错题,以为一个地方不能重复覆盖,我一想值这不是sb题吗,直接每个power check一下就好....复杂度nlogn 然后发现不是,这样的话,对于每个po ...

  5. Educational Codeforces Round 45 (Rated for Div. 2) G - GCD Counting

    G - GCD Counting 思路:我猜测了一下gcd的个数不会很多,然后我就用dfs回溯的时候用map暴力合并就好啦. 终判被卡了MLE.....  需要每次清空一下子树的map... #inc ...

  6. Educational Codeforces Round 45 (Rated for Div. 2)

    A bracket sequence is a string containing only characters "(" and ")". A regular ...

  7. Educational Codeforces Round 50 (Rated for Div. 2) F - Relatively Prime Powers(数学+容斥)

    题目链接:http://codeforces.com/contest/1036/problem/F 题意: 题解:求在[2,n]中,x != a ^ b(b >= 2 即为gcd)的个数,那么实 ...

  8. Educational Codeforces Round 58 (Rated for Div. 2) F dp + 优化(新坑) + 离线处理

    https://codeforces.com/contest/1101/problem/F 题意 有n个城市,m辆卡车,每辆卡车有起点\(s_i\),终点\(f_i\),每公里油耗\(c_i\),可加 ...

  9. Educational Codeforces Round 42 (Rated for Div. 2)F - Simple Cycles Edges

    http://codeforces.com/contest/962/problem/F 求没有被两个及以上的简单环包含的边 解法:双联通求割顶,在bcc中看这是不是一个简单环,是的话把整个bcc的环加 ...

随机推荐

  1. Git之安装及使用

    学习使用Git来管理平时自己写的demo代码和阅读的一些源码,因为一直在windows中操作所以开始学习用Git Bash操作在github上的代码.git命令和svn命令是很相似的,我觉得没有必要把 ...

  2. 手脱nSPack 2.2

    1.PEID查壳 深度扫描下:nSPack 2.2 -> North Star/Liu Xing Ping 2.载入OD,上来就是一个大跳转,F8单步跟下去 0040101B >- E9 ...

  3. 查看git拉取地址

    在项目地址下面输入:git remote -v 即可查看到地址啦.

  4. 在vue中使用animate.css

    animate.css是一款前端动画库,相似的有velocity-animate 用法: 首先 npm install animate.css --save 然后在vue文件的script中引入: i ...

  5. Oracle用imp导入dmp文件记录一下

    ---------------------------------------------------------------------------------------------------- ...

  6. Python学习笔记(三十一)正则表达式

    ---恢复内容开始--- 摘抄自:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000 ...

  7. 26 THINGS I LEARNED IN THE DEEP LEARNING SUMMER SCHOOL

    26 THINGS I LEARNED IN THE DEEP LEARNING SUMMER SCHOOL In the beginning of August I got the chance t ...

  8. CSS 文本属性

    一.文本位置 text-align: 参数 /** * left center right:左中右**/text-align: left; 二.文本行间距 line-height: 参数 特别要注意: ...

  9. Spring容器简介

    Spring 是面向 Bean 的编程(BOP,Bean Oriented Programming),提供了 IOC 容器通过配置文件或者注解的方式来管理对象之间的依赖关系. 控制反转模式(也称作依赖 ...

  10. 提高效率!15款最好的 Bug 跟踪应用程序

    当涉及到开发项目时,其中比较重要的事情是它需要某​​种形式的错误和问题跟踪工具来发现和解决问题,否则会浪费大量的时间. 此外,你总是要标签应用来标示那些悬而未决的问题,而这种分期执行的项目进度将帮助您 ...