Submission #1332439

#TimeUsernameProblemLanguageResultExecution timeMemory
13324392sqrt3Island Hopping (JOI24_island)C++20
2 / 100
2 ms412 KiB
#include "island.h"
#include <vector>
#include <numeric>
#include <algorithm>

using namespace std;

struct DSU {
    vector<int> f;
    DSU(int n) {
        f.resize(n + 1);
        iota(f.begin(), f.end(), 0);
    }
    int find(int x) {
        return f[x] == x ? x : f[x] = find(f[x]);
    }
    void unite(int x, int y) {
        x = find(x); y = find(y);
        if (x != y) f[x] = y;
    }
};

void solve(int N, int L) {
    DSU dsu(N);
    vector<int> count(N + 1, 1);
    vector<bool> vis(N + 1, false);
    
    vis[1] = true;
    for (int i = 2; i <= N; ++i) {
        if (vis[dsu.find(i)]) continue;

        vector<int> path;
        int curr = i;
        while (!vis[dsu.find(curr)]) {
            path.push_back(curr);
            int v = query(curr, count[curr]);
            bool in_path = false;
            for(int p : path) if(p == v) { in_path = true; break; }

            if (in_path) {
                count[curr]++;
            } else {
                if (vis[dsu.find(v)]) {
                    int last = v;
                    while (!path.empty()) {
                        int node = path.back();
                        path.pop_back();
                        answer(node, last);
                        dsu.unite(node, last);
                        vis[node] = true;
                        last = node;
                    }
                    break;
                } else {
                    curr = v;
                }
            }
        }
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...