#include <bits/stdc++.h>
#include "gardenlib.h"
#include "garden.h"
using namespace std;
const int N = 3e5 + 10;
int chk[N], d[N], to[N], cyc;
vector<int> vec, g[N], g2[N];
void dfs( int u ) {
//printf("U %d\n",u);
chk[u] = 1, vec.emplace_back( u );
for( int v : g[u] ) {
if( !chk[v] ) d[v] = d[u] + 1, dfs( v );
else if( chk[v] == 1 ) for( int i = (int)vec.size() - 1 ; i >= 0 ; i-- ) {
cyc++;
if( vec[i] == v ) break;
}
}
chk[u] = 2, vec.pop_back();
}
//void answer( int x ) { printf("%d ",x); }
void count_routes( int n, int m, int p, int r[][2], int q, int G[] ) {
for( int i = 0 ; i < m ; i++ ) {
if( g2[r[i][0]].size() < 2 ) g2[r[i][0]].emplace_back( r[i][1] );
if( g2[r[i][1]].size() < 2 ) g2[r[i][1]].emplace_back( r[i][0] );
}
for(int i = 0; i < n; i++) if(g2[i].size() == 1) g2[i].emplace_back(g2[i][0]);
//printf("\n");
for(int i = 0; i < n; i++) for(int j = 0; j < (int)g2[i].size(); j++) {
int v = g2[i][j];
//printf("%d %d\n",i,v);
if(g2[v][0] == i) v += n;
g[v].emplace_back(i + (j ? n : 0));
}
/*for( int i = 0 ; i < 2*n ; i++ ) printf("%d ",to[i]);
printf("\n");*/
/*for( int i = 0 ; i < 2*n ; i++ ) g[i].clear();
for( int i = 0 ; i < 2*n ; i++ ) g[to[i]].emplace_back( i );*/
/*for( int i = 0 ; i < 2*n ; i++ ) {
printf("%d : ",i);
for( int v : g[i] ) printf("%d ",v);
printf("\n");
}
printf("\n");*/
vector<int> ans( q, 0 );
vec.clear(), memset( d, 127, sizeof d ), d[p] = cyc = 0, memset( chk, 0, sizeof chk );
dfs( p );
//printf("%d\n",cyc);
for( int i = 0 ; i < q ; i++ ) {
for( int j = 0 ; j < n ; j++ ) {
if( d[j] > G[i] ) continue ;
if( cyc ) ans[i] += ( ( G[i] - d[j] ) % cyc == 0 );
else ans[i] += ( d[j] == G[i] );
}
}
vec.clear(), memset( d, 127, sizeof d ), d[p+n] = cyc = 0, memset( chk, 0, sizeof chk );
dfs( p+n );
//printf("%d\n",cyc);
for( int i = 0 ; i < q ; i++ ) {
for( int j = 0 ; j < n ; j++ ) {
if( d[j] > G[i] ) continue ;
if( cyc ) ans[i] += ( ( G[i] - d[j] ) % cyc == 0 );
else ans[i] += ( d[j] == G[i] );
}
//
answer( ans[i] );
}
//return ans;
}
/*int main()
{
int n, m, p, q, r[100][2], G[100];
scanf("%d %d %d %d",&n,&m,&p,&q);
for( int i = 0 ; i < m ; i++ ) {
scanf("%d %d",&r[i][0],&r[i][1]);
}
for( int i = 0 ; i < q ; i++ ) scanf("%d",&G[i]);
count_routes( n, m, p, r, q, G );
}*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
16896 KB |
Output is correct |
2 |
Correct |
15 ms |
16896 KB |
Output is correct |
3 |
Correct |
15 ms |
16896 KB |
Output is correct |
4 |
Correct |
15 ms |
16768 KB |
Output is correct |
5 |
Correct |
14 ms |
16768 KB |
Output is correct |
6 |
Correct |
15 ms |
17024 KB |
Output is correct |
7 |
Correct |
14 ms |
16768 KB |
Output is correct |
8 |
Correct |
16 ms |
16896 KB |
Output is correct |
9 |
Correct |
16 ms |
17024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
16896 KB |
Output is correct |
2 |
Correct |
15 ms |
16896 KB |
Output is correct |
3 |
Correct |
15 ms |
16896 KB |
Output is correct |
4 |
Correct |
15 ms |
16768 KB |
Output is correct |
5 |
Correct |
14 ms |
16768 KB |
Output is correct |
6 |
Correct |
15 ms |
17024 KB |
Output is correct |
7 |
Correct |
14 ms |
16768 KB |
Output is correct |
8 |
Correct |
16 ms |
16896 KB |
Output is correct |
9 |
Correct |
16 ms |
17024 KB |
Output is correct |
10 |
Correct |
14 ms |
16768 KB |
Output is correct |
11 |
Correct |
26 ms |
18944 KB |
Output is correct |
12 |
Correct |
42 ms |
20344 KB |
Output is correct |
13 |
Correct |
73 ms |
35160 KB |
Output is correct |
14 |
Correct |
140 ms |
28280 KB |
Output is correct |
15 |
Correct |
181 ms |
28644 KB |
Output is correct |
16 |
Correct |
127 ms |
25848 KB |
Output is correct |
17 |
Correct |
111 ms |
24696 KB |
Output is correct |
18 |
Correct |
45 ms |
20344 KB |
Output is correct |
19 |
Correct |
136 ms |
28280 KB |
Output is correct |
20 |
Correct |
174 ms |
28664 KB |
Output is correct |
21 |
Correct |
141 ms |
25720 KB |
Output is correct |
22 |
Correct |
117 ms |
24696 KB |
Output is correct |
23 |
Correct |
146 ms |
29560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
16896 KB |
Output is correct |
2 |
Correct |
15 ms |
16896 KB |
Output is correct |
3 |
Correct |
15 ms |
16896 KB |
Output is correct |
4 |
Correct |
15 ms |
16768 KB |
Output is correct |
5 |
Correct |
14 ms |
16768 KB |
Output is correct |
6 |
Correct |
15 ms |
17024 KB |
Output is correct |
7 |
Correct |
14 ms |
16768 KB |
Output is correct |
8 |
Correct |
16 ms |
16896 KB |
Output is correct |
9 |
Correct |
16 ms |
17024 KB |
Output is correct |
10 |
Correct |
14 ms |
16768 KB |
Output is correct |
11 |
Correct |
26 ms |
18944 KB |
Output is correct |
12 |
Correct |
42 ms |
20344 KB |
Output is correct |
13 |
Correct |
73 ms |
35160 KB |
Output is correct |
14 |
Correct |
140 ms |
28280 KB |
Output is correct |
15 |
Correct |
181 ms |
28644 KB |
Output is correct |
16 |
Correct |
127 ms |
25848 KB |
Output is correct |
17 |
Correct |
111 ms |
24696 KB |
Output is correct |
18 |
Correct |
45 ms |
20344 KB |
Output is correct |
19 |
Correct |
136 ms |
28280 KB |
Output is correct |
20 |
Correct |
174 ms |
28664 KB |
Output is correct |
21 |
Correct |
141 ms |
25720 KB |
Output is correct |
22 |
Correct |
117 ms |
24696 KB |
Output is correct |
23 |
Correct |
146 ms |
29560 KB |
Output is correct |
24 |
Correct |
15 ms |
16768 KB |
Output is correct |
25 |
Correct |
107 ms |
19000 KB |
Output is correct |
26 |
Correct |
124 ms |
20352 KB |
Output is correct |
27 |
Correct |
2544 ms |
35440 KB |
Output is correct |
28 |
Correct |
948 ms |
28416 KB |
Output is correct |
29 |
Correct |
2994 ms |
28696 KB |
Output is correct |
30 |
Correct |
1731 ms |
25976 KB |
Output is correct |
31 |
Correct |
1745 ms |
24740 KB |
Output is correct |
32 |
Correct |
138 ms |
20344 KB |
Output is correct |
33 |
Correct |
925 ms |
28408 KB |
Output is correct |
34 |
Correct |
2982 ms |
28804 KB |
Output is correct |
35 |
Correct |
1843 ms |
25976 KB |
Output is correct |
36 |
Correct |
2342 ms |
24696 KB |
Output is correct |
37 |
Correct |
697 ms |
29560 KB |
Output is correct |
38 |
Correct |
2790 ms |
41844 KB |
Output is correct |