/*
u -> second best and others
u + n -> best
*/
#include "garden.h"
#include "gardenlib.h"
#include<bits/stdc++.h>
using namespace std;
#define INF 1000000
#define MAXN 150000
int N;
vector<pair<int, int> > oriAdj[MAXN + 3]; // {beauty, v}
vector<int> adj[2 * MAXN + 3], revAdj[2 * MAXN + 3];
int dist[2 * MAXN + 3][2], cycleLength[2];
bool visited[2 * MAXN + 3][2];
void addEdgeToNewGraph(int u, int v, int beauty) {
bool isBestForV = (beauty == oriAdj[v][0].first);
int resolvedV = (isBestForV ? v + N : v);
adj[u].push_back(resolvedV);
}
void dfs(int u, int t, int step, int origin) {
dist[u][t] = step;
visited[u][t] = true;
for (auto v: revAdj[u]) {
if (v == origin) {
cycleLength[t] = step + 1;
}
if (!visited[v][t]) {
dfs(v, t, step + 1, origin);
}
}
}
void count_routes(int n, int m, int p, int r[][2], int q, int g[]) {
N = n;
for (int i = 0; i < m; i++) {
int u = r[i][0], v = r[i][1], beauty = i;
oriAdj[u].push_back({beauty, v});
oriAdj[v].push_back({beauty, u});
}
for (int u = 0; u < n; u++) {
sort(oriAdj[u].begin(), oriAdj[u].end());
}
for (int u = 0; u < n; u++) {
// Add directed edge from u to v or (v + n).
if (true) {
auto [beauty, v] = oriAdj[u][0];
addEdgeToNewGraph(u, v, beauty);
}
// Add directed edge from (u + n) to v or (v + n).
if (true) {
auto [beauty, v] = oriAdj[u].size() > 1 ? oriAdj[u][1] : oriAdj[u][0];
addEdgeToNewGraph(u + n, v, beauty);
}
}
// cout <<"\nadj\n";
// for (int u = 0; u < 2 * n; u++) {
// cout << u << ":";
// for (auto v: adj[u]) {
// cout << " " << v;
// }
// cout << endl;
// }
/*
Reverse the edges of the new graph.
This could be done previously, but to make it more understandable, I do it separately.
*/
for (int u = 0; u < 2 * n; u++) {
for (auto v: adj[u]) {
revAdj[v].push_back(u);
}
}
// cout <<"\nrevAdj\n";
// for (int u = 0; u < 2 * n; u++) {
// cout << u << ":";
// for (auto v: revAdj[u]) {
// cout << " " << v;
// }
// cout << endl;
// }
for (int t = 0; t < 2; t++) {
cycleLength[t] = -1;
for (int i = 0; i < 2 * n; i++) {
dist[i][t] = INF;
visited[i][t] = false;
}
}
dfs(p, 0, 0, p);
dfs(p + n, 1, 0, p + n);
// cout << "\ncycleLength[0]: " << cycleLength[0] << endl;
// cout << "cycleLength[1]: " << cycleLength[1] << endl;
// cout << "\ndist0:\n";
// for (int u = 0; u < 2 * n; u++) {
// cout << u << ": " << dist[u][0] << endl;
// }
// cout << "\ndist1:\n";
// for (int u = 0; u < 2 * n; u++) {
// cout << u << ": " << dist[u][1] << endl;
// }
for (int query = 0; query < q; query++) {
int ans = 0;
for (int u = 0; u < n; u++) {
bool possible = false;
for (int t = 0; t < 2; t++) {
if (dist[u][t] == g[query] || (cycleLength[t] != -1 && g[query] >= dist[u][t] && (g[query] - dist[u][t])%cycleLength[t] == 0)) {
possible = true;
}
// if (oriAdj[u].size() == 1) {
// if (dist[u + n][t] == g[query] || (cycleLength[t] != -1 && dist[u + n][t] == g[query]%cycleLength[t])) {
// possible = true;
// }
// }
}
if (possible) {
ans += 1;
}
}
answer(ans);
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
18080 KB |
Output is correct |
2 |
Correct |
11 ms |
18056 KB |
Output is correct |
3 |
Correct |
10 ms |
18060 KB |
Output is correct |
4 |
Correct |
10 ms |
17876 KB |
Output is correct |
5 |
Correct |
9 ms |
17876 KB |
Output is correct |
6 |
Correct |
10 ms |
18260 KB |
Output is correct |
7 |
Correct |
9 ms |
17876 KB |
Output is correct |
8 |
Correct |
10 ms |
18064 KB |
Output is correct |
9 |
Correct |
11 ms |
18320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
18080 KB |
Output is correct |
2 |
Correct |
11 ms |
18056 KB |
Output is correct |
3 |
Correct |
10 ms |
18060 KB |
Output is correct |
4 |
Correct |
10 ms |
17876 KB |
Output is correct |
5 |
Correct |
9 ms |
17876 KB |
Output is correct |
6 |
Correct |
10 ms |
18260 KB |
Output is correct |
7 |
Correct |
9 ms |
17876 KB |
Output is correct |
8 |
Correct |
10 ms |
18064 KB |
Output is correct |
9 |
Correct |
11 ms |
18320 KB |
Output is correct |
10 |
Correct |
9 ms |
17896 KB |
Output is correct |
11 |
Incorrect |
21 ms |
21964 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
18080 KB |
Output is correct |
2 |
Correct |
11 ms |
18056 KB |
Output is correct |
3 |
Correct |
10 ms |
18060 KB |
Output is correct |
4 |
Correct |
10 ms |
17876 KB |
Output is correct |
5 |
Correct |
9 ms |
17876 KB |
Output is correct |
6 |
Correct |
10 ms |
18260 KB |
Output is correct |
7 |
Correct |
9 ms |
17876 KB |
Output is correct |
8 |
Correct |
10 ms |
18064 KB |
Output is correct |
9 |
Correct |
11 ms |
18320 KB |
Output is correct |
10 |
Correct |
9 ms |
17896 KB |
Output is correct |
11 |
Incorrect |
21 ms |
21964 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |