제출 #786652

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

using namespace std;

typedef pair<int, int> pi;

int N, M, Q;
vector<map<int, int>> dist;
vector<vector<int>> adj_list;
int BLOCK_SIZE;

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<int>& banned)
{
    dp.assign(N, -1);

    for (int i = 0; i < N; i++) {
        if (!find(i, banned)) {
            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<int> banned)
{
    int ans = -1;
    for (auto v : dist[c]) {

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

    return ans;
}

void insert_to(int dest, int src, int len)
{
    if (dist[dest].find(src) != dist[dest].end()) {
        dist[dest][src] = max(dist[dest][src], len);
        return;
    }

    if (dist[dest].size() < BLOCK_SIZE) {
        dist[dest][src] = len;
    }
    else {
        auto v = *dist[dest].begin();
        if (v.second < len) {
            dist[dest].erase(v.first);
            dist[dest][src] = len;
        }
    }
}

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

    BLOCK_SIZE = ceil(sqrt(N));

    adj_list.resize(N);
    dist.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++) {
        insert_to(i, i, 0);

        for (auto neigh : adj_list[i]) {
            for (auto city : dist[i]) {
                insert_to(neigh, city.first, city.second + 1);
            }
        }
    }

    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);
        }
        sort(curbanned.begin(), curbanned.end());

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

        curbanned.clear();
        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 insert_to(int, int, int)':
bitaro.cpp:64:27: warning: comparison of integer expressions of different signedness: 'std::map<int, int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   64 |     if (dist[dest].size() < BLOCK_SIZE) {
      |         ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
bitaro.cpp: In function 'int main()':
bitaro.cpp:78:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   78 |     scanf("%d %d %d", &N, &M, &Q);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
bitaro.cpp:87:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   87 |         scanf("%d %d", &u, &v);
      |         ~~~~~^~~~~~~~~~~~~~~~~
bitaro.cpp:106:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  106 |         scanf("%d %d", &T, &Y);
      |         ~~~~~^~~~~~~~~~~~~~~~~
bitaro.cpp:111:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  111 |             scanf("%d", &c);
      |             ~~~~~^~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...