Submission #639739

# Submission time Handle Problem Language Result Execution time Memory
639739 2022-09-11T12:14:50 Z Hacv16 Tropical Garden (IOI11_garden) C++17
0 / 100
15 ms 22580 KB
#include<bits/stdc++.h>
#include "garden.h"
#include "gardenlib.h"
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int MAX = 2e5 + 15;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;

#define pb push_back
#define sz(x) (int) x.size()
#define fr first
#define sc second
#define mp make_pair
#define all(x) x.begin(), x.end()
#define dbg(x) cerr << #x << ": " << "[ " << x << " ]\n"

int dist[MAX][2], cyc[2];
bool seen[MAX][2];
vector<int> adj[MAX], rev[MAX];

void dfs(int u, int end, int d, int aux){
    seen[u][aux] = true;
    dist[u][aux] = d;

    for(auto v : rev[u]){
        if(v == end){
            cyc[aux] = dist[u][aux] + 1;
            return;
        } 

        if(seen[v][aux]) continue;
        dfs(v, end, d + 1, aux);
    }
}

void count_routes(int n, int m, int p, int r[][2], int q, int qrs[]) {
    for(int i = 0; i < m; i++){
        int u = r[i][0], v = r[i][1];
        if(sz(adj[u]) < 2) adj[u].pb(v); //only 2 edges matter
        if(sz(adj[v]) < 2) adj[v].pb(u);
    }

    vector<int> pai(2 * n, -1); //functional graph

    for(int i = 0; i < n; i++){
        for(int j = 0; j < min(2, sz(adj[i])); j++){
            int v = adj[i][j];
            int cur = (sz(adj[v]) && adj[v][0] == i) * n + v;
            pai[j * n + i] = cur;
        }
    }

    for(int i = n; i < 2 * n; i++)
        if(pai[i] == -1) pai[i] = pai[i - n];

    for(int i = 0; i < 2 * n; i++)
        rev[pai[i]].pb(i); 
    
    memset(dist, -1, sizeof(dist));

    dfs(p, p, 0, 0);
    dfs(p + n, p + n, 0, 1);

    vector<vector<int>> fdist(2, vector<int>(2 * n));

    for(int i = 0; i < 2; i++){
        for(int j = 0; j < n; j++){
            if(dist[j][i] == -1) continue;
            fdist[i][dist[j][i]]++;
        }
    }

    for(int i = 0; i < q; i++){
        int k = qrs[i], ans = 0;
        
        for(int j = 0; j < 2; j++){
            for(int d = k % cyc[j]; d <= min(n, k); d += cyc[j])
                ans += fdist[j][d];
        }

        answer(ans);
    }
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 11476 KB Output is correct
2 Correct 6 ms 11348 KB Output is correct
3 Correct 6 ms 11348 KB Output is correct
4 Runtime error 15 ms 22580 KB Execution killed with signal 8
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 11476 KB Output is correct
2 Correct 6 ms 11348 KB Output is correct
3 Correct 6 ms 11348 KB Output is correct
4 Runtime error 15 ms 22580 KB Execution killed with signal 8
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 11476 KB Output is correct
2 Correct 6 ms 11348 KB Output is correct
3 Correct 6 ms 11348 KB Output is correct
4 Runtime error 15 ms 22580 KB Execution killed with signal 8
5 Halted 0 ms 0 KB -