1.题目描述:

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.

2.题意概述:

给一个包含N个顶点,M条边,无自环和重边的简单无向图,初始每个点颜色都为0,每条边的长度为1,连接着ai,bi两个节点。经过若干个操作,

每次将与某个点vi距离不超过di的所有点染成某种颜色ci,求最终每个点的颜色。

3.解题思路:

反过来做,染色过的点就不再染色
对于点u来说,如果以它为中心,距离为d的所有点都被染色过,那么下次你要对u距离为d’ (d’<d) 的点染色时就可以直接退出了

4.AC代码:

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define maxn 100100
#define lson root << 1
#define rson root << 1 | 1
#define lent (t[root].r - t[root].l + 1)
#define lenl (t[lson].r - t[lson].l + 1)
#define lenr (t[rson].r - t[rson].l + 1)
#define N 1111
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
using namespace std;
const int mod = 1e9 + 7;
typedef long long ll;
typedef unsigned long long ull;
vector<int> g[maxn];
int vis[maxn], col[maxn];
tuple<int, int, int> op[maxn];
void dfs(int v, int d, int c)
{if (vis[v] >= d)return;vis[v] = d;if (!col[v])col[v] = c;if (d){int sz = g[v].size();for (int i = 0; i < sz; i++)dfs(g[v][i], d - 1, c);}
}
int main()
{int n, m, q;while (~scanf("%d%d", &n, &m)){for (int i = 1; i <= n; i++)g[i].clear();fill(vis, vis + n + 1, -1);fill(col, col + n + 1, 0);while (m--){int u, v;scanf("%d%d", &u, &v);g[u].push_back(v);g[v].push_back(u);}scanf("%d", &q);for (int i = 0; i < q; i++){int v, d, c;scanf("%d%d%d", &v, &d, &c);op[i] = tuple<int, int, int>(v, d, c);}for (int i = q - 1; i >= 0; i--){int v, d, c;tie(v, d, c) = op[i];dfs(v, d, c);}for (int i = 1; i <= n; i++)printf("%d\n", col[i]);}return 0;
}

AtCoder2362 - Splatter Painting - DFS+思维相关推荐

  1. AT2362 [AGC012B] Splatter Painting(思维、dfs染色、剪枝)

    AT2362 [AGC012B] Splatter Painting 题意 给一个n个点m条边的无向图,有q次操作 第i次操作,给出v,d,c,把所有到点v的距离不超过d的点都染上颜色c 问最后每个点 ...

  2. Splatter Painting

    题目链接:Splatter Painting 因为距离很小. 所以,我们可以逆向操作,当前仅当这个点未被染色才去递归子节点.然后如果枚举到这个点,之前一个到过的节点还能到的距离大于当前还能到的距离就r ...

  3. AtCoder Grand Round 012B Splatter Painting

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  4. 2019.11.2图论专题(AtCoder Splatter Painting、President and Roads、Shortest Cycle、ISlands II)

    D:AtCoder Grand Contest 012 Splatter Painting 题目描述 Squid喜欢在图中为一些顶点染色(毕竟是鱿鱼 ) 现在有一张由 N 个顶点和 M 条边组成的简单 ...

  5. stcoder Splatter Painting 记忆化搜索

    5647: Splatter Painting 时间限制: 2 Sec  内存限制: 256 MB 提交: 32  解决: 16 [提交] [状态] [讨论版] [命题人:admin] 题目描述 Sq ...

  6. Atcoder #2362 Splatter Painting(dfs+优化)

    原题链接(vjudge上的) 这道题的大意是:对于一个无向图,有n个节点和m条边,每个操作将与节点v的距离小于等于d的节点涂为颜色c.n,m,p<=105n,m,p,d<=10d. 给出输 ...

  7. 【AtCoder】【思维】【图论】Splatter Painting(AGC012)

    题意: 有一个含有n个点的无向图,所有的点最初颜色均为0.有q次操作,每次操作将v[i]周围的距离小于等于d[i]的点全部都染成颜色c[i].最后输出每个点的最终的颜色. 数据范围: 1<=n, ...

  8. CF982 C Cut 'em all!【树/DFS/思维】

    [链接]:CF982C [题意]:有一颗树,你需要切掉一些边,使这颗树分拆成若干个节点为偶数的联通分量,最多能切掉几条边.若不能切,输出-1. [分析]: 1.若点数n为奇数,因为奇数不可能分为偶数, ...

  9. Monopole Magnets CodeForces - 1345D(dfs+思维)

    A monopole magnet is a magnet that only has one pole, either north or south. They don't actually exi ...

最新文章

  1. python 序列排序 排序后返回相应的索引
  2. 一维二维码的提取、识别和产生
  3. CentOS 5.4下的Memcache安装步骤(Linux+Nginx+PHP+Memcached)
  4. 配置Docker代理已实现外网访问
  5. educoder 使用线程锁(lock)实现线程同步_线程间的通信(一)
  6. vs code使用问题
  7. 使用FFTW的fftw_plan_dft_c2r_1d()由于未归一化结果错误的解决方案
  8. Adopt Open JDK官方文档(五) Docker镜像
  9. centos7 更新php版本,Centos7升级php版本到php7实例分享
  10. html表格边框去重复,css怎么解决表格边框线重复问题.
  11. 流体连续性方程【The Equation of Continuity】
  12. python当前运行目录_Python获取运行目录与当前脚本目录的方法
  13. include指令包含网站banner和版权信息栏
  14. 计算机所有接口都没反应,如何解决Win7系统USB接口没反应的问题
  15. 【正点原子STM32连载】第二十一章 通用定时器实验 摘自【正点原子】MiniPro STM32H750 开发指南_V1.1
  16. 思科网络技术学院2002年会(上)(转)
  17. 嵌入式Linux添加ssh服务
  18. 2018数据智能生态系统峰会举办在即
  19. 2021年安全员-B证考试题及安全员-B证考试题库
  20. c语言烟花程序视频,如何用c语言编写动态烟花

热门文章

  1. 【专题教程第4期】SEGGER的J-Scope波形上位机软件,HSS模式简单易用,无需额外资源,也不需要写目标板代码
  2. 为什么outlook不能改成HTML格式,如何解决Outlook 2016中的HTML格式问题
  3. BugkuCTF-MISC-图穷匕见
  4. mbedtls 连接 阿里云物联网
  5. 什么是 Pandas?
  6. 校验码 汉明码 CRC码
  7. OpenAI-ChatGPT最新官方接口《审核机制》全网最详细中英文实用指南和教程,助你零基础快速轻松掌握全新技术(七)(附源码)
  8. C语言实现幅值减小的正弦波,实现PMSM电机正弦电压控制的理想低成本解决方案...
  9. 前端学习——JSON格式详解
  10. SAP FICO 解析成本要素类别