Submission #43016

# Submission time Handle Problem Language Result Execution time Memory
43016 2018-03-08T05:39:37 Z RayaBurong25_1 One-Way Streets (CEOI17_oneway) C++14
100 / 100
153 ms 21792 KB
#include <stdio.h>
#include <vector>
#include <cassert>
typedef struct edge edge;
struct edge
{
    int u, v;
};
std::vector<edge> E;
int inMST[100005];
std::vector<edge> AdjList[100005];
int Pa[100005];
int UF(int u)
{
    return (Pa[u] == 0)?u:(Pa[u] = UF(Pa[u]));
}
int vis[100005];
int p[100005][20];
int h[100005];
int pi[100005];
void dfsCyc(int u)
{
    // printf("dfsCyc %d\n", u);
    int i, v, s = AdjList[u].size();
    for (i = 0; i < s; i++)
    {
        v = AdjList[u][i].u;
        if (v != p[u][0])
        {
            // printf("  v %d\n", v);
            p[v][0] = u;
            h[v] = h[u] + 1;
            if (E[AdjList[u][i].v - 1].u == u)
                pi[v] = AdjList[u][i].v;
            else
                pi[v] = -AdjList[u][i].v;
            dfsCyc(v);
        }
    }
}
int markCyc[100005];
char Ans[100005];
int LCA(int u, int v)
{
    int i;
    if (h[u] > h[v])
    {
        for (i = 19; i >= 0; i--)
            if (h[p[u][i]] > h[v])
                u = p[u][i];
        u = p[u][0];
    }
    if (h[v] > h[u])
    {
        for (i = 19; i >= 0; i--)
            if (h[p[v][i]] > h[u])
                v = p[v][i];
        v = p[v][0];
    }
    if (u == v)
        return u;
    for (i = 19; i >= 0; i--)
    {
        if (p[u][i] != p[v][i])
        {
            u = p[u][i];
            v = p[v][i];
        }
    }
    return p[u][0];
}
int abs(int a)
{
    return (a < 0)?-a:a;
}
void resolveCyc(int u)
{
    int i, v, s = AdjList[u].size();
    for (i = 0; i < s; i++)
    {
        v = AdjList[u][i].u;
        if (v != p[u][0])
        {
            resolveCyc(v);
            markCyc[u] += markCyc[v];
        }
    }
    // printf("u%d %d\n", u, markCyc[u]);
    if (markCyc[u] > 0)
        Ans[abs(pi[u])] = 'B';
}
int markDir[100005];
void resolveDir(int u)
{
    int i, v, s = AdjList[u].size();
    for (i = 0; i < s; i++)
    {
        v = AdjList[u][i].u;
        if (v != p[u][0])
        {
            resolveDir(v);
            markDir[u] += markDir[v];
        }
    }
    if (Ans[abs(pi[u])] == 'B')
        return;
    // assert(Ans[abs(pi[u])] == 0);
    if (markDir[u] > 0)
    {
        if (pi[u] < 0)
            Ans[abs(pi[u])] = 'R';
        else
            Ans[abs(pi[u])] = 'L';
    }
    else if (markDir[u] < 0)
    {
        if (pi[u] < 0)
            Ans[abs(pi[u])] = 'L';
        else
            Ans[abs(pi[u])] = 'R';
    }
}
int main()
{
    int N, M;
    scanf("%d %d", &N, &M);
    int i, j, u, v;
    for (i = 0; i < M; i++)
    {
        scanf("%d %d", &u, &v);
        E.push_back({u, v});
    }
    int cnt = 0;
    int pu, pv;
    for (i = 0; i < M; i++)
    {
        u = E[i].u;
        v = E[i].v;
        if ((pu = UF(u)) != (pv = UF(v)))
        {
            Pa[pu] = pv;
            AdjList[u].push_back({v, i + 1});
            AdjList[v].push_back({u, i + 1});
            inMST[i] = 1;
            cnt++;
        }
    }
    for (i = 1; i <= N; i++)
    {
        if (vis[UF(i)] < 1)
        {
            dfsCyc(UF(i));
            vis[UF(i)] = 1;
        }
    }
    for (i = 1; i < 20; i++)
    {
        for (j = 1; j <= N; j++)
        {
            p[j][i] = p[p[j][i - 1]][i - 1];
        }
    }
    for (i = 0; i < M; i++)
    {
        if (!inMST[i])
        {
            markCyc[E[i].u] += 1;
            markCyc[E[i].v] += 1;
            markCyc[LCA(E[i].u, E[i].v)] += -2;
            // printf("%d + 1 %d + 1 %d - 2\n", E[i].u, E[i].v, LCA(E[i].u, E[i].v));
            Ans[i + 1] = 'B';
        }
    }
    for (i = 1; i <= N; i++)
    {
        if (vis[UF(i)] < 2)
        {
            resolveCyc(UF(i));
            vis[UF(i)] = 2;
        }
    }

    int P;
    scanf("%d", &P);
    for (i = 0; i < P; i++)
    {
        scanf("%d %d", &u, &v);
        markDir[u] += 1;
        markDir[v] += -1;
    }
    for (i = 1; i <= N; i++)
    {
        if (vis[UF(i)] < 3)
        {
            resolveDir(UF(i));
            vis[UF(i)] = 3;
        }
    }
    for (i = 1; i <= M; i++)
    {
        if (Ans[i] == 0)
            Ans[i] = 'B';
        printf("%c", Ans[i]);
    }
    // printf("\n");
}

