#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define sz(x) (int) x.size()
#define endl '\n'
#define pb push_back
#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#include "garden.h"
#include "gardenlib.h"
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ii = pair<int,int>;
using iii = tuple<int,int,int>;
const int inf = 2e9+1;
const int mod = 1e9+7;
const int maxn = 1e6+100;
template<typename X, typename Y> bool ckmin(X& x, const Y& y) { return (y < x) ? (x=y,1):0; }
template<typename X, typename Y> bool ckmax(X& x, const Y& y) { return (x < y) ? (x=y,1):0; }
vector<int>g[maxn], ciclo[maxn], g2[maxn];
int n, f[maxn], dist[maxn], vis[maxn], res[maxn];
// void answer(int x){cout<<x<<endl;}
void dfs(int u,int d){
vis[u] = 2;
dist[u] = d;
for(auto v:g2[u]){
if(vis[v]!=2)dfs(v,d+1);
}
}
void compute(int P, int Q,int G[]){
for(int i=0;i<=2*n;++i){
vis[i] = dist[i] = 0;
ciclo[i].clear();
}
int cur = P, tciclo = 0;
while(vis[cur]==0){
vis[cur] = 1, cur = f[cur];
}
while(vis[cur]==1){
vis[cur] = 2, cur = f[cur], ++tciclo;
}
//achei o ciclo da componente de p
if(vis[P]==1){
tciclo = 1e9, vis[f[P]] = 2;
dfs(P,0);
vis[f[P]] = 0;
}else{
cur = P;
int d = tciclo;
do{
dfs(cur, d%tciclo), cur = f[cur], d--;
} while(cur!=P);
if(P<n)ciclo[0].pb(tciclo);
}
for(int i=0;i<n;++i){
if(vis[i]&&dist[i]){
ciclo[dist[i]%tciclo].pb(dist[i]);
}
}
for(int i=0;i<min(tciclo,3*n);++i){
sort(all(ciclo[i]));
}
for(int i=0;i<Q;++i){
int cur = G[i];
if(tciclo==1e9&&cur>2*n)continue;
int ans = upper_bound(all(ciclo[cur%tciclo]),cur) - begin(ciclo[cur%tciclo]);
res[i] += ans;
}
}
void count_routes(int N, int M, int P, int R[][2], int Q, int G[])
{
n = N;
for(int i=0;i<M;++i){
int u = R[i][0], v = R[i][1];
g[u].pb(v), g[v].pb(u);
}
//grafo funcional, achar distancia de cada vertice pra p
for(int i=0;i<2*n;++i){
int act = i%n;
if(i<n||sz(g[act])==1){
int best = g[act][0];
if(g[best][0]==act)f[i] = best+n;
else f[i] = best;
}else{
if(g[g[act][1]][0]==act)f[i] = g[act][1] + n;
else f[i] = g[act][1];
}
g2[i].pb(f[i]), g2[f[i]].pb(i);
// cout<<i<<" "<<f[i]<<endl;
}
compute(P,Q,G), compute(P+n,Q,G);
for(int i=0;i<Q;++i){
answer(res[i]);
}
}
// int v[maxn][2], queries[maxn];
// int main(){
// int n,m,p,q;cin>>n>>m>>p>>q;
// for(int i=0;i<m;++i){
// cin>>v[i][0]>>v[i][1];
// }
// for(int i=0;i<q;++i)cin>>queries[i];
// count_routes(n,m,p,v,q,queries);
// }
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
70868 KB |
Output is correct |
2 |
Correct |
33 ms |
70812 KB |
Output is correct |
3 |
Correct |
38 ms |
70896 KB |
Output is correct |
4 |
Correct |
33 ms |
70752 KB |
Output is correct |
5 |
Correct |
33 ms |
70792 KB |
Output is correct |
6 |
Correct |
33 ms |
70952 KB |
Output is correct |
7 |
Correct |
33 ms |
70740 KB |
Output is correct |
8 |
Correct |
34 ms |
70872 KB |
Output is correct |
9 |
Correct |
41 ms |
70996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
70868 KB |
Output is correct |
2 |
Correct |
33 ms |
70812 KB |
Output is correct |
3 |
Correct |
38 ms |
70896 KB |
Output is correct |
4 |
Correct |
33 ms |
70752 KB |
Output is correct |
5 |
Correct |
33 ms |
70792 KB |
Output is correct |
6 |
Correct |
33 ms |
70952 KB |
Output is correct |
7 |
Correct |
33 ms |
70740 KB |
Output is correct |
8 |
Correct |
34 ms |
70872 KB |
Output is correct |
9 |
Correct |
41 ms |
70996 KB |
Output is correct |
10 |
Correct |
32 ms |
70708 KB |
Output is correct |
11 |
Correct |
44 ms |
73672 KB |
Output is correct |
12 |
Correct |
57 ms |
75780 KB |
Output is correct |
13 |
Correct |
75 ms |
84072 KB |
Output is correct |
14 |
Correct |
171 ms |
88476 KB |
Output is correct |
15 |
Correct |
221 ms |
89356 KB |
Output is correct |
16 |
Correct |
216 ms |
83816 KB |
Output is correct |
17 |
Correct |
178 ms |
81856 KB |
Output is correct |
18 |
Correct |
60 ms |
76264 KB |
Output is correct |
19 |
Correct |
174 ms |
90220 KB |
Output is correct |
20 |
Correct |
211 ms |
90956 KB |
Output is correct |
21 |
Correct |
207 ms |
85256 KB |
Output is correct |
22 |
Correct |
143 ms |
83208 KB |
Output is correct |
23 |
Correct |
168 ms |
92032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
70868 KB |
Output is correct |
2 |
Correct |
33 ms |
70812 KB |
Output is correct |
3 |
Correct |
38 ms |
70896 KB |
Output is correct |
4 |
Correct |
33 ms |
70752 KB |
Output is correct |
5 |
Correct |
33 ms |
70792 KB |
Output is correct |
6 |
Correct |
33 ms |
70952 KB |
Output is correct |
7 |
Correct |
33 ms |
70740 KB |
Output is correct |
8 |
Correct |
34 ms |
70872 KB |
Output is correct |
9 |
Correct |
41 ms |
70996 KB |
Output is correct |
10 |
Correct |
32 ms |
70708 KB |
Output is correct |
11 |
Correct |
44 ms |
73672 KB |
Output is correct |
12 |
Correct |
57 ms |
75780 KB |
Output is correct |
13 |
Correct |
75 ms |
84072 KB |
Output is correct |
14 |
Correct |
171 ms |
88476 KB |
Output is correct |
15 |
Correct |
221 ms |
89356 KB |
Output is correct |
16 |
Correct |
216 ms |
83816 KB |
Output is correct |
17 |
Correct |
178 ms |
81856 KB |
Output is correct |
18 |
Correct |
60 ms |
76264 KB |
Output is correct |
19 |
Correct |
174 ms |
90220 KB |
Output is correct |
20 |
Correct |
211 ms |
90956 KB |
Output is correct |
21 |
Correct |
207 ms |
85256 KB |
Output is correct |
22 |
Correct |
143 ms |
83208 KB |
Output is correct |
23 |
Correct |
168 ms |
92032 KB |
Output is correct |
24 |
Correct |
34 ms |
70732 KB |
Output is correct |
25 |
Correct |
44 ms |
73832 KB |
Output is correct |
26 |
Correct |
59 ms |
76272 KB |
Output is correct |
27 |
Correct |
77 ms |
85128 KB |
Output is correct |
28 |
Correct |
172 ms |
90116 KB |
Output is correct |
29 |
Correct |
205 ms |
91084 KB |
Output is correct |
30 |
Correct |
188 ms |
85244 KB |
Output is correct |
31 |
Correct |
142 ms |
83376 KB |
Output is correct |
32 |
Correct |
61 ms |
76388 KB |
Output is correct |
33 |
Correct |
176 ms |
90176 KB |
Output is correct |
34 |
Correct |
232 ms |
91004 KB |
Output is correct |
35 |
Correct |
186 ms |
85336 KB |
Output is correct |
36 |
Correct |
146 ms |
83300 KB |
Output is correct |
37 |
Correct |
173 ms |
92116 KB |
Output is correct |
38 |
Correct |
148 ms |
95036 KB |
Output is correct |