Submission #765626

# Submission time Handle Problem Language Result Execution time Memory
765626 2023-06-24T23:45:02 Z phoebe Tropical Garden (IOI11_garden) C++17
100 / 100
116 ms 49764 KB
#include <bits/stdc++.h>
#include "garden.h"
#include "gardenlib.h"
using namespace std;

#define pii pair<int, int>
#define F first
#define S second
#define ALL(x) x.begin(), x.end()

const int maxn = 150051;
int n, m, p, dist1[maxn * 2], dist2[maxn * 2], 
// dist1 = best, dist2 = 2nd best
dist3[maxn * 2],
r[maxn * 2], cycle1 = -1, cycle2 = -1;
vector<pii> adj[maxn];
vector<int> adj2[maxn * 2]; 
// i * 2 = incoming is not best, leads to best
// i * 2 + 1 = incoming is best, leads to second best

map<int, vector<int>> mp1, mp2, ans1, ans2;
// make functional group
map<int, int> re;

void bfs(){
    queue<int> q; q.push(p * 2); dist1[p * 2] = 0;
    while (!q.empty()){
        int v = q.front(); q.pop();
        for (auto u : adj2[v]){
            if (dist1[u] != -1) continue;
            dist1[u] = dist1[v] + 1;
            q.push(u);
        }
    }
    q.push(p * 2 + 1); dist2[p * 2 + 1] = 0;
    while (!q.empty()){
        int v = q.front(); q.pop();
        for (auto u : adj2[v]){
            if (dist2[u] != -1) continue;
            dist2[u] = dist2[v] + 1;
            q.push(u);
        }
    }
}

void get_cycle(){
    int v = p * 2; 
    fill(dist3, dist3 + n * 2, -1); dist3[p * 2] = 0;
    while (true){
        if (dist3[r[v]] != -1) break;
        dist3[r[v]] = dist3[v] + 1;
        v = r[v];
    }
    if (r[v] == p * 2){ // has cycle
        cycle1 = dist3[v] + 1;
    }
    // for (int i = 0; i < n * 2; i++) cout << dist3[i * 2] << ' '; cout << endl;
    v = p * 2 + 1;
    fill(dist3, dist3 + n * 2, -1); dist3[p * 2 + 1] = 0;
    while (true){
        if (dist3[r[v]] != -1) break;
        dist3[r[v]] = dist3[v] + 1;
        v = r[v];
    }
    if (r[v] == p * 2 + 1){ // has cycle
        cycle2 = dist3[v] + 1;
    }
    // for (int i = 0; i < n * 2; i++) cout << dist3[i * 2] << ' '; cout << endl;
}

void solve(){
    for (auto &pt : ans1){
        int x = pt.F; sort(ALL(mp1[x]));
        sort(ALL(pt.S)); 
        pt.S.resize(distance(pt.S.begin(), unique(ALL(pt.S))));
        for (auto g : pt.S){
            int cnt = upper_bound(ALL(mp1[x]), g) - mp1[x].begin();
            re[g] += cnt;
        }
    }
    for (auto &pt : ans2){
        int x = pt.F; sort(ALL(mp2[x]));
        sort(ALL(pt.S)); 
        pt.S.resize(distance(pt.S.begin(), unique(ALL(pt.S))));
        for (auto g : pt.S){
            int cnt = upper_bound(ALL(mp2[x]), g) - mp2[x].begin();
            re[g] += cnt;
        }
    }
}

