涉及知识点:图的遍历。
我们观察样例可以发现,染色之后的图是一颗树,而且还是 dfs
树。
题目要求所以路径上的颜色都是交替的,所以直接交替染色即可。
注意:建图的时候需要记录当前边的编号。
代码
#include <bits/stdc++.h>
#define int long long
#define ll __int128
#define db double
#define ldb long double
#define vo void
#define endl '\n'
#define il inline
#define re register
#define ve vector
#define p_q priority_queue
#define PII pair<int, int>
using namespace std;
//#define O2 1
#ifdef O2
#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3, "Ofast", "inline")
#endif
namespace OI {
template <typename T>
il T read() {
T x = 0, f = 1;
int ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = (x << 3) + (x << 1) + (ch ^ 48);
ch = getchar();
}
return x * f;
}
template <typename TE>
il void write(TE x) {
if (x < 0) {
x = -x;
putchar('-');
}
TE sta[35];
int top = 0;
do {
sta[top++] = x % 10, x /= 10;
} while (x);
while (top) putchar(sta[--top] + '0');
}
il string read_with_string() {
string s = "";
char ch = getchar();
while ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9')) {
s += ch;
ch = getchar();
}
return s;
}
il void write_with_string(string s) {
for (int i = 0; i < s.size(); i ++ ) putchar(s[i]);
}
}
namespace COMB {
int fact[200000];
int Triangle[1010][1010];
void Fact(int n, int mod) {
fact[0] = 1;
for (int i = 1; i <= n; i ++ ) fact[i] = ((fact[i - 1]) % mod * (i % mod)) % mod;
}
void Pascal_s_triangle(int n, int mod) {
for (int i = 0; i <= n; i ++ ) Triangle[i][0] = 1;
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= i; j ++ )
Triangle[i][j] = (Triangle[i - 1][j] + Triangle[i - 1][j - 1]) % mod;
}
int pw(int x, int y, int mod) {
int res = 1;
while (y) {
if (y & 1) res = ((res % mod) * (x % mod)) % mod;
x = (x % mod) * (x % mod) % mod;
y >>= 1;
}
return res;
}
int pw(int x, int y) {
int res = 1;
while (y) {
if (y & 1) res *= x;
x *= x;
y >>= 1;
}
}
int GCD(int x, int y, int mod) {
return __gcd(x, y) % mod;
}
int LCM(int x, int y, int mod) {
return (((x % mod) * (y % mod)) % mod / (GCD(x, y, mod) % mod)) % mod;
}
int C(int n, int m, int mod) {
if (m > n || m < 0) return 0;
return fact[n] * pw(fact[m], mod - 2, mod) % mod * pw(fact[n - m], mod - 2, mod) % mod;
}
int Ask_triangle(int x, int y) {
return Triangle[x][y];
}
}
using namespace OI;
using namespace COMB;
//#define fre 1
#define IOS 1
//#define multitest 1
const int N = 2e5 + 10;
const int M = 4e5 + 10;
const int inf = 1e12;
int n, m;
int color[N];
int f[N];
ve<PII> g[N];
il void Init() {
memset(color, -1, sizeof color);
cin >> n >> m;
for (int i = 1; i <= m; i ++ ) {
int x, y;
cin >> x >> y;
g[x].push_back({y, i});
g[y].push_back({x, i});
}
}
il void dfs(int u, int cl) {
f[u] = 1;
for (int i = 0; i < g[u].size(); i ++ ) {
int v = g[u][i].first;
if (f[v]) continue;
color[g[u][i].second] = cl % 2;
dfs(v, cl + 1);
}
}
il void Solve() {
for (int i = 1; i <= n; i ++ )
if (!f[i])
dfs(i, 0);
for (int i = 1; i <= m; i ++ ) {
if (color[i] == -1) cout << "G";
else if (color[i] == 0) cout << "B";
else cout << "R";
}
}
signed main() {
int T;
#ifdef IOS
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#endif
#ifdef fre
freopen(".in", "r", stdin);
freopen(".out", "w", stdout);
#endif
#ifdef multitest
cin >> T;
#else
T = 1;
#endif
while (T--) {
Init();
Solve();
}
return 0;
}
/*
*/
标签:ch,return,int,题解,S4,2024,while,define,mod
From: https://www.cnblogs.com/zla2012/p/18502117