Submission #1107659

# Submission time Handle Problem Language Result Execution time Memory
1107659 2024-11-01T20:39:34 Z laight Tropical Garden (IOI11_garden) C++17
100 / 100
2059 ms 33396 KB
#include "garden.h"
#include "gardenlib.h"

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define pi pair<int, int>
#define fi first
#define se second
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define mp make_pair
#define pll pair<ll, ll>
#define umi unordered_map<int, int>
#define umll unordered_map<ll, ll>
#define umllv unordered_map<ll, vector<ll>>

#define getchar() (*_pinp?*_pinp++:(_inp[fread(_pinp=_inp, 1, 4096, stdin)]='\0', *_pinp++))
char _inp[4097], *_pinp=_inp;
#define scan(x) do{while((x=getchar())<'-'); _ssign=x=='-'; if(_ssign) while((x=getchar())<'0'); for(x-='0'; '0'<=(_=getchar()); x=(x<<3)+(x<<1)+_-'0'); x=_ssign?-x:x;}while(0)
char _; bool _ssign;

// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx2")

using namespace std; using namespace __gnu_pbds;

const int MM = 1e5+5e4+5;
const ll inf = 2e15+1;
const int lg = 17; // 17 for 1e5, 31 for 1e9

int n, m, p, a, b, dis[2*MM], lv, cycl, q, inp; bool cyc[2*MM], vis[2*MM];
vector<int> adj[2*MM], g(2005), ans(2005), to(2*MM); vector<pi> graph[MM];

