제출 #1123128

#제출 시각아이디문제언어결과실행 시간메모리
1123128SalihSahin게임 (IOI14_game)C++20
15 / 100
1 ms332 KiB
#include <bits/stdc++.h>
#define pb push_back
//#define int long long
using namespace std;
#include "game.h"


int N;
vector<int> par;

struct DSU{
    vector<int> arr;
    int option;
};

vector<DSU> dsuarr;
vector<vector<int> > edge;

int fnd(int a){
    if(a == par[a]) return a;
    else return par[a] = fnd(par[a]);
}

void merge(int a, int b){
    int olda = a, oldb = b;
    a = fnd(a), b = fnd(b);

    int new_op = dsuarr[a].option + dsuarr[b].option - 2;
    par[b] = a;
    for(auto itr: dsuarr[b].arr){
        dsuarr[a].arr.pb(itr);
    }
    dsuarr[a].option = new_op;
    edge[olda][oldb] = edge[oldb][olda] = 1;
}

void initialize(int n) {
    N = n;
    edge.resize(n);
    for(int i = 0; i < n; i++){
        edge[i].resize(n);
        edge[i][i] = -1;
    }
    dsuarr.resize(n);
    for(int i = 0; i < n; i++){
        dsuarr[i].arr.pb(i);
        dsuarr[i].option = n-1;
    }
    par.resize(n);
    for(int i = 0; i < n; i++){
        par[i] = i;
    }
}


int hasEdge(int u, int v) {
    int uset = fnd(u), vset = fnd(v);
    if(edge[u][v] != 0){
        if(edge[u][v] == -1) return 0;
        else return 1;
    }

    if(dsuarr[uset].option == 1 || dsuarr[vset].option == 1){
        merge(u, v);
        return 1;
    }

    dsuarr[uset].option--;
    dsuarr[vset].option--;
    edge[u][v] = edge[v][u] = -1;

    while(dsuarr[fnd(u)].option == 1){
        int x = 0, y = 0;
        vector<int> vis(N);
        for(auto itr: dsuarr[fnd(u)].arr){
            vis[itr] = 1;
        }
        vector<int> others;
        for(int i = 0; i < N; i++){
            if(!vis[i]) others.pb(i);
        }

        for(auto itr: dsuarr[fnd(u)].arr){
            for(auto j: others){
                if(edge[itr][j] == 0){
                    x = itr;
                    y = j;
                }
            }
        }

        merge(x, y);
    }

    while(dsuarr[fnd(v)].option == 1){
        int x = 0, y = 0;
        vector<int> vis(N);
        for(auto itr: dsuarr[fnd(v)].arr){
            vis[itr] = 1;
        }
        vector<int> others;
        for(int i = 0; i < N; i++){
            if(!vis[i]) others.pb(i);
        }

        for(auto itr: dsuarr[fnd(v)].arr){
            for(auto j: others){
                if(edge[itr][j] == 0){
                    x = itr;
                    y = j;
                }
            }
        }

        merge(x, y);
    }

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...