#include "garden.h"
#include "gardenlib.h"
#include<bits/stdc++.h>
using namespace std;
#define MAXN 150000
#define MAXE 300000
#define MAXLG 18
struct DirectedEdge {
int id, from, to, beauty;
DirectedEdge(int id_, int from_, int to_, int beauty_): id(id_), from(from_), to(to_), beauty(beauty_) {}
bool operator<(DirectedEdge other) const {
return beauty < other.beauty;
}
};
vector<DirectedEdge> directedEdges, goingOutEdges[MAXN + 3];
int nxt[MAXE + 3][MAXLG + 3];
void count_routes(int n, int m, int p, int r[][2], int q, int g[]) {
int numberOfDirectedEdges = 0;
for (int i = 0; i < m; i++) {
int u = r[i][0], v = r[i][1], beauty = i;
DirectedEdge uvEdge = DirectedEdge(numberOfDirectedEdges++, u, v, beauty);
DirectedEdge vuEdge = DirectedEdge(numberOfDirectedEdges++, v, u, beauty);
directedEdges.push_back(uvEdge);
goingOutEdges[u].push_back(uvEdge);
directedEdges.push_back(vuEdge);
goingOutEdges[v].push_back(vuEdge);
}
for (int i = 0; i < n; i++) {
sort(goingOutEdges[i].begin(), goingOutEdges[i].end());
}
// cout << "\ngoingOutEdges\n";
// for (int i = 0; i < n; i++) {
// cout << i << ":";
// for (auto edge: goingOutEdges[i]) {
// cout << " (" << edge.id << ", " << edge.from << ", " << edge.to << ", " << edge.beauty << ")";
// }
// cout << endl;
// }
for (auto directedEdge: directedEdges) {
int firstEdgeId = directedEdge.id;
int secondEdgeId;
if (goingOutEdges[directedEdge.to].size() == 1) {
secondEdgeId = goingOutEdges[directedEdge.to][0].id;
} else {
for (auto goingOutEdge: goingOutEdges[directedEdge.to]) {
if (goingOutEdge.beauty != directedEdge.beauty) {
secondEdgeId = goingOutEdge.id;
break;
}
}
}
nxt[firstEdgeId][0] = secondEdgeId;
}
// cout << "\nnxt\n";
// for (int i = 0; i < numberOfDirectedEdges; i++) {
// cout << i << ": " << nxt[i][0] << endl;
// }
for (int lg = 1; lg <= MAXLG; lg++) {
for (int i = 0; i < numberOfDirectedEdges; i++) {
nxt[i][lg] = nxt[nxt[i][lg - 1]][lg - 1];
}
}
for (int i = 0; i < q; i++) {
int ans = 0;
for (int startingNode = 0; startingNode < n; startingNode++) {
int currentEdgeId = goingOutEdges[startingNode][0].id;
// Jump (g[i] - 1) times
int numJumps = g[i] - 1;
for (int lg = 0; lg <= MAXLG; lg++) {
if (numJumps & (1 << lg)) {
currentEdgeId = nxt[currentEdgeId][lg];
}
}
if (directedEdges[currentEdgeId].to == p) {
ans += 1;
}
}
answer(ans);
}
}
Compilation message
garden.cpp: In function 'void count_routes(int, int, int, int (*)[2], int, int*)':
garden.cpp:63:25: warning: 'secondEdgeId' may be used uninitialized in this function [-Wmaybe-uninitialized]
63 | nxt[firstEdgeId][0] = secondEdgeId;
| ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4052 KB |
Output is correct |
2 |
Correct |
3 ms |
4104 KB |
Output is correct |
3 |
Correct |
3 ms |
4108 KB |
Output is correct |
4 |
Correct |
3 ms |
3796 KB |
Output is correct |
5 |
Correct |
2 ms |
3796 KB |
Output is correct |
6 |
Correct |
4 ms |
4308 KB |
Output is correct |
7 |
Correct |
3 ms |
3840 KB |
Output is correct |
8 |
Correct |
3 ms |
4100 KB |
Output is correct |
9 |
Correct |
6 ms |
5968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4052 KB |
Output is correct |
2 |
Correct |
3 ms |
4104 KB |
Output is correct |
3 |
Correct |
3 ms |
4108 KB |
Output is correct |
4 |
Correct |
3 ms |
3796 KB |
Output is correct |
5 |
Correct |
2 ms |
3796 KB |
Output is correct |
6 |
Correct |
4 ms |
4308 KB |
Output is correct |
7 |
Correct |
3 ms |
3840 KB |
Output is correct |
8 |
Correct |
3 ms |
4100 KB |
Output is correct |
9 |
Correct |
6 ms |
5968 KB |
Output is correct |
10 |
Correct |
2 ms |
3796 KB |
Output is correct |
11 |
Incorrect |
16 ms |
9860 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4052 KB |
Output is correct |
2 |
Correct |
3 ms |
4104 KB |
Output is correct |
3 |
Correct |
3 ms |
4108 KB |
Output is correct |
4 |
Correct |
3 ms |
3796 KB |
Output is correct |
5 |
Correct |
2 ms |
3796 KB |
Output is correct |
6 |
Correct |
4 ms |
4308 KB |
Output is correct |
7 |
Correct |
3 ms |
3840 KB |
Output is correct |
8 |
Correct |
3 ms |
4100 KB |
Output is correct |
9 |
Correct |
6 ms |
5968 KB |
Output is correct |
10 |
Correct |
2 ms |
3796 KB |
Output is correct |
11 |
Incorrect |
16 ms |
9860 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |