답안 #442699

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
442699 2021-07-08T16:29:11 Z BeanZ One-Way Streets (CEOI17_oneway) C++14
100 / 100
516 ms 83436 KB
// I_Love_LPL 11m
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
const int N = 3e5 + 5;
long long mod = 1000007;
const int lim = 4e5 + 5;
const int lg = 20;
const int base = 311;
const long double eps = 1e-6;
ll ans = 0, timer = 0, cnt = 0;
vector<pair<ll, ll>> node[N];
vector<ll> adj[N];
ll ids[N], low[N], in[N], vis[N], s[N], e[N], sz[N], dep[N], dp[N][lg + 1], grp[N];
char res[N];
ll root;
vector<ll> st;
map<pair<ll, ll>, ll> dir, mem;
void dfs3(ll u, ll p){
    vis[u] = 1;
    for (auto j : adj[u]){
        if (j == p) continue;
        dfs3(j, u);
        sz[u] += sz[j];
    }
    if (sz[u] < 0){
        if (dir[{p, u}]) res[mem[{p, u}]] = 'R';
        else res[mem[{p, u}]] = 'L';
    } else if (sz[u] > 0) {
        if (dir[{p, u}]) res[mem[{p, u}]] = 'L';
        else res[mem[{p, u}]] = 'R';
    }
}
ll LCA(ll u, ll v){
    if (dep[u] > dep[v]) swap(u, v);
    ll dist = dep[v] - dep[u];
    for (int i = 0; i <= lg; i++){
        if (dist & (1 << i)){
            v = dp[v][i];
        }
    }
    if (u == v) return u;
    for (int i = lg; i >= 0; i--){
        if (dp[v][i] != dp[u][i]){
            v = dp[v][i];
            u = dp[u][i];
        }
    }
    return dp[v][0];
}
void dfs2(ll u, ll p){
    vis[u] = 1;
    for (int i = 1; i <= lg; i++) dp[u][i] = dp[dp[u][i - 1]][i - 1];
    for (auto j : adj[u]){
        if (j == p) continue;
        dep[j] = dep[u] + 1;
        dp[j][0] = u;
        dfs2(j, u);
    }
}
void dfs(ll u, ll p){
    vis[u] = 1;
    timer++;
    ids[u] = low[u] = timer;
    st.push_back(u);
    in[u] = 1;
    for (auto j : node[u]){
        if (j.second == p) continue;
        if (in[j.first]){
            low[u] = min(low[u], ids[j.first]);
        } else if (vis[j.first] == 0){
            dfs(j.first, j.second);
            low[u] = min(low[u], low[j.first]);
        }
    }
    if (low[u] == ids[u]){
        ans++;
        while (true){
            ll x = st.back();
            grp[x] = ans;
            in[x] = 0;
            st.pop_back();
            low[x] = low[u];
            if (x == u) break;
        }
    }
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    if (fopen("tests.inp", "r")){
        freopen("test.inp", "r", stdin);
        freopen("test.out", "w", stdout);
    }
    ll n, m, p;
    cin >> n >> m;
    for (int i = 1; i <= m; i++){
        cin >> s[i] >> e[i];
        node[s[i]].push_back({e[i], i});
        node[e[i]].push_back({s[i], i});
        res[i] = '?';
    }
    for (int i = 1; i <= n; i++){
        if (vis[i]) continue;
        root = i;
        dfs(i, 0);
    }
    memset(vis, 0, sizeof(vis));
    for (int i = 1; i <= m; i++){
        ll u = grp[s[i]], v = grp[e[i]];
        if (u == v) continue;
        if (mem[{u, v}] == 0){
            adj[u].push_back(v);
            adj[v].push_back(u);
            mem[{u, v}] = i;
            mem[{v, u}] = i;
            dir[{u, v}] = 1;
            dir[{v, u}] = 0;
        }
    }
    for (int i = 1; i <= ans; i++){
        if (vis[i]) continue;
        dfs2(i, i);
    }
    cin >> p;
    for (int i = 1; i <= p; i++){
        ll u, v;
        cin >> u >> v;
        u = grp[u];
        v = grp[v];
        ll lca = LCA(grp[u], grp[v]);
        sz[u] += 1;
        sz[lca] -= 1;
        sz[v] -= 1;
        sz[lca] += 1;
    }
    memset(vis, 0, sizeof(vis));
    for (int i = 1; i <= ans; i++){
        if (vis[i]) continue;
        dfs3(i, i);
    }
    for (int i = 1; i <= m; i++){
        if (res[i] == '?') res[i] = 'B';
        cout << res[i];
    }
}
/*
Ans:

Out:
*/

