AtCoder Grand Contest 012 B - Splatter Painting(dp)
Time limit : 2sec / Memory limit : 256MB
Score : 700 points
Problem Statement
Squid loves painting vertices in graphs.
There is a simple undirected graph consisting of N vertices numbered 1 through N, and M edges. Initially, all the vertices are painted in color 0. The i-th edge bidirectionally connects two vertices ai and bi. The length of every edge is 1.
Squid performed Q operations on this graph. In the i-th operation, he repaints all the vertices within a distance of di from vertex vi, in color ci.
Find the color of each vertex after the Q operations.
Constraints
- 1≤N,M,Q≤105
- 1≤ai,bi,vi≤N
- ai≠bi
- 0≤di≤10
- 1≤ci≤105
- di and ci are all integers.
- There are no self-loops or multiple edges in the given graph.
Partial Score
- 200 points will be awarded for passing the testset satisfying 1≤N,M,Q≤2,000.
Input
Input is given from Standard Input in the following format:
N M
a1 b1
:
aM bM
Q
v1 d1 c1
:
vQ dQ cQ
Output
Print the answer in N lines. In the i-th line, print the color of vertex i after the Q operations.
Sample Input 1
7 7
1 2
1 3
1 4
4 5
5 6
5 7
2 3
2
6 1 1
1 2 2
Sample Output 1
2
2
2
2
2
1
0
Initially, each vertex is painted in color 0. In the first operation, vertices 5 and 6 are repainted in color 1. In the second operation, vertices 1, 2, 3, 4 and 5 are repainted in color 2.

