Codeforces Round #592 (Div. 2) D - Paint the Tree
题目链接:https://codeforces.com/contest/1244/problem/D
题意:给你一个树,让你把树上的每个节点染成三种颜色,使得任意三个互相相邻的节点颜色都不一样(意思是如果两个节点相邻,那么与这两个节点相邻的节点的颜色得和这两个节点都不一样)。这里给出每个节点染成三种颜色的代价,让我们求全部染色代价最小的方案与最小代价。
解析:如果一个点的度数大于等于3,那么肯定没有方案。所有这个树一定只是一条链,我们从度数为1的点开始dfs,把这条链存在数组里,就相当于我们只需要把数组连接三个数染成不同颜色需要付出的最小方案即可。由于情况只有3!=6种,我们可以全排列枚举,然后取最小值即可。
#pragma GCC optimize(2)
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5+;
ll c[][N];
int deg[N],col[N];
vector<int>adj[N],vts;
int res[N];
void dfs(int s,int p=-)
{
vts.push_back(s);
for(auto v:adj[s])
{
if(v!=p)
{
dfs(v,s);
}
}
}
int main()
{
int n;
cin>>n;
for(int i=;i<;i++)
{
for(int j=;j<n;j++)
{
cin>>c[i][j];
}
}
for(int i=;i<n-;i++)
{
int x,y;
cin>>x>>y;
x--;y--;
deg[x]++;
deg[y]++;
adj[x].push_back(y);
adj[y].push_back(x);
}
for(int i=;i<n;i++)
{
if(deg[i]>=)
{
cout<<"-1\n";
return ;
}
}
for(int i=;i<n;i++)
{
if(deg[i]==)
{
dfs(i);
break;
}
}
ll ans=LLONG_MAX;
vector<int>p={,,},be;
do{
ll cur=;
for(int i=;i<n;i++)
{
cur+=c[p[i%]][vts[i]];
}
if(cur<ans)
{
ans=cur;
be=p;
}
}while(next_permutation(p.begin(),p.end()));
cout<<ans<<"\n"; for(int i=;i<n;i++)
{
res[vts[i]]=be[i%];
}
for(int i=;i<n;i++)
{
cout<<res[i]+<<" ";
}
return ;
}
Codeforces Round #592 (Div. 2) D - Paint the Tree的更多相关文章
- Codeforces Round #592 (Div. 2)
A. Pens and Pencils 题目链接:https://codeforces.com/contest/1244/problem/A 题意: 给定五个数 a , b , c , d , k 求 ...
- 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/ ...
- Codeforces Round #382 (Div. 2)E. Ostap and Tree
E. Ostap and Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Codeforces Round #124 (Div. 1) C. Paint Tree(极角排序)
C. Paint Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Codeforces Round #592 (Div. 2)【C题】{补题ING}
思路:x,y,z肯定不为负数xw+dy=p,直接枚举系数较小的y即可,y的范围:y<w,因为大于w的时候,不如去增加x,这样x+y的和还能保持尽可能小. /* x*w+y*d=p; x*w+(K ...
- Codeforces Round #592 (Div. 2) E
给你一个数组,你最多可以进行k次操作,每次操作可以使一个数+1或者-1,问操作之后数组的极差最小可能是多少 利用map来模拟移动,可以观察到每次应该选择数量少的一组数让他们进行移动是最优的 int m ...
- Codeforces Round #592 (Div. 2)G(模拟)
#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;long long a[1000007],b[ ...
- Codeforces Round #379 (Div. 2) E. Anton and Tree 缩点 直径
E. Anton and Tree 题目连接: http://codeforces.com/contest/734/problem/E Description Anton is growing a t ...
- Codeforces Round #379 (Div. 2) E. Anton and Tree —— 缩点 + 树上最长路
题目链接:http://codeforces.com/contest/734/problem/E E. Anton and Tree time limit per test 3 seconds mem ...
随机推荐
- DBA常用SQL之DDL生成语句-2
------数据迁移常用SQL SELECT 'DROP USER '||u.username ||' CASCADE;' AS dropstrs FROM DBA_USERS U where u.u ...
- 杂记 -- 关于vue-router样式、vuecli引用全局js函数、vue.slot用法
1.routerLinkTo 样式设置 首先,点击routerlink标签如下图:添加:router-link-active,router-link-exact-active两个类的样式 router ...
- Shiro自动登录
Shiro RememberMe spring.xml <bean class="org.apache.shiro.web.mgt.DefaultWebSecurityManager& ...
- Codeforces Round #617 (Div. 3) D. Fight with Monsters
D : Fight with Monsters 题目大意 : 有一组数,每个值对应着一个怪物的 hp 值,现在有两个人,一个自己一个对手,每个人有一个攻击值, 两个人轮流攻击怪物,如果是自己将怪物先打 ...
- pytorch之 compare with numpy
import torch import numpy as np # details about math operation in torch can be found in: http://pyto ...
- sparc v8 stack frame calling convention
main.c ; int main() { int a, b; int sum; a = ; b = ; sum = add(a, b); ; } int add(int a, int b) { in ...
- vi/vim编辑器操作梳理
1. vi/vim编辑器详细使用讲解 1.1 vi/vim编辑器的3种模式 1.2 vi/vim编辑器操作说明 参数/命令/模式 说明 ###普通模式 :set nu 显示行号 :set non ...
- Android 7.0新特性“Nougat”(牛轧糖)。
1.Unicode 9支持和全新的emoji表情符号 Android Nougat将会支持Unicode 9,并且会新增大约70种emoji表情符号.这些表情符号大多数都是人形的,并且提供不同的肤色, ...
- 前端html,css考点
1, 内联元素,块级元素相关知识点 参考链接:https://edu.aliyun.com/a/103378 (1)置换元素 概念:浏览器根据元素的标签和属性,来决定元素的具体显示内容.<img ...
- SAP 如何看某个TR是否传入了Q或者P系统?
SAP 如何看某个TR是否传入了Q或者P系统? 两种方式可以查询. 1)进入Q系统或者P系统.SE16,看表TPALOG, 输入请求号码, 执行,看记录里的字段TPSTAT_KEY是否为空,如果不为空 ...