Compilation message

oneway.cpp: In function 'int main()':
oneway.cpp:126:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d", &N, &M);
                           ^
oneway.cpp:130:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d", &u, &v);
                               ^
oneway.cpp:184:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &P);
                    ^
oneway.cpp:187:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d", &u, &v);
                               ^
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2680 KB Output is correct
2 Correct 4 ms 2680 KB Output is correct
3 Correct 4 ms 2848 KB Output is correct
4 Correct 3 ms 2932 KB Output is correct
5 Correct 4 ms 3040 KB Output is correct
6 Correct 3 ms 3040 KB Output is correct
7 Correct 4 ms 3040 KB Output is correct
8 Correct 4 ms 3040 KB Output is correct
9 Correct 3 ms 3040 KB Output is correct
10 Correct 4 ms 3132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2680 KB Output is correct
2 Correct 4 ms 2680 KB Output is correct
3 Correct 4 ms 2848 KB Output is correct
4 Correct 3 ms 2932 KB Output is correct
5 Correct 4 ms 3040 KB Output is correct
6 Correct 3 ms 3040 KB Output is correct
7 Correct 4 ms 3040 KB Output is correct
8 Correct 4 ms 3040 KB Output is correct
9 Correct 3 ms 3040 KB Output is correct
10 Correct 4 ms 3132 KB Output is correct
11 Correct 71 ms 6580 KB Output is correct
12 Correct 84 ms 8480 KB Output is correct
13 Correct 94 ms 11264 KB Output is correct
14 Correct 125 ms 15168 KB Output is correct
15 Correct 148 ms 16384 KB Output is correct
16 Correct 124 ms 17964 KB Output is correct
17 Correct 112 ms 20108 KB Output is correct
18 Correct 113 ms 20108 KB Output is correct
19 Correct 109 ms 20144 KB Output is correct
20 Correct 94 ms 20144 KB Output is correct
21 Correct 71 ms 20144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2680 KB Output is correct
2 Correct 4 ms 2680 KB Output is correct
3 Correct 4 ms 2848 KB Output is correct
4 Correct 3 ms 2932 KB Output is correct
5 Correct 4 ms 3040 KB Output is correct
6 Correct 3 ms 3040 KB Output is correct
7 Correct 4 ms 3040 KB Output is correct
8 Correct 4 ms 3040 KB Output is correct
9 Correct 3 ms 3040 KB Output is correct
10 Correct 4 ms 3132 KB Output is correct
11 Correct 71 ms 6580 KB Output is correct
12 Correct 84 ms 8480 KB Output is correct
13 Correct 94 ms 11264 KB Output is correct
14 Correct 125 ms 15168 KB Output is correct
15 Correct 148 ms 16384 KB Output is correct
16 Correct 124 ms 17964 KB Output is correct
17 Correct 112 ms 20108 KB Output is correct
18 Correct 113 ms 20108 KB Output is correct
19 Correct 109 ms 20144 KB Output is correct
20 Correct 94 ms 20144 KB Output is correct
21 Correct 71 ms 20144 KB Output is correct
22 Correct 153 ms 20312 KB Output is correct
23 Correct 152 ms 20312 KB Output is correct
24 Correct 151 ms 20312 KB Output is correct
25 Correct 145 ms 21792 KB Output is correct
26 Correct 143 ms 21792 KB Output is correct
27 Correct 143 ms 21792 KB Output is correct
28 Correct 62 ms 21792 KB Output is correct
29 Correct 116 ms 21792 KB Output is correct
30 Correct 102 ms 21792 KB Output is correct
31 Correct 108 ms 21792 KB Output is correct
32 Correct 114 ms 21792 KB Output is correct