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. 二型错误和功效(Type II Errors and Test Power)

    sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005269003&am ...

  2. Java的三大特性之封装

    java提高篇(一)-----理解java的三大特性之封装 三大特性之---封装 封装从字面上来理解就是包装的意思,专业点就是信息隐藏,是指利用抽象数据类型将数据和基于数据的操作封装在一起,使其构成一 ...

  3. 巧用Java中Calendar工具类

    Java的JDK中提供了一系列好用的util工具类.Calendar就是java.util中用于处理日期的工具类.且该工具类易学易用实用. 工具类Calendar是抽象类. PS:为什么把Calend ...

  4. Linux 下 JDK + Eclipse + PyDev 安装与配置

    一:JDK / JRE 环境 Eclipse 是运行于Java虚拟机中的,所以必须先安装Java环境才能进行开发测试.JRE(Java Runtime Environment)是运行环境,JDK(Ja ...

  5. Java集合(4)一 红黑树、TreeMap与TreeSet(下)

    目录 Java集合(1)一 集合框架 Java集合(2)一 ArrayList 与 LinkList Java集合(3)一 红黑树.TreeMap与TreeSet(上) Java集合(4)一 红黑树. ...

  6. 构造+分块思想 Codeforces Round #319 (Div. 1) C

    http://codeforces.com/contest/576/problem/C 题目大意: 给你一个曼哈顿距离的图,然后要求你找到一个链,链穿了所有的点 然后要求这链的长度<=25*10 ...

  7. Maven项目Eclipse启动时报错: java.lang.ClassNotFoundException: org.springframework.web.util.IntrospectorCleanupListener

    Eclipse中启动Maven项目时报如下错误: 严重: Error configuring application listener of class org.springframework.web ...

  8. Map总结

    Map是键值对集合,是一对一对往上存的,要保持键的唯一性 形式:Map<K, V> 方法: 增 put(K key, V value) 若存储时Map中有相同的键,则返回原来键的值,并覆盖 ...

  9. 51nod 1161 Partial Sums

    给出一个数组A,经过一次处理,生成一个数组S,数组S中的每个值相当于数组A的累加,比如:A = {1 3 5 6} => S = {1 4 9 15}.如果对生成的数组S再进行一次累加操作,{1 ...

  10. 【CodeForces】913 F. Strongly Connected Tournament 概率和期望DP

    [题目]F. Strongly Connected Tournament [题意]给定n个点(游戏者),每轮游戏进行下列操作: 1.每对游戏者i和j(i<j)进行一场游戏,有p的概率i赢j(反之 ...