****************************************************************************************************************************************************
题意:给你N个点,M条边(可能有点的没有边),有Q次染色操作,每次染第vi节点的di范围内的所有节点。节点的颜色可以被覆 盖。
解题思路:
1)暴力dfs
保存每一个顶点连同的顶点。
每一次输入v,d,c,搜索v节点的d范围内能够染色的点,每一次d-1;
2)dp
先贴上官方题解:https://agc012.contest.atcoder.jp/editorial;
**************************************************************************************************************************
简单翻译一下(虽然是看着jcvb dalao理解的)
dp[i][d]表示当前i节点d距离时的颜色,所以dp[i][0]为i节点的颜色,
所以就可以在dfs时判断如果当前节点dp[i][d]已经染了色,就可以不用染色了。
还有的就是:因为颜色是覆盖的,所以最后染的颜色就是结束的颜色。所以我们应该从后往前开始染色。
这样就可以保证,如果这节点d[i][0]染了色,就不会被覆盖了。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
typedef long long LL;
const int maxn = 100000+10;
int color[maxn][15];
int v[maxn];
int d[maxn];
int c[maxn];
vector <int> node[maxn];
void paint(int v1, int d1, int c1)
{
if(d1==-1) return;
if(color[v1][d1] ) return;
color[v1][d1] = c1;
for(int i =0;i<node[v1].size();i++)
paint(node[v1][i], d1-1, c1);
}
int main()
{
ios::sync_with_stdio(0);
memset(color, 0, sizeof color);
int N, M;
cin >> N >>M;
for(int i=1;i<=M;i++){
int a, b;
cin >> a >> b;
node[a].push_back(b);
node[b].push_back(a);
}
///自己指向自己, 才能够自己跑到color[i][0]
for(int i=1;i<=N;i++)
node[i].push_back(i);
int q;
cin >> q;
for(int i=1;i<=q;i++)
cin >> v[i] >> d[i] >> c[i];
for(int i=q;i>=1;i--)
paint(v[i], d[i], c[i]);
for(int i=1;i<=N;i++)
cout << color[i][0] << endl;
return 0;
}
可以用链式前向星来代替vector。
AtCoder Grand Contest 012 B - Splatter Painting(dp)的更多相关文章
- AtCoder Grand Contest 012 B Splatter Painting (反向处理 + 记忆化)
题目链接 agc012 Problem B 题意 给定一个$n$个点$m$条边的无向图,现在有$q$个操作.对距离$v$不超过$d$的所有点染色,颜色编号为$c$. 求每个点最后的颜色状态. 倒过 ...
- AtCoder Grand Contest 012 B Splatter Painting(记忆化搜索)
题意: 给一个包含N个顶点,M条边,无自环和重边的简单无向图,初始每个点颜色都为0,每条边的长度为1,连接着ai,bi两个节点.经过若干个操作, 每次将与某个点vi距离不超过di的所有点染成某种颜色c ...
- AtCoder Grand Contest 012
AtCoder Grand Contest 012 A - AtCoder Group Contest 翻译 有\(3n\)个人,每一个人有一个强大值(看我的假翻译),每三个人可以分成一组,一组的强大 ...
- AtCoder Grand Contest 012 B
B - Splatter Painting Time limit : 2sec / Memory limit : 256MB Score : 700 points Problem Statement ...
- AtCoder Grand Contest 012 A
A - AtCoder Group Contest Time limit : 2sec / Memory limit : 256MB Score : 300 points Problem Statem ...
- AtCoder Grand Contest 012 A - AtCoder Group Contest(贪心)
Time limit : 2sec / Memory limit : 256MB Score : 300 points Problem Statement There are 3N participa ...
- AtCoder Grand Contest 031 B - Reversi(DP)
B - Reversi 题目链接:https://atcoder.jp/contests/agc031/tasks/agc031_b 题意: 给出n个数,然后现在你可以对一段区间修改成相同的值,前提是 ...
- AtCoder Grand Contest 012 C:Tautonym Puzzle
题目传送门:https://agc012.contest.atcoder.jp/tasks/agc012_c 题目翻译 如果一个字符串是好的,那么这个字符串的前半部分和后半部分肯定一模一样.比如\(a ...
- AtCoder Grand Contest 012 D:Colorful Balls
题目传送门:https://agc012.contest.atcoder.jp/tasks/agc012_d 题目翻译 给你一排一共\(N\)个球,每个球有一个颜色\(c_i\)和一个重量\(w_i\ ...
随机推荐
- 自动化测试中,元素无法点击定位等问题的解决:js的使用方法
在自动化测试中经常会遇到使用selenium方法定位元素点击操作失败的情况,例如,我们想实现在浏览器输入http://www.baidu.com,进入百度首页后,鼠标悬停在“更多产品”上,点击“全部产 ...
- 【ABAP系列】SAP ABAP 优化LOOP循环的一点点建议
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP 优化LOOP循 ...
- 【Unity Shader】---准确认识SubShader语义块结构、渲染状态设定、Tags标签
一[SubShader] 每个UnityShader文件可以包含多个SubShader语义块,但至少要有一个.当Unity需要加载这个UnityShader时,Unity会扫描所有的SubShader ...
- CSP2019 —— 今年欢笑复明年,不知退役在眼前
关于2019CSP-J/-S的一些体会 又是一年退役季,想起在群里看到大佬的一句诗,感慨万千. 今年欢笑复明年,不知退役在眼前 于是便心生文意,随便写点东西来给自己康康. 先说说这次的成绩吧.大家应该 ...
- Mac下安装配置gradle
1.下载gradle2.解压3.获得gradle解压后的路径4.修改.bash_profile文件,配置环境变量 vi ~./bash_profile export GRADLE_HOME=/User ...
- Notepad++-第一篇命令行语句执行之编译、运行Java
1.让Notepad++编译和运行Java,在电脑上要已经配置好了Java的开发环境 2.在Notepad++上面的选项栏中找到 Plugins--->Plugin Admin 3.在Avail ...
- 《jmeter:菜鸟入门到进阶系列》
jmeter是我从事软件测试工作以来接触的第一个性能测试工具,也是耗费时间精力最多的一个工具,当然,学习jmeter过程中,由于知识储备不够,也顺带学习了很多其他相关的一些知识. 一直有个想法,就是把 ...
- P5444 [APIO2019]奇怪装置
传送门 考虑求出最小的循环节 $G$ 使得 $t,t+G$ 得到的数对是一样的 由 $y \equiv t \mod B$ ,得到 $G$ 一定是 $B$ 的倍数,设 $zB=G$,则 $t,t+zB ...
- passwd - 密码文件
描述 Passwd 是个文本文件, 它包含了一个系统帐户列表, 给出每个帐户一些有用的信息,比如用户 ID,组 ID, 家目录, shell,等. 通常它也包含了每个用户经过加密的密码. 它通常应该是 ...
- .net 项目中应用Web Services(vs2012)
一.在asp.net项目中添加Web services1.新建一个asp.net项目(目前尚未验证是否可以在MVC项目中添加)2.在项目名上右击,选择添加→新建项→Web服务,输好名称后确定即可 二. ...