답안 #776993

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
776993 2023-07-08T13:08:43 Z I_Love_EliskaM_ 열대 식물원 (Tropical Garden) (IOI11_garden) C++14
100 / 100
2233 ms 59852 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,2*n) cout<<i<<"->"<<to[i]<<'\n';
  //// 0->1->2->7->6->9->10->11->1
  //// 0->6->7->8->1->0
  //// 3->2->1 4->2->1
  //// 8=1 7=2 6=3 0=4 1=5 2=6 3=7 4=7
  //forn(i,2*n) cout<<vis[i]<<' '<<d[i]<<' '<<iscyc[i]<<' '<<dsu.get(i)<<' '<<sz[dsu.get(i)]<<'\n';
  //return;
  
  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(i,n) cout<<mod[i]<<' '<<dist[i]<<' '<<mod2[i]<<' '<<dist2[i]<<'\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 10 ms 14904 KB Output is correct
2 Correct 10 ms 14884 KB Output is correct
3 Correct 9 ms 14904 KB Output is correct
4 Correct 9 ms 14636 KB Output is correct
5 Correct 9 ms 14648 KB Output is correct
6 Correct 9 ms 14904 KB Output is correct
7 Correct 10 ms 14692 KB Output is correct
8 Correct 12 ms 14932 KB Output is correct
9 Correct 11 ms 15032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 14904 KB Output is correct
2 Correct 10 ms 14884 KB Output is correct
3 Correct 9 ms 14904 KB Output is correct
4 Correct 9 ms 14636 KB Output is correct
5 Correct 9 ms 14648 KB Output is correct
6 Correct 9 ms 14904 KB Output is correct
7 Correct 10 ms 14692 KB Output is correct
8 Correct 12 ms 14932 KB Output is correct
9 Correct 11 ms 15032 KB Output is correct
10 Correct 9 ms 14648 KB Output is correct
11 Correct 22 ms 20520 KB Output is correct
12 Correct 32 ms 25200 KB Output is correct
13 Correct 52 ms 44188 KB Output is correct
14 Correct 93 ms 49856 KB Output is correct
15 Correct 101 ms 50684 KB Output is correct
16 Correct 86 ms 40384 KB Output is correct
17 Correct 66 ms 37256 KB Output is correct
18 Correct 32 ms 24896 KB Output is correct
19 Correct 93 ms 50088 KB Output is correct
20 Correct 104 ms 50648 KB Output is correct
21 Correct 75 ms 40056 KB Output is correct
22 Correct 70 ms 37052 KB Output is correct
23 Correct 120 ms 53276 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 14904 KB Output is correct
2 Correct 10 ms 14884 KB Output is correct
3 Correct 9 ms 14904 KB Output is correct
4 Correct 9 ms 14636 KB Output is correct
5 Correct 9 ms 14648 KB Output is correct
6 Correct 9 ms 14904 KB Output is correct
7 Correct 10 ms 14692 KB Output is correct
8 Correct 12 ms 14932 KB Output is correct
9 Correct 11 ms 15032 KB Output is correct
10 Correct 9 ms 14648 KB Output is correct
11 Correct 22 ms 20520 KB Output is correct
12 Correct 32 ms 25200 KB Output is correct
13 Correct 52 ms 44188 KB Output is correct
14 Correct 93 ms 49856 KB Output is correct
15 Correct 101 ms 50684 KB Output is correct
16 Correct 86 ms 40384 KB Output is correct
17 Correct 66 ms 37256 KB Output is correct
18 Correct 32 ms 24896 KB Output is correct
19 Correct 93 ms 50088 KB Output is correct
20 Correct 104 ms 50648 KB Output is correct
21 Correct 75 ms 40056 KB Output is correct
22 Correct 70 ms 37052 KB Output is correct
23 Correct 120 ms 53276 KB Output is correct
24 Correct 10 ms 14648 KB Output is correct
25 Correct 103 ms 20436 KB Output is correct
26 Correct 137 ms 25264 KB Output is correct
27 Correct 2066 ms 44344 KB Output is correct
28 Correct 795 ms 50628 KB Output is correct
29 Correct 2233 ms 51308 KB Output is correct
30 Correct 1302 ms 40884 KB Output is correct
31 Correct 1263 ms 37588 KB Output is correct
32 Correct 126 ms 24992 KB Output is correct
33 Correct 825 ms 50524 KB Output is correct
34 Correct 2223 ms 51248 KB Output is correct
35 Correct 1399 ms 40588 KB Output is correct
36 Correct 1265 ms 37364 KB Output is correct
37 Correct 714 ms 53896 KB Output is correct
38 Correct 1839 ms 59852 KB Output is correct