`
阿尔萨斯
  • 浏览: 4169072 次
社区版块
存档分类
最新评论

uva 11307 - Alternative Arborescence(树形dp)

 
阅读更多

题目链接:uva 11307 - Alternative Arborescence


题目大意:给出一棵树,然后在每个节点上填上任意的数字,要求相邻的节点不能填相同的数字,问说所有节点数字的和最小为多少。


解题思路:这题读入部分有点坑爹,其他都还好,dp[i][j]表示节点i填j的情况。


#include <stdio.h>
#include <string.h>
#include <vector>

using namespace std;
const int N = 1e4+5;
const int M = 6;
const int INF = 0x3f3f3f3f;

int n, dp[N][M+5];
vector<int> g[N];

bool get () {

	char s[N*M];
	gets(s);

	if (s[0] == '\0') return false;

	int len = strlen(s), i = 0;

	int u = -1, v = -1;
	while (s[i] < '0' && s[i] > '9') i++;

	for (; s[i] != ':'; i++) {
		if (u == -1) u = s[i] - '0';
		else u = u * 10 + s[i] - '0';
	}

	for (; i <= len; i++) {
		if (s[i] >= '0' && s[i] <= '9') {
			if (v == -1) v = s[i] - '0';
			else v = v * 10 + s[i] - '0';
		} else {
			if (v != -1) {
				g[u].push_back(v);
				g[v].push_back(u);
				v = -1;
			}
		}
	}

	return true;
}

void dfs (int u, int f) {
	for (int i = 1; i <= M; i++)
		dp[u][i] = i;

	for (int i = 0; i < g[u].size(); i++) {

		int v = g[u][i];
		if (v == f) continue;

		dfs(v, u);

		for (int x = 1; x <= M; x++) {
			int cur = INF;
			for (int y = 1; y <= M; y++) {
				if (x == y) continue;
				cur = min(cur, dp[v][y]);
			}
			dp[u][x] += cur;
		}
	}
}

int main () {
	while (scanf("%d%*c", &n), n) {
		for (int i = 0; i < n; i++)
			g[i].clear();

		while (get()) ;

		dfs(0, -1);
		int ans = INF;
		for (int i = 1; i <= M; i++)
			ans = min(ans, dp[0][i]);
		printf("%d\n", ans);
	}
	return 0;
}




分享到:
评论

相关推荐

    min-cost-arborescence:通过实现edmond算法在有向图中计算最小成本生成树的C ++代码

    通过实现edmond算法,在有向图中计算最小成本生成树的C ++代码。 我们使用每个节点都具有的有向生成树的属性(源除外)具有1度的度数,因此我们使用全局数组parent [n]表示每个点的树状结构 输入格式 第一行:测试...

    Générateur d'arborescence:生成目录和文件树-开源

    指示目录之一的路径以显示其所有目录和子文件的树结构。

    flotte_M2_2020-2021-preparateur

    新手入门与入门: Se positionner dans le git sur le pc: cd &lt;Arborescence&gt; Recuperer les nouveaux fichiers du serveur sur son pc: git pull 分支机构发展计划: git checkout develop Créerune Nouvelle ...

    D2D big data

    meeting dynamics, social arborescence, privacy preservation policies and so on, we verify and evaluate the D2D Big Data platform regarding predictive content propagating coverage. Finally, we discuss ...

    Composer-1:任务5.1

    拨1 任务5.1 :flexed_biceps: 挑战创建项目架构您必须创建一个简约的项目体系结构。 文件夹树应如下所示: ... 验证标准Ton code est sur un repo personnel sur github.Ton arborescence correspond à ce qui a été

    positions:GLPI的插件位置

    通过单击电话号码,将直接在IP电话上发起呼叫可以与“人力资源”插件一起使用: : GLPI&gt; 0.84:可与“ Arborescence”插件一起使用: ://forge.glpi-project.org/projects/show/treeview 此插件可让您将坐标添加到...

    Nextcloud

    通过Docker安装Nextcloud进阶装置安装的Nous allons d'abordcréerl'arborescence的产品。 赖氨酸下云海事数据库配置数据DOSSIERS DE TRAVAIL 在利用donc les commandes ci-dessous时: 在CreélerépertoireRacine ...

    hothothot

    TéléchargerGoogle lib 网址的自动验证Google(Vous ne pouvez faire marcher la connexion Google sur votre Vhost s'il n'est pas en HTTPS域名公开域名的扩展: .com .fr ):Créerune Arborescence

    Prog-C-INFO1:程序集取消编程C(INFO1 Esipe)

    预定的Voici l'arborescence: tp ├── 1tp │ ├── 1tp_cr_progc.md │ ├── CR_TP1_C_Arnaud_Apelbaum_v2.odt │ ├── docs_tp1 │ │ ├── dir1 │ │ │ └── titi │ │ ├── dir2 ...

Global site tag (gtag.js) - Google Analytics