Submission #77584

# Submission time Handle Problem Language Result Execution time Memory
77584 2018-09-28T18:34:03 Z duality One-Way Streets (CEOI17_oneway) C++11
0 / 100
12 ms 10108 KB
#define DEBUG 0

#include <bits/stdc++.h>
using namespace std;

#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}

// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
    public:
        template<typename T>
        _Debug& operator,(T val) {
            cout << val << endl;
            return *this;
        }
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif

// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back

// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;

// ---------- END OF TEMPLATE ----------

int a[100000],b[100000],x[100000],y[100000];
vi adjList[100000];
int disc[100000],low[100000],num = 0;
int com[100000],bcc = 0;
vi S;
int doDFS(int u,int p) {
    int i;
    disc[u] = low[u] = num++;
    S.pb(u);
    for (i = 0; i < adjList[u].size(); i++) {
        int v = adjList[u][i];
        if (disc[v] == -1) {
            doDFS(v,u);
            low[u] = min(low[u],low[v]);
            if (low[v] > disc[u]) {
                while (1) {
                    int w = S.back();
                    S.pop_back();
                    com[w] = bcc;
                    if (w == v) break;
                }
                bcc++;
            }
        }
        else if (v != p) low[u] = min(low[u],disc[v]);
    }
    return 0;
}
vi adjList2[100000];
int parent[100000][17],depth[100000];
int toUpdate[100000][17];
int doDFS2(int u,int p,int d) {
    int i;
    assert(depth[u] == -1);
    parent[u][0] = p,depth[u] = d;
    for (i = 0; i < adjList2[u].size(); i++) {
        int v = adjList2[u][i];
        if (v != p) doDFS2(v,u,d+1);
    }
    return 0;
}
int main() {
    int i;
    int n,m,p;
    scanf("%d %d",&n,&m);
    for (i = 0; i < m; i++) {
        scanf("%d %d",&a[i],&b[i]);
        a[i]--,b[i]--;
        adjList[a[i]].pb(b[i]);
        adjList[b[i]].pb(a[i]);
    }
    scanf("%d",&p);
    for (i = 0; i < p; i++) {
        scanf("%d %d",&x[i],&y[i]);
        x[i]--,y[i]--;
    }

    for (i = 0; i < n; i++) disc[i] = low[i] = -1;
    for (i = 0; i < n; i++) {
        if (disc[i] == -1) {
            doDFS(i,-1);
            while (!S.empty()) {
                int u = S.back();
                S.pop_back();
                com[u] = bcc;
            }
            bcc++;
        }
    }
    for (i = 0; i < m; i++) {
        if (com[a[i]] != com[b[i]]) {
            adjList2[com[a[i]]].pb(com[b[i]]);
            adjList2[com[b[i]]].pb(com[a[i]]);
        }
    }

    int j;
    fill(depth,depth+bcc,-1);
    for (i = 0; i < bcc; i++) {
        if (depth[i] == -1) doDFS2(i,-1,0);
    }
    for (i = 1; (1 << i) < bcc; i++) {
        for (j = 0; j < bcc; j++) {
            if (parent[j][i-1] != -1) parent[j][i] = parent[parent[j][i-1]][i-1];
            else parent[j][i] = -1;
        }
    }
    int logn = i;
    for (i = 0; i < p; i++) {
        int u = com[x[i]],v = com[y[i]],f = 1;
        if (depth[u] < depth[v]) swap(u,v),f = -1;
        for (j = logn-1; j >= 0; j--) {
            if ((parent[u][j] != -1) && (depth[parent[u][j]] >= depth[v])) {
                toUpdate[u][j] = f;
                u = parent[u][j];
            }
        }
        if (u != v) {
            for (j = logn-1; j >= 0; j--) {
                if (parent[u][j] != parent[v][j]) {
                    toUpdate[u][j] = f,toUpdate[v][j] = -f;
                    u = parent[u][j],v = parent[v][j];
                }
            }
            toUpdate[u][0] = f,toUpdate[v][0] = -f;
        }
    }
    for (i = logn-1; i > 0; i--) {
        for (j = 0; j < bcc; j++) {
            if (toUpdate[j][i] != 0) toUpdate[j][i-1] = toUpdate[parent[j][i-1]][i-1] = toUpdate[j][i];
        }
    }
    for (i = 0; i < m; i++) {
        int u = com[a[i]],v = com[b[i]];
        if (u == v) printf("B");
        else {
            int ans;
            if (parent[u][0] == v) ans = toUpdate[u][0];
            else ans = -toUpdate[v][0];
            printf((ans == 0) ? "B":((ans == -1) ? "L":"R"));
        }
    }
    printf("\n");

    return 0;
}

Compilation message

oneway.cpp: In function 'int doDFS(int, int)':
oneway.cpp:67:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (i = 0; i < adjList[u].size(); i++) {
                 ~~^~~~~~~~~~~~~~~~~~~
oneway.cpp: In function 'int doDFS2(int, int, int)':
oneway.cpp:93:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (i = 0; i < adjList2[u].size(); i++) {
                 ~~^~~~~~~~~~~~~~~~~~~~
oneway.cpp: In function 'int main()':
oneway.cpp:102: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:104:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d",&a[i],&b[i]);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~
oneway.cpp:109:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d",&p);
     ~~~~~^~~~~~~~~
oneway.cpp:111:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d",&x[i],&y[i]);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Runtime error 12 ms 10108 KB Execution killed with signal 11 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Runtime error 12 ms 10108 KB Execution killed with signal 11 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Runtime error 12 ms 10108 KB Execution killed with signal 11 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -