LED-led Paths

题目链接http://acm.csu.edu.cn:20080/csuoj/problemset/problem?pid=2314

Time Limit: 3 Sec Memory Limit: 512 Mb

Description

In a large city there are n junctions and m one-way streets connecting those junctions. It is known that one can’t walk along the streets infinitely, as there is no directed cycle of streets in the city.

The tourism department has decided to install colored illumination using the LED (light-emitting diode) technology. Each street is to be illuminated with either red ®, green (G), or blue (B) color.

The official city maps for tourists will indicate the selected colors. All continuous paths illuminated by the same color will be suggested for walking and sightseeing. As LEDs will lead people along beautiful ways, these paths will be called LED-led paths.

The health department says that if there is a very long single-color LED-led path, some tourists might overestimate their strength, walk for too long, get too tired, and dislike the city.

Propose a three-color illumination plan in which no single-color LED-led path consists of more than 42 streets.

Input

The first line of the input contains two integers n and m — the number of junctions and streets, respectively (2 ≤ n ≤ 50 000; 1 ≤ m ≤ 200 000).

Each of the following m lines contains two integers ui and vi, denoting a one-way street from junction ui to junction vi (1 ≤ ui, vi ≤ n; ui ≠ vi).

The given city is guaranteed to be acyclic. Each pair of junctions is connected by at most one street.

Output

For each street, in the order of input, output its color on a separate line — a single letter R, G, or B.

Sample Input

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

Sample Output

B
R
G
R
B
G

Hint
In this example, all single-color LED-led paths are not longer than one street (and therefore not longer than 42 streets), which is absolutely OK according to the health department.


题目大意:给每个街道染色,其中规定不得有连续超过42条街道是同一种颜色,你只有R,G,B三种颜色。
给你n,m(街道数和相连的数目)

。。。这题倒不是蒟蒻的我做的,我也就只能刷刷前面的几题水题。。。
就直接贴代码了。。。。

#include<bits/stdc++.h>
#define fo(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=5e4+5, maxm=2e5+5;
int n,m;
char clr[3]= {'R','B','G'};
int tot,go[maxm],fro[maxm],nxt[maxm],f1[maxn],com[maxn];
void ins(int x,int y) {
	go[++tot]=y;
	fro[tot]=x;
	nxt[tot]=f1[x];
	f1[x]=tot;
	com[y]++;
}
int d[maxn],dis[maxn];
void topo() {
	int j=0;
	fo(i,1,n) if (!com[i]) {
		d[++j]=i;
		dis[i]=1;
	}
	for(int i=1; i<=j; i++) {
		for(int p=f1[d[i]]; p; p=nxt[p]) {
			dis[go[p]]=max(dis[go[p]],dis[d[i]]+1);
			if (--com[go[p]]==0) d[++j]=go[p];
		}
	}
}
int main() {
	scanf("%d %d",&n,&m);
	fo(i,1,m) {
		int x,y;
		scanf("%d %d",&x,&y);
		ins(x,y);
	}
	topo();
	fo(i,1,m) {
		int w=0;
		for(int x=dis[fro[i]]^dis[go[i]]; x; x>>=1, w++);
		printf("%c\n",clr[w%3]);
	}
}