AtCoder Grand Contest 012 B

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

2ab7e180230b159d42d35ea7e555b3b0.png

 


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.

题意:可以参考图,问你最后的染色情况

解法:因为后面的染色会覆盖前面的,我们就倒过来处理,另外保存每个点的范围

比如1-2-3-4-5-6,

我们从3处理,距离是2

3的处理范围2

2的处理范围1

1的处理范围0

4的处理范围1

5的处理范围0

就是说1,5已经到染色边界了

那么每次染色我们都比较上一次这个点的处理范围,比这一次的大,说明一定会被上一次的覆盖,没必要遍历下去了,或者处理没有染色的部分

 1 #include
 2 using namespace std;
 3 #define ll long long
 4 vector<int>q[200000];
 5 int color[200000];
 6 int flag[200000];
 7 int n,m;
 8 int v[200000],d[200000],c[200000];
 9 void dfs(int x,int cnt,int c)
10 {
11    if(!color[x])
12    {
13        color[x]=c;
14    }
15    if(flag[x]>=cnt)
16    {
17        return;
18    }
19    if(cnt==0)
20    {
21        return;
22    }
23    flag[x]=cnt;
24    for(int i=0;i)
25    {
26        dfs(q[x][i],cnt-1,c);
27    }
28 }
29 int main()
30 {
31     std::ios::sync_with_stdio(false);
32     cin>>n>>m;
33     for(int i=1;i<=m;i++)
34     {
35         int x,y;
36         cin>>x>>y;
37         q[x].push_back(y);
38         q[y].push_back(x);
39     }
40     int q;
41     cin>>q;
42     for(int i=1;i<=q;i++)
43     {
44        cin>>v[i]>>d[i]>>c[i];
45     }
46     for(int i=q;i>=1;i--)
47     {
48         dfs(v[i],d[i],c[i]);
49     }
50     for(int i=1;i<=n;i++)
51     {
52         cout<endl;
53     }
54     return 0;
55 }

 

 

 

转载于:https://www.cnblogs.com/yinghualuowu/p/6659964.html

你可能感兴趣的:(AtCoder Grand Contest 012 B)