P4652

Sol

保证有解,这题就会方便很多。

不难注意到两端在同个双连通分量的边定向一定任意,所以我们可以先缩边双,然后考虑缩点后的树即可。

考虑一种差分(边权差分),d[s]++,d[t]--,考虑这种差分会影响到哪些点的权值,会让 sLCA(a,b) 边权 +1,让 LCA(a,b)b 边权 1,那么此时就可以根据任意一条边的正负来判断方向,如果边权为 0 则任意。

Code

#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define pf push_front
#define desktop "C:\\Users\\incra\\Desktop\\"
#define IOS ios :: sync_with_stdio (false),cin.tie (0),cout.tie (0)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair <int,int> PII;
const int dx[] = {1,0,-1,0},dy[] = {0,-1,0,1};
template <typename T1,typename T2> bool tomax (T1 &x,T2 y) {
	if (y > x) return x = y,true;
	return false;
}
template <typename T1,typename T2> bool tomin (T1 &x,T2 y) {
	if (y < x) return x = y,true;
	return false;
}
LL power (LL a,LL b,LL p) {
	LL ans = 1;
	while (b) {
		if (b & 1) ans = ans * a % p;
		a = a * a % p;
		b >>= 1;
	}
	return ans;
}
int fastio = (IOS,0);
#define endl '\n'
#define puts(s) cout << (s) << endl
const int N = 100010;
int n,m,q;
PII e[N];
vector <PII> g[N];
int dfn[N],low[N],timestamp;
int stk[N],top;
int id[N],dcc_cnt;
vector <int> ng[N];
int dep[N],s[N];
bool vis[N];
void tarjan (int u,int fa) {
    dfn[u] = low[u] = ++timestamp;
    stk[++top] = u;
    for (auto [v,id] : g[u]) {
        if (id == fa) continue;
        if (!dfn[v]) {
            tarjan (v,id);
            tomin (low[u],low[v]);
        }
        else tomin (low[u],dfn[v]);
    }
    if (low[u] == dfn[u]) {
        int v;
        dcc_cnt++;
        do {
            v = stk[top--];
            id[v] = dcc_cnt;
        }
        while (v != u) ;
    }
}
void DFS (int u,int fa) {
    vis[u] = 1;
    for (int v : ng[u]) {
        if (v == fa) continue;
        dep[v] = dep[u] + 1;
        DFS (v,u);
        s[u] += s[v];
    }
}
void mian () {
	cin >> n >> m;
    for (int i = 1;i <= m;i++) {
        int a,b;
        cin >> a >> b;
        e[i] = {a,b};
        g[a].pb ({b,i}),g[b].pb ({a,i});
    }
    for (int i = 1;i <= n;i++) if (!dfn[i]) tarjan (i,-1);
    for (int i = 1;i <= m;i++) {
        int a,b;
        tie (a,b) = e[i];
        a = id[a],b = id[b];
        if (a == b) continue;
        ng[a].pb (b),ng[b].pb (a);
    }
    cin >> q;
    for (int i = 1;i <= q;i++) {
        int a,b;
        cin >> a >> b;
        a = id[a],b = id[b];
        if (a == b) continue;
        s[a]++,s[b]--;
    }
    for (int i = 1;i <= dcc_cnt;i++) {
        if (!vis[i]) DFS (i,-1);
    }
    for (int i = 1;i <= m;i++) {
        int a = id[e[i].x],b = id[e[i].y];
        if (a == b) {
            cout << 'B';
            continue;
        }
        if (dep[a] > dep[b]) {
            if (s[a] > 0) cout << 'R';
            else if (!s[a]) cout << 'B';
            else cout << 'L';
        }
        else {
            if (s[b] > 0) cout << 'L';
            else if (!s[b]) cout << 'B';
            else cout << 'R';
        }
    }
    cout << endl;
}
int main () {
	int T = 1;
	// cin >> T;
	while (T--) mian ();
	return 0;
}
posted @   incra  阅读(6)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示