题意

一个 \(n\) 个点 \(m\) 条边的无向连通图中每个点都有一个权值,现在要求给每条边定一个权值,满足每个点的权值等于所有相连的边权之和,权值可负。

题解

如果图是一棵树,那么方案就是唯一的,直接判一下就可以了,因为可以从叶子开始逐个确定回去。

否则先搞一棵 \(Dfs\) 树,先不管其他边,跑一遍,这时根节点可能还不满足条件(权值不为 \(0\) )。

这时考虑其他的边,一条非树边(返祖边)由于会形成一个环:

  • 如果是偶环,无论这条边权值如何变,都不会对根节点产生贡献;

  • 如果是奇环,当这条边权值改变 \(w\) 的时候,根据上面那个节点的奇偶性会对根产生 \(\pm 2w\) 的贡献。

    此时如果根节点需要的权值是奇数如何变化都是无法满足的,当为偶数的时候可以构造出一组合法方案。

总结

树上构造,常常从叶子往回考虑。

图上构造,可以先构造树,然后考虑非树边的贡献就行了。

代码

很好写qwq 但要注意开 long long

#include <bits/stdc++.h>

#define For(i, l, r) for(register int i = (l), i##end = (int)(r); i <= i##end; ++i)
#define Fordown(i, r, l) for(register int i = (r), i##end = (int)(l); i >= i##end; --i)
#define Set(a, v) memset(a, v, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define debug(x) cout << #x << ": " << (x) << endl
#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#define Travel(i, u, v) for (int i = Head[u], v = to[i]; i; v = to[i = Next[i]]) using namespace std; typedef long long ll; template<typename T> inline bool chkmin(T &a, T b) {return b < a ? a = b, 1 : 0;}
template<typename T> inline bool chkmax(T &a, T b) {return b > a ? a = b, 1 : 0;} inline int read() {
int x(0), sgn(1); char ch(getchar());
for (; !isdigit(ch); ch = getchar()) if (ch == '-') sgn = -1;
for (; isdigit(ch); ch = getchar()) x = (x * 10) + (ch ^ 48);
return x * sgn;
} void File() {
#ifdef zjp_shadow
freopen ("D.in", "r", stdin);
freopen ("D.out", "w", stdout);
#endif
} const int N = 1e5 + 1e3, M = N << 1; int n, m, need[N]; int Head[N], Next[M], to[M], e = 1; inline void add_edge(int u, int v) {
to[++ e] = v; Next[e] = Head[u]; Head[u] = e;
} bitset<N> vis;
int dep[N], from[N], cid, cu, cv; ll val[N]; void Dfs(int u, int fa) {
ll cur = need[u];
dep[u] = dep[fa] ^ 1; vis[u] = true;
Travel(i, u, v)
if (!vis[v])
from[v] = i ^ 1, Dfs(v, u), cur -= val[i >> 1];
else if (!(dep[u] ^ dep[v]))
cid = i >> 1, cu = u, cv = v;
val[from[u] >> 1] = cur;
} inline void Change(int u, int End, int w) {
for (; u != End; u = to[from[u]])
val[from[u] >> 1] += w, w *= -1;
} inline void Out(bool flag) {
if (!flag) return (void)puts("NO");
puts("YES");
For (i, 1, m)
printf ("%lld\n", val[i]);
} int main () { File(); n = read(); m = read();
For (i, 1, n)
need[i] = read();
For (i, 1, m) {
int u = read(), v = read();
add_edge(u, v); add_edge(v, u);
} Dfs(1, 0);
ll w = val[0];
if (!w) return Out(true), 0;
if ((w & 1) || !cid) return Out(false), 0; int opt = dep[cu] ? 1 : -1;
Change(cu, 1, - opt * w);
Change(cv, cu, - opt * w >> 1);
val[cid] = opt * w >> 1; Out(true); return 0; }

