P2996 传送门 题意: 给你一棵树,每一条边上最多选一个点,问你选的点数. 我的思想: 一开始我是想用黑白点染色的思想来做,就是每一条边都选择一个点. 可以跑两边一遍在意的时候染成黑,第二遍染成白,取一个最大值. 就可以得到\(30\)分的高分. #include <bits/stdc++.h> #define N 100010 #define M 1010 #define _ 0 using namespace std; int n, tot, ans, add_edge, color[…
题目 树形dp 设f[i][j]表示走到第i号节点的最大权值 j为0/1表示这个点选或者不选 如果这个点不选 就从他的子树里的选或者不选选最大 如果这个点选 就加上他子树的不选 f[x][0] += max(f[to][1], f[to][0]); f[x][1] += f[to][0]; 注意第二维要开到2 Code: //设f[i][j]表示选到第i号节点的最大权值 #include <cstdio> #include <iostream> using namespace st…
之前写在洛谷,结果没保存,作废…… 听说考前写题解RP++哦 思路 很容易想到是 树形DP 如果树形DP不知道是什么的话推荐百度一下 我在这里用vector储存边 设状态f[i][0]为i点不访问,f[i][1]为i点访问 那么f[u][1] +=  f[y][0]表示u点要访问,(u,y)有连边f[u][0] +=  max(f[v][0], f[v][1])表示u点不访问,(u,y)有连边 上面就是我们的转移方程了 介绍一下vector吧 vector是STL里的一个向量容器 也叫动态数组…
P2868 [USACO07DEC]观光奶牛Sightseeing Cows 题目描述 Farmer John has decided to reward his cows for their hard work by taking them on a tour of the big city! The cows must decide how best to spend their free time. Fortunately, they have a detailed city map sh…
一道\(0/1\)分数规划+负环 POJ原题链接 洛谷原题链接 显然是\(0/1\)分数规划问题. 二分答案,设二分值为\(mid\). 然后对二分进行判断,我们建立新图,没有点权,设当前有向边为\(z=(x,y)\),\(time\)为原边权,\(fun\)为原点权,则将该边权换成\(mid\times time[z]+fun[x]\),然后在上面跑\(SPFA\). 如果有一个环使得\(\sum\{mid\times time[z]+fun[x]\}<0\),则说明\(mid\)小了,而式子…
P3088 [USACO13NOV]挤奶牛Crowded Cows 题目描述 Farmer John's N cows (1 <= N <= 50,000) are grazing along a one-dimensional fence. Cow i is standing at location x(i) and has height h(i) (1 <= x(i),h(i) <= 1,000,000,000). A cow feels "crowded"…
题目描述 Farmer John has decided to reward his cows for their hard work by taking them on a tour of the big city! The cows must decide how best to spend their free time. Fortunately, they have a detailed city map showing the L (2 ≤ L ≤ 1000) major landma…
题目描述 Farmer John has decided to reward his cows for their hard work by taking them on a tour of the big city! The cows must decide how best to spend their free time. Fortunately, they have a detailed city map showing the L (2 ≤ L ≤ 1000) major landma…
题意 题目链接 Sol 复习一下01分数规划 设\(a_i\)为点权,\(b_i\)为边权,我们要最大化\(\sum \frac{a_i}{b_i}\).可以二分一个答案\(k\),我们需要检查\(\sum \frac{a_i}{b_i} \geqslant k\)是否合法,移向之后变为\(\sum_{a_i} - k\sum_{b_i} \geqslant 0\).把\(k * b_i\)加在出发点的点权上检查一下有没有负环就行了 #include<bits/stdc++.h> #defin…
题目戳这里 一句话题意 L个点,P条有向边,求图中最大比率环(权值(Fun)与长度(Tim)的比率最大的环). Solution 巨说这是0/1分数规划. 话说 0/1分数规划 是真的难,但貌似有一些规律,总是离不开一个二分和带mid的不等式. 记环S=({vi},{ei}), 其中{vi}为环上结点的集合,{ei}为环上的边的集合 我们先分析一波公式:不过是要求\(\sum_{i=1}^{t}Fun[v[i]]/\sum_{i=1}^{t}Tim[e[i]]>mid\) 最小 不难想到要二分一…