Submission #1041295

# Submission time Handle Problem Language Result Execution time Memory
1041295 2024-08-01T20:20:17 Z YassirSalama Island Hopping (JOI24_island) C++17
100 / 100
6 ms 724 KB
#include "island.h"
#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define all(v) v.begin(),v.end()
#define pb push_back
template<typename T> 
void dbg(const T& t){
  cout<<t<<endl;
}
template<typename T,typename... Args>
void dbg(const T&t,const Args&... args){
  cout<<t<<" , ";
  dbg(args...);
}
#define dbg(...) cout<<"("<<#__VA_ARGS__<<"): ";dbg(__VA_ARGS__);
set<int> seen;
const int maxn=310;
set<int> v[maxn];
map<pair<int,int>,int> mp;
int q(int a,int b){
  if(mp.count(make_pair(a,b))) return mp[make_pair(a,b)];
  return mp[(make_pair(a,b))]=query(a,b);
}
int par[maxn];
int find(int node){
  return node==par[node]?node:par[node]=find(par[node]);
}
bool visited[maxn];
void dfs(int node,int par){
  visited[node]=1;
  for(auto x:v[node]){
    if(x==par) continue;
    dfs(x,node);
  }
}
void solve(int N, int L) {
  vector<pair<int,int>> edges;
  map<pair<int,int>,bool> vis;
  int n=N;
  for(int i=0;i<=n;i++) par[i]=i;
  for(int node=1;node<=n;node++){
    for(int j=1;j<n;j++){
      int x=query(node,j);
      if(find(x)==find(node)){
        break;
      }
      if(x>=node){
        break;
      }
      if(vis.count({node,x})||vis.count({x,node})) continue;
      vis[{node,x}]=vis[{x,node}]=1;
      v[node].insert(x);
      v[x].insert(node);
      edges.pb({node,x});
      int a=find(node);
      int b=find(x);
      par[b]=a;
    }
  }
  for(auto x:edges){
    answer(x.F,x.S);
    // dbg(x.F,x.S)
  }
}


#ifdef IOI
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <random>
#include <utility>
#include <vector>

#include "island.h"

namespace {

const int invalid_v_range = 1;
const int invalid_k_range = 2;
const int query_limit_exceeded = 3;
const int invalid_xy_range = 4;
const int no_edge = 5;
const int answer_twice = 6;
const int invalid_answer_times = 7;

int N, L, ok;
std::vector<int> A, B, answered;
std::vector<std::vector<int>> g, dist, que;
std::vector<std::pair<int, int>> p;

int query_count = 0;
int answer_count = 0;

void wronganswer(int code) {
  printf("Wrong Answer [%d]\n", code);
  exit(0);
}

void dfs(int now, int pre, std::vector<std::vector<int>> &g,
         std::vector<int> &dist) {
  for (int nxt : g[now]) {
    if (nxt == pre) {
      continue;
    }
    dist[nxt] = dist[now] + 1;
    dfs(nxt, now, g, dist);
  }
}

}  // namespace

int query(int v, int k) {
  if (v < 1 || N < v) {
    wronganswer(invalid_v_range);
  }
  if (k < 1 || N - 1 < k) {
    wronganswer(invalid_k_range);
  }

  query_count++;
  if (query_count > L) {
    wronganswer(query_limit_exceeded);
  }

  return que[v - 1][k] + 1;
}

void answer(int x, int y) {
  if (x < 1 || N < x || y < 1 || N < y) {
    wronganswer(invalid_xy_range);
  }
  if (x > y) {
    std::swap(x, y);
  }

  answer_count++;
  if (answer_count > N - 1) {
    wronganswer(invalid_answer_times);
  }

  ok = -1;
  for (int i = 0; i < N - 1; i++) {
    if (A[i] == x && B[i] == y) {
      if (answered[i] != 0) {
        wronganswer(answer_twice);
      }
      ok = i;
    }
  }

  if (ok == -1) {
    wronganswer(no_edge);
  }

  answered[ok] = 1;
}

