#include <bits/stdc++.h>
#include "garden.h"
#include "gardenlib.h"
//~ #include "grader.cpp"
using namespace std;
const int maxn = 150001;
const int maxk = 31;
int n, m, p, g[maxn][maxk];
vector <int> v[maxn];
int go(int node, int prev){
if(v[node].size() == 1) return v[node][0];
if(v[node][0] == prev) return v[node][1];
return v[node][0];
}
int calc(int node, int steps){
int prev = -1;
while(steps--){
int pcur = node;
node = go(node, prev);
prev = pcur;
}
return node;
}
int lift(int node, int s){
for(int i = maxk - 1 ; i >= 0 && s > 4 ; i--){
if((s >> i) & 1){
node = g[node][i];
s -= (1 << i);
}
}
return calc(node, s);
}
void count_routes(int N, int M, int P, int R[][2], int Q, int G[]){
n = N;
m = M;
p = P;
for(int i = 0 ; i < M ; i++){
v[R[i][0]].push_back(R[i][1]);
v[R[i][1]].push_back(R[i][0]);
}
for(int node = 0 ; node < n ; node++){
g[node][0] = calc(node, 1);
g[node][1] = calc(node, 2);
g[node][2] = calc(node, 4);
}
for(int j = 3 ; j < maxk ; j++){
for(int node = 0 ; node < n ; node++){
g[node][j] = g[g[node][j - 1]][j - 1];
}
}
for(int i = 0 ; i < Q ; i++){
int ans = 0;
for(int node = 0 ; node < n ; node++){
ans += lift(node, G[i]) == p;
}
answer(ans);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
3932 KB |
Output is correct |
2 |
Incorrect |
6 ms |
3960 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
3932 KB |
Output is correct |
2 |
Incorrect |
6 ms |
3960 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
3932 KB |
Output is correct |
2 |
Incorrect |
6 ms |
3960 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |