Submission #56065

# Submission time Handle Problem Language Result Execution time Memory
56065 2018-07-09T18:03:55 Z gabrielsimoes One-Way Streets (CEOI17_oneway) C++17
0 / 100
4 ms 3064 KB
#include <bits/stdc++.h>
using namespace std;

typedef pair<int,int> pii;

const int MAXN = 1e5+10;

int n, m, p;
vector<pii> edges;
vector<pii> requirements;
vector<int> gix[MAXN];

bool isFirst(int cur, int edge) {
    return cur == edges[edge].first;
}

int other(int cur, int edge) {
    if (isFirst(cur, edge)) return edges[edge].second;
    else return edges[edge].first;
}

bool isPartOfCycle[MAXN];
bool edgeUsed[MAXN];
int status[MAXN];
vector<pii> stk;
void dfsCycles(int cur, int inIx) {
    status[cur] = 1;
    stk.push_back({cur, inIx});

    for (int ix : gix[cur]) {
        if (edgeUsed[ix]) continue;
        edgeUsed[ix] = true;

        int nx = other(cur, ix);

        if (status[nx] == 0) {
            dfsCycles(nx, ix);
        } else if (status[nx] == 1) {
            isPartOfCycle[ix] = true;
            int i = stk.size();
            while (--i >= 0) {
                if (stk[i].first != nx)
                    isPartOfCycle[stk[i].second] = true;
                else
                    break;
            }
        } else {
            fprintf(stderr, "ERROR: dfsCycles visited vertex already out of stack.");
            exit(1);
        }
    }

    stk.pop_back();
    status[cur] = 2;
}

int uf[MAXN];
void reset() { for (int i = 0; i < MAXN; i++) uf[i] = i; }
int get(int i) { return uf[i] == i ? i : uf[i] = get(uf[i]); }
void join(int a, int b) { uf[get(b)] = get(a); }

enum Answer {UNDEFINED, LEFT, RIGHT, BOTH};
Answer ans[MAXN];

int parent[MAXN], depth[MAXN], edgeParent[MAXN];
void dfsLCA(int cur, int pre) {
    parent[cur] = pre;
    depth[cur] = depth[pre] + 1;
    status[cur] = 1;

    for (int ix : gix[cur]) {
        int nx = other(cur, ix);

        if (nx != pre) {
            edgeParent[nx] = ix;
            dfsLCA(nx, cur);
        }
    }
}

int main() {
    scanf("%d %d", &n, &m);
    for (int i = 0,a,b; i < m; i++) {
        scanf("%d %d", &a, &b);
        edges.push_back({a,b});
        gix[a].push_back(i);
        gix[b].push_back(i);
    }

    scanf("%d", &p);
    for (int i = 0,a,b; i < p; i++) {
        scanf("%d %d", &a, &b);
        requirements.push_back({a,b});
    }

    for (int i = 1; i <= n; i++) {
        if (status[i] == 0) {
            dfsCycles(i, -1);
        }
    }

    reset();

    for (int i = 0; i < m; i++) {
        if (isPartOfCycle[i]) {
            ans[i] = BOTH;
            join(edges[i].first, edges[i].second);
        }
    }

    for (int i = 1; i <= n; i++) {
        gix[i].clear();
        status[i] = 0;
    }

    for (int i = 0; i < m; i++) {
        int a = get(edges[i].first), b = get(edges[i].second);
        edges[i] = {a, b};

        if (a != b) {
            gix[a].push_back(i);
            gix[b].push_back(i);
        }
    }

    for (int i = 1; i <= n; i++) {
        if (status[i] == 0) {
            dfsLCA(i, i);
        }
    }

    for (int i = 0; i < p; i++) {
        int a = get(requirements[i].first), b = get(requirements[i].second);

        while (a != b) {
            int da = depth[a], db = depth[b];

            if (da >= db) {
                ans[edgeParent[a]] = RIGHT;
                a = parent[a];
            }

            if (db >= da) {
                ans[edgeParent[b]] = LEFT;
                b = parent[b];
            }
        }
    }

    for (int i = 0; i < m; i++) {
        switch(ans[i]) {
            case LEFT: printf("L"); break;
            case RIGHT: printf("R"); break;
            default: printf("B");
        }
    }

    printf("\n");
}

Compilation message

oneway.cpp: In function 'int main()':
oneway.cpp:82:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d", &n, &m);
     ~~~~~^~~~~~~~~~~~~~~~~
oneway.cpp:84:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d", &a, &b);
         ~~~~~^~~~~~~~~~~~~~~~~
oneway.cpp:90:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &p);
     ~~~~~^~~~~~~~~~
oneway.cpp:92:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d", &a, &b);
         ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 3064 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 3064 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 3064 KB Output isn't correct
2 Halted 0 ms 0 KB -