Submission #753937

# Submission time Handle Problem Language Result Execution time Memory
753937 2023-06-06T10:55:13 Z lukameladze Tropical Garden (IOI11_garden) C++14
69 / 100
5000 ms 63144 KB
#include "garden.h"
#include "gardenlib.h"
#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
//#define int long long
#define pii pair <int, int>
#define pb push_back
const int MX = 3e5 + 5;
int n,m,q,p,k[MX],dist[MX],ans[MX], in_cycle, cycle_l,best[MX], s_best[MX],ans1[MX];
vector <int> v[MX], vr[MX],n_g[MX];
vector <pii> g[MX];
int can(int i) {
    return 2 * i - 1;
}
int cnnt(int i) {
    return 2 * i;
}
 
void bfs(int a) {
    queue <int> q;
    int pp = a;
    for (int i = 1; i <= 2 * n; i++) {
        dist[i] = -1;
    }
    q.push(a); dist[a] = 0;
    while (!q.empty()) {
        int x = q.front();
        q.pop();
        for (int to : vr[x]) {
            if (to == pp) {
                in_cycle = 1;
                cycle_l = dist[x] + 1;
            }
            if (dist[to] == -1) {
                dist[to] = dist[x] + 1;
                q.push(to);
            }
        }
    }
}
void count_routes(int N, int M, int P, int R[][2], int Q, int G[]) {
	n = N; m = M; p = P; p++; q = Q;
	for (int i = 1; i <= m; i++) {
		int a, b;
		a = R[i - 1][0]; b = R[i - 1][1];
		a++; b++;
		g[a].pb({i - 1, b});
		g[b].pb({i - 1, a});
	}
    for (int i = 1; i <= q; i++) {
        k[i] = G[i - 1];
    }
    for (int i = 1; i <= n; i++) {
        sort(g[i].begin(), g[i].end());
        best[i] = g[i][0].s;
        s_best[i] = ((int)g[i].size() == 1 ? g[i][0].s : g[i][1].s);
    }
    for (int i = 1; i <= n; i++) {
        int a = i, b = best[i];
        if (best[b] == a) n_g[can(a)].pb(cnnt(b));
        else n_g[can(a)].pb(can(b));
 
        b = s_best[i];
        if (best[b] == a) n_g[cnnt(a)].pb(cnnt(b));
        else n_g[cnnt(a)].pb(can(b));
    }
    for (int i = 1; i <= n; i++) {
        v[i].clear();
    }
    for (int i = 1; i <= 2 * n; i++) {
        for (int x : n_g[i]) {
//            cout<<i<<" -- "<<x<<"\n";
            v[i].pb(x); vr[x].pb(i);
        }
    }
    bfs(can(p));
    for (int i = 1; i <= n; i++) {
        if (dist[can(i)] == -1) continue;
        ans[dist[can(i)]]++;
    }
    int fin = in_cycle, fl = cycle_l;
/*    if (in_cycle) {
        for (int i = cycle_l; i < MX; i++) {
            ans[i] += ans[i - cycle_l];
        }
    }*/
    in_cycle = 0;
    bfs(cnnt(p));
    int c_in = in_cycle, sl = cycle_l;
    for (int i = 1; i <= n; i++) {
        if (dist[can(i)] == -1) continue;
        ans1[dist[can(i)]]++;
    }
/*    if(in_cycle) {
        for (int i = cycle_l; i < MX; i++) {
            ans1[i] += ans1[i - cycle_l];
        }
    }*/
	for(int i = 1; i <= q; i++) {
		int res = 0;
		for (int j = 0; j <= min(k[i], n); j++) {
			if (j == k[i]) {
				res += ans[j];
			} else if (fin && k[i] % fl == j % fl) {
				res += ans[j];
			}
		}
		for (int j = 0; j <= min(k[i], n); j++) {
			if (j == k[i]) res += ans1[j];
			else if (c_in && k[i] % sl == j % sl) res += ans1[j];
		}
		answer(res);
	}
}
# Verdict Execution time Memory Grader output
1 Correct 15 ms 28628 KB Output is correct
2 Correct 16 ms 28756 KB Output is correct
3 Correct 15 ms 28768 KB Output is correct
4 Correct 14 ms 28488 KB Output is correct
5 Correct 13 ms 28500 KB Output is correct
6 Correct 15 ms 28756 KB Output is correct
7 Correct 14 ms 28448 KB Output is correct
8 Correct 16 ms 28672 KB Output is correct
9 Correct 16 ms 28992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 28628 KB Output is correct
2 Correct 16 ms 28756 KB Output is correct
3 Correct 15 ms 28768 KB Output is correct
4 Correct 14 ms 28488 KB Output is correct
5 Correct 13 ms 28500 KB Output is correct
6 Correct 15 ms 28756 KB Output is correct
7 Correct 14 ms 28448 KB Output is correct
8 Correct 16 ms 28672 KB Output is correct
9 Correct 16 ms 28992 KB Output is correct
10 Correct 13 ms 28500 KB Output is correct
11 Correct 28 ms 33624 KB Output is correct
12 Correct 46 ms 37808 KB Output is correct
13 Correct 64 ms 50768 KB Output is correct
14 Correct 149 ms 60024 KB Output is correct
15 Correct 171 ms 60680 KB Output is correct
16 Correct 133 ms 51532 KB Output is correct
17 Correct 116 ms 48784 KB Output is correct
18 Correct 47 ms 37768 KB Output is correct
19 Correct 139 ms 60108 KB Output is correct
20 Correct 174 ms 60632 KB Output is correct
21 Correct 131 ms 51532 KB Output is correct
22 Correct 114 ms 48724 KB Output is correct
23 Correct 143 ms 63144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 28628 KB Output is correct
2 Correct 16 ms 28756 KB Output is correct
3 Correct 15 ms 28768 KB Output is correct
4 Correct 14 ms 28488 KB Output is correct
5 Correct 13 ms 28500 KB Output is correct
6 Correct 15 ms 28756 KB Output is correct
7 Correct 14 ms 28448 KB Output is correct
8 Correct 16 ms 28672 KB Output is correct
9 Correct 16 ms 28992 KB Output is correct
10 Correct 13 ms 28500 KB Output is correct
11 Correct 28 ms 33624 KB Output is correct
12 Correct 46 ms 37808 KB Output is correct
13 Correct 64 ms 50768 KB Output is correct
14 Correct 149 ms 60024 KB Output is correct
15 Correct 171 ms 60680 KB Output is correct
16 Correct 133 ms 51532 KB Output is correct
17 Correct 116 ms 48784 KB Output is correct
18 Correct 47 ms 37768 KB Output is correct
19 Correct 139 ms 60108 KB Output is correct
20 Correct 174 ms 60632 KB Output is correct
21 Correct 131 ms 51532 KB Output is correct
22 Correct 114 ms 48724 KB Output is correct
23 Correct 143 ms 63144 KB Output is correct
24 Correct 18 ms 28500 KB Output is correct
25 Correct 968 ms 33896 KB Output is correct
26 Correct 1860 ms 37980 KB Output is correct
27 Correct 3683 ms 50988 KB Output is correct
28 Execution timed out 5053 ms 60280 KB Time limit exceeded
29 Halted 0 ms 0 KB -