This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/** made by amunduzbaev **/
#include "garden.h"
#include "gardenlib.h"
//#include "grader.cpp"
#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define ub upper_bound
#define lb lower_bound
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(),x.rend()
#define fastios ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define Pi acos(-1);
#define mod 1e9+7
#define inf 1e18
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<int> vii;
typedef vector<pll> vpll;
typedef vector<pii> vpii;
template<class T> bool umin(T& a, const T& b) {return a > b? a = b, true:false;}
template<class T> bool umax(T& a, const T& b) {return a < b? a = b, true:false;}
int msb(int val){return sizeof(int)*8-__builtin_clzll(val)-1;}
const int NN = 3e5+5;
vii edges[NN], gg[NN];
stack<int> ss;
int goal, oncyc[NN], last, used[NN], len[NN], in[NN];
int comp[NN], pos[NN], bup[NN];
void dfs(int u){
if(used[u] && in[u]){
oncyc[u] = ++last;
int tmp = 0;
len[last]++;
pos[u] = ++tmp;
//cout<<u<<" ";
while(!ss.empty() && ss.top() != u){
oncyc[ss.top()] = last;
pos[ss.top()] = ++tmp;
len[last]++;
//cout<<ss.top()<<" ";
ss.pop();
}//cout<<"\n";
return;
}
if(used[u]) return;
used[u] = 1;
ss.push(u);
in[u] = 1;
dfs(bup[u]);
in[u] = 0;
}
int tin[NN], tout[NN], tim;
int rr[NN], ddr[NN];
void treepr(int u, int root){
comp[u] = comp[root];
rr[u] = root;
tin[u] = ++tim;
for(auto x:edges[u]){
if(oncyc[x]) continue;
ddr[x] = ddr[u]+1;
treepr(x, root);
}
tout[u] = tim;
}
int dis(int x, int y, int id){
//cout<<x<<" "<<y<<" "<<len[id]<<"\n";
if(y > x) return x + len[id] - y;
return x - y;
}
bool check(int u, int k, int goal){
if(comp[u] != comp[goal]) return 0;
if(oncyc[u] && !oncyc[goal]) return 0;
if(!oncyc[u]){
if(!oncyc[goal]){
return (tin[goal] <= tin[u] && tout[goal] >= tout[u] ? ddr[u] - ddr[goal] == k : 0);
}
else{
k -= ddr[u];
k -= dis(pos[rr[u]], pos[goal], oncyc[goal]);
return (k >= 0 && k % len[oncyc[goal]] == 0);
}
}
else{
//cout<<dis(pos[rr[u]], pos[goal], oncyc[goal])<<"\n";
int tmp = dis(pos[u], pos[goal], oncyc[goal]);
//cout<<u<<" "<<goal<<" "<<tmp<<" "<<k<<"\n";
k -= tmp;
return (k >= 0 && k % len[oncyc[goal]] == 0);
}
}
void count_routes(int n, int m, int p, int R[][2], int q, int kk[]){
for(int i=0; i<m; i++){
int a = R[i][0], b = R[i][1];
if(sz(gg[a]) < 2) gg[a].pb(b);
if(sz(gg[b]) < 2) gg[b].pb(a);
}
memset(rr, -1, sizeof rr);
memset(bup, -1, sizeof bup);
for(int i=0;i<n;i++){
for(int j=0;j<sz(gg[i]);j++){
int to = gg[i][j];
if(gg[to][0] == i && sz(gg[to]) > 1){
bup[i + j * n] = to+n;
edges[to+n].pb(i + j*n);
}else{
bup[i + j * n] = to;
edges[to].pb(i + j*n);
}
}
}
goal = p;
for(int i=0;i<2*n;i++){
if(used[i]) continue;
while(!ss.empty()) ss.pop();
dfs(i);
}
for(int i=0;i<2*n;i++){
if(oncyc[i]){
comp[i] = oncyc[i];
rr[i] = i;
treepr(i, i);
}
}
//cout<<'\n';
for(int i=0;i<q;i++){
int cnt = 0;
for(int j=0;j<n;j++){
cnt += ( check(j, kk[i], p) | check(j, kk[i], p+n) );
//cout<<j<<" "<<cnt<<"\n";
}
//cout<<'\n';
answer(cnt);
}
}
/*
6 6 0
1 2
0 1
0 3
3 4
4 5
1 5
1
3
2
5 5 2
1 0
1 2
3 2
1 3
4 2
2
3 1
1 2
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |