Submission #776995

# Submission time Handle Problem Language Result Execution time Memory
776995 2023-07-08T13:10:28 Z I_Love_EliskaM_ Tropical Garden (IOI11_garden) C++14
100 / 100
2244 ms 58000 KB
#include "gardenlib.h"
#include <bits/stdc++.h>
using namespace std;
#define forn(i,n) for(int i=0;i<n;++i)
#define pi pair<int,int>
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(), x.end()

bool foo(pi a, pi b) {
  return a.s < b.s;
}

struct DSU {
  vector<int> p,sz;
  DSU(int n) {
    forn(i,n) p.pb(i), sz.pb(1);
  }
  int get(int u) {
    return p[u]==u?u:get(p[u]);
  }
  void uni(int u, int v) {
    u=get(u), v=get(v);
    if (u==v) return;
    if (sz[u]<sz[v]) swap(u,v);
    p[v]=u;
    sz[u]+=sz[v];
  }
};

const int N=4e5;
const int K=20;
int up[N][K];
vector<pi> adj[N];
int to[N+5];
int in[N+5];
int d[N+5];
int vis[N+5];
int iscyc[N+5];
DSU dsu(N);
int dist[N], dist2[N];
int mod[N], mod2[N];
int sz[N];

int cyc=0, pt=-1;

void dfs(int u) {
  if (vis[u]==1) {
    cyc=1;
    pt=u;
    d[u]=0;
    return;
  }
  if (vis[u]==2) return;
  vis[u]=1;
  dfs(to[u]);
  dsu.uni(u,to[u]);
  vis[u]=2;
  d[u]=d[to[u]]+1;
  iscyc[u]=cyc;
  if (cyc) sz[dsu.get(u)]=max(sz[dsu.get(u)],d[u]);
  if (u==pt) {
    cyc=0;
    pt=-1;
  }
}

int jump(int u, int x) {
  for (int j=K-1; j>=0; --j) {
    if ((x>>j)&1) {
      u=up[u][j];
    }
  }
  return u;
}