int main(int argc, char **argv) {
  if (scanf("%d", &N) != 1) {
    fprintf(stderr, "Error while reading input1.\n");
    exit(1);
  }
  if (scanf("%d", &L) != 1) {
    fprintf(stderr, "Error while reading input2.\n");
    exit(1);
  }

  A.resize(N - 1);
  B.resize(N - 1);
  answered.resize(N - 1, 0);
  g.resize(N);
  dist.resize(N, std::vector<int>(N));
  que.resize(N);

  for (int i = 0; i < N - 1; i++) {
    if (scanf("%d", &A[i]) != 1) {
      fprintf(stderr, "Error while reading input3.\n");
      exit(1);
    }
    if (scanf("%d", &B[i]) != 1) {
      fprintf(stderr, "Error while reading input4.\n");
      exit(1);
    }
  }
  for (int i = 0; i < N - 1; i++) {
    if (A[i] >= B[i]) {
      std::swap(A[i], B[i]);
    }
    g[A[i] - 1].emplace_back(B[i] - 1);
    g[B[i] - 1].emplace_back(A[i] - 1);
  }

  for (int i = 0; i < N; i++) {
    dist[i][i] = 0;
    dfs(i, -1, g, dist[i]);

    p.clear();
    for (int j = 0; j < N; j++) {
      p.emplace_back(dist[i][j], j);
    }
    std::sort(p.begin(), p.end());

    for (int j = 0; j < N; j++) {
      que[i].emplace_back(p[j].second);
    }
  }

  solve(N, L);
  if (answer_count != N - 1) {
    wronganswer(invalid_answer_times);
  }
  printf("Accepted: %d\n", query_count);
  return 0;
}
#endif
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 6 ms 344 KB Output is correct
6 Correct 4 ms 344 KB Output is correct
7 Correct 4 ms 344 KB Output is correct
8 Correct 4 ms 456 KB Output is correct
9 Correct 6 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 340 KB Output is correct
2 Correct 3 ms 344 KB Output is correct
3 Correct 3 ms 344 KB Output is correct
4 Correct 3 ms 344 KB Output is correct
5 Correct 4 ms 344 KB Output is correct
6 Correct 5 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 3 ms 344 KB Output is correct
3 Correct 3 ms 344 KB Output is correct
4 Correct 3 ms 448 KB Output is correct
5 Correct 3 ms 496 KB Output is correct
6 Correct 3 ms 344 KB Output is correct
7 Correct 4 ms 452 KB Output is correct
8 Correct 4 ms 480 KB Output is correct
9 Correct 3 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 344 KB Output is correct
2 Correct 4 ms 344 KB Output is correct
3 Correct 4 ms 444 KB Output is correct
4 Correct 3 ms 344 KB Output is correct
5 Correct 4 ms 344 KB Output is correct
6 Correct 4 ms 344 KB Output is correct
7 Correct 4 ms 344 KB Output is correct
8 Correct 4 ms 600 KB Output is correct
9 Correct 4 ms 488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 6 ms 344 KB Output is correct
6 Correct 4 ms 344 KB Output is correct
7 Correct 4 ms 344 KB Output is correct
8 Correct 4 ms 456 KB Output is correct
9 Correct 6 ms 344 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 3 ms 344 KB Output is correct
12 Correct 3 ms 344 KB Output is correct
13 Correct 3 ms 448 KB Output is correct
14 Correct 3 ms 496 KB Output is correct
15 Correct 3 ms 344 KB Output is correct
16 Correct 4 ms 452 KB Output is correct
17 Correct 4 ms 480 KB Output is correct
18 Correct 3 ms 344 KB Output is correct
19 Correct 4 ms 344 KB Output is correct
20 Correct 4 ms 440 KB Output is correct
21 Correct 4 ms 456 KB Output is correct
22 Correct 4 ms 344 KB Output is correct
23 Correct 4 ms 344 KB Output is correct
24 Correct 3 ms 344 KB Output is correct
25 Correct 4 ms 344 KB Output is correct
26 Correct 3 ms 464 KB Output is correct
27 Correct 4 ms 344 KB Output is correct
28 Correct 4 ms 452 KB Output is correct
29 Correct 4 ms 344 KB Output is correct
30 Correct 5 ms 464 KB Output is correct
31 Correct 4 ms 344 KB Output is correct
32 Correct 4 ms 344 KB Output is correct
33 Correct 3 ms 476 KB Output is correct
34 Correct 4 ms 484 KB Output is correct
35 Correct 4 ms 476 KB Output is correct
36 Correct 3 ms 344 KB Output is correct
37 Correct 4 ms 344 KB Output is correct
38 Correct 4 ms 344 KB Output is correct
39 Correct 5 ms 344 KB Output is correct
40 Correct 4 ms 444 KB Output is correct
41 Correct 3 ms 344 KB Output is correct
42 Correct 3 ms 344 KB Output is correct
43 Correct 4 ms 468 KB Output is correct
44 Correct 5 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 4 ms 344 KB Output is correct
5 Correct 4 ms 344 KB Output is correct
6 Correct 4 ms 444 KB Output is correct
7 Correct 3 ms 344 KB Output is correct
8 Correct 4 ms 344 KB Output is correct
9 Correct 4 ms 344 KB Output is correct
10 Correct 4 ms 344 KB Output is correct
11 Correct 4 ms 600 KB Output is correct
12 Correct 4 ms 488 KB Output is correct
13 Correct 4 ms 344 KB Output is correct
14 Correct 4 ms 488 KB Output is correct
15 Correct 5 ms 344 KB Output is correct
16 Correct 4 ms 484 KB Output is correct
17 Correct 4 ms 344 KB Output is correct
18 Correct 3 ms 480 KB Output is correct
19 Correct 4 ms 344 KB Output is correct
20 Correct 4 ms 600 KB Output is correct
21 Correct 3 ms 484 KB Output is correct
22 Correct 4 ms 344 KB Output is correct
23 Correct 3 ms 444 KB Output is correct
24 Correct 4 ms 344 KB Output is correct
25 Correct 3 ms 344 KB Output is correct
26 Correct 4 ms 500 KB Output is correct
27 Correct 4 ms 344 KB Output is correct
28 Correct 4 ms 440 KB Output is correct
29 Correct 4 ms 344 KB Output is correct
30 Correct 3 ms 492 KB Output is correct
31 Correct 4 ms 344 KB Output is correct
32 Correct 4 ms 452 KB Output is correct
33 Correct 4 ms 344 KB Output is correct
34 Correct 3 ms 344 KB Output is correct
35 Correct 4 ms 344 KB Output is correct
36 Correct 4 ms 448 KB Output is correct
37 Correct 4 ms 344 KB Output is correct
38 Correct 4 ms 500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 340 KB Output is correct
2 Correct 3 ms 344 KB Output is correct
3 Correct 3 ms 344 KB Output is correct
4 Correct 3 ms 344 KB Output is correct
5 Correct 4 ms 344 KB Output is correct
6 Correct 5 ms 344 KB Output is correct
7 Correct 4 ms 344 KB Output is correct
8 Correct 4 ms 496 KB Output is correct
9 Correct 4 ms 344 KB Output is correct
10 Correct 5 ms 516 KB Output is correct
11 Correct 4 ms 344 KB Output is correct
12 Correct 3 ms 344 KB Output is correct
13 Correct 3 ms 448 KB Output is correct
14 Correct 3 ms 344 KB Output is correct
15 Correct 4 ms 344 KB Output is correct
16 Correct 4 ms 344 KB Output is correct
17 Correct 3 ms 724 KB Output is correct
18 Correct 4 ms 344 KB Output is correct
19 Correct 4 ms 444 KB Output is correct
20 Correct 4 ms 344 KB Output is correct
21 Correct 4 ms 344 KB Output is correct
22 Correct 4 ms 340 KB Output is correct
23 Correct 4 ms 344 KB Output is correct
24 Correct 4 ms 344 KB Output is correct
25 Correct 5 ms 344 KB Output is correct
26 Correct 5 ms 344 KB Output is correct
27 Correct 4 ms 344 KB Output is correct
28 Correct 4 ms 344 KB Output is correct
29 Correct 3 ms 344 KB Output is correct
30 Correct 4 ms 344 KB Output is correct
31 Correct 4 ms 472 KB Output is correct
32 Correct 4 ms 344 KB Output is correct
33 Correct 4 ms 444 KB Output is correct
34 Correct 4 ms 344 KB Output is correct
35 Correct 4 ms 344 KB Output is correct
36 Correct 4 ms 496 KB Output is correct
37 Correct 4 ms 476 KB Output is correct
38 Correct 3 ms 480 KB Output is correct
39 Correct 4 ms 600 KB Output is correct
40 Correct 4 ms 344 KB Output is correct