답안 #469090

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
469090 2021-08-30T16:35:55 Z wdjpng One-Way Streets (CEOI17_oneway) C++17
100 / 100
322 ms 52440 KB
#include <bits/stdc++.h>
//#define double double
#define int long long
#define rep(i,n) for(int i = 0; i < n; i++)
#define all(a) a.begin(), a.end()

using namespace std;

struct edge{
    int b=1;
    int rev=0;
    int i=1;
};

int t,m;
vector<vector<edge>>E;
vector<int>pre, low;
vector<vector<int>>low_constrained;
vector<vector<vector<int>>>rests;
vector<int>orient;

int counter=0;
void dfs(int v, int i)
{
    pre[v]=counter++;
    low[v]=pre[v];
    rep(i,2) low_constrained[i][v] = pre[v];

    for(edge w : E[v])
    {
        if(w.i==i) continue;
        if(pre[w.b]!=-1) {low[v]=min(low[w.b], low[v]); continue;}
        dfs(w.b, w.i);
        if(low[w.b]==pre[w.b]) // edge w is a bridge
        {
            rep(i,2) {
                if(low_constrained[i][w.b]<=pre[v]) 
                    orient[w.i] = (w.rev+i+1) % 2;
            }
        }
        low[v]=min(low[w.b], low[v]);
        rep(i,2) low_constrained[i][v] = min(low_constrained[i][v], low_constrained[i][w.b]);
    }

    rep(i,2)
    {
        for(int w : rests[i][v])
        {
            low_constrained[i][v] = min(low_constrained[i][w], low_constrained[i][v]);
        }
    }
}

int c = 0;
vector<int>preord, postord;
vector<vector<int>>up;


bool isanc(int w, int v)
{
    return preord[w]>=preord[v]&&postord[w]<=postord[v];
}

int lca(int a, int b)
{
    if(isanc(a,b)) return b;
    if(isanc(b,a)) return a;
    for(int i = 19; i>= 0; i--) if(!isanc(a, up[b][i])) b = up[b][i];
    return up[b][0];
}

vector<bool>vis(1e5+1);
void dfs2(int v, int p)
{
    vis[v]=true;
    preord[v]=c++;
    up[v][0]=p;
    for(auto w : E[v])
    {
        if(!vis[w.b]) dfs2(w.b,v);
    }
    postord[v]=c++;
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    
    int n,m;
    cin>>n>>m;

    E.resize(n);
    rep(i, m)
    {
        int a,b;
        cin>>a>>b;
        a--; b--;
        E[a].push_back({b, 0, i});
        E[b].push_back({a, 1, i});
    }

    pre.assign(n, -1);
    low.assign(n, -1);
    low_constrained.assign(2, vector<int>(n, 1e16));
    orient.assign(m, 2);
    rests.assign(2, vector<vector<int>>(n));
    up.assign(n, vector<int>(20));
    preord.assign(n,-1);
    postord.assign(n,-2);

    dfs2(0, 0);
    for(int i = 1; i < 20;i++)
    {
        rep(j,n) up[j][i] = up[up[j][i-1]][i-1];
    }
    int p;
    cin>>p;
    rep(i,p)
    {
        int a,b;
        cin>>a>>b;
        a--; b--;
        int c = lca(a,b);
        rests[0][a].push_back(c);
        rests[0][c].push_back(b);
        
        rests[1][b].push_back(c);
        rests[1][c].push_back(a);
    }

    rep(i, n)
    {
        if(pre[i]!=-1) continue;
        dfs(i, -1);
    }

    for(int o : orient)
    {
        if(o==0) cout<<"R";
        if(o==1) cout<<"L";
        if(o==2) cout<<"B";
    }
    cout<<"\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 588 KB Output is correct
4 Correct 1 ms 716 KB Output is correct
5 Correct 2 ms 716 KB Output is correct
6 Correct 1 ms 588 KB Output is correct
7 Correct 1 ms 716 KB Output is correct
8 Correct 1 ms 716 KB Output is correct
9 Correct 1 ms 588 KB Output is correct
10 Correct 1 ms 460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 588 KB Output is correct
4 Correct 1 ms 716 KB Output is correct
5 Correct 2 ms 716 KB Output is correct
6 Correct 1 ms 588 KB Output is correct
7 Correct 1 ms 716 KB Output is correct
8 Correct 1 ms 716 KB Output is correct
9 Correct 1 ms 588 KB Output is correct
10 Correct 1 ms 460 KB Output is correct
11 Correct 62 ms 16268 KB Output is correct
12 Correct 75 ms 20984 KB Output is correct
13 Correct 102 ms 28064 KB Output is correct
14 Correct 138 ms 37340 KB Output is correct
15 Correct 175 ms 40124 KB Output is correct
16 Correct 145 ms 40204 KB Output is correct
17 Correct 145 ms 42216 KB Output is correct
18 Correct 162 ms 40180 KB Output is correct
19 Correct 134 ms 43588 KB Output is correct
20 Correct 91 ms 26240 KB Output is correct
21 Correct 79 ms 25848 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 588 KB Output is correct
4 Correct 1 ms 716 KB Output is correct
5 Correct 2 ms 716 KB Output is correct
6 Correct 1 ms 588 KB Output is correct
7 Correct 1 ms 716 KB Output is correct
8 Correct 1 ms 716 KB Output is correct
9 Correct 1 ms 588 KB Output is correct
10 Correct 1 ms 460 KB Output is correct
11 Correct 62 ms 16268 KB Output is correct
12 Correct 75 ms 20984 KB Output is correct
13 Correct 102 ms 28064 KB Output is correct
14 Correct 138 ms 37340 KB Output is correct
15 Correct 175 ms 40124 KB Output is correct
16 Correct 145 ms 40204 KB Output is correct
17 Correct 145 ms 42216 KB Output is correct
18 Correct 162 ms 40180 KB Output is correct
19 Correct 134 ms 43588 KB Output is correct
20 Correct 91 ms 26240 KB Output is correct
21 Correct 79 ms 25848 KB Output is correct
22 Correct 310 ms 48252 KB Output is correct
23 Correct 274 ms 46228 KB Output is correct
24 Correct 285 ms 46144 KB Output is correct
25 Correct 309 ms 52440 KB Output is correct
26 Correct 322 ms 48032 KB Output is correct
27 Correct 294 ms 46460 KB Output is correct
28 Correct 47 ms 12320 KB Output is correct
29 Correct 232 ms 31560 KB Output is correct
30 Correct 217 ms 31808 KB Output is correct
31 Correct 217 ms 32424 KB Output is correct
32 Correct 233 ms 38704 KB Output is correct