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;
typedef long long ll;
typedef pair<int,int> pii;
const int MAXN = 2e5 + 5;
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define mk make_pair
#define pb push_back
#define fr first
#define sc second
int dist[2][MAXN][2];
bool vis[MAXN][2];
vector<int> edges[MAXN];
vector<pii> inv_edges[MAXN][2];
void dfs(int tipo, int x, int flag, int d) {
if(vis[x][flag]) {
if(dist[tipo][x][flag] == 0) dist[tipo][x][flag] = d;
return;
}
dist[tipo][x][flag] = d;
vis[x][flag] = 1;
for(auto [a,b] : inv_edges[x][flag])
dfs(tipo, a, b, d+1);
}
void count_routes(int N, int M, int P, int R[][2], int Q, int G[]) {
for(int i = 0; i < M; i++) {
int u = R[i][0], v = R[i][1];
if(sz(edges[u]) < 2) edges[u].pb(v);
if(sz(edges[v]) < 2) edges[v].pb(u);
}
for(int i = 0; i < N; i++) {
if(sz(edges[i]) == 1) edges[i].pb(edges[i][0]);
int x = edges[i][0], y = edges[i][1];
inv_edges[x][edges[x][0] == i].pb({i,0});
inv_edges[y][edges[y][0] == i].pb({i,1});
}
memset(dist, -1, sizeof(dist));
dfs(0, P, 0, 0);
memset(vis, 0, sizeof(vis));
dfs(1, P, 1, 0);
for(int t = 0; t < Q; t++) {
int ans = 0, qtd = G[t];
for(int i = 0, D_0, C_0, D_1, C_1; i < N; i++) {
D_0 = dist[0][i][0];
D_1 = dist[1][i][0];
C_0 = dist[0][P][0];
C_1 = dist[1][P][1];
if(D_0 == qtd and C_0 == 0) ans++;
if(C_0 != 0 and D_0 <= qtd and D_0%C_0 == qtd%C_0 and D_0 != -1) ans++;
if(D_1 == qtd and C_1 == 0) ans++;
if(C_1 != 0 and D_1 <= qtd and D_1%C_1 == qtd%C_1 and D_1 != -1) ans++;
}
answer(ans);
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |