Submission #1091447

# Submission time Handle Problem Language Result Execution time Memory
1091447 2024-09-20T21:52:38 Z raphaelp One-Way Streets (CEOI17_oneway) C++14
100 / 100
97 ms 19400 KB
#include <bits/stdc++.h>
using namespace std;
int dfs(int x, int p, int id, vector<vector<pair<int, int>>> &AR, int &buff, vector<int> &ordre, vector<int> &low, vector<int> &incycle, vector<int> &occ)
{
    occ[x] = 1;
    if (low[x] != low.size())
    {
        incycle.push_back(id);
        return low[x];
    }
    ordre[x] = buff;
    low[x] = buff++;
    for (int i = 0; i < AR[x].size(); i++)
    {
        if (AR[x][i].second == id)
            continue;
        low[x] = min(low[x], dfs(AR[x][i].first, x, AR[x][i].second, AR, buff, ordre, low, incycle, occ));
    }
    if (low[x] < ordre[x])
        incycle.push_back(id);
    return low[x];
}
int find(int x, vector<int> &p)
{
    return (p[x] == x) ? x : p[x] = find(p[x], p);
}
int dfs2(int x, int par, int id, vector<int> &p, vector<vector<pair<int, int>>> &AR, vector<int> &cities, vector<char> &ans, vector<pair<int, int>> &aretes, vector<int> &occ)
{
    if (occ[x])
        return 0;
    occ[x] = 1;
    int cur = 0;
    for (int i = 0; i < AR[x].size(); i++)
    {
        if (AR[x][i].second == id)
            continue;
        if (find(AR[x][i].first, p) == x)
            continue;
        cur += dfs2(find(AR[x][i].first, p), x, AR[x][i].second, p, AR, cities, ans, aretes, occ);
    }
    cur += cities[x];
    if (id < 0)
        return cur;
    if (cur == 0)
        ans[id] = 'B';
    else
    {
        if (find(aretes[id].first, p) == x)
        {
            if (cur > 0)
                ans[id] = 'R';
            else
                ans[id] = 'L';
        }
        else
        {
            if (cur > 0)
                ans[id] = 'L';
            else
                ans[id] = 'R';
        }
    }
    return cur;
}
int main()
{
    int N, M;
    cin >> N >> M;
    vector<vector<pair<int, int>>> AR(N);
    vector<pair<int, int>> aretes;
    for (int i = 0; i < M; i++)
    {
        int a, b;
        cin >> a >> b;
        a--, b--;
        AR[a].push_back({b, i});
        AR[b].push_back({a, i});
        aretes.push_back({a, b});
    }
    int buff = 0;
    vector<int> low(N, N), ordre(N);
    vector<int> incycle;
    vector<int> occ(N);
    for (int i = 0; i < N; i++)
    {
        if (occ[i])
            continue;
        int temp = dfs(i, i, -1, AR, buff, ordre, low, incycle, occ);
    }
    vector<char> ans(M);
    vector<int> p(N), size(N, 1);
    for (int i = 0; i < N; i++)
        size[i] = AR[i].size();
    for (int i = 0; i < N; i++)
        p[i] = i;
    for (int i = 0; i < incycle.size(); i++)
    {
        ans[incycle[i]] = 'B';
        int x = find(aretes[incycle[i]].first, p), y = find(aretes[incycle[i]].second, p);
        if (x == y)
            continue;
        if (size[x] > size[y])
            swap(x, y);
        p[x] = y;
        size[y] += size[x];
        for (auto j : AR[x])
            AR[y].push_back(j);
    }
    int P;
    cin >> P;
    vector<int> cities(N, 0);
    for (int i = 0; i < P; i++)
    {
        int a, b;
        cin >> a >> b;
        a--, b--;
        cities[find(a, p)]++;
        cities[find(b, p)]--;
    }
    occ.assign(N, 0);
    for (int i = 0; i < N; i++)
    {
        if (occ[i])
            continue;
        int temp = dfs2(find(i, p), find(i, p), -1, p, AR, cities, ans, aretes, occ);
    }
    for (int i = 0; i < M; i++)
        cout << ans[i];
}

Compilation message

oneway.cpp: In function 'int dfs(int, int, int, std::vector<std::vector<std::pair<int, int> > >&, int&, std::vector<int>&, std::vector<int>&, std::vector<int>&, std::vector<int>&)':
oneway.cpp:6:16: warning: comparison of integer expressions of different signedness: '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    6 |     if (low[x] != low.size())
oneway.cpp:13:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   13 |     for (int i = 0; i < AR[x].size(); i++)
      |                     ~~^~~~~~~~~~~~~~
oneway.cpp: In function 'int dfs2(int, int, int, std::vector<int>&, std::vector<std::vector<std::pair<int, int> > >&, std::vector<int>&, std::vector<char>&, std::vector<std::pair<int, int> >&, std::vector<int>&)':
oneway.cpp:33:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   33 |     for (int i = 0; i < AR[x].size(); i++)
      |                     ~~^~~~~~~~~~~~~~
oneway.cpp: In function 'int main()':
oneway.cpp:88:13: warning: unused variable 'temp' [-Wunused-variable]
   88 |         int temp = dfs(i, i, -1, AR, buff, ordre, low, incycle, occ);
      |             ^~~~
oneway.cpp:96:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   96 |     for (int i = 0; i < incycle.size(); i++)
      |                     ~~^~~~~~~~~~~~~~~~
oneway.cpp:125:13: warning: unused variable 'temp' [-Wunused-variable]
  125 |         int temp = dfs2(find(i, p), find(i, p), -1, p, AR, cities, ans, aretes, occ);
      |             ^~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 388 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 388 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 52 ms 12232 KB Output is correct
12 Correct 58 ms 13900 KB Output is correct
13 Correct 61 ms 15944 KB Output is correct
14 Correct 68 ms 17260 KB Output is correct
15 Correct 67 ms 17604 KB Output is correct
16 Correct 61 ms 10952 KB Output is correct
17 Correct 63 ms 13400 KB Output is correct
18 Correct 55 ms 10912 KB Output is correct
19 Correct 59 ms 15228 KB Output is correct
20 Correct 56 ms 12628 KB Output is correct
21 Correct 58 ms 12104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 388 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 52 ms 12232 KB Output is correct
12 Correct 58 ms 13900 KB Output is correct
13 Correct 61 ms 15944 KB Output is correct
14 Correct 68 ms 17260 KB Output is correct
15 Correct 67 ms 17604 KB Output is correct
16 Correct 61 ms 10952 KB Output is correct
17 Correct 63 ms 13400 KB Output is correct
18 Correct 55 ms 10912 KB Output is correct
19 Correct 59 ms 15228 KB Output is correct
20 Correct 56 ms 12628 KB Output is correct
21 Correct 58 ms 12104 KB Output is correct
22 Correct 97 ms 14460 KB Output is correct
23 Correct 85 ms 11972 KB Output is correct
24 Correct 93 ms 11976 KB Output is correct
25 Correct 91 ms 19400 KB Output is correct
26 Correct 96 ms 14024 KB Output is correct
27 Correct 87 ms 12164 KB Output is correct
28 Correct 48 ms 8592 KB Output is correct
29 Correct 87 ms 13128 KB Output is correct
30 Correct 92 ms 13384 KB Output is correct
31 Correct 86 ms 13588 KB Output is correct
32 Correct 90 ms 16136 KB Output is correct