void g_f(int par, int c, int d){
    dis[c] = d;
    for (int i : adj[c]){
        if (i == par || cyc[i]) continue;
        g_f(c, i, d+1);
    }
}
void cyc_f(int c){
    vis[c] = 1;
    if (vis[to[c]]){
        int st = to[c];
        while (st != c){
            cyc[st] = 1; st = to[st];
            ++lv;
        }
        cyc[c] = 1;
        ++lv;
        return;
    }
    cyc_f(to[c]);
}
void sol(int sp){
    memset(dis, -1, sizeof(dis));
    memset(vis, 0, sizeof(vis));
    memset(cyc, 0, sizeof(cyc));
    lv=0;
    bool incyc=0;
    cyc_f(sp);
    cycl = lv;
    if (cyc[sp]){
        incyc=1;
        g_f(-1, sp, 0);
        int nxt=to[sp];
        while (nxt != sp){
            g_f(-1, nxt, --lv);
            nxt = to[nxt];
        }
    }
    else{
        cyc[to[sp]] = 1;
        g_f(-1, sp, 0);
    }
    for (int i=0; i<q; ++i){
        for (int j=0; j<n; ++j){
            if (dis[j] == -1) continue;
            if (incyc){
                if (g[i] >= dis[j] && (g[i]%cycl == dis[j]%cycl)){
                    ++ans[i];
                }
            }
            else{
                if (g[i] == dis[j]) ++ans[i];
            }
        }
    }
}
void count_routes(int N, int M, int P, int R[][2], int Q, int G[]){
    // cin.tie(0); cin.sync_with_stdio(0);
    n = N; m = M; p = P;
    for (int i=0; i<m; ++i){
        graph[R[i][0]].pb({i+1, R[i][1]}); graph[R[i][1]].pb({i+1, R[i][0]});
    }
    q = Q; for (int i=0; i<q; i++) {g[i] = G[i];}
    for (int i=0; i<n; ++i){
        if (graph[i][0].fi == graph[graph[i][0].se][0].fi){
            to[i] = n+graph[i][0].se;
        }
        else to[i] = graph[i][0].se;
        if (graph[i].size() >= 2){
            if (graph[i][1].fi == graph[graph[i][1].se][0].fi){
                to[n+i] = n+graph[i][1].se;
            }
            else to[n+i] = graph[i][1].se;
        }
        else{
            if (graph[i][0].fi == graph[graph[i][0].se][0].fi){
                to[n+i] = n+graph[i][0].se;
            }
            else to[n+i] = graph[i][0].se;
        }
    }
    for (int i=0; i<2*n; ++i){
        adj[i].pb(to[i]); adj[to[i]].pb(i);
    }
    sol(p); sol(n+p);
    for (int i=0; i<q; ++i){
        answer(ans[i]);
    }
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 14160 KB Output is correct
2 Correct 4 ms 14136 KB Output is correct
3 Correct 4 ms 14160 KB Output is correct
4 Correct 4 ms 13904 KB Output is correct
5 Correct 3 ms 13904 KB Output is correct
6 Correct 4 ms 14160 KB Output is correct
7 Correct 4 ms 13904 KB Output is correct
8 Correct 4 ms 14160 KB Output is correct
9 Correct 5 ms 14260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 14160 KB Output is correct
2 Correct 4 ms 14136 KB Output is correct
3 Correct 4 ms 14160 KB Output is correct
4 Correct 4 ms 13904 KB Output is correct
5 Correct 3 ms 13904 KB Output is correct
6 Correct 4 ms 14160 KB Output is correct
7 Correct 4 ms 13904 KB Output is correct
8 Correct 4 ms 14160 KB Output is correct
9 Correct 5 ms 14260 KB Output is correct
10 Correct 4 ms 13904 KB Output is correct
11 Correct 11 ms 18364 KB Output is correct
12 Correct 24 ms 20664 KB Output is correct
13 Correct 35 ms 25072 KB Output is correct
14 Correct 64 ms 31616 KB Output is correct
15 Correct 74 ms 32072 KB Output is correct
16 Correct 54 ms 27812 KB Output is correct
17 Correct 50 ms 26712 KB Output is correct
18 Correct 20 ms 20792 KB Output is correct
19 Correct 68 ms 31444 KB Output is correct
20 Correct 73 ms 31896 KB Output is correct
21 Correct 63 ms 27668 KB Output is correct
22 Correct 54 ms 26540 KB Output is correct
23 Correct 70 ms 33352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 14160 KB Output is correct
2 Correct 4 ms 14136 KB Output is correct
3 Correct 4 ms 14160 KB Output is correct
4 Correct 4 ms 13904 KB Output is correct
5 Correct 3 ms 13904 KB Output is correct
6 Correct 4 ms 14160 KB Output is correct
7 Correct 4 ms 13904 KB Output is correct
8 Correct 4 ms 14160 KB Output is correct
9 Correct 5 ms 14260 KB Output is correct
10 Correct 4 ms 13904 KB Output is correct
11 Correct 11 ms 18364 KB Output is correct
12 Correct 24 ms 20664 KB Output is correct
13 Correct 35 ms 25072 KB Output is correct
14 Correct 64 ms 31616 KB Output is correct
15 Correct 74 ms 32072 KB Output is correct
16 Correct 54 ms 27812 KB Output is correct
17 Correct 50 ms 26712 KB Output is correct
18 Correct 20 ms 20792 KB Output is correct
19 Correct 68 ms 31444 KB Output is correct
20 Correct 73 ms 31896 KB Output is correct
21 Correct 63 ms 27668 KB Output is correct
22 Correct 54 ms 26540 KB Output is correct
23 Correct 70 ms 33352 KB Output is correct
24 Correct 4 ms 14072 KB Output is correct
25 Correct 69 ms 18492 KB Output is correct
26 Correct 88 ms 20828 KB Output is correct
27 Correct 1360 ms 25156 KB Output is correct
28 Correct 676 ms 31844 KB Output is correct
29 Correct 1453 ms 32072 KB Output is correct
30 Correct 831 ms 27900 KB Output is correct
31 Correct 829 ms 26724 KB Output is correct
32 Correct 107 ms 20816 KB Output is correct
33 Correct 685 ms 31768 KB Output is correct
34 Correct 1389 ms 32120 KB Output is correct
35 Correct 870 ms 27668 KB Output is correct
36 Correct 1233 ms 26652 KB Output is correct
37 Correct 485 ms 33396 KB Output is correct
38 Correct 2059 ms 32116 KB Output is correct