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 "garden.h"
#include "gardenlib.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(x) begin(x), end(x)
#define sz(x) (int)x.size()
#define pb push_back
const int maxn = 300000;
vector<int> adj[maxn];
bool bl[maxn];
int d[maxn];
void dfs(int s, int p){
if(p != -1) d[s] = d[p]+1;
for(auto u: adj[s]){
if(u == p or bl[u]) continue;
dfs(u, s);
}
}
void count_routes(int N, int M, int P, int R[][2], int Q, int G[]){
int n = N, m = M;
vector<pair<int, int>> vec[n];
int mn[n] = {};
for(int i=1; i<=m; i++){
int a, b; a = R[i-1][0], b = R[i-1][1];
vec[a].pb({b, i}); vec[b].pb({a, i});
if(mn[a] == 0) mn[a] = i;
if(mn[b] == 0) mn[b] = i;
}
int to[2*n] = {};
for(int a=0; a<n; a++){
auto [b, i] = vec[a][0];
if(mn[b] == i) to[a] = n+b;
else to[a] = b;
if(sz(vec[a]) >= 2){
auto [c, j] = vec[a][1];
if(mn[c] == j) to[n+a] = n+c;
else to[n+a] = c;
}
else{
if(mn[b] == i) to[n+a] = n+b;
else to[n+a] = b;
}
}
for(int a=0; a<2*n; a++){
adj[a].pb(to[a]); adj[to[a]].pb(a);
}
int p = P;
int q = Q;
int qry[q] = {};
for(int i=0; i<q; i++) qry[i] = G[i];
int ans[q] = {};
auto count = [&](int p){
int x = p, y = to[p];
while(x != y){
x = to[x];
y = to[to[y]];
}
vector<int> cyc; cyc.pb(x);
fill(bl, bl+2*n, 0);
bl[x] = 1;
while(to[cyc.back()] != x){
cyc.pb(to[cyc.back()]);
bl[cyc.back()] = 1;
}
int l = sz(cyc);
fill(d, d+2*n, -1);
int ok = 0;
if(find(all(cyc), p) != end(cyc)){
ok = 1;
d[p] = 0;
dfs(p, -1);
x = to[p];
int c = l-1;
while(x != p){
d[x] = c;
dfs(x, -1);
x = to[x];
c--;
}
}
else{
bl[to[p]] = 1;
d[p] = 0;
dfs(p, -1);
}
for(int i=0; i<q; i++){
int k = qry[i];
for(int a=0; a<n; a++){
if(d[a] == -1) continue;
if(ok){
if(k >= d[a] and (k-d[a])%l == 0) ans[i]++;
}
else{
if(k == d[a]) ans[i]++;
}
}
}
};
count(p); count(n+p);
for(int i=0; i<q; i++){
answer(ans[i]);
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |