#include "garden.h"
#include "gardenlib.h"
#include<bits/stdc++.h>
using namespace std;
/*
#define MAX_M 1000000
#define MAX_Q 2000
static int N, M, P, Q;
static int R[MAX_M][2];
static int G[MAX_Q];
static int solutions[MAX_Q];
static int answers[MAX_Q];
static int answer_count;
void read_input()
{
int i;
scanf("%d %d %d",&N,&M,&P);
for(i=0; i<M; i++)
scanf("%d %d",&R[i][0],&R[i][1]);
scanf("%d",&Q);
for(i=0; i<Q; i++)
scanf("%d",&G[i]);
for(i=0; i<Q; i++)
scanf("%d",&solutions[i]);
}
void answer(int x)
{
if(answer_count>=Q) {
printf("Incorrect. Too many answers.\n");
exit(0);
}
answers[answer_count] = x;
answer_count++;
}
*/
#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][2];
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 rk,int cnt,int id) {
if (u==p and rk==id) {
len[id]=cnt;
return;
}
vis[u][rk]=true;
int v = best[u],rkk=0;
if (v==fa and best2[u]!=-1) v = best2[u],rkk=1;
if (vis[v][rkk]) return;
dfs(v,u,rkk,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,0,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,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) for (int k=G[i]%len[j];k<maxn&&k<G[i];k+=len[j]) res+=ans[k][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;
}
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
24440 KB |
Output is correct |
2 |
Correct |
14 ms |
24396 KB |
Output is correct |
3 |
Correct |
14 ms |
24404 KB |
Output is correct |
4 |
Incorrect |
12 ms |
24388 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
24440 KB |
Output is correct |
2 |
Correct |
14 ms |
24396 KB |
Output is correct |
3 |
Correct |
14 ms |
24404 KB |
Output is correct |
4 |
Incorrect |
12 ms |
24388 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
24440 KB |
Output is correct |
2 |
Correct |
14 ms |
24396 KB |
Output is correct |
3 |
Correct |
14 ms |
24404 KB |
Output is correct |
4 |
Incorrect |
12 ms |
24388 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |