#include "garden.h"
#include "gardenlib.h"
#include <vector>
#include <iostream>
using namespace std;
const int nmax=300005;
int i,nod,old,state,oldstate,nr,x,nxt,cycles,sz1,sz2,j;
vector<int> v[2*nmax];
int p[nmax][2],mare[nmax];
int viz[2*nmax],dist[2][2*nmax],st[2*nmax],cyc[nmax],sz[2*nmax],rel[nmax];
void link_(int A,int B)
{
if(p[A][0]==-1) p[A][0]=B;
else if(p[A][1]==-1) p[A][1]=B;
if(mare[A]==-1) mare[A]=B;
}
void dfs(int B,int x)
{
for(int i=0;i<v[x].size();i++)
if(!dist[B][v[x][i]])
{
dist[B][v[x][i]]=dist[B][x]+1;
dfs(B,v[x][i]);
}
}
void count_routes(int N, int M, int P, int R[][2], int Q, int G[])
{
for(i=0;i<N;i++)
p[i][0]=p[i][1]=mare[i]=-1;
for(i=0;i<M;i++)
{
link_(R[i][0],R[i][1]);
link_(R[i][1],R[i][0]);
}
for(i=0;i<N;i++)
if(p[i][1]==-1)
mare[i]=N+1;
for(nod=0;nod<N;nod++)
if(!viz[2*nod])
{
x=nod;old=-1;state=2*N;nr=0;oldstate=-1;
while(!viz[x*2+(old==mare[x])])
{
state=x*2+(old==mare[x]);
st[++nr]=state;
if(oldstate!=-1)v[state].push_back(oldstate);
if(old==mare[x]&&p[x][1]!=-1) x=p[x][1];
else x=p[x][0];
old=state/2;
viz[state]=1;
oldstate=state;
}
st[nr+1]=x*2+(old==mare[x]);
if(nr)v[st[nr+1]].push_back(st[nr]);
if(cyc[st[nr+1]])
for(i=nr;i>=1;i--)
{
x=st[i];nxt=st[i+1];
cyc[x]=cyc[nxt];
}
else
{
cycles++;
for(i=1;i<=nr;i++)
{
x=st[i];
cyc[x]=cycles;
if(st[i]==st[nr+1])
{
for(int k=i;k<=nr;k++)
sz[st[k]]=nr+1-i;
}
}
}
}
nr=0;
for(i=0;i<N;i++)
if(cyc[2*i]==cyc[2*P]||cyc[2*i]==cyc[2*P+1])
{
rel[++nr]=i;
}
dist[0][2*P]=dist[1][2*P+1]=1;
dfs(0,2*P);
dfs(1,2*P+1);
for(i=0;i<2;i++)
for(j=0;j<2*N;j++)
if(!dist[i][j])
dist[i][j]=1000*1000*1000+5;
sz1=sz[2*P];sz2=sz[2*P+1];
for(i=0; i<Q; i++)
{
int ret=0,ok=0;
G[i]++;
for(j=1;j<=nr;j++)
{
x=2*rel[j];ok=0;
if(sz1)
{
if((dist[0][x]<=G[i]&&(G[i]-dist[0][x])%sz1==0))
ok=1;
}
else
if(dist[0][x]==G[i])
ok=1;
if(viz[2*P+1])
{
if(sz2)
{
if((dist[1][x]<=G[i]&&(G[i]-dist[1][x])%sz2==0))
ok=1;
}
else
if(dist[1][x]==G[i])
ok=1;
}
ret+=ok;
}
answer(ret);
}
}
Compilation message
garden.cpp: In function 'void dfs(int, int)':
garden.cpp:19:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<v[x].size();i++)
~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
14584 KB |
Output is correct |
2 |
Correct |
15 ms |
14584 KB |
Output is correct |
3 |
Correct |
15 ms |
14584 KB |
Output is correct |
4 |
Correct |
14 ms |
14548 KB |
Output is correct |
5 |
Correct |
15 ms |
14456 KB |
Output is correct |
6 |
Correct |
15 ms |
14584 KB |
Output is correct |
7 |
Correct |
15 ms |
14524 KB |
Output is correct |
8 |
Correct |
15 ms |
14500 KB |
Output is correct |
9 |
Correct |
17 ms |
14584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
14584 KB |
Output is correct |
2 |
Correct |
15 ms |
14584 KB |
Output is correct |
3 |
Correct |
15 ms |
14584 KB |
Output is correct |
4 |
Correct |
14 ms |
14548 KB |
Output is correct |
5 |
Correct |
15 ms |
14456 KB |
Output is correct |
6 |
Correct |
15 ms |
14584 KB |
Output is correct |
7 |
Correct |
15 ms |
14524 KB |
Output is correct |
8 |
Correct |
15 ms |
14500 KB |
Output is correct |
9 |
Correct |
17 ms |
14584 KB |
Output is correct |
10 |
Correct |
15 ms |
14492 KB |
Output is correct |
11 |
Correct |
25 ms |
16732 KB |
Output is correct |
12 |
Correct |
37 ms |
17436 KB |
Output is correct |
13 |
Correct |
68 ms |
33784 KB |
Output is correct |
14 |
Correct |
97 ms |
23836 KB |
Output is correct |
15 |
Correct |
107 ms |
24312 KB |
Output is correct |
16 |
Correct |
88 ms |
21624 KB |
Output is correct |
17 |
Correct |
75 ms |
20344 KB |
Output is correct |
18 |
Correct |
39 ms |
17580 KB |
Output is correct |
19 |
Correct |
123 ms |
23748 KB |
Output is correct |
20 |
Correct |
115 ms |
24376 KB |
Output is correct |
21 |
Correct |
93 ms |
22136 KB |
Output is correct |
22 |
Correct |
129 ms |
21272 KB |
Output is correct |
23 |
Correct |
131 ms |
25204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
14584 KB |
Output is correct |
2 |
Correct |
15 ms |
14584 KB |
Output is correct |
3 |
Correct |
15 ms |
14584 KB |
Output is correct |
4 |
Correct |
14 ms |
14548 KB |
Output is correct |
5 |
Correct |
15 ms |
14456 KB |
Output is correct |
6 |
Correct |
15 ms |
14584 KB |
Output is correct |
7 |
Correct |
15 ms |
14524 KB |
Output is correct |
8 |
Correct |
15 ms |
14500 KB |
Output is correct |
9 |
Correct |
17 ms |
14584 KB |
Output is correct |
10 |
Correct |
15 ms |
14492 KB |
Output is correct |
11 |
Correct |
25 ms |
16732 KB |
Output is correct |
12 |
Correct |
37 ms |
17436 KB |
Output is correct |
13 |
Correct |
68 ms |
33784 KB |
Output is correct |
14 |
Correct |
97 ms |
23836 KB |
Output is correct |
15 |
Correct |
107 ms |
24312 KB |
Output is correct |
16 |
Correct |
88 ms |
21624 KB |
Output is correct |
17 |
Correct |
75 ms |
20344 KB |
Output is correct |
18 |
Correct |
39 ms |
17580 KB |
Output is correct |
19 |
Correct |
123 ms |
23748 KB |
Output is correct |
20 |
Correct |
115 ms |
24376 KB |
Output is correct |
21 |
Correct |
93 ms |
22136 KB |
Output is correct |
22 |
Correct |
129 ms |
21272 KB |
Output is correct |
23 |
Correct |
131 ms |
25204 KB |
Output is correct |
24 |
Correct |
19 ms |
14456 KB |
Output is correct |
25 |
Correct |
50 ms |
16740 KB |
Output is correct |
26 |
Correct |
51 ms |
17424 KB |
Output is correct |
27 |
Correct |
1661 ms |
34648 KB |
Output is correct |
28 |
Correct |
425 ms |
24776 KB |
Output is correct |
29 |
Correct |
1853 ms |
24332 KB |
Output is correct |
30 |
Correct |
1121 ms |
21664 KB |
Output is correct |
31 |
Correct |
956 ms |
20472 KB |
Output is correct |
32 |
Correct |
126 ms |
17576 KB |
Output is correct |
33 |
Correct |
379 ms |
23776 KB |
Output is correct |
34 |
Correct |
1759 ms |
24388 KB |
Output is correct |
35 |
Correct |
1237 ms |
22108 KB |
Output is correct |
36 |
Correct |
1014 ms |
21264 KB |
Output is correct |
37 |
Correct |
270 ms |
26268 KB |
Output is correct |
38 |
Correct |
3151 ms |
40940 KB |
Output is correct |