Submission #1105192

# Submission time Handle Problem Language Result Execution time Memory
1105192 2024-10-25T16:50:28 Z anarch_y Tropical Garden (IOI11_garden) C++17
100 / 100
2165 ms 33724 KB
#include "garden.h"
#include "gardenlib.h"

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(x) begin(x), end(x)
#define sz(x) (int)x.size()
#define pb push_back

const int maxn = 300000;
vector<int> adj[maxn];
bool bl[maxn];
int d[maxn];

void dfs(int s, int p){
    if(p != -1) d[s] = d[p]+1;
    for(auto u: adj[s]){
        if(u == p or bl[u]) continue;
        dfs(u, s);
    }
}

void count_routes(int N, int M, int P, int R[][2], int Q, int G[]){
    int n = N, m = M;
    vector<pair<int, int>> vec[n];
    int mn[n] = {};
    for(int i=1; i<=m; i++){
        int a, b; a = R[i-1][0], b = R[i-1][1];
        vec[a].pb({b, i}); vec[b].pb({a, i});
        if(mn[a] == 0) mn[a] = i;
        if(mn[b] == 0) mn[b] = i; 
    } 
    int to[2*n] = {};
    for(int a=0; a<n; a++){
        auto [b, i] = vec[a][0];
        if(mn[b] == i) to[a] = n+b;
        else to[a] = b;
        if(sz(vec[a]) >= 2){
            auto [c, j] = vec[a][1];
            if(mn[c] == j) to[n+a] = n+c;
            else to[n+a] = c; 
        }
        else{
            if(mn[b] == i) to[n+a] = n+b;
            else to[n+a] = b;
        }
    }
    for(int a=0; a<2*n; a++){
        adj[a].pb(to[a]); adj[to[a]].pb(a);
    }
    int p = P;
    int q = Q;
    int qry[q] = {};
    for(int i=0; i<q; i++) qry[i] = G[i];
    int ans[q] = {};
    auto count = [&](int p){
        int x = p, y = to[p];
        while(x != y){
            x = to[x];
            y = to[to[y]];
        }
        vector<int> cyc; cyc.pb(x);
        fill(bl, bl+2*n, 0);
        bl[x] = 1;
        while(to[cyc.back()] != x){
            cyc.pb(to[cyc.back()]);
            bl[cyc.back()] = 1;
        }
        int l = sz(cyc);
        fill(d, d+2*n, -1);
        int ok = 0;
        if(find(all(cyc), p) != end(cyc)){
            ok = 1;
            d[p] = 0;
            dfs(p, -1);
            x = to[p];
            int c = l-1;
            while(x != p){
                d[x] = c;
                dfs(x, -1);
                x = to[x];
                c--;
            }
        }
        else{
            bl[to[p]] = 1;
            d[p] = 0;
            dfs(p, -1);
        }
        for(int i=0; i<q; i++){
            int k = qry[i];
            for(int a=0; a<n; a++){
                if(d[a] == -1) continue;
                if(ok){
                    if(k >= d[a] and (k-d[a])%l == 0) ans[i]++;
                }
                else{
                    if(k == d[a]) ans[i]++;
                }
            }
        }
    };
    count(p); count(n+p);
    for(int i=0; i<q; i++){
        answer(ans[i]);
    }
}    
# Verdict Execution time Memory Grader output
1 Correct 3 ms 10832 KB Output is correct
2 Correct 4 ms 10832 KB Output is correct
3 Correct 3 ms 10832 KB Output is correct
4 Correct 2 ms 10576 KB Output is correct
5 Correct 2 ms 10576 KB Output is correct
6 Correct 3 ms 10832 KB Output is correct
7 Correct 2 ms 10576 KB Output is correct
8 Correct 3 ms 10832 KB Output is correct
9 Correct 4 ms 10832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 10832 KB Output is correct
2 Correct 4 ms 10832 KB Output is correct
3 Correct 3 ms 10832 KB Output is correct
4 Correct 2 ms 10576 KB Output is correct
5 Correct 2 ms 10576 KB Output is correct
6 Correct 3 ms 10832 KB Output is correct
7 Correct 2 ms 10576 KB Output is correct
8 Correct 3 ms 10832 KB Output is correct
9 Correct 4 ms 10832 KB Output is correct
10 Correct 3 ms 10576 KB Output is correct
11 Correct 11 ms 13904 KB Output is correct
12 Correct 22 ms 16080 KB Output is correct
13 Correct 38 ms 23540 KB Output is correct
14 Correct 89 ms 29404 KB Output is correct
15 Correct 85 ms 29656 KB Output is correct
16 Correct 60 ms 24260 KB Output is correct
17 Correct 57 ms 22344 KB Output is correct
18 Correct 29 ms 16208 KB Output is correct
19 Correct 84 ms 29384 KB Output is correct
20 Correct 87 ms 29768 KB Output is correct
21 Correct 77 ms 24136 KB Output is correct
22 Correct 70 ms 22352 KB Output is correct
23 Correct 96 ms 31428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 10832 KB Output is correct
2 Correct 4 ms 10832 KB Output is correct
3 Correct 3 ms 10832 KB Output is correct
4 Correct 2 ms 10576 KB Output is correct
5 Correct 2 ms 10576 KB Output is correct
6 Correct 3 ms 10832 KB Output is correct
7 Correct 2 ms 10576 KB Output is correct
8 Correct 3 ms 10832 KB Output is correct
9 Correct 4 ms 10832 KB Output is correct
10 Correct 3 ms 10576 KB Output is correct
11 Correct 11 ms 13904 KB Output is correct
12 Correct 22 ms 16080 KB Output is correct
13 Correct 38 ms 23540 KB Output is correct
14 Correct 89 ms 29404 KB Output is correct
15 Correct 85 ms 29656 KB Output is correct
16 Correct 60 ms 24260 KB Output is correct
17 Correct 57 ms 22344 KB Output is correct
18 Correct 29 ms 16208 KB Output is correct
19 Correct 84 ms 29384 KB Output is correct
20 Correct 87 ms 29768 KB Output is correct
21 Correct 77 ms 24136 KB Output is correct
22 Correct 70 ms 22352 KB Output is correct
23 Correct 96 ms 31428 KB Output is correct
24 Correct 4 ms 10576 KB Output is correct
25 Correct 81 ms 13904 KB Output is correct
26 Correct 134 ms 16208 KB Output is correct
27 Correct 713 ms 24220 KB Output is correct
28 Correct 851 ms 31156 KB Output is correct
29 Correct 1122 ms 31560 KB Output is correct
30 Correct 561 ms 25864 KB Output is correct
31 Correct 604 ms 23880 KB Output is correct
32 Correct 145 ms 16712 KB Output is correct
33 Correct 831 ms 31308 KB Output is correct
34 Correct 1043 ms 31304 KB Output is correct
35 Correct 594 ms 25828 KB Output is correct
36 Correct 1006 ms 23880 KB Output is correct
37 Correct 736 ms 33352 KB Output is correct
38 Correct 2165 ms 33724 KB Output is correct