Submission #247388

# Submission time Handle Problem Language Result Execution time Memory
247388 2020-07-11T10:06:28 Z tqbfjotld Lokahian Relics (FXCUP4_lokahia) C++17
0 / 100
6 ms 896 KB
#include "lokahia.h"
#include <bits/stdc++.h>
using namespace std;

int NN;

vector<int> thing[205];
int qLim = 300;
int p[205];
int queried[205][205];
bool vis[205];
int root(int a){
return p[a]==a?a:p[a]=root(p[a]);
}
priority_queue<pair<pair<int,int>,int>,vector<pair<pair<int,int>,int> >,greater<pair<pair<int,int>,int> > > pq;
int sz[205];

int queryCount = 0;

int query(int a, int b){
    if (queryCount>=qLim){
        return -1;
    }
    if (a==b) return a;
    if (a<0 || a>=NN || b<0 || b>=NN) return 1/0;
    if (queried[a][b]!=-1) return queried[a][b];
    int res = CollectRelics(a,b);
    queryCount++;
    if (res!=-1){
        thing[res].push_back(a);
        thing[res].push_back(b);
        //printf("add edge %d %d, %d %d\n",res,a,res,b);
    }
    queried[a][b] = res;
    queried[b][a] = res;
    return res;
}

int dfs(int node){
    vis[node] = true;
    int res = 1;
    for (auto x : thing[node]){
        if (!vis[x]){
            res += dfs(x);
        }
    }
    return res;
}

int FindBase(int N){
    memset(queried,-1,sizeof(queried));
    NN = N;
    srand(time(NULL));
    if (N==1) return 0;
	for (int x = 0; x<N; x++){
        pq.push({{1,rand()},x});
        p[x] = x;
        sz[x] = 1;
	}
	int mn = -1;
	vector<int> mns;
    while (!pq.empty()){
        if (pq.size()<2){
            if (pq.size()==1) {mns.push_back(pq.top().second);pq.pop();}
            continue;
        }
        auto a = pq.top();
        pq.pop();
        auto b = pq.top();
        pq.pop();
        if (a.first!=b.first) {
            pq.push(b);
            continue;
        }
        int res = query(root(a.second),root(b.second));
        if (res==-2) return -1;
        if (res==-1){
        }
        else{
            if (root(b.second)!=root(res)){
                sz[root(res)] += sz[root(b.second)];
                p[root(b.second)] = res;
            }
            if (root(a.second)!=root(res)){
                sz[root(res)] += sz[root(a.second)];
                p[root(a.second)] = res;
            }
            pq.push({{sz[root(res)],rand()},res});
        }
    }
    vector<pair<int,int> > mn2;
    for (int x = 0; x<N; x++){
        mn2.push_back({sz[root(x)],x});
    }
    sort(mn2.begin(),mn2.end(),greater<pair<int,int> >());
    int c = 0;
    for (auto mnx : mn2){
        int mn = mnx.second;
        for (auto y : mn2){
            int x = y.second;
            if (root(x)!=root(mn)&&queryCount<qLim){
                int res = query(root(x),root(mn));
                if (res==-1) continue;
                p[root(x)] = res;
                p[root(mn)] = res;
            }
        }
    }

    for (int x = 0; x<N; x++){
            for (int y = 0; y<N; y++){
                vis[y] = false;
            }
        if (dfs(x)>N/2){
            return x;
        }
    }


	return -1;
}

Compilation message

lokahia.cpp: In function 'int query(int, int)':
lokahia.cpp:25:47: warning: division by zero [-Wdiv-by-zero]
     if (a<0 || a>=NN || b<0 || b>=NN) return 1/0;
                                              ~^~
lokahia.cpp: In function 'int FindBase(int)':
lokahia.cpp:60:6: warning: unused variable 'mn' [-Wunused-variable]
  int mn = -1;
      ^~
lokahia.cpp:96:9: warning: unused variable 'c' [-Wunused-variable]
     int c = 0;
         ^
# Verdict Execution time Memory Grader output
1 Correct 6 ms 768 KB Correct : C = 197
2 Correct 5 ms 768 KB Correct : C = 117
3 Incorrect 6 ms 768 KB Wrong
4 Correct 6 ms 768 KB Correct : C = 300
5 Correct 6 ms 768 KB Correct : C = 198
6 Correct 6 ms 768 KB Correct : C = 300
7 Correct 5 ms 768 KB Correct : C = 300
8 Correct 5 ms 768 KB Correct : C = 300
9 Correct 5 ms 768 KB Correct : C = 118
10 Correct 6 ms 768 KB Correct : C = 300
11 Correct 6 ms 768 KB Correct : C = 300
12 Correct 5 ms 768 KB Correct : C = 300
13 Incorrect 6 ms 768 KB Wrong
14 Correct 6 ms 768 KB Correct : C = 300
15 Correct 6 ms 768 KB Correct : C = 300
16 Correct 5 ms 768 KB Correct : C = 300
17 Correct 6 ms 768 KB Correct : C = 300
18 Correct 5 ms 768 KB Correct : C = 300
19 Correct 5 ms 768 KB Correct : C = 300
20 Correct 5 ms 768 KB Correct : C = 300
21 Correct 5 ms 640 KB Correct : C = 3
22 Correct 6 ms 768 KB Correct : C = 300
23 Correct 6 ms 816 KB Correct : C = 300
24 Correct 5 ms 640 KB Correct : C = 0
25 Correct 6 ms 768 KB Correct : C = 300
26 Correct 5 ms 768 KB Correct : C = 300
27 Correct 6 ms 896 KB Correct : C = 300