#include "garden.h"
#include "gardenlib.h"
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pb push_back
#define debug(x) cerr<<#x<<"="<<x<<endl
#define pq priority_queue
#define inf 0x3f
#define rep(i,a,b) for (int i=a;i<(b);i++)
#define MP make_pair
#define SZ(x) (int(x.size()))
#define ll long long
#define mod 1000000007
#define ALL(x) x.begin(),x.end()
void inc(int &a,int b) {a=(a+b)%mod;}
void dec(int &a,int b) {a=(a-b+mod)%mod;}
int lowbit(int x) {return x&(-x);}
ll p0w(ll base,ll p) {ll ret=1;while(p>0){if (p%2ll==1ll) ret=ret*base%mod;base=base*base%mod;p/=2ll;}return ret;}
const int maxn = 3e5;
int best[maxn],best2[maxn];
vector <int> edge[maxn][2];
int len[2]={0,0};
bool vis[maxn];
int dis[maxn][2][2];
int ans[maxn][2];
pq <pii,vector<pii>,greater<pii> > q;
int p;
void dfs(int u,int fa,int cnt,int id) {
if (u==p) {
len[id]=cnt;
return;
}
vis[u]=true;
int v = best[u];
if (v==fa and best2[u]!=-1) v = best2[u];
if (vis[v]) return;
dfs(v,u,cnt+1,id);
}
void dijkstra(int id) {
while (!q.empty()) {
int uu = q.top().se;
int d = q.top().fi;
int u = uu%maxn,a=uu/maxn;
q.pop();
if (dis[u][a][id]!=d) continue;
if (a==0) {
ans[d][id]++;
for (int v:edge[u][0]) if (dis[v][0][id]>d+1) {
dis[v][0][id]=d+1;
q.push(MP(d+1,v));
}
for (int v:edge[u][1]) if (dis[v][1][id]>d+1) {
dis[v][1][id]=d+1;
q.push(MP(d+1,v+maxn));
}
}
else {
if (best[best[u]]==u&&dis[best[u]][0][id]>d+1) {
dis[best[u]][0][id]=d+1;
q.push(MP(d+1,best[u]));
}
else if (best2[best[u]]==u&&dis[best[u]][1][id]>d+1) {
dis[best[u]][1][id]=d+1;
q.push(MP(d+1,best[u]));
}
}
}
}
void count_routes(int N, int M, int P, int R[][2], int Q, int G[])
{
p = P;
memset(best,-1,sizeof(best));
memset(best2,-1,sizeof(best2));
memset(dis,inf,sizeof(dis));
memset(ans,0,sizeof(ans));
rep(i,0,M) {
if (best[R[i][0]]==-1) best[R[i][0]] = R[i][1],edge[R[i][1]][0].pb(R[i][0]);
else if (best2[R[i][0]]==-1) best2[R[i][0]] = R[i][1],edge[R[i][1]][1].pb(R[i][0]);
if (best[R[i][1]]==-1) best[R[i][1]] = R[i][0],edge[R[i][0]][0].pb(R[i][1]);
else if (best2[R[i][1]]==-1) best2[R[i][1]] = R[i][0],edge[R[i][0]][1].pb(R[i][1]);
}
memset(vis,0,sizeof(vis));
dfs(best[p],p,1,0);
dis[p][0][0] = 0;
q.push(MP(0,p));
dijkstra(0);
memset(vis,0,sizeof(vis));
dfs(best2[p],p,1,1);
dis[p][1][1] = 0;
q.push(MP(0,maxn+p));
dijkstra(1);
rep(i,0,Q) {
int res = 0;
rep(j,0,2) {
if (len[j]==0 and G[i]<maxn) res+=ans[G[i]][j];
else if (len[j]>0) res+=ans[G[i]%len[0]][j];
}
answer(res);
}
}
/*
int main()
{
int correct, i;
read_input();
answer_count = 0;
count_routes(N,M,P,R,Q,G);
if(answer_count!=Q) {
printf("Incorrect. Too few answers.\n");
exit(0);
}
correct = 1;
for(i=0; i<Q; i++)
if(answers[i]!=solutions[i])
correct = 0;
if(correct)
printf("Correct.\n");
else {
printf("Incorrect.\n");
printf("Expected: ");
for(i=0; i<Q; i++)
printf("%d ",solutions[i]);
printf("\nReturned: ");
for(i=0; i<Q; i++)
printf("%d ",answers[i]);
}
return 0;
}
5 5 2
1 0
1 2
3 2
1 3
4 2
2
3 1
1 2
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
12 ms |
24148 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
12 ms |
24148 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
12 ms |
24148 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |