Submission #797522

# Submission time Handle Problem Language Result Execution time Memory
797522 2023-07-29T14:05:13 Z farhan132 Tropical Garden (IOI11_garden) C++17
100 / 100
2389 ms 24528 KB
#include "garden.h"
#include "gardenlib.h"
#include <bits/stdc++.h>
 
using namespace std;
 
typedef int ll;
typedef double dd;
typedef pair<ll , ll> ii;
typedef tuple < ll,  ll, ll > tp;
 
#define ff first
#define ss second
#define pb push_back
#define in insert
#define mem(a , b) memset(a, b ,sizeof(a))


const ll N = 2e5 + 5;
const ll INF = 1e9 + 3;

vector < ll > v[N];

ii nxt(ll node, ll t){
  if(v[node].size() == 1 && t > 0) t = 0; 
  return {v[node][t] , ((node == v[v[node][t]][0] && v[v[node][t]].size() > 1) ? 1 : 0)};
}
ll vis[N][2];
ll dp[N][2][2], P;

ll go(ll node, ll t, ll tar){
  if(make_pair(node, t) == make_pair(P, tar)){
    return dp[node][t][tar] = 0;
  }
  if(dp[node][t][tar] != -1){
    return dp[node][t][tar];
  }
  vis[node][t] = 1;
  auto [n1, t1] = nxt(node, t);
  if(vis[n1][t1] == 1){
    vis[node][t] = 2;
    return dp[node][t][tar] = INF;
  }
  dp[node][t][tar] = min(INF, go(n1, t1, tar) + 1);
  vis[node][t] = 2;
  return dp[node][t][tar];
}

void count_routes(int n, int m, int _P, int R[][2], int Q, int G[]){

  P = _P;
  
  for(ll i = 0; i < m; i++){
    v[R[i][0]].pb(R[i][1]);
    v[R[i][1]].pb(R[i][0]);
  }

  ll cycle[2] = {0, 0}; 
  mem(vis, 0); 
  ii cur = {P, 0};
  while(true){
    vis[cur.ff][cur.ss] = 1;
    //cout << cur.ff << ' ' << cur.ss << '\n';
    ii NXT = nxt(cur.ff, cur.ss);
    cycle[0]++;
    if(NXT == make_pair(P, 0)){
      break;
    }
    if(vis[NXT.ff][NXT.ss]){
      cycle[0] = INF;
      break;
    }
    cur = NXT;
  }
  if(v[P].size() == 1){
      cycle[1] = INF; 
  }else{
    mem(vis, 0); 
    cur = {P, 1};
    while(true){
      vis[cur.ff][cur.ss] = 1;
      ii NXT = nxt(cur.ff, cur.ss);
      cycle[1]++;
      if(NXT == make_pair(P, 1)){
        break;
      }
      if(vis[NXT.ff][NXT.ss]){
        cycle[1] = INF;
        break;
      }
      cur = NXT;
    }
  }
  //cout << cycle[0] << ' ' << cycle[1] << '\n';
  mem(dp, -1);
  for(ll tar = 0; tar < 2; tar++){
    mem(vis, 0);
    for(ll i = 0; i < n; i++){
      for(ll j = 0; j < 1; j++){
        go(i, j, tar);
      }
    }
  }
  for(ll j = 0; j < Q; j++){
    ll ans = 0;
    for(ll tar = 0; tar < 2; tar++){
      for(ll i = 0; i < n; i++){
        if(dp[i][0][tar] <= G[j] && (G[j] - dp[i][0][tar]) % cycle[tar] == 0){
         ans++;
        }
      }
    }
    answer(ans);
  }

  return;
}


# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 4 ms 9684 KB Output is correct
5 Correct 5 ms 9684 KB Output is correct
6 Correct 5 ms 9760 KB Output is correct
7 Correct 4 ms 9684 KB Output is correct
8 Correct 5 ms 9812 KB Output is correct
9 Correct 6 ms 9992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 4 ms 9684 KB Output is correct
5 Correct 5 ms 9684 KB Output is correct
6 Correct 5 ms 9760 KB Output is correct
7 Correct 4 ms 9684 KB Output is correct
8 Correct 5 ms 9812 KB Output is correct
9 Correct 6 ms 9992 KB Output is correct
10 Correct 4 ms 9684 KB Output is correct
11 Correct 10 ms 11060 KB Output is correct
12 Correct 18 ms 12180 KB Output is correct
13 Correct 35 ms 22064 KB Output is correct
14 Correct 46 ms 16888 KB Output is correct
15 Correct 49 ms 17096 KB Output is correct
16 Correct 42 ms 15788 KB Output is correct
17 Correct 38 ms 15412 KB Output is correct
18 Correct 20 ms 11940 KB Output is correct
19 Correct 45 ms 16780 KB Output is correct
20 Correct 49 ms 16972 KB Output is correct
21 Correct 44 ms 15700 KB Output is correct
22 Correct 46 ms 15308 KB Output is correct
23 Correct 49 ms 17688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 4 ms 9684 KB Output is correct
5 Correct 5 ms 9684 KB Output is correct
6 Correct 5 ms 9760 KB Output is correct
7 Correct 4 ms 9684 KB Output is correct
8 Correct 5 ms 9812 KB Output is correct
9 Correct 6 ms 9992 KB Output is correct
10 Correct 4 ms 9684 KB Output is correct
11 Correct 10 ms 11060 KB Output is correct
12 Correct 18 ms 12180 KB Output is correct
13 Correct 35 ms 22064 KB Output is correct
14 Correct 46 ms 16888 KB Output is correct
15 Correct 49 ms 17096 KB Output is correct
16 Correct 42 ms 15788 KB Output is correct
17 Correct 38 ms 15412 KB Output is correct
18 Correct 20 ms 11940 KB Output is correct
19 Correct 45 ms 16780 KB Output is correct
20 Correct 49 ms 16972 KB Output is correct
21 Correct 44 ms 15700 KB Output is correct
22 Correct 46 ms 15308 KB Output is correct
23 Correct 49 ms 17688 KB Output is correct
24 Correct 6 ms 9684 KB Output is correct
25 Correct 89 ms 11072 KB Output is correct
26 Correct 124 ms 12296 KB Output is correct
27 Correct 2032 ms 22036 KB Output is correct
28 Correct 771 ms 16928 KB Output is correct
29 Correct 2389 ms 17132 KB Output is correct
30 Correct 1382 ms 15860 KB Output is correct
31 Correct 1403 ms 15436 KB Output is correct
32 Correct 136 ms 12068 KB Output is correct
33 Correct 761 ms 16924 KB Output is correct
34 Correct 2371 ms 17132 KB Output is correct
35 Correct 1483 ms 15716 KB Output is correct
36 Correct 1809 ms 15368 KB Output is correct
37 Correct 660 ms 17884 KB Output is correct
38 Correct 2113 ms 24528 KB Output is correct