#include <bits/stdc++.h>
#include "garden.h"
#include "gardenlib.h"
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
#define block_of_code if(true)
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll gcd(ll a, ll b){return __gcd(a, b);}
ll lcm(ll a, ll b){return a / gcd(a, b) * b;}
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
double rngesus_d(double l, double r){
double cur = rngesus(0, MASK(60) - 1);
cur /= MASK(60) - 1;
return l + cur * (r - l);
}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
template <class T>
void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
for(auto item: container) out << item << separator;
out << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
void answer(int w){
cout << w << "\n";
}
const int N = 150069;
int n, m, p, q;
vector<pair<int,int>> graph[N];
int mi[N];
int nxt[N * 2];
int visited[N * 2];
int cycle[N * 2];
void reset(){
for(int i = 0; i<n; ++i){
graph[i].clear();
}
}
void dfs(int u){
if (visited[u] == 2) return;
if (visited[u] == 1){
int v = u;
while(!cycle[v]){
cycle[v] = 1;
v = nxt[v];
}
return;
}
visited[u] = 1;
dfs(nxt[u]);
visited[u] = 2;
}
vector<int> function_graph[N * 2];
vector<int> answer_queries(int p, int G[]){
vector<int> ans(q);
for(int i =0; i < n * 2; ++i){
visited[i] = cycle[i] = 0;
function_graph[i].clear();
}
dfs(p);
for(int i = 0; i<n*2; ++i) if (i != p) function_graph[nxt[i]].push_back(i);
if (cycle[p]){
int cycle_sz = 0;
for(int i = 0; i<n*2; ++i) cycle_sz += cycle[i];
vector<vector<int>> sigma(cycle_sz);
deque<pair<int, int>> q;
q.push_back({p, 0});
while(q.size()){
pair<int,int> u = q.front(); q.pop_front();
if (u.first % 2 == 0) {
sigma[u.second % cycle_sz].push_back(u.second);
}
for(int v: function_graph[u.first])
q.push_back({v, u.second + 1});
}
for(int i = 0; i<cycle_sz; ++i) sort(ALL(sigma[i]));
for(int i = 0; i < ans.size(); ++i) {
int r = G[i] % cycle_sz;
ans[i] = upper_bound(ALL(sigma[r]), G[i]) - sigma[r].begin();
}
}
else{
map<int, int> mp;
deque<pair<int, int>> q;
q.push_back({p, 0});
while(q.size()){
pair<int,int> u = q.front(); q.pop_front();
if (u.first % 2 == 0) mp[u.second]++;
for(int v: function_graph[u.first])
q.push_back({v, u.second + 1});
}
for(int i = 0; i < ans.size(); ++i) {
ans[i] = mp[G[i]];
}
}
return ans;
}
void count_routes(int N, int M, int P, int R[][2], int Q, int G[]){
n = N, m = M, p = P, q = Q;
reset();
for(int i = 0; i<m; ++i){
int u = R[i][0], v = R[i][1];
graph[u].push_back({v, i});
graph[v].push_back({u, i});
}
for(int i = 0; i<n; ++i){
sort(ALL(graph[i]), [](pair<int,int> x, pair<int,int> y){return x.second < y.second;});
mi[i] = graph[i][0].second;
}
for(int i = 0; i<n; ++i){
for(int j = 0; j <= 1; ++j){
int cur, color;
tie(cur, color) = graph[i][min(j, graph[i].size() - 1)];
if (color == mi[cur]) cur = cur * 2 + 1;
else cur = cur * 2;
nxt[i * 2 + j] = cur;
}
}
// answering queries to check if vertices u will end up on p * 2
vector<int> ans1 = answer_queries(p * 2, G);
// answering queries to check if vertices u will end up on p * 2 + 1
vector<int> ans2 = answer_queries(p * 2 + 1, G);
for(int i = 0; i<q; ++i) answer(ans1[i] + ans2[i]);
}
// int main(void){
// ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
// clock_t start = clock();
// int n, m, p; cin >> n >> m >> p;
// int r[m][2];
// for(int i = 0; i<m; ++i) cin >> r[i][0] >> r[i][1];
// int q; cin >> q;
// int g[q];
// for(int i = 0; i<q; ++i){
// cin >> g[i];
// }
// count_routes(n, m, p, r, q, g);
// cerr << "Time elapsed: " << clock() - start << " ms\n";
// return 0;
// }
Compilation message
garden.cpp: In function 'std::vector<int> answer_queries(int, int*)':
garden.cpp:122:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
122 | for(int i = 0; i < ans.size(); ++i) {
| ~~^~~~~~~~~~~~
garden.cpp:139:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
139 | for(int i = 0; i < ans.size(); ++i) {
| ~~^~~~~~~~~~~~
/usr/bin/ld: /tmp/cc38h9Rz.o: in function `answer(int)':
grader.cpp:(.text+0x130): multiple definition of `answer(int)'; /tmp/ccXQPszA.o:garden.cpp:(.text+0xce0): first defined here
collect2: error: ld returned 1 exit status