답안 #579917

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
579917 2022-06-20T09:13:39 Z dxz05 One-Way Streets (CEOI17_oneway) C++14
0 / 100
15 ms 23764 KB
#pragma GCC optimize("Ofast,O2,O3,unroll-loops")
#pragma GCC target("avx2")

#include <bits/stdc++.h>

#include <random>

using namespace std;

void debug_out() { cerr << endl; }

template<typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
    cerr << "[" << H << "]";
    debug_out(T...);
}

#ifdef dddxxz
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

#define SZ(s) ((int)s.size())
#define all(x) (x).begin(), (x).end()
#define lla(x) (x).rbegin(), (x).rend()
#define bpc(x) __builtin_popcount(x)
#define bpcll(x) __builtin_popcountll(x)

clock_t startTime;

double getCurrentTime() {
    return (double) (clock() - startTime) / CLOCKS_PER_SEC;
}

#define MP make_pair

typedef long long ll;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const double eps = 0.000001;
const int MOD = 1e9 + 7;
const int INF = 1000000101;
const long long LLINF = 1223372000000000555;
const int N = 1e6 + 3e2;
const int M = 1111;

int eu[N], ev[N];

vector<int> g[N];

int flag[N];

bool used[N];

set<pair<int, int>> bridges;

int tin[N], fup[N], timer = 0;
void dfs(int v, int p){
    tin[v] = fup[v] = ++timer;

    used[v] = true;
    for (int u : g[v]){
        if (u == p) continue;

        if (!used[u]){
            dfs(u, v);
            fup[v] = min(fup[v], fup[u]);
            if (fup[u] > tin[v]) bridges.insert(MP(min(u, v), max(u, v)));
        } else {
            fup[v] = min(fup[v], tin[u]);
        }

    }
}

int p[N];

int find(int x){
    return (x == p[x] ? x : p[x] = find(p[x]));
}

void unite(int x, int y){
    x = find(x);
    y = find(y);
    if (rng() & 1) swap(x, y);
    p[x] = y;
}

void bfs(int x, vector<int> &d){
    queue<int> q;
    fill(all(d), INF);
    d[x] = 0;
    q.push(x);

    while (!q.empty()){
        x = q.front();
        q.pop();
        for (int y : g[x]){
            if (d[y] == INF){
                d[y] = d[x] + 1;
                q.push(y);
            }
        }
    }
}

bool swapped[N];
void solve(int TC) {
    int n, m;
    cin >> n >> m;

    map<pair<int, int>, int> mp;
    for (int i = 1; i <= m; i++){
        int x, y;
        cin >> x >> y;

        eu[i] = x, ev[i] = y;

        if (x > y){
            swap(x, y);
            swapped[i] = true;
        }
        if (mp.find(MP(x, y)) != mp.end()){
            flag[i] = flag[mp[MP(x, y)]] = 3;
            continue;
        }

        if (x == y){
            flag[i] = 3;
            continue;
        }

        mp[MP(x, y)] = i;

        g[x].push_back(y);
        g[y].push_back(x);
    }

    int q;
    cin >> q;

    vector<pair<int, int>> important;
    while (q--){
        int x, y;
        cin >> x >> y;
        important.emplace_back(x, y);
    }

    dfs(1, 1);

    for (int i = 1; i <= m; i++){
        int x = min(eu[i], ev[i]), y = max(eu[i], ev[i]);
        if (!bridges.count(MP(x, y))) flag[i] = 3;
    }

    iota(p + 1, p + n + 1, 1);

    for (int i = 1; i <= m; i++){
        if (flag[i] == 3) unite(eu[i], ev[i]);
    }

    for (int i = 1; i <= n; i++) g[i].clear();

    int root = n;

    for (int i = 1; i <= m; i++){
        int x = find(eu[i]), y = find(ev[i]);
        root = min({root, x, y});
        if (x != y){
            g[x].push_back(y);
            g[y].push_back(x);
        }
    }

    for (auto [x, y] : important){
        int X = x, Y = y;
        x = find(x), y = find(y);

        vector<int> dx(n + 1), dy(n + 1);
        bfs(x, dx);
        bfs(y, dy);

        for (auto [u, v] : bridges){
            int U = u, V = v;

            u = find(u), v = find(v);

            int dist = min(dx[u] + dy[v], dx[v] + dy[u]) + 1;
            if (dist != dx[y]) continue;

            if (dx[u] + dy[v] + 1 == dist){ // x -> u -> v -> y
//                cout << "R " << X << ' ' << U << ' ' << V << ' ' << Y << endl;
                if (U > V) swap(U, V);
                flag[mp[MP(U, V)]] = 2;
            } else { // x -> v -> u -> y
//                cout << "L " << X << ' ' << V << ' ' << U << ' ' << Y << endl;
                if (U > V) swap(U, V);
                flag[mp[MP(U, V)]] = 1;
            }

        }

    }

    for (int i = 1; i <= m; i++){
        if (flag[i] == 0 || flag[i] == 3){
            cout << 'B';
            continue;
        }
        char ans = (flag[i] == 1 ? 'L' : 'R');
        if (swapped[i]) ans ^= 'L' ^ 'R';
        cout << ans;
    }

}

int main() {
    startTime = clock();
    ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);

#ifdef dddxxz
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    int TC = 1;
    //    cin >> TC;

    for (int test = 1; test <= TC; test++) {
        //debug(test);
        //cout << "Case #" << test << ": ";
        solve(test);
    }

#ifdef dddxxz
    cerr << endl << "Time: " << int(getCurrentTime() * 1000) << " ms" << endl;
#endif

    return 0;
}

Compilation message

oneway.cpp: In function 'void solve(int)':
oneway.cpp:175:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  175 |     for (auto [x, y] : important){
      |               ^
oneway.cpp:183:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  183 |         for (auto [u, v] : bridges){
      |                   ^
oneway.cpp:176:13: warning: unused variable 'X' [-Wunused-variable]
  176 |         int X = x, Y = y;
      |             ^
oneway.cpp:176:20: warning: unused variable 'Y' [-Wunused-variable]
  176 |         int X = x, Y = y;
      |                    ^
# 결과 실행 시간 메모리 Grader output
1 Incorrect 15 ms 23764 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 15 ms 23764 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 15 ms 23764 KB Output isn't correct
2 Halted 0 ms 0 KB -