답안 #1027349

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1027349 2024-07-19T03:54:53 Z vjudge1 One-Way Streets (CEOI17_oneway) C++17
100 / 100
208 ms 44228 KB
#include<bits/stdc++.h>
using namespace std;
set<pair<int, int>> edges;
map<pair<int, int>, int> f;
map<pair<int, int>, pair<int, int>> edge_tree_to_graph;
vector<pair<int, int>> bridges;
vector<int> ad[100005], ad2[100005];
int low[100005], num[100005], c[100005], dp[100005], ans[100005], dfsc = 0, color = 0;
//0 = B; 1 = L; 2 = R;
bool visited[100005];
inline pair<int, int> edgeCorrection(pair<int, int> x)
{
    if(edges.count(x) == 0) swap(x.first, x.second);
    return x;
}
void dfs(int u, int par)
{
    low[u] = num[u] = ++dfsc;
    bool avoidpar = 1;
    for(int v : ad[u]){
        if(v != par || avoidpar == 0){
            if(num[v] > 0) low[u] = min(low[u], num[v]);
            else{
                dfs(v, u);
                low[u] = min(low[u], low[v]);
            }
        }
        else avoidpar = 0;
    }
}
void dfscolor(int u, int co)
{
    //cout<<"A"<<u<<" "<<color<<endl;
    visited[u] = 1; c[u] = co;
    for(int v : ad[u]) if(visited[v] == 0){
        if(low[v] == num[v]){
            dfscolor(v, ++color);
            bridges.push_back(edgeCorrection({u, v}));
        }
        else dfscolor(v, co);
    }
}
void dfsdirect(int u)
{
    visited[u] = 1;
    for(int v : ad2[u]) if(visited[v] == 0){
        //if(u == c[4]) cout<<"B"<<v<<endl;
        dfsdirect(v); dp[u] += dp[v];
        if(dp[v] != 0){
            //if(v == c[4]) cout<<"B"<<u<<" "<<v<<endl;
            pair<int, int> thisedge = edge_tree_to_graph[{u, v}];
            if(dp[v] > 0){
                if(c[thisedge.first] == u) ans[f[thisedge]] = 1;
                else ans[f[thisedge]] = 2;
            }
            else{
                if(c[thisedge.first] == u) ans[f[thisedge]] = 2;
                else ans[f[thisedge]] = 1;
            }
        }
    }
    //cout<<u<<" "<<dp[u]<<endl;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n, m;
    cin>>n>>m;
    for(int i = 1; i <= m; i++){
        int u, v;
        cin>>u>>v;
        if(u == v) continue;
        ad[u].push_back(v);
        ad[v].push_back(u);
        edges.insert({u, v});
        f[{u, v}] = i;
    }
    for(int i = 1; i <= n; i++) if(num[i] == 0){
        dfs(i, -1);
        color++;
        dfscolor(i, color);
    }
    for(pair<int, int> i : bridges){
        ad2[c[i.first]].push_back(c[i.second]);
        ad2[c[i.second]].push_back(c[i.first]);
        edge_tree_to_graph[{c[i.first], c[i.second]}] = i;
        edge_tree_to_graph[{c[i.second], c[i.first]}] = i;
    }
    int p; cin>>p;
    for(int i = 1; i <= p; i++){
        int u, v;
        cin>>u>>v;
        dp[c[u]]++; dp[c[v]]--;
    }
    //cout<<"A"<<dp[c[4]]<<endl;
    memset(visited, 0, sizeof(visited));
    for(int i = 1; i <= color; i++) if(visited[i] == 0){
        dfsdirect(i);
    }
    for(int i = 1; i <= m; i++){
        if(ans[i] == 0) cout<<"B";
        else if(ans[i] == 1) cout<<"L";
        else cout<<"R";
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 7004 KB Output is correct
2 Correct 1 ms 7004 KB Output is correct
3 Correct 1 ms 7260 KB Output is correct
4 Correct 2 ms 7260 KB Output is correct
5 Correct 2 ms 7260 KB Output is correct
6 Correct 1 ms 7004 KB Output is correct
7 Correct 2 ms 7260 KB Output is correct
8 Correct 2 ms 7260 KB Output is correct
9 Correct 1 ms 7260 KB Output is correct
10 Correct 2 ms 7260 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 7004 KB Output is correct
2 Correct 1 ms 7004 KB Output is correct
3 Correct 1 ms 7260 KB Output is correct
4 Correct 2 ms 7260 KB Output is correct
5 Correct 2 ms 7260 KB Output is correct
6 Correct 1 ms 7004 KB Output is correct
7 Correct 2 ms 7260 KB Output is correct
8 Correct 2 ms 7260 KB Output is correct
9 Correct 1 ms 7260 KB Output is correct
10 Correct 2 ms 7260 KB Output is correct
11 Correct 64 ms 20944 KB Output is correct
12 Correct 73 ms 21880 KB Output is correct
13 Correct 77 ms 23016 KB Output is correct
14 Correct 89 ms 26500 KB Output is correct
15 Correct 94 ms 27596 KB Output is correct
16 Correct 137 ms 37832 KB Output is correct
17 Correct 172 ms 40104 KB Output is correct
18 Correct 147 ms 38100 KB Output is correct
19 Correct 152 ms 41672 KB Output is correct
20 Correct 71 ms 21236 KB Output is correct
21 Correct 71 ms 20884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 7004 KB Output is correct
2 Correct 1 ms 7004 KB Output is correct
3 Correct 1 ms 7260 KB Output is correct
4 Correct 2 ms 7260 KB Output is correct
5 Correct 2 ms 7260 KB Output is correct
6 Correct 1 ms 7004 KB Output is correct
7 Correct 2 ms 7260 KB Output is correct
8 Correct 2 ms 7260 KB Output is correct
9 Correct 1 ms 7260 KB Output is correct
10 Correct 2 ms 7260 KB Output is correct
11 Correct 64 ms 20944 KB Output is correct
12 Correct 73 ms 21880 KB Output is correct
13 Correct 77 ms 23016 KB Output is correct
14 Correct 89 ms 26500 KB Output is correct
15 Correct 94 ms 27596 KB Output is correct
16 Correct 137 ms 37832 KB Output is correct
17 Correct 172 ms 40104 KB Output is correct
18 Correct 147 ms 38100 KB Output is correct
19 Correct 152 ms 41672 KB Output is correct
20 Correct 71 ms 21236 KB Output is correct
21 Correct 71 ms 20884 KB Output is correct
22 Correct 208 ms 40136 KB Output is correct
23 Correct 194 ms 37824 KB Output is correct
24 Correct 187 ms 37824 KB Output is correct
25 Correct 203 ms 44228 KB Output is correct
26 Correct 190 ms 39496 KB Output is correct
27 Correct 206 ms 37888 KB Output is correct
28 Correct 40 ms 9296 KB Output is correct
29 Correct 91 ms 20936 KB Output is correct
30 Correct 82 ms 21072 KB Output is correct
31 Correct 83 ms 21328 KB Output is correct
32 Correct 90 ms 26728 KB Output is correct