답안 #77585

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
77585 2018-09-28T18:41:02 Z duality One-Way Streets (CEOI17_oneway) C++11
100 / 100
246 ms 55080 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];
vpii 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].first;
        if (disc[v] == -1) {
            doDFS(v,adjList[u][i].second);
            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 (adjList[u][i].second != 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;
    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(mp(b[i],i));
        adjList[b[i]].pb(mp(a[i],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:92: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:101: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:103: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:108:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d",&p);
     ~~~~~^~~~~~~~~
oneway.cpp:110: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]);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 6 ms 5116 KB Output is correct
3 Correct 6 ms 5288 KB Output is correct
4 Correct 6 ms 5332 KB Output is correct
5 Correct 7 ms 5492 KB Output is correct
6 Correct 6 ms 5492 KB Output is correct
7 Correct 8 ms 5712 KB Output is correct
8 Correct 7 ms 5712 KB Output is correct
9 Correct 6 ms 5712 KB Output is correct
10 Correct 6 ms 5712 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 6 ms 5116 KB Output is correct
3 Correct 6 ms 5288 KB Output is correct
4 Correct 6 ms 5332 KB Output is correct
5 Correct 7 ms 5492 KB Output is correct
6 Correct 6 ms 5492 KB Output is correct
7 Correct 8 ms 5712 KB Output is correct
8 Correct 7 ms 5712 KB Output is correct
9 Correct 6 ms 5712 KB Output is correct
10 Correct 6 ms 5712 KB Output is correct
11 Correct 57 ms 11544 KB Output is correct
12 Correct 63 ms 13524 KB Output is correct
13 Correct 79 ms 15880 KB Output is correct
14 Correct 106 ms 20024 KB Output is correct
15 Correct 91 ms 22296 KB Output is correct
16 Correct 147 ms 28476 KB Output is correct
17 Correct 124 ms 35232 KB Output is correct
18 Correct 130 ms 35232 KB Output is correct
19 Correct 121 ms 38832 KB Output is correct
20 Correct 63 ms 38832 KB Output is correct
21 Correct 64 ms 38832 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 6 ms 5116 KB Output is correct
3 Correct 6 ms 5288 KB Output is correct
4 Correct 6 ms 5332 KB Output is correct
5 Correct 7 ms 5492 KB Output is correct
6 Correct 6 ms 5492 KB Output is correct
7 Correct 8 ms 5712 KB Output is correct
8 Correct 7 ms 5712 KB Output is correct
9 Correct 6 ms 5712 KB Output is correct
10 Correct 6 ms 5712 KB Output is correct
11 Correct 57 ms 11544 KB Output is correct
12 Correct 63 ms 13524 KB Output is correct
13 Correct 79 ms 15880 KB Output is correct
14 Correct 106 ms 20024 KB Output is correct
15 Correct 91 ms 22296 KB Output is correct
16 Correct 147 ms 28476 KB Output is correct
17 Correct 124 ms 35232 KB Output is correct
18 Correct 130 ms 35232 KB Output is correct
19 Correct 121 ms 38832 KB Output is correct
20 Correct 63 ms 38832 KB Output is correct
21 Correct 64 ms 38832 KB Output is correct
22 Correct 246 ms 44928 KB Output is correct
23 Correct 227 ms 46140 KB Output is correct
24 Correct 229 ms 48404 KB Output is correct
25 Correct 217 ms 54516 KB Output is correct
26 Correct 201 ms 54516 KB Output is correct
27 Correct 192 ms 55080 KB Output is correct
28 Correct 56 ms 55080 KB Output is correct
29 Correct 95 ms 55080 KB Output is correct
30 Correct 100 ms 55080 KB Output is correct
31 Correct 102 ms 55080 KB Output is correct
32 Correct 142 ms 55080 KB Output is correct