# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
411043 | nxteru | Tropical Garden (IOI11_garden) | C++14 | 0 ms | 0 KiB |
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 pair<int,int> P;
#define F first
#define S second
#define PB push_back
#define INF 1000000001
int n,m,q,k,nx[300005],t[2],fi[150005],vis[300005],sz[300005],d[2][300005],dd[300005][2];
vector<P>e[150005];
vector<int>g[300005];
bool rp[300005];
void dfs(int v,int cn){
vis[v]=k;
sz[v]=cn;
int u=nx[v];
if(vis[u]==-1)dfs(u,cn+1);
else if(vis[u]==k){
int s=cn-sz[u]+1;
u=v;
do{
u=nx[u];
rp[u]=true;
sz[u]=s;
}while(u!=v);
}
}
void count_routes(int N, int M, int p, int R[][2], int Q, int G[]){
n=N,m=M,q=Q;
for(int i=0;i<m;i++){
int a=R[i][0],b=R[i][1];
e[a].PB(P(i,0));
e[b].PB(P(i,1));
}
for(int i=0;i<m;i++){
int a=R[i][0],b=R[i][1];
if(e[b].size()==1||e[b][0].F!=i)nx[i*2]=e[b][0].F*2+e[b][0].S;
else nx[i*2]=e[b][1].F*2+e[b][1].S;
if(e[a].size()==1||e[a][0].F!=i)nx[i*2+1]=e[a][0].F*2+e[a][0].S;
else nx[i*2+1]=e[a][1].F*2+e[a][1].S;
}
for(int i=0;i<n;i++)fi[i]=e[i][0].F*2+e[i][0].S;
t[0]=fi[p];
if(e[p].size()>1)t[1]=e[p][1].F*2+e[p][1].S;
else t[1]=t[0];
for(int i=0;i<m*2;i++)vis[i]=-1;
for(int i=0;i<m*2;i++){
if(vis[i]==-1){
dfs(i,0);
k++;
}
}
for(int i=0;i<m*2;i++)g[nx[i]].PB(i);
for(int i=0;i<2;i++){
for(int j=0;j<m*2;j++)d[i][j]=INF;
d[i][t[i]]=0;
queue<int>bfs;
bfs.push(t[i]);
while(!bfs.empty()){
int v=bfs.front(),c=d[i][v];
bfs.pop();
for(int j=0;j<g[v].size();j++){
int u=g[v][j];
if(d[i][u]==INF){
d[i][u]=c+1;
bfs.push(u);
}
}
}
}
for(int i=0;i<n;i++){
for(int j=;j<2;j++){
dd[i][j]=d[j][i];
}
}
for(int i=0;i<q;i++){
int ans=0,x=G[i];
for(int j=0;j<n;j++){
int v=fi[j];
bool ok=false;
for(int l=0;l<2;l++)if(dd[v][l]==x||(rp[t[l]]&&dd[v][l]<=x&&(x-d[v][l])%sz[t[l]]==0))ok=true;
if(ok)ans++;
}
answer(ans);
}
}