stcoder Splatter Painting 记忆化搜索

5647: Splatter Painting

时间限制: 2 Sec  内存限制: 256 MB
提交: 32  解决: 16
[提交] [状态] [讨论版] [命题人:admin]

题目描述

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,vi≤N
ai≠bi
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 is given from Standard Input in the following format:

N M
a1 b1
:
aM bM
Q
v1 d1 c1
:
vQ dQ cQ

 

输出

Print the answer in N lines. In the i-th line, print the color of vertex i after the Q operations.

 

样例输入

复制样例数据

7 7
1 2
1 3
1 4
4 5
5 6
5 7
2 3
2
6 1 1
1 2 2

样例输出

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 1, 2, 3, 4 and 5 are repainted in color 2.

stcoder Splatter Painting 记忆化搜索_第1张图片

题意:给一个图,按照要求将点v d距离之内的点染c色,染过色的则覆盖,最后求每个点的颜色

因为d最大是10,所以用一个二维数组ans[ v ][ d ] 来表示距离v点d距离的点所染颜色

因为颜色课覆盖,所以倒着染色,染过色的则不需要再考虑

代码:

#include 

typedef long long ll;
using namespace std;
const int maxn = 1e5 + 100;
vector s[maxn];
int v[maxn], d[maxn], c[maxn];
int ans[maxn][20];

void fun(int v, int d, int c) {
    if (d == -1) return;
    if (ans[v][d]) return; //染过色的不用考虑
    ans[v][d] = c;
    for (int i = 0; i < s[v].size(); i++) {
        fun(s[v][i], d - 1, c);
    }
}

int main() {
    //freopen("out.txt", "r", stdin);
    int n, m;
    scanf("%d%d", &n, &m);
    int a, b;
    for (int i = 1; i <= m; i++) {
        scanf("%d%d", &a, &b);
        s[a].push_back(b);
        s[b].push_back(a);
    }
    for (int i = 1; i <= n; i++)
        s[i].push_back(i);  //加上自己这个点,才可以给这个点染色
    int q;
    scanf("%d", &q);
    for (int i = 1; i <= q; i++)
        scanf("%d%d%d", &v[i], &d[i], &c[i]);
    for (int i = q; i > 0; i--)
        fun(v[i], d[i], c[i]);
    for (int i = 1; i <= n; i++)
        printf("%d\n", ans[i][0]);
    return 0;
}

 

你可能感兴趣的:(ACM)