Atcoder Grand Contest 012 B - Splatter Painting解题报告

xiaoxiao2021-02-28  96

题目:http://agc012.contest.atcoder.jp/tasks/agc012_b

有一个n点m边的图,(不一定联通) 还有q个操作:每次将一个点v及其周围距离<=d的点涂成颜色c(可以覆盖) 现在询问每个点的颜色

1≤N,M,Q≤10^5 1≤ai,bi,vi≤N ai≠bi 0≤di≤10 1≤ci≤105 无自环,无重边

dfs在最坏情况下耗时10^5*10^5 = 10^10

此时可以倒序涂色,不覆盖颜色,并开一个记录数组防止无效访问,这样的话,每个点最多只会被访问10次 时间复杂度:O(10n) O(能过)

贴代码:(有点卡时限)

#include <cstdio> #define N 200100 int n,m,q,i,dep[N],col[N],tot; bool vis[N]; struct C {int v,d,c;} c[N]; struct node {int to; node* nxt;} g[N],*last[N]; inline void read(int &p) { char c = getchar(); p = 0; while (c < '0' || c > '9') c = getchar(); while (c >= '0' && c <= '9') p = p*10+c-48,c=getchar(); } void dfs(int v,int d,int c) { vis[v] = true; if (!col[v]) col[v] = c; dep[v] = d; for (node *p=last[v];p;p=p->nxt) if (dep[p->to] < d-1 && !vis[p->to]) dfs(p->to,d-1,c); vis[v] = false; } int main() { read(n); read(m); for (i=0;i<m;i++) { int a,b; read(a); read(b); last[a] = &(g[tot++] = (node){b,last[a]}); last[b] = &(g[tot++] = (node){a,last[b]}); } read(q); for (i=0;i<q;i++) read(c[i].v),read(c[i].d),read(c[i].c); while (q--) dfs(c[q].v,c[q].d+1,c[q].c); for (i=1;i<=n;i++) printf("%d\n",col[i]); }
转载请注明原文地址: https://www.6miu.com/read-57714.html

最新回复(0)