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

题目: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]);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值