QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#110056 | #2001. Tropical Garden | thenymphsofdelphi# | 49 | 4ms | 16000kb | C++20 | 7.4kb | 2023-05-31 12:43:44 | 2024-05-31 13:52:17 |
Judging History
answer
#include "garden.h"
#include "gardenlib.h"
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define fi first
#define se second
#define For(i, l, r) for (auto i = (l); i < (r); i++)
#define ForE(i, l, r) for (auto i = (l); i <= (r); i++)
#define FordE(i, l, r) for (auto i = (l); i >= (r); i--)
#define Fora(v, a) for (auto v: (a))
#define bend(a) (a).begin(), (a).end()
#define isz(a) ((signed)(a).size())
using ll = long long;
using ld = long double;
using pii = pair <int, int>;
using vi = vector <int>;
using vpii = vector <pii>;
using vvi = vector <vi>;
struct functional_graph_processor{
functional_graph_processor(const vector<int> &next): n((int)next.size()), cycle_id(n, -1), cycle_pos(n, -1), root(n, -1), depth(n, -1), abr(n){
vector<int> was(n);
for(auto u = 0; u < n; ++ u){
if(was[u]) continue;
int v = u;
while(!was[v]){
was[v] = true;
v = next[v];
}
if(!~depth[v]){
int w = v;
vector<int> c;
while(!~depth[w]){
cycle_id[w] = (int)cycle.size();
cycle_pos[w] = (int)c.size();
c.push_back(w);
root[w] = w;
depth[w] = 0;
w = next[w];
}
cycle.push_back(c);
size.push_back((int)cycle.size());
}
auto dfs = [&](auto self, int u)->void{
if(~depth[u]) return;
int v = next[u];
self(self, v);
root[u] = root[v];
++ size[cycle_id[root[u]]];
depth[u] = depth[v] + 1;
abr[v].push_back(u);
};
dfs(dfs, u);
}
}
// Requires graph
template<class Graph>
functional_graph_processor(const Graph &g){
int n = g.n;
assert(n == (int)g.edge.size());
vector<int> pv(n, -1), state(n), on_cycle(n);
vector<vector<int>> cycle;
auto dfs = [&](auto self, int u, int p)->void{
state[u] = 1;
for(auto id: g.adj[u]){
if(g.ignore && g.ignore(id)) continue;
auto &e = g.edge[id];
int v = u ^ e.from ^ e.to;
if(v == p) continue;
if(state[v] == 1){
cycle.emplace_back();
for(auto w = u; w != v; w = pv[w]){
cycle.back().push_back(w);
on_cycle[w] = true;
}
cycle.back().push_back(v);
on_cycle[v] = true;
}
else if(state[v] == 0){
pv[v] = u;
self(self, v, u);
}
}
state[u] = 2;
};
for(auto u = 0; u < n; ++ u){
if(state[u] != 2){
assert(state[u] == 0);
dfs(dfs, u, -1);
}
}
vector<int> next(n, -1);
auto dfs2 = [&](auto self, int u, int p)->void{
for(auto id: g.adj[u]){
if(g.ignore && g.ignore(id)) continue;
auto &e = g.edge[id];
int v = u ^ e.from ^ e.to;
if(v == p || on_cycle[v]) continue;
next[v] = u;
self(self, v, u);
}
};
for(auto &c: cycle){
for(auto i = 0; i < (int)c.size(); ++ i) next[c[i]] = c[(i + 1) % (int)c.size()];
for(auto u: c) dfs2(dfs2, u, -1);
}
*this = functional_graph_processor(next);
}
int n;
vector<vector<int>> cycle; // main cycles
vector<int> cycle_id; // id of the cycle it belongs to, -1 if not part of one
vector<int> cycle_pos; // position in the cycle, -1 if not part of one
vector<int> prev; // previous vertex in the cycle, -1 if not part of one
vector<int> size; // size of the weakly connected component of the i-th main cycle
vector<int> root; // first reachable node in the main cycle
vector<int> depth; // # of edges from the main cycle
vector<vector<int>> abr; // forest of arborescences of reversed edges not on the main cycle
};
const int N = 3e5 + 5;
struct edge{
int from, to;
int weight;
};
int n, m, t;
edge a[N];
vi adj[N];
pii b[N];
int nxt[N];
vi adj2[N];
int dist[3][N];
void reach_t(int j, const vi& vs){
queue <int> qu;
Fora(s, vs){
dist[j][s] = 0;
qu.emplace(s);
}
while (not qu.empty()){
int u = qu.front(); qu.pop();
Fora(v, adj2[u]){
if (dist[j][v] == -1){
dist[j][v] = dist[j][u] + 1;
qu.emplace(v);
}
}
}
}
void count_routes(int _n, int _m, int _t, int _edge[][2], int q, int ak[]){
n = _n; m = _m; t = _t;
For(i, 0, m){
auto [u, v] = pair{_edge[i][0], _edge[i][1]};
a[i * 2] = edge{u, v, i};
adj[u].emplace_back(i * 2);
a[i * 2 + 1] = edge{v, u, i};
adj[v].emplace_back(i * 2 + 1);
}
For(u, 0, n){
b[u] = {-1, -1};
Fora(i, adj[u]){
if (b[u].fi == -1 or a[b[u].fi].weight > a[i].weight){
b[u].se = b[u].fi;
b[u].fi = i;
}
else if (b[u].se == -1 or a[b[u].se].weight > a[i].weight){
b[u].se = i;
}
}
}
For(i, 0, 2 * m){
if (b[a[i].to].fi != (i ^ 1) or b[a[i].to].se == -1){
nxt[i] = b[a[i].to].fi;
}
else{
nxt[i] = b[a[i].to].se;
}
adj2[nxt[i]].emplace_back(i);
}
functional_graph_processor fgp(vi(nxt, nxt + 2 * m));
vvi spec(1);
For(i, 0, 2 * m){
if (a[i].to != t){
continue;
}
if (fgp.cycle_id[i] == -1){
spec[0].emplace_back(i);
}
else{
spec.emplace_back(vi{i});
}
}
memset(dist, -1, sizeof(dist));
For(j, 0, isz(spec)){
reach_t(j, spec[j]);
}
// For(i, 0, q){
// int k = ak[i] - 1;
// int ans = 0;
// For(u, 0, n){
// int id = b[u].fi;
// For(j, 0, isz(spec)){
// if (dist[j][id] == -1){
// continue;
// }
// if (k == dist[j][id]){
// ans++;
// break;
// }
// if (j != 0 and (k - dist[j][id]) % isz(fgp.cycle[fgp.cycle_id[spec[j][0]]]) == 0){
// ans++;
// break;
// }
// }
// }
// answer(ans);
// }
For(i, 0, q){
int k = ak[i] - 1;
int ans = 0;
For(u, 0, n){
int id = b[u].fi;
ForE(turn, 1, k){
id = nxt[id];
}
if (a[id].to == t){
ans++;
}
}
answer(ans);
}
}
/*
==================================================+
INPUT |
--------------------------------------------------|
--------------------------------------------------|
==================================================+
OUTPUT |
--------------------------------------------------|
--------------------------------------------------|
==================================================+
*/
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 49
Accepted
Test #1:
score: 49
Accepted
time: 4ms
memory: 12336kb
input:
780 780 0 1 0 0 2 2 3 3 1 4 1 5 4 6 5 7 6 8 7 9 8 10 9 11 10 12 11 13 12 14 13 15 14 16 15 17 16 18 17 19 18 20 19 21 20 22 21 23 22 24 23 25 24 26 25 27 26 28 27 29 28 30 29 31 30 32 31 33 32 34 33 35 34 36 35 37 36 38 37 39 38 40 39 41 40 42 41 43 42 44 43 45 44 46 45 47 46 48 47 49 48 50 49 51 50...
output:
Correct.
Test #2:
score: 0
Accepted
time: 2ms
memory: 14900kb
input:
950 950 0 1 0 0 2 2 3 3 1 4 1 5 4 6 5 7 6 8 7 9 8 10 9 11 10 12 11 13 12 14 13 15 14 16 15 17 16 18 17 19 18 20 19 21 20 22 21 23 22 24 23 25 24 26 25 27 26 28 27 29 28 30 29 31 30 32 31 33 32 34 33 35 34 36 35 37 36 38 37 39 38 40 39 41 40 42 41 43 42 44 43 45 44 46 45 47 46 48 47 49 48 50 49 51 50...
output:
Correct.
Test #3:
score: 0
Accepted
time: 0ms
memory: 15236kb
input:
999 1099 0 1 0 0 2 2 3 3 1 4 1 5 4 6 5 7 6 8 7 9 8 10 9 11 10 12 11 13 12 14 13 15 14 16 15 17 16 18 17 19 18 20 19 21 20 22 21 23 22 24 23 25 24 26 25 27 26 28 27 29 28 30 29 31 30 32 31 33 32 34 33 35 34 36 35 37 36 38 37 39 38 40 39 41 40 42 41 43 42 44 43 45 44 46 45 47 46 48 47 49 48 50 49 51 5...
output:
Correct.
Test #4:
score: 0
Accepted
time: 0ms
memory: 14940kb
input:
45 44 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 20 20 21 21 22 22 23 23 24 24 25 25 26 26 27 27 28 28 29 29 30 30 31 31 32 32 33 33 34 34 35 35 36 36 37 37 38 38 39 39 40 40 41 41 42 42 43 43 44 1 100 1
output:
Correct.
Test #5:
score: 0
Accepted
time: 0ms
memory: 14476kb
input:
17 46 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 11 16 14 2 13 1 0 6 3 14 0 15 14 0 12 0 5 3 13 16 8 13 5 16 14 10 10 13 9 12 16 4 11 1 5 0 12 8 8 2 12 16 13 7 9 1 10 3 15 5 15 12 12 7 7 10 9 4 10 12 1 100 3
output:
Correct.
Test #6:
score: 0
Accepted
time: 0ms
memory: 12144kb
input:
1000 2000 7 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 20 20 21 21 22 22 23 23 24 24 25 25 26 26 0 0 27 27 28 28 29 29 30 30 31 31 32 32 33 33 34 34 35 35 36 36 37 37 38 38 39 39 40 40 41 41 42 42 43 43 44 44 45 45 46 46 47 47 48 48 49 49 50 50 ...
output:
Correct.
Test #7:
score: 0
Accepted
time: 0ms
memory: 11656kb
input:
70 170 10 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 20 20 21 21 22 22 23 23 24 24 25 25 26 26 27 27 28 28 29 29 30 30 31 31 32 32 33 33 34 34 35 35 36 36 37 37 38 38 39 39 40 40 41 41 42 42 43 43 44 44 45 45 46 46 47 47 48 48 49 49 50 50 51 51 ...
output:
Correct.
Test #8:
score: 0
Accepted
time: 3ms
memory: 12172kb
input:
1000 1000 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 0 8 0 9 0 10 2 11 6 12 11 13 9 14 2 15 3 16 14 17 15 18 5 19 2 20 3 21 1 22 2 23 4 24 9 25 2 26 17 27 15 28 5 29 20 30 12 31 24 32 28 33 26 34 25 35 32 36 8 37 16 38 2 39 18 40 3 41 38 42 15 43 39 44 18 45 23 46 37 47 10 48 24 49 47 50 40 51 4 52 19 53 50 54...
output:
Correct.
Test #9:
score: 0
Accepted
time: 0ms
memory: 16000kb
input:
1000 8000 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 0 13 4 14 0 15 10 16 5 17 6 18 14 19 2 20 6 21 10 22 18 23 18 24 0 25 10 26 22 27 11 28 5 29 16 30 14 31 2 32 0 33 0 34 9 35 24 36 34 37 34 38 37 39 15 40 22 41 1 42 32 43 10 44 18 45 35 46 21 47 45 48 32 49 16 50 30 51 44 52 31 53 ...
output:
Correct.
Subtask #2:
score: 0
Time Limit Exceeded
Dependency #1:
100%
Accepted
Test #10:
score: 20
Accepted
time: 2ms
memory: 11952kb
input:
100 100 13 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 0 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 20 20 21 21 22 22 23 23 24 24 25 25 26 26 10 27 28 28 29 29 30 30 31 31 32 32 33 33 34 34 35 35 36 36 27 37 38 38 39 39 40 40 41 41 42 42 43 43 44 44 45 45 46 46 47 47 48 48 49 49 50 50 51 51 ...
output:
Correct.
Test #11:
score: -20
Time Limit Exceeded
input:
135000 142500 2 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 65899 68039 16 0 17 18 18 19 19 20 20 21 21 22 125073 94485 22 23 23 24 24 25 25 26 26 27 27 28 77287 19161 28 29 29 30 30 31 31 32 32 33 33 34 34 17 35 36 36 37 37 38 38 39 39 40 40 41 41 42 42 43 43 44 44 ...
output:
Unauthorized output
Subtask #3:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
0%