题目链接:uva 10859 - Placing Lampposts
题目大意:给定一个无向无环图,要求在点上放灯,如果某一点上放了等,则可以照亮与它相通的边,现在要求放尽量少得等,使得所有边都被照亮,并且输出灯数,被照亮两次的边数(即边的两个端点均放置灯),被照亮一次的边。 如果等数一样的话,按照被照亮一次边越大的方案。
解题思路:树形dp,每次枚举某个顶点作为根,对于每个点有放与不放两种可能,均进行考虑;然后每放一盏灯+2000(因为边的最大数量为1000),每增加1条照亮一次的边+1.
#include <stdio.h>
#include <string.h>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 1010;
const int M = 2000;
int n, m, v[N], dp[N][2];
vector<int> g[N];
void init() {
int a, b;
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) g[i].clear();
memset(v, 0, sizeof(v));
memset(dp, 0, sizeof(dp));
for (int i = 0; i < m; i++) {
scanf("%d%d", &a, &b);
g[a].push_back(b);
g[b].push_back(a);
}
}
void dfs(int x) {
v[x] = 1;
dp[x][0] = 0;
dp[x][1] = M;
for (int i = 0; i < g[x].size(); i++) {
int u = g[x][i];
if (v[u]) continue;
dfs(u);
dp[x][0] += dp[u][1] + 1;
if (dp[u][1] > dp[u][0]) {
dp[x][1] += dp[u][0] + 1;
} else {
dp[x][1] += dp[u][1];
}
}
}
void solve () {
int ans = 0;
for (int i = 0; i < n; i++) if (!v[i]) {
dfs(i);
ans += min(dp[i][0], dp[i][1]);
}
printf("%d %d %d\n", ans/M, m - (ans%M), ans%M);
}
int main () {
int cas;
scanf("%d", &cas);
while (cas--) {
init();
solve();
}
return 0;
}
分享到:
相关推荐
外贸英语函电chapter-8-Placing-Orders-and-Executing-Orders.ppt
Android-Img-Placing-Game 游戏用于将图像放置在正确的背景上
本科论文对于我的本科论文,我正在尝试开发一种有效的算法来放置服务器,以便即使在k个服务器发生故障后,每个客户端也至少可以在d距离内获得ap台服务器。
Placing const in Declarations by Dan Saks
Chapter 24 - Placing an Edit Box on a Toolbar Chapter 25 - Serialization Chapter 26 - Review of Menu and Toolbar-Based Functionality: Part II Chapter 27 - Printing and Print Preview Chapter 28 - ...
(b) placing SQL keywords, such as select, under the column names they want to retrieve (c) typing a syntactically correct SQL query that uses column and table names similar to the correct column and...
Placing of blinding Layer for Foundation→ Installation of Precast Concrete Blocks→ Placing of Filter Layer Rock on the Excavated Slope→ Placing of Quarry Run as Rock Mound behind Blocks→ Placing ...
中文名: Massive Software Learning Tutorials ...2-3 PLACING AGENTS IN A SCENE TERRAIN MAPS 2-6a CREATING FLOWFIELDS 2-7a PAINTING COLOUR RENDERING 6-2 CAMERAS 6-3 THE SIM DIALOG 6-4 RENDERING
Tomcat's WebappClassLoader is currently not instrumentable, so Spring provides a custom ClassLoader that can be used by placing spring-instrument-tomcat.jar in $TOMCAT_HOME/lib and putting a loader ...
The third objective is to present design methodologies for efficiently placing on-chip decoupling capacitors in nanoscale integrated circuits. Finally, the fourth objective is to suggest novel ...
due to bugs, placing projects in directories containing spaces in the path, or characters like ", ' and &, have had issues. We're working to eliminate these bugs, but to save yourself headaches you ...
Call by passing the argument '--dts_out[=]' to protoc.You may specify parameters on the command-line by placing it before the output directory, separated by a colon: protoc --dts_out=enable_bar:...
(~6% increase) and placing an order (~10% increase). Furthermore, we find interesting heterogeneous treatment effects that align with social learning mechanism: the effect of the integrated Facebook ...
placing and routing. A novel searching strategy is also designed to find the optimal result efficiently. Finally, we built a complete flow of mapping loop nests onto CGRA. Experiment results on most ...
n queen problems- c program for placing queens in a chess board
Placing the cluster in unmanaged mode 6.2.2. Backing up the CIB 6.2.3. Stopping Heartbeat services 6.2.4. Wiping files related to the CRM 6.2.5. Restoring the CIB 6.2.6. Upgrading software ...
Placing special emphasis on creating cost-effective solutions based on real-world scenarios, the authors show how to enhance and manage the features of Access 2013 and cover critical changes that may...
If we would represent the same field placing the hint numbers described above, we would end up with: *100 2210 1*10 1110 As you may have already noticed, each square may have at most 8 adjacent ...
Using numerical integration in the formulation of the finite-element geometric stiffness matrix and placing movable nodes at integration points causes the geometric stiffness matrix to become lumped ...
视频人脸识别,取代jmf。。。 Introduction JavaCV uses wrappers from the JavaCPP Presets of commonly used libraries by researchers in the field of computer vision (OpenCV, FFmpeg, libdc1394, PGR ...