void count_routes(int n, int m, int p, int r[][2], int q, int *g) {

  forn(i,N+5) to[i]=i;

  forn(i,m) {
    int u=r[i][0], v=r[i][1];
    adj[u].pb({v,i});
    adj[v].pb({u,i});
  }
  forn(i,n) sort(all(adj[i]),foo);
  forn(i,n) {
    int v=adj[i][0].f;
    if (adj[v].size()==1) {
      to[i]=v;
    } else {
      if (i==adj[v][0].f) {
        to[i]=v+n;
      } else {
        to[i]=v;
      }
    }

    if (adj[i].size()>1) {
      v=adj[i][1].f;
      if (adj[v].size()==1) {
        to[i+n]=v;
      } else {
        if (i==adj[v][0].f) {
          to[i+n]=v+n;
        } else {
          to[i+n]=v;
        }
      }
    }
  }

  forn(i,2*n) up[i][0]=to[i];
  for (int j=1;j<K;++j) {
    forn(i,2*n) up[i][j]=up[up[i][j-1]][j-1];
  }

  forn(i,n) in[to[i]]++, in[to[i+n]]++;
  forn(i,n) {
    if (!in[i]) dfs(i);
    if (!in[i+n]) dfs(i);
  }
  forn(i,2*n) if (!vis[i]) dfs(i);
  
  forn(i,n) {
    mod[i]=mod2[i]=-1;
    if (dsu.get(i)==dsu.get(p)) {
      if (iscyc[p]) {
        mod[i]=sz[dsu.get(p)];
        dist[i]=(d[i]-d[p]);
        if (dist[i]<0) dist[i]+=mod[i];
      } else {
        if (d[i] >= d[p]) {
          int u=jump(i,d[i]-d[p]);
          if (u==p) {
            dist[i] = d[i]-d[p];
          }
        }
      }
    }
    p+=n;
    if (dsu.get(i)==dsu.get(p)) {
      if (iscyc[p]) {
        mod2[i]=sz[dsu.get(p)];
        dist2[i]=(d[i]-d[p]);
        if (dist2[i]<0) dist2[i]+=mod2[i];
      } else {
        if (d[i] >= d[p]) {
          int u=jump(i,d[i]-d[p]);
          if (u==p) {
            dist2[i] = d[i]-d[p];
          }
        }
      }
    }
    p-=n;
  }

  forn(j,q) {
    int k=g[j];
    int ans=0;
    forn(i,n) {
      int z=0;
      if (mod[i]==-1) {
        z|=dist[i]==k;
      } else {
        if (k>=dist[i]) z|=(k-dist[i])%mod[i] == 0;
      }
      if (mod2[i]==-1) {
        z|=dist2[i]==k;
      } else {
        if (k>=dist2[i]) z|=(k-dist2[i])%mod2[i] == 0;
      }
      ans+=z;
    }
    answer(ans);
  }

}
# Verdict Execution time Memory Grader output
1 Correct 11 ms 14900 KB Output is correct
2 Correct 10 ms 14900 KB Output is correct
3 Correct 10 ms 14912 KB Output is correct
4 Correct 10 ms 14648 KB Output is correct
5 Correct 9 ms 14648 KB Output is correct
6 Correct 10 ms 14904 KB Output is correct
7 Correct 11 ms 14604 KB Output is correct
8 Correct 9 ms 14836 KB Output is correct
9 Correct 11 ms 15088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 14900 KB Output is correct
2 Correct 10 ms 14900 KB Output is correct
3 Correct 10 ms 14912 KB Output is correct
4 Correct 10 ms 14648 KB Output is correct
5 Correct 9 ms 14648 KB Output is correct
6 Correct 10 ms 14904 KB Output is correct
7 Correct 11 ms 14604 KB Output is correct
8 Correct 9 ms 14836 KB Output is correct
9 Correct 11 ms 15088 KB Output is correct
10 Correct 9 ms 14640 KB Output is correct
11 Correct 18 ms 20188 KB Output is correct
12 Correct 37 ms 24548 KB Output is correct
13 Correct 53 ms 43184 KB Output is correct
14 Correct 93 ms 48512 KB Output is correct
15 Correct 96 ms 49400 KB Output is correct
16 Correct 73 ms 39096 KB Output is correct
17 Correct 66 ms 35984 KB Output is correct
18 Correct 34 ms 24372 KB Output is correct
19 Correct 101 ms 48876 KB Output is correct
20 Correct 97 ms 49368 KB Output is correct
21 Correct 73 ms 38844 KB Output is correct
22 Correct 75 ms 35800 KB Output is correct
23 Correct 103 ms 52060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 14900 KB Output is correct
2 Correct 10 ms 14900 KB Output is correct
3 Correct 10 ms 14912 KB Output is correct
4 Correct 10 ms 14648 KB Output is correct
5 Correct 9 ms 14648 KB Output is correct
6 Correct 10 ms 14904 KB Output is correct
7 Correct 11 ms 14604 KB Output is correct
8 Correct 9 ms 14836 KB Output is correct
9 Correct 11 ms 15088 KB Output is correct
10 Correct 9 ms 14640 KB Output is correct
11 Correct 18 ms 20188 KB Output is correct
12 Correct 37 ms 24548 KB Output is correct
13 Correct 53 ms 43184 KB Output is correct
14 Correct 93 ms 48512 KB Output is correct
15 Correct 96 ms 49400 KB Output is correct
16 Correct 73 ms 39096 KB Output is correct
17 Correct 66 ms 35984 KB Output is correct
18 Correct 34 ms 24372 KB Output is correct
19 Correct 101 ms 48876 KB Output is correct
20 Correct 97 ms 49368 KB Output is correct
21 Correct 73 ms 38844 KB Output is correct
22 Correct 75 ms 35800 KB Output is correct
23 Correct 103 ms 52060 KB Output is correct
24 Correct 10 ms 14648 KB Output is correct
25 Correct 96 ms 20224 KB Output is correct
26 Correct 135 ms 24676 KB Output is correct
27 Correct 2057 ms 43232 KB Output is correct
28 Correct 806 ms 48944 KB Output is correct
29 Correct 2244 ms 49496 KB Output is correct
30 Correct 1313 ms 39200 KB Output is correct
31 Correct 1264 ms 36028 KB Output is correct
32 Correct 127 ms 24368 KB Output is correct
33 Correct 820 ms 48824 KB Output is correct
34 Correct 2223 ms 49500 KB Output is correct
35 Correct 1396 ms 39008 KB Output is correct
36 Correct 1265 ms 35788 KB Output is correct
37 Correct 705 ms 52100 KB Output is correct
38 Correct 1837 ms 58000 KB Output is correct