제출 #786696

#제출 시각아이디문제언어결과실행 시간메모리
786696PanosPaskBitaro’s Party (JOI18_bitaro)C++14
7 / 100
2079 ms211420 KiB
#include <bits/stdc++.h>
#define pb push_back

using namespace std;

typedef pair<int, int> pi;

int N, M, Q;
vector<vector<pi>> dist;
vector<vector<int>> adj_list;
int BLOCK_SIZE;
vector<bool> ban;
vector<int> curbanned;
vector<int> dp;

int find(int i, vector<int>& v)
{
    int p = lower_bound(v.begin(), v.end(), i) - v.begin();

    if (p < v.size() && v[p] == i)
        return true;
    else
        return false;
}

int naive_dp(int dest, vector<bool>& banned)
{
    dp.assign(N, -1);

    for (int i = 0; i < N; i++) {
        if (!banned[i]) {
            dp[i] = max(dp[i], 0);
        }
        if (dp[i] == -1) {
            continue;
        }

        for (auto v : adj_list[i])
            dp[v] = max(dp[v], dp[i] + 1);
    }

    return dp[dest];
}

int find_from_precalculate(int c, vector<bool>& banned)
{
    int ans = -1;
    for (auto v : dist[c]) {

        if (!banned[v.first])
            ans = max(ans, v.second);
    }

    return ans;
}

void merge(vector<pi>& a, vector<pi>& b)
{
    vector<pi> c;
    unordered_map<int, bool> use;

    int p_a = 0;
    int p_b = 0;
    int ins = 0;

    while (p_a < a.size() && p_b < b.size() && ins < BLOCK_SIZE) {
        if (a[p_a].second >= b[p_b].second + 1) {
            if (!use[a[p_a].first]) {
                use[a[p_a].first] = true;
                c.push_back(a[p_a]);
                ins++;
            }
            p_a++;
        }
        else {
            if (!use[b[p_b].first]) {
                use[b[p_b].first] = true;
                ins++;
                c.push_back(b[p_b]);
                c.back().second++;
            }
            p_b++;
        }
    }

    while (p_a < a.size() && ins < BLOCK_SIZE) {
        if (!use[a[p_a].first]) {
            use[a[p_a].first] = true;
            c.push_back(a[p_a]);
            ins++;
        }
        p_a++;
    }
    while (p_b < b.size() && ins < BLOCK_SIZE) {
        if (!use[b[p_b].first]) {
            use[b[p_b].first] = true;
            ins++;
            c.push_back(b[p_b]);
            c.back().second++;
        }
        p_b++;
    }


    swap(a, c);
}

int main(void)
{
    scanf("%d %d %d", &N, &M, &Q);

    BLOCK_SIZE = ceil(sqrt(N));

    adj_list.resize(N);
    dist.resize(N);
    ban.resize(N);

    for (int i = 0; i < M; i++) {
        int u, v;
        scanf("%d %d", &u, &v);
        u--; v--;

        adj_list[u].pb(v);
    }

    // Precalculate best cities
    for (int i = 0; i < N; i++) {
        if (dist[i].size() < BLOCK_SIZE)
            dist[i].push_back({i, 0});

        for (auto neigh : adj_list[i]) {
            merge(dist[neigh], dist[i]);
        }
    }


    for (int i = 0; i < Q; i++) {
        int T, Y;
        scanf("%d %d", &T, &Y);
        T--;

        for (int j = 0; j < Y; j++) {
            int c;
            scanf("%d", &c);
            c--;

            curbanned.push_back(c);
            ban[c] = true;
        }
        sort(curbanned.begin(), curbanned.end());

        int ans = 0;
        if (Y >= BLOCK_SIZE)
            ans = naive_dp(T, ban);
        else
            ans = find_from_precalculate(T, ban);

        while (curbanned.size()) {
            ban[curbanned.back()] = false;
            curbanned.pop_back();
        }

        printf("%d\n", ans);
    }

    return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

bitaro.cpp: In function 'int find(int, std::vector<int>&)':
bitaro.cpp:20:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   20 |     if (p < v.size() && v[p] == i)
      |         ~~^~~~~~~~~~
bitaro.cpp: In function 'void merge(std::vector<std::pair<int, int> >&, std::vector<std::pair<int, int> >&)':
bitaro.cpp:66:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   66 |     while (p_a < a.size() && p_b < b.size() && ins < BLOCK_SIZE) {
      |            ~~~~^~~~~~~~~~
bitaro.cpp:66:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   66 |     while (p_a < a.size() && p_b < b.size() && ins < BLOCK_SIZE) {
      |                              ~~~~^~~~~~~~~~
bitaro.cpp:86:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   86 |     while (p_a < a.size() && ins < BLOCK_SIZE) {
      |            ~~~~^~~~~~~~~~
bitaro.cpp:94:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   94 |     while (p_b < b.size() && ins < BLOCK_SIZE) {
      |            ~~~~^~~~~~~~~~
bitaro.cpp: In function 'int main()':
bitaro.cpp:128:28: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  128 |         if (dist[i].size() < BLOCK_SIZE)
      |             ~~~~~~~~~~~~~~~^~~~~~~~~~~~
bitaro.cpp:110:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  110 |     scanf("%d %d %d", &N, &M, &Q);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
bitaro.cpp:120:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  120 |         scanf("%d %d", &u, &v);
      |         ~~~~~^~~~~~~~~~~~~~~~~
bitaro.cpp:139:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  139 |         scanf("%d %d", &T, &Y);
      |         ~~~~~^~~~~~~~~~~~~~~~~
bitaro.cpp:144:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  144 |             scanf("%d", &c);
      |             ~~~~~^~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...