답안 #298257

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
298257 2020-09-12T16:31:57 Z tatyam 열대 식물원 (Tropical Garden) (IOI11_garden) C++17
100 / 100
2906 ms 25748 KB
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
const int INF = 0x3fffffff;
template<class T> bool chmin(T& a, const T& b){ if(a > b){ a = b; return 1; } return 0; }

void answer(int x);

void count_routes(int N, int M, int P, int R[][2], int Q, int G[]){
    vector g(N, array{pii{INF, -1}, pii{INF, -1}});
    // 各頂点から最も美しい 2 つの辺を持つ
    for(int i = 0; i < M; i++){
        auto [a, b] = R[i];
        if(chmin(g[a][1], {i, b}) && g[a][1] < g[a][0]) swap(g[a][0], g[a][1]);
        if(chmin(g[b][1], {i, a}) && g[b][1] < g[b][0]) swap(g[b][0], g[b][1]);
    }
    for(auto& [e1, e2] : g) if(e2.first == INF) e2 = e1;
    vector<int> next(N * 2);
    vector rev(N * 2, vector<int>());
    for(int i = 0; i < N; i++){
        {
            const auto [a, b] = g[i][0];
            if(a != INF){
                next[i * 2] = b * 2 + (a == g[b][0].first);
                rev[next[i * 2]].push_back(i * 2);
            }
        }
        {
            const auto [a, b] = g[i][1];
            if(a != INF){
                next[i * 2 + 1] = b * 2 + (a == g[b][0].first);
                rev[next[i * 2 + 1]].push_back(i * 2 + 1);
            }
        }
    }
    const int t1 = [&]{
        int at = P * 2;
        for(int i = 1; i <= N * 2; i++){
            at = next[at];
            if(at == P * 2) return i;
        }
        return INF;
    }();
    const int t2 = [&]{
        int at = P * 2 + 1;
        for(int i = 1; i <= N * 2; i++){
            at = next[at];
            if(at == P * 2 + 1) return i;
        }
        return INF;
    }();
    vector f1(N * 2, INF), f2(N * 2, INF);
    queue<int> q;
    f1[P * 2] = 0;
    q.push(P * 2);
    while(q.size()){
        int at = q.front();
        q.pop();
        for(int i : rev[at]) if(chmin(f1[i], f1[at] + 1)) q.push(i);
    }
    f2[P * 2 + 1] = 0;
    q.push(P * 2 + 1);
    while(q.size()){
        int at = q.front();
        q.pop();
        for(int i : rev[at]) if(chmin(f2[i], f2[at] + 1)) q.push(i);
    }
    for(int i = 0; i < Q; i++){
        const int K = G[i];
        int ans = 0;
        for(int i = 0; i < N; i++) ans += K >= f1[i * 2] && (K - f1[i * 2]) % t1 == 0 || K >= f2[i * 2] && (K - f2[i * 2]) % t2 == 0;
        answer(ans);
    }
}

Compilation message

garden.cpp: In function 'void count_routes(int, int, int, int (*)[2], int, int*)':
garden.cpp:71:58: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   71 |         for(int i = 0; i < N; i++) ans += K >= f1[i * 2] && (K - f1[i * 2]) % t1 == 0 || K >= f2[i * 2] && (K - f2[i * 2]) % t2 == 0;
      |                                           ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 512 KB Output is correct
3 Correct 1 ms 512 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 1 ms 512 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 512 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 512 KB Output is correct
3 Correct 1 ms 512 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 1 ms 512 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 512 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 11 ms 3456 KB Output is correct
12 Correct 25 ms 5372 KB Output is correct
13 Correct 44 ms 13688 KB Output is correct
14 Correct 111 ms 17656 KB Output is correct
15 Correct 155 ms 17912 KB Output is correct
16 Correct 99 ms 12664 KB Output is correct
17 Correct 91 ms 11000 KB Output is correct
18 Correct 26 ms 5376 KB Output is correct
19 Correct 117 ms 17656 KB Output is correct
20 Correct 148 ms 18040 KB Output is correct
21 Correct 103 ms 12664 KB Output is correct
22 Correct 92 ms 10932 KB Output is correct
23 Correct 117 ms 19324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 512 KB Output is correct
3 Correct 1 ms 512 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 1 ms 512 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 512 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 11 ms 3456 KB Output is correct
12 Correct 25 ms 5372 KB Output is correct
13 Correct 44 ms 13688 KB Output is correct
14 Correct 111 ms 17656 KB Output is correct
15 Correct 155 ms 17912 KB Output is correct
16 Correct 99 ms 12664 KB Output is correct
17 Correct 91 ms 11000 KB Output is correct
18 Correct 26 ms 5376 KB Output is correct
19 Correct 117 ms 17656 KB Output is correct
20 Correct 148 ms 18040 KB Output is correct
21 Correct 103 ms 12664 KB Output is correct
22 Correct 92 ms 10932 KB Output is correct
23 Correct 117 ms 19324 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 97 ms 3456 KB Output is correct
26 Correct 136 ms 5376 KB Output is correct
27 Correct 2542 ms 13816 KB Output is correct
28 Correct 1071 ms 19576 KB Output is correct
29 Correct 2906 ms 19820 KB Output is correct
30 Correct 1652 ms 14328 KB Output is correct
31 Correct 1612 ms 12792 KB Output is correct
32 Correct 144 ms 6012 KB Output is correct
33 Correct 1038 ms 19448 KB Output is correct
34 Correct 2886 ms 19832 KB Output is correct
35 Correct 1745 ms 14328 KB Output is correct
36 Correct 1885 ms 12664 KB Output is correct
37 Correct 787 ms 21264 KB Output is correct
38 Correct 2224 ms 25748 KB Output is correct