답안 #776996

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
776996 2023-07-08T13:11:13 Z TimDee 열대 식물원 (Tropical Garden) (IOI11_garden) C++17
100 / 100
2247 ms 57992 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);
  }

}
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14904 KB Output is correct
2 Correct 8 ms 14904 KB Output is correct
3 Correct 12 ms 14904 KB Output is correct
4 Correct 8 ms 14720 KB Output is correct
5 Correct 9 ms 14604 KB Output is correct
6 Correct 10 ms 14904 KB Output is correct
7 Correct 10 ms 14648 KB Output is correct
8 Correct 10 ms 14856 KB Output is correct
9 Correct 13 ms 15036 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14904 KB Output is correct
2 Correct 8 ms 14904 KB Output is correct
3 Correct 12 ms 14904 KB Output is correct
4 Correct 8 ms 14720 KB Output is correct
5 Correct 9 ms 14604 KB Output is correct
6 Correct 10 ms 14904 KB Output is correct
7 Correct 10 ms 14648 KB Output is correct
8 Correct 10 ms 14856 KB Output is correct
9 Correct 13 ms 15036 KB Output is correct
10 Correct 9 ms 14648 KB Output is correct
11 Correct 18 ms 20268 KB Output is correct
12 Correct 29 ms 24548 KB Output is correct
13 Correct 63 ms 43204 KB Output is correct
14 Correct 94 ms 48548 KB Output is correct
15 Correct 142 ms 49416 KB Output is correct
16 Correct 75 ms 39080 KB Output is correct
17 Correct 69 ms 35964 KB Output is correct
18 Correct 32 ms 24360 KB Output is correct
19 Correct 103 ms 48836 KB Output is correct
20 Correct 113 ms 49388 KB Output is correct
21 Correct 75 ms 38752 KB Output is correct
22 Correct 78 ms 35776 KB Output is correct
23 Correct 114 ms 52048 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14904 KB Output is correct
2 Correct 8 ms 14904 KB Output is correct
3 Correct 12 ms 14904 KB Output is correct
4 Correct 8 ms 14720 KB Output is correct
5 Correct 9 ms 14604 KB Output is correct
6 Correct 10 ms 14904 KB Output is correct
7 Correct 10 ms 14648 KB Output is correct
8 Correct 10 ms 14856 KB Output is correct
9 Correct 13 ms 15036 KB Output is correct
10 Correct 9 ms 14648 KB Output is correct
11 Correct 18 ms 20268 KB Output is correct
12 Correct 29 ms 24548 KB Output is correct
13 Correct 63 ms 43204 KB Output is correct
14 Correct 94 ms 48548 KB Output is correct
15 Correct 142 ms 49416 KB Output is correct
16 Correct 75 ms 39080 KB Output is correct
17 Correct 69 ms 35964 KB Output is correct
18 Correct 32 ms 24360 KB Output is correct
19 Correct 103 ms 48836 KB Output is correct
20 Correct 113 ms 49388 KB Output is correct
21 Correct 75 ms 38752 KB Output is correct
22 Correct 78 ms 35776 KB Output is correct
23 Correct 114 ms 52048 KB Output is correct
24 Correct 11 ms 14648 KB Output is correct
25 Correct 109 ms 20312 KB Output is correct
26 Correct 144 ms 24672 KB Output is correct
27 Correct 2057 ms 43228 KB Output is correct
28 Correct 846 ms 48940 KB Output is correct
29 Correct 2247 ms 49484 KB Output is correct
30 Correct 1316 ms 39204 KB Output is correct
31 Correct 1271 ms 36032 KB Output is correct
32 Correct 134 ms 24420 KB Output is correct
33 Correct 861 ms 48792 KB Output is correct
34 Correct 2237 ms 49480 KB Output is correct
35 Correct 1405 ms 39008 KB Output is correct
36 Correct 1272 ms 35788 KB Output is correct
37 Correct 705 ms 52096 KB Output is correct
38 Correct 1847 ms 57992 KB Output is correct