This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ff first
#define ss second
#define pb push_back
#define SZ(x) ((int)(x).size())
#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << ": " << x << " "
#define nl cout << "\n"
#define rep(i, a, b) for(int i = (a); i < (b); i++)
#define per(i, a, b) for(int i = (a); i >= (b); i--)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MAXK = 30;
const int MAXN = 2e5+100;
int N, M, P, nxt[2][MAXK][MAXN], wei[2][MAXK][MAXN];
vector<pii> g[MAXN];
void build(){
rep(i, 0, N){
sort(all(g[i]), greater<pii>());
while(SZ(g[i]) > 2) g[i].pop_back();
if(SZ(g[i]) < 2) g[i].pb(g[i][0]);
nxt[0][0][i] = g[i][0].ss;
wei[0][0][i] = g[i][0].ff;
nxt[1][0][i] = g[i][1].ss;
wei[1][0][i] = g[i][1].ff;
}
rep(k, 0, MAXK - 1){
rep(i, 0, N){
int u;
u = nxt[0][k][i];
nxt[0][k + 1][i] = nxt[0][k][u];
wei[0][k + 1][i] = wei[0][k][u];
if(wei[0][k][i] == wei[0][0][u]){
nxt[0][k + 1][i] = nxt[1][k][u];
wei[0][k + 1][i] = wei[1][k][u];
}
u = nxt[1][k][i];
nxt[1][k + 1][i] = nxt[0][k][u];
wei[1][k + 1][i] = wei[0][k][u];
if(wei[1][k][i] == wei[0][0][u]){
nxt[1][k + 1][i] = nxt[1][k][u];
wei[1][k + 1][i] = wei[1][k][u];
}
}
}
}
int jump(int v, int K){
int w = -1;
per(k, MAXK - 1, 0){
if(K >> k & 1){
if(wei[0][0][v] != w){
w = wei[0][k][v];
v = nxt[0][k][v];
} else{
w = wei[1][k][v];
v = nxt[1][k][v];
}
}
}
return v;
}
int query(int K){
int ans = 0;
rep(i, 0, N){
if(jump(i, K) == P)
ans++;
}
return ans;
}
void answer(int x);
void count_routes(int N, int M, int P, int R[][2], int Q, int G[]){
::N = N, ::M = M, ::P = P;
per(m, M - 1, 0){
int u = R[m][0], v = R[m][1];
g[u].pb({m, v});
g[v].pb({m, u});
}
build();
rep(q, 0, Q){
answer(query(G[q]));
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |