B - Splatter Painting


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,viN
  • aibi
  • 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

Copy
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

Copy
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 1234 and 5 are repainted in color 2.


Sample Input 2

Copy
14 10
1 4
5 7
7 11
4 10
14 7
14 3
6 14
8 11
5 13
8 3
8
8 6 2
9 7 85
6 9 3
6 7 5
10 3 1
12 9 4
9 6 6
8 2 3

Sample Output 2

Copy
1
0
3
1
5
5
3
3
6
1
3
4
5
3

The given graph may not be connected.

题意:可以参考图,问你最后的染色情况

解法:因为后面的染色会覆盖前面的,我们就倒过来处理,另外保存每个点的范围

比如1-2-3-4-5-6,

我们从3处理,距离是2

3的处理范围2

2的处理范围1

1的处理范围0

4的处理范围1

5的处理范围0

就是说1,5已经到染色边界了

那么每次染色我们都比较上一次这个点的处理范围,比这一次的大,说明一定会被上一次的覆盖,没必要遍历下去了,或者处理没有染色的部分

 #include<bits/stdc++.h>
using namespace std;
#define ll long long
vector<int>q[];
int color[];
int flag[];
int n,m;
int v[],d[],c[];
void dfs(int x,int cnt,int c)
{
if(!color[x])
{
color[x]=c;
}
if(flag[x]>=cnt)
{
return;
}
if(cnt==)
{
return;
}
flag[x]=cnt;
for(int i=;i<q[x].size();i++)
{
dfs(q[x][i],cnt-,c);
}
}
int main()
{
std::ios::sync_with_stdio(false);
cin>>n>>m;
for(int i=;i<=m;i++)
{
int x,y;
cin>>x>>y;
q[x].push_back(y);
q[y].push_back(x);
}
int q;
cin>>q;
for(int i=;i<=q;i++)
{
cin>>v[i]>>d[i]>>c[i];
}
for(int i=q;i>=;i--)
{
dfs(v[i],d[i],c[i]);
}
for(int i=;i<=n;i++)
{
cout<<color[i]<<endl;
}
return ;
}

最新文章

  1. China Mobile 免流原理
  2. nginx的内存管理
  3. iOS开发--网络下载
  4. BZOJ2151: 种树
  5. Apache多站点设定
  6. system partition table
  7. Chrome小技巧:如何下载离线版安装文件
  8. ubuntu下php安装xdebug
  9. 转化json
  10. Android-----View绘制流程以及invalidate()等相关方法分析 .
  11. self sqflite sample =======================
  12. 测试那些事儿-软测必备的linux知识(五)
  13. Java 微信公众号迁移
  14. 转载&gt;&gt;C# Invoke和BeginInvoke区别和使用场景
  15. H/s:哈希率单位转换
  16. SpaceNet 数据集
  17. 分布式事务框架&amp;解决方案参考
  18. C#访问远程主机资源的方法,多种方式
  19. spfa【模板】
  20. dede数据库内容替换,去掉文章内容中的img标签

热门文章

  1. Codeforces Round #422 (Div. 2) D. My pretty girl Noora 数学
  2. Codeforces Round #422 (Div. 2) A. I&#39;m bored with life 暴力
  3. console调试命令
  4. html 常用转译空格字符
  5. delphi 八字排盘源码(post数据以后,又分析数据)
  6. Windows命令行bat批处理延迟sleep方法
  7. BZOJ_3295_[Cqoi2011]动态逆序对_CDQ分治+树状数组
  8. bzoj1003物流运输——DP
  9. 语言学习系列-Scala连接数据库示例
  10. 3.11-3.15 HDFS HA