Submission #14840

#TimeUsernameProblemLanguageResultExecution timeMemory
14840gs14004Tropical Garden (IOI11_garden)C++14
0 / 100
23 ms23672 KiB
#include "garden.h" #include "gardenlib.h" #include <queue> #include <vector> #include <cstring> #include <algorithm> using namespace std; typedef pair<int,int> pi; vector<pi> graph0[150005]; vector<int> graph1[300005]; vector<int> graph2[300005]; vector<int> proc; int nxt[300005]; int low[150005]; int deg[300005]; int dist0[300005]; int dist1[300005]; int ped0[300005]; int ped1[300005]; int n, qcnt; int *g; queue<int> q, p, d; bool vis[300005]; void get_periods(){ for (int i=0; i<2*n; i++) { if(deg[i] == 1){ q.push(i); } } while (!q.empty()) { int qf = q.front(); q.pop(); deg[qf] = 0; for (auto &i : graph1[qf]){ deg[i]--; if(deg[i] == 1){ q.push(i); } } } int cnt = 0; for (int i=0; i<2*n; i++) { if(deg[i] != 0){ cnt++; } } for (int i=0; i<2*n; i++) { if(deg[i] != 0){ ped0[i] = cnt; ped1[i] = cnt; } } } void solve(int st, int *dist, int *ped){ memset(vis,0,sizeof(vis)); q.push(st); p.push(-1); d.push(0); vis[st] = 1; while (!q.empty()) { int qf = q.front(), pf = p.front(), df = d.front(); q.pop(), p.pop(), d.pop(); pf = max(pf, ped[qf]); ped[qf] = pf; if(qf % 2 == 0){ dist[qf / 2] = df; } for (auto &i : graph2[qf]){ if(vis[i]) continue; vis[i] = 1; q.push(i); p.push(pf); d.push(df + 1); } } } void count_routes(int N, int M, int P, int R[][2], int Q, int G[]) { n = N, qcnt = Q, g = G; memset(low,0x3f,sizeof(low)); memset(dist0,-1,sizeof(dist0)); memset(dist1,-1,sizeof(dist1)); memset(ped0,-1,sizeof(ped0)); memset(ped1,-1,sizeof(ped1)); for (int i=0; i<M; i++) { for (int j=0; j<2; j++) { low[R[i][j]] = min(low[R[i][j]],i); } graph0[R[i][0]].push_back(pi(R[i][1],i)); graph0[R[i][1]].push_back(pi(R[i][0],i)); } for (int i=0; i<N; i++) { if(low[graph0[i][0].first] == graph0[i][0].second){ nxt[2*i] = graph0[i][0].first * 2 + 1; } else{ nxt[2*i] = graph0[i][0].first * 2; } if(graph0[i].size() == 1){ nxt[2*i+1] = nxt[2*i]; } else{ if(low[graph0[i][1].first] == graph0[i][1].second){ nxt[2*i+1] = graph0[i][1].first * 2 + 1; } else{ nxt[2*i+1] = graph0[i][1].first * 2; } } } for (int i=0; i<2*N; i++){ // printf("%d %d\n",i,nxt[i]); graph1[nxt[i]].push_back(i); graph1[i].push_back(nxt[i]); graph2[nxt[i]].push_back(i); deg[i]++; deg[nxt[i]]++; } get_periods(); solve(2*P,dist0,ped0); solve(2*P+1,dist1,ped1); for (int i=0; i<Q; i++) { int ret = 0; for (int j=0; j<N; j++) { if(dist0[j] != -1){ if(ped0[2*j] == -1){ if(dist0[j] == G[i]){ ret++; continue; } } else{ if(dist0[j] <= G[i] && dist0[j] % ped0[2*j] == G[i] % ped0[2*j]){ ret++; continue; } } } if(dist1[j] != -1){ if(ped1[2*j+1] == -1){ if(dist1[j] == G[i]){ ret++; continue; } } else{ if(dist1[j] <= G[i] && dist1[j] % ped1[2*j+1] == G[i] % ped1[2*j+1]){ ret++; continue; } } } } answer(ret); } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...