답안 #578037

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
578037 2022-06-15T19:46:34 Z vladislav11 One-Way Streets (CEOI17_oneway) C++14
100 / 100
266 ms 34624 KB
#include <bits/stdc++.h>

using namespace std;

int n, m, p, ttime;
vector< vector< pair<int,int> > > grp;
vector< pair<int,int> > ribs;

vector< pair<int,int> > a;
vector<int> used, tin, tup, brg;

void dfs_time ( int v, int pind )
{
    used[v] = 1;
    tin[v] = tup[v] = ttime++;

    for ( auto& [to,ind] : grp[v] )
    if ( ind != pind )
    {
        if ( !used[to] )
        {
            dfs_time( to, ind );

            if ( tup[to] > tin[v] )
                brg[ind] = 1;

            tup[v] = min( tup[v], tup[to] );
        }
        else
            tup[v] = min( tup[v], tin[to] );
    }
}

vector< vector<int> > tree;
vector<int> color;
int ccol;

void dfs_color ( int v, int cl )
{
    color[v] = cl;

    for ( auto& [to,ind] : grp[v] )
    if ( !brg[ind] && !color[to] )
        dfs_color( to, cl );
}

vector< vector<int> > up;
vector<int> h, ctin, ctout;

void dfs_up ( int v, int p )
{
    ctin[v] = ttime++;

    up[v][0] = p;

    for ( int i=1; i<21; i++ )
    if ( up[v][i-1] != -1 )
        up[v][i] = up[ up[v][i-1] ][i-1];

    for ( auto& to : tree[v] )
    if ( to != p )
    {
        h[to] = h[v] + 1;

        dfs_up( to, v );
    }

    ctout[v] = ttime++;
}

int lca ( int u, int v )
{
    if ( u == v )
        return u;

    if ( h[u] > h[v] )
        swap( u, v );

    for ( int i=20; i>=0; i-- )
    if ( up[v][i] != -1 )
    {
        int to = up[v][i];

        if ( ctin[to] <= ctin[u] && ctout[to] >= ctout[u] )
            continue;

        v = to;
    }

    return up[v][0];
}

vector<int> rib_dir;

int dfs_dir ( int v, int p )
{
    for ( auto& to : tree[v] )
    if ( to != p )
        rib_dir[v] += dfs_dir( to, v );

    return rib_dir[v];
}

int main ()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> m;

    grp.assign( n+1, vector< pair<int,int> >() );

    for ( int i=0; i<m; i++ )
    {
        int u, v;
        cin >> u >> v;

        grp[u].push_back({ v, i });
        grp[v].push_back({ u, i });

        ribs.push_back({ u, v });
    }

    cin >> p;

    a.resize( p );

    for ( auto& [x,y] : a )
        cin >> x >> y;

    used = tin = tup = vector<int>( n+1, 0 );
    brg.assign( m, 0 );

    for ( int i=1; i<=n; i++ )
    if ( !used[i] )
        dfs_time( i, -1 );

    color.assign( n+1, 0 );

    for ( int i=1; i<=n; i++ )
    if ( !color[i] )
        dfs_color( i, ++ccol );

    /*for ( int i=1; i<=n; i++ )
        cout << color[i] << ' ';
    cout << '\n';*/

    tree.assign( ccol+1, vector<int>() );

    for ( int i=1; i<=n; i++ )
    for ( auto& [to,ind] : grp[i] )
    if ( i < to && brg[ind] )
    {
        tree[ color[i] ].push_back( color[to] );
        tree[ color[to] ].push_back( color[i] );
    }

    /*cout << "tree:\n";
    for ( int i=1; i<=ccol; i++ )
    {
        cout << i << ": ";

        for ( auto& to : tree[i] )
            cout << to << ' ';
        cout << '\n';
    }*/

    up.assign( ccol+1, vector<int>( 21, -1 ) );
    h = ctin = ctout = vector<int>( ccol+1, 0 );

    dfs_up( 1, -1 );

    rib_dir.assign( ccol+1, 0 );

    for ( auto& [x,y] : a )
    {
        x = color[x];
        y = color[y];

        if ( x == y )
            continue;

        int w = lca( x, y );

        rib_dir[x]++;
        rib_dir[w]--;

        rib_dir[y]--;
        rib_dir[w]++;
    }

    dfs_dir( 1, -1 );

    for ( auto& [x,y] : ribs )
    {
        if ( color[x] == color[y] )
            cout << 'B';

        else
        {
            x = color[x];
            y = color[y];

            int w = h[x] < h[y] ? y : x;

            if ( rib_dir[w] < 0 )
                cout << ( w == y ? 'R' : 'L' );

            else if ( rib_dir[w] > 0 )
                cout << ( w == y ? 'L' : 'R' );

            else
                cout << 'B';
        }
    }

    return 0;
}

Compilation message

oneway.cpp: In function 'void dfs_time(int, int)':
oneway.cpp:17:17: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   17 |     for ( auto& [to,ind] : grp[v] )
      |                 ^
oneway.cpp: In function 'void dfs_color(int, int)':
oneway.cpp:42:17: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   42 |     for ( auto& [to,ind] : grp[v] )
      |                 ^
oneway.cpp: In function 'int main()':
oneway.cpp:128:17: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  128 |     for ( auto& [x,y] : a )
      |                 ^
oneway.cpp:151:17: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  151 |     for ( auto& [to,ind] : grp[i] )
      |                 ^
oneway.cpp:175:17: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  175 |     for ( auto& [x,y] : a )
      |                 ^
oneway.cpp:194:17: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  194 |     for ( auto& [x,y] : ribs )
      |                 ^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 596 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 596 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 35 ms 6724 KB Output is correct
12 Correct 43 ms 7820 KB Output is correct
13 Correct 45 ms 9900 KB Output is correct
14 Correct 65 ms 15496 KB Output is correct
15 Correct 73 ms 17940 KB Output is correct
16 Correct 118 ms 29204 KB Output is correct
17 Correct 143 ms 30300 KB Output is correct
18 Correct 121 ms 29248 KB Output is correct
19 Correct 136 ms 31156 KB Output is correct
20 Correct 46 ms 8124 KB Output is correct
21 Correct 39 ms 8160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 596 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 35 ms 6724 KB Output is correct
12 Correct 43 ms 7820 KB Output is correct
13 Correct 45 ms 9900 KB Output is correct
14 Correct 65 ms 15496 KB Output is correct
15 Correct 73 ms 17940 KB Output is correct
16 Correct 118 ms 29204 KB Output is correct
17 Correct 143 ms 30300 KB Output is correct
18 Correct 121 ms 29248 KB Output is correct
19 Correct 136 ms 31156 KB Output is correct
20 Correct 46 ms 8124 KB Output is correct
21 Correct 39 ms 8160 KB Output is correct
22 Correct 242 ms 32208 KB Output is correct
23 Correct 162 ms 30964 KB Output is correct
24 Correct 158 ms 31132 KB Output is correct
25 Correct 266 ms 34624 KB Output is correct
26 Correct 203 ms 32004 KB Output is correct
27 Correct 189 ms 31108 KB Output is correct
28 Correct 30 ms 5556 KB Output is correct
29 Correct 61 ms 9820 KB Output is correct
30 Correct 61 ms 9872 KB Output is correct
31 Correct 57 ms 10064 KB Output is correct
32 Correct 137 ms 17376 KB Output is correct