Compilation message

oneway.cpp: In function 'int main()':
oneway.cpp:93:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   93 |         freopen("test.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
oneway.cpp:94:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   94 |         freopen("test.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 16716 KB Output is correct
2 Correct 10 ms 16844 KB Output is correct
3 Correct 10 ms 16944 KB Output is correct
4 Correct 12 ms 17356 KB Output is correct
5 Correct 11 ms 17356 KB Output is correct
6 Correct 10 ms 16844 KB Output is correct
7 Correct 12 ms 17332 KB Output is correct
8 Correct 12 ms 17376 KB Output is correct
9 Correct 11 ms 16844 KB Output is correct
10 Correct 10 ms 16972 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 16716 KB Output is correct
2 Correct 10 ms 16844 KB Output is correct
3 Correct 10 ms 16944 KB Output is correct
4 Correct 12 ms 17356 KB Output is correct
5 Correct 11 ms 17356 KB Output is correct
6 Correct 10 ms 16844 KB Output is correct
7 Correct 12 ms 17332 KB Output is correct
8 Correct 12 ms 17376 KB Output is correct
9 Correct 11 ms 16844 KB Output is correct
10 Correct 10 ms 16972 KB Output is correct
11 Correct 54 ms 26648 KB Output is correct
12 Correct 64 ms 27916 KB Output is correct
13 Correct 78 ms 30712 KB Output is correct
14 Correct 139 ms 40844 KB Output is correct
15 Correct 167 ms 45252 KB Output is correct
16 Correct 404 ms 74720 KB Output is correct
17 Correct 292 ms 76872 KB Output is correct
18 Correct 378 ms 74696 KB Output is correct
19 Correct 269 ms 79092 KB Output is correct
20 Correct 61 ms 27740 KB Output is correct
21 Correct 74 ms 27576 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 16716 KB Output is correct
2 Correct 10 ms 16844 KB Output is correct
3 Correct 10 ms 16944 KB Output is correct
4 Correct 12 ms 17356 KB Output is correct
5 Correct 11 ms 17356 KB Output is correct
6 Correct 10 ms 16844 KB Output is correct
7 Correct 12 ms 17332 KB Output is correct
8 Correct 12 ms 17376 KB Output is correct
9 Correct 11 ms 16844 KB Output is correct
10 Correct 10 ms 16972 KB Output is correct
11 Correct 54 ms 26648 KB Output is correct
12 Correct 64 ms 27916 KB Output is correct
13 Correct 78 ms 30712 KB Output is correct
14 Correct 139 ms 40844 KB Output is correct
15 Correct 167 ms 45252 KB Output is correct
16 Correct 404 ms 74720 KB Output is correct
17 Correct 292 ms 76872 KB Output is correct
18 Correct 378 ms 74696 KB Output is correct
19 Correct 269 ms 79092 KB Output is correct
20 Correct 61 ms 27740 KB Output is correct
21 Correct 74 ms 27576 KB Output is correct
22 Correct 484 ms 77984 KB Output is correct
23 Correct 429 ms 75844 KB Output is correct
24 Correct 516 ms 75844 KB Output is correct
25 Correct 426 ms 83436 KB Output is correct
26 Correct 386 ms 77764 KB Output is correct
27 Correct 384 ms 75860 KB Output is correct
28 Correct 65 ms 23916 KB Output is correct
29 Correct 88 ms 28352 KB Output is correct
30 Correct 96 ms 28604 KB Output is correct
31 Correct 88 ms 28860 KB Output is correct
32 Correct 163 ms 44936 KB Output is correct