Codeforces Round #453 (Div. 1) D. Weighting a Tree(构造)的更多相关文章

  1. 【做题】Codeforces Round #453 (Div. 1) D. Weighting a Tree——拆环

    前言:结论题似乎是我的硬伤…… 题意是给你一个无向图,已知连接到每一个点的边的权值和(为整数,且属于区间[-n,n]),需要求出每条边权值的一个合法解(都要是在区间[-2*n^2,2*n^2]内的整数 ...

  2. Codeforces Round #319 (Div. 1) B. Invariance of Tree 构造

    B. Invariance of Tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/576/ ...

  3. Codeforces Round #453 (Div. 1)

    Codeforces Round #453 (Div. 1) A. Hashing Trees 题目描述:给出一棵树的高度和每一层的节点数,问是否有两棵树都满足这个条件,若有,则输出这两棵树,否则输出 ...

  4. Codeforces Round #275 (Div. 2) C - Diverse Permutation (构造)

    题目链接:Codeforces Round #275 (Div. 2) C - Diverse Permutation 题意:一串排列1~n.求一个序列当中相邻两项差的绝对值的个数(指绝对值不同的个数 ...

  5. Codeforces Round #453 (Div. 1) 901C C. Bipartite Segments

    题 http://codeforces.com/contest/901/problem/C codeforces 901C 解 首先因为图中没有偶数长度的环,所以: 1.图中的环长度全是奇数,也就是说 ...

  6. Codeforces Round #453 ( Div. 2) Editorial ABCD

    A. Visiting a Friend time limit per test 1 second memory limit per test 256 megabytes input standard ...

  7. Codeforces Round #453 Div. 2 A B C D (暂时)

    // 从大作业和实验报告中爬出来水一发 // 补题...还是得排在写完实验报告之后... A. Visiting a Friend 题意 给定若干段行车区间,问能否从起点到终点 思路 扫描一遍,维护最 ...

  8. 【Codeforces Round #453 (Div. 2) A】 Visiting a Friend

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 维护最右端的端点就好. [代码] #include <bits/stdc++.h> using namespace st ...

  9. 【Codeforces Round #453 (Div. 2) B】Coloring a Tree

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 从根节点开始. 显然它是什么颜色.就要改成对应的颜色.(如果上面已经有某个点传了值就不用改 然后往下传值. [代码] #includ ...

随机推荐

  1. win64位安装python-mysqldb1.2.3

    在其他版本的mysqldb里面时间查询有问题最后确定还是在 1.2.5 版本下来解决,需要解决的问题就是这个:“Cannot open include file: 'config-win.h': No ...

  2. Python_计算文件夹大小

    计算文件夹大小 os.listdir('dirname') 列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印 os.path.join(path1[, path2[, ...]]) 将 ...

  3. Shell脚本2

      5 Shell传递参数 我们可以在执行 Shell 脚本时,向脚本传递参数, 脚本内获取参数的格式为:$n.n 代表一个数字,1 为执行脚本的第一个参数,2 为执行脚本的第二个参数,以此类推…… ...

  4. oracle创建视图时一些问题

    这几天创建视图的时候,遇见的问题. 一:创建视图的时候Oracle-报错:文字与格式字符串不匹配(ORA-01861) 我创建的时候用的 是to_date 然后我改成了to_char select X ...

  5. Flutter的scope_model使用mixin语法报错

    在pubspec.yaml同级目录下创建analysis_options.yaml文件,内容: # https://www.dartlang.org/guides/language/analysis- ...

  6. 【git】如何去解决fatal: refusing to merge unrelated histories

    我在Github新建一个仓库,写了License,然后把本地一个写了很久仓库上传. 先pull,因为两个仓库不同,发现refusing to merge unrelated histories,无法p ...

  7. Golang的聊天服务器实践(群聊,广播)(一)

    其实从上学开始就一直想写一个im. 最近深入go,真是学会了太多,感觉人森虽然苦短,但是也不能只用python.很多知识是不用编译型语言无法了解的. 该来的还是会来,现在会一步一步用go把这个服务器完 ...

  8. 老男孩python学习自修第五天【集合】

    特点: (1)无序 (2)不重复 使用场景: (1)关系测试 (2)去重 x & y 求交集 x | y 求并集 x - y 求差集 x ^ y 求对称差集 x.intersection(y) ...

  9. QTP键盘操作笔记

    micCtrlDwn  Presses the Ctrl key. micCtrlUp  Releases the Ctrl key. micLCtrlDwn  Presses the left Ct ...

  10. QTP 自动货测试桌面程序-笔记 (单据-下拉框选择、对话框 、菜单)

    1 录制下拉框使用键盘上下键 回车键选择记录行 Window("驷惠WIN系列[汽车4S连锁管理软件] 6.").Window("采购计划").WinObjec ...