void count_routes(int N, int M, int P, int R[][2], int Q, int G[]){
    n = N, m = M, p = P;
    for (int i = 0; i < m; i++){
        adj[R[i][0]].push_back({i, R[i][1]});
        adj[R[i][1]].push_back({i, R[i][0]});
    }
    for (int v = 0; v < n; v++){
        sort(adj[v].begin(), adj[v].end());
    }
    for (int v = 0; v < n; v++){
        int best = adj[v][0].second;
        int bests_best = adj[best][0].second;
        // cout << v << ' ' << best << ' ' << adj[v].size() << endl;
        if (v != bests_best){
            adj2[best * 2].push_back(v * 2);
            r[v * 2] = best * 2;
        }
        else{
            adj2[best * 2 + 1].push_back(v * 2);
            // adj2[v * 2 + 1].push_back(best * 2);
            r[v * 2] = best * 2 + 1;
        }
        int second_best = adj[v][min((int)adj[v].size() - 1, 1)].second;
        int second_bests_best = adj[second_best][0].second;
        // cout << v << ' ' << second_best << endl;
        if (v != second_bests_best){
            adj2[second_best * 2].push_back(v * 2 + 1);
            r[v * 2 + 1] = second_best * 2;
        }
        else{
            adj2[second_best * 2 + 1].push_back(v * 2 + 1);
            r[v * 2 + 1] = second_best * 2 + 1;
        }
    }
    // for (int i = 0; i < n * 2; i++) cout << r[i] << ' '; cout << endl;
    fill(dist1, dist1 + n * 2, -1);
    fill(dist2, dist2 + n * 2, -1);
    bfs();
    get_cycle();
    // cout << cycle1 << ' ' << cycle2 << endl;
    // return;
    for (int v = 0; v < n; v++){
        // if (v == p) continue;
        if (dist1[v * 2] != -1){
            if (cycle1 == -1) mp1[dist1[v * 2]].push_back(dist1[v * 2]);
            else mp1[dist1[v * 2] % cycle1].push_back(dist1[v * 2]);
        }
        if (dist2[v * 2] != -1){
            if (cycle2 == -1) mp2[dist2[v * 2]].push_back(dist2[v * 2]);
            else mp2[dist2[v * 2] % cycle2].push_back(dist2[v * 2]);
        }
    }

    // return;
    for (int i = 0; i < Q; i++){
        if (cycle1 == -1) ans1[G[i]].push_back(G[i]);
        else ans1[G[i] % cycle1].push_back(G[i]);
        if (cycle2 == -1) ans2[G[i]].push_back(G[i]);
        else ans2[G[i] % cycle2].push_back(G[i]);
    }
    solve();
    // for (int i = 0; i < 5; i++) cout << re[i] << ' '; cout << endl;
    // cout << "bruh" << endl;
    for (int i = 0; i < Q; i++){
        // cout << re[G[i]] << ' '; cout << endl;
        answer(re[G[i]]);
    }
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 10964 KB Output is correct
2 Correct 6 ms 10964 KB Output is correct
3 Correct 6 ms 10964 KB Output is correct
4 Correct 5 ms 10836 KB Output is correct
5 Correct 5 ms 10836 KB Output is correct
6 Correct 6 ms 11068 KB Output is correct
7 Correct 5 ms 10916 KB Output is correct
8 Correct 5 ms 10964 KB Output is correct
9 Correct 7 ms 11176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 10964 KB Output is correct
2 Correct 6 ms 10964 KB Output is correct
3 Correct 6 ms 10964 KB Output is correct
4 Correct 5 ms 10836 KB Output is correct
5 Correct 5 ms 10836 KB Output is correct
6 Correct 6 ms 11068 KB Output is correct
7 Correct 5 ms 10916 KB Output is correct
8 Correct 5 ms 10964 KB Output is correct
9 Correct 7 ms 11176 KB Output is correct
10 Correct 5 ms 10836 KB Output is correct
11 Correct 13 ms 13592 KB Output is correct
12 Correct 23 ms 15512 KB Output is correct
13 Correct 70 ms 40816 KB Output is correct
14 Correct 105 ms 26092 KB Output is correct
15 Correct 88 ms 26956 KB Output is correct
16 Correct 81 ms 23372 KB Output is correct
17 Correct 77 ms 21732 KB Output is correct
18 Correct 30 ms 15436 KB Output is correct
19 Correct 76 ms 26060 KB Output is correct
20 Correct 88 ms 26956 KB Output is correct
21 Correct 69 ms 23048 KB Output is correct
22 Correct 66 ms 21708 KB Output is correct
23 Correct 112 ms 27556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 10964 KB Output is correct
2 Correct 6 ms 10964 KB Output is correct
3 Correct 6 ms 10964 KB Output is correct
4 Correct 5 ms 10836 KB Output is correct
5 Correct 5 ms 10836 KB Output is correct
6 Correct 6 ms 11068 KB Output is correct
7 Correct 5 ms 10916 KB Output is correct
8 Correct 5 ms 10964 KB Output is correct
9 Correct 7 ms 11176 KB Output is correct
10 Correct 5 ms 10836 KB Output is correct
11 Correct 13 ms 13592 KB Output is correct
12 Correct 23 ms 15512 KB Output is correct
13 Correct 70 ms 40816 KB Output is correct
14 Correct 105 ms 26092 KB Output is correct
15 Correct 88 ms 26956 KB Output is correct
16 Correct 81 ms 23372 KB Output is correct
17 Correct 77 ms 21732 KB Output is correct
18 Correct 30 ms 15436 KB Output is correct
19 Correct 76 ms 26060 KB Output is correct
20 Correct 88 ms 26956 KB Output is correct
21 Correct 69 ms 23048 KB Output is correct
22 Correct 66 ms 21708 KB Output is correct
23 Correct 112 ms 27556 KB Output is correct
24 Correct 6 ms 10964 KB Output is correct
25 Correct 17 ms 14224 KB Output is correct
26 Correct 25 ms 16184 KB Output is correct
27 Correct 91 ms 42696 KB Output is correct
28 Correct 78 ms 27816 KB Output is correct
29 Correct 116 ms 28816 KB Output is correct
30 Correct 79 ms 25272 KB Output is correct
31 Correct 63 ms 23632 KB Output is correct
32 Correct 30 ms 16980 KB Output is correct
33 Correct 81 ms 27880 KB Output is correct
34 Correct 92 ms 28744 KB Output is correct
35 Correct 72 ms 24988 KB Output is correct
36 Correct 78 ms 23800 KB Output is correct
37 Correct 92 ms 29672 KB Output is correct
38 Correct 114 ms 49764 KB Output is correct