# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1107658 | laight | Tropical Garden (IOI11_garden) | C++17 | 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 <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define pi pair<int, int>
#define fi first
#define se second
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define mp make_pair
#define pll pair<ll, ll>
#define umi unordered_map<int, int>
#define umll unordered_map<ll, ll>
#define umllv unordered_map<ll, vector<ll>>
#define getchar() (*_pinp?*_pinp++:(_inp[fread(_pinp=_inp, 1, 4096, stdin)]='\0', *_pinp++))
char _inp[4097], *_pinp=_inp;
#define scan(x) do{while((x=getchar())<'-'); _ssign=x=='-'; if(_ssign) while((x=getchar())<'0'); for(x-='0'; '0'<=(_=getchar()); x=(x<<3)+(x<<1)+_-'0'); x=_ssign?-x:x;}while(0)
char _; bool _ssign;
// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx2")
using namespace std; using namespace __gnu_pbds;
const int MM = 1e5+5e4+5;
const ll inf = 2e15+1;
const int lg = 17; // 17 for 1e5, 31 for 1e9
int n, m, p, a, b, dis[2*MM], lv, cycl, q, inp; bool cyc[2*MM], vis[2*MM];
vector<int> adj[2*MM], g(2005), ans(2005), to(2*MM); vector<pi> graph[MM];
void g_f(int par, int c, int d){
dis[c] = d;
for (int i : adj[c]){
if (i == par || cyc[i]) continue;
g_f(c, i, d+1);
}
}
void cyc_f(int c){
vis[c] = 1;
if (vis[to[c]]){
int st = to[c];
while (st != c){
cyc[st] = 1; st = to[st];
++lv;
}
cyc[c] = 1;
++lv;
return;
}
cyc_f(to[c]);
}
void sol(int sp){
memset(dis, -1, sizeof(dis));
memset(vis, 0, sizeof(vis));
memset(cyc, 0, sizeof(cyc));
lv=0;
bool incyc=0;
cyc_f(sp);
cycl = lv;
if (cyc[sp]){
incyc=1;
g_f(-1, sp, 0);
int nxt=to[sp];
while (nxt != sp){
g_f(-1, nxt, --lv);
nxt = to[nxt];
}
}
else{
cyc[to[sp]] = 1;
g_f(-1, sp, 0);
}
for (int i=0; i<q; ++i){
for (int j=0; j<n; ++j){
if (dis[j] == -1) continue;
if (incyc){
if (g[i] >= dis[j] && (g[i]%cycl == dis[j]%cycl)){
++ans[i];
}
}
else{
if (g[i] == dis[j]) ++ans[i];
}
}
}
}
void count_routes(int N, int M, int P, int R[][2], int Q, int G[]){
// cin.tie(0); cin.sync_with_stdio(0);
n = N; m = M; p = P;
for (int i=0; i<m; ++i){
graph[R[i][0]].pb({i+1, R[i][1]}); graph[R[i][1]].pb({i+1, R[i][0]});
}
q = Q; for (int i=0; i<q; i++) {g[i] = G[i];}
for (int i=0; i<n; ++i){
if (graph[i][0].fi == graph[graph[i][0].se][0].fi){
to[i] = n+graph[i][0].se;
}
else to[i] = graph[i][0].se;
if (graph[i].size() >= 2){
if (graph[i][1].fi == graph[graph[i][1].se][0].fi){
to[n+i] = n+graph[i][1].se;
}
else to[n+i] = graph[i][1].se;
}
else{
if (graph[i][0].fi == graph[graph[i][0].se][0].fi){
to[n+i] = n+graph[i][0].se;
}
else to[n+i] = graph[i][0].se;
}
}
for (int i=0; i<2*n; ++i){
adj[i].pb(to[i]); adj[to[i]].pb(i);
}
sol(p); sol(n+p);
for (int i=0; i<q; ++i){
answer(ans[i]);
}
}