AtCoder 2362

Splatter Painting

大致题意:
给一个 n n n 个点 m m m 条边的无向图,有 q q q 次操作 第 i i i 次操作,给出 v , d , c , v,d,c, vdc把所有到点 v v v 的距离不超过 d d d 的点都染上颜色 c c c 问最后每个点的颜色 n , m , q , c < = 100000 , d < = 10. n, m, q, c <= 100000, d <= 10. n,m,q,c<=100000,d<=10.

解题报告:
考虑最终颜色显然逆向操作
直接上 d f s dfs dfs 加上一些剪枝。
代码展示:

#include<bits/stdc++.h>
#define LL  long long
#define pii pair<LL,int>
#define all(x) x.begin(),x.end()
#define wp(x) write(x),putchar('\n')
#define wpl(x) write(x),putchar(' ')
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int maxn = 5e5 + 5;
const int MOD = 998244353;

inline int read() {
	int s = 0, f = 1;
	char  ch = getchar();
	while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
	while (ch >= '0' && ch <= '9') {s = (s << 1) + (s << 3) + ch - '0'; ch = getchar();}
	return s * f;
}
inline void write(LL x) {
	if (x < 0) putchar('-'), x = -x;
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}
struct TMD {
	int to, next;
} edge[maxn << 1];
int head[maxn], cnt, n, ans[maxn], dis[maxn];
void add(int from, int to) {
	edge[++cnt] = {to, head[from]}; head[from] = cnt;
}
struct node {
	int x, y, z;
} p[maxn];
void dfs(int x, int y, int z) {
	if (!ans[x]) ans[x] = z;// 未染色过
	if (y == 0 || dis[x] >= y) return; // 单次染色到了终点或者之前
	dis[x] = y;//更新从x点开始访问过最远的距离
	for (int i = head[x]; i; i = edge[i].next) {
		int to = edge[i].to;
		dfs(to, y - 1, z);
	}
}
int main() {
	int n = read(), m = read(), q, u, v;
	for (int i = 1; i <= m; i++) {
		u = read(); v = read();
		add(u, v); add(v, u);
	}
	q = read();
	for (int i = 1; i <= q; i++) p[i] = {read(), read(), read()};
	for (int i = q; i >= 1; i--) {
		dfs(p[i].x, p[i].y, p[i].z);
	}
	for (int i = 1; i <= n; i++) printf("%d\n", ans[i]);
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值