#include "lokahia.h"
#include<algorithm>
#include<vector>
const int NMAX = 200;
int p[NMAX];
std::vector<int> child[NMAX];
int Find(int u){return p[u];}
bool Merge(int u, int v, int c){
u = Find(u), v = Find(v), c = Find(c);
if(u==v && v==c) return false;
if(u==v){
for(int x:child[u]){
p[x] = c;
child[c].push_back(x);
}
}
else if(c==u){
for(int x:child[v]){
p[x] = u;
child[u].push_back(x);
}
}
else if(c==v){
for(int x:child[u]){
p[x] = v;
child[v].push_back(x);
}
}
else{
for(int x:child[u]){
p[x] = c;
child[c].push_back(x);
}
for(int x:child[v]){
p[x] = c;
child[c].push_back(x);
}
}
return true;
}
int cache[NMAX][NMAX]; // init by -2
int COUNT = 0;
const int LIMIT = 600;
int CR(int u, int v){
u=Find(u), v=Find(v);
if(u==v) return u;
if(u>v) std::swap(u,v);
if(cache[u][v]!=-2) return cache[u][v];
if(COUNT>=LIMIT) return -1;
COUNT++;
return cache[u][v] = CollectRelics(u,v);
}
int FindBase(int N){
if(N==1) return 0;
for(int i=0;i<N;i++) child[i].push_back(i), p[i]=i;
for(int i=0;i<N;i++) for(int j=0;j<N;j++) cache[i][j] = -2;
while(1){
std::vector<int> roots;
for(int i=0;i<N;i++) if(Find(i)==i) roots.push_back(i);
std::random_shuffle(roots.begin(), roots.end());
std::vector<int> reals;
for(int root:roots){
std::random_shuffle(child[root].begin(), child[root].end());
for(int c:child[root]) reals.push_back(c);
}
std::vector<int> L, R;
int sz = N/2;
for(int i=0;i<sz;i++) L.push_back(reals[i]);
for(int i=sz;i<sz+sz;i++) R.push_back(reals[i]);
bool all_false = true;
for(int i=0;i<sz;i++){
int u = L[i], v = R[i];
int c = CR(u, v);
if(c!=-1){
if(Merge(u, v, c)) all_false = false;
}
}
if(N%2){
int last = reals.back();
int l = L.back(), r = R.back();
int c = CR(l, last);
if(c!=-1){
if(Merge(l, last, c)) all_false = false;
}
c = CR(r, last);
if(c!=-1){
if(Merge(r, last, c)) all_false = false;
}
}
if(all_false) break;
}
for(int i=0;i<N;i++) if(Find(i)==i && child[i].size() > N/2) return i;
return -1;
}
Compilation message
lokahia.cpp: In function 'int FindBase(int)':
lokahia.cpp:105:59: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<N;i++) if(Find(i)==i && child[i].size() > N/2) return i;
~~~~~~~~~~~~~~~~^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
640 KB |
Correct : C = 167 |
2 |
Correct |
5 ms |
620 KB |
Correct : C = 61 |
3 |
Correct |
6 ms |
768 KB |
Correct : C = 224 |
4 |
Correct |
5 ms |
512 KB |
Correct : C = 3 |
5 |
Correct |
6 ms |
768 KB |
Correct : C = 300 |
6 |
Correct |
6 ms |
640 KB |
Correct : C = 158 |
7 |
Correct |
7 ms |
768 KB |
Correct : C = 100 |
8 |
Correct |
5 ms |
640 KB |
Correct : C = 165 |
9 |
Correct |
6 ms |
768 KB |
Correct : C = 231 |
10 |
Correct |
6 ms |
640 KB |
Correct : C = 60 |
11 |
Correct |
6 ms |
640 KB |
Correct : C = 60 |
12 |
Correct |
6 ms |
768 KB |
Correct : C = 100 |
13 |
Correct |
6 ms |
768 KB |
Correct : C = 250 |
14 |
Partially correct |
6 ms |
768 KB |
Partially correct : C = 600 |
15 |
Partially correct |
7 ms |
768 KB |
Partially correct : C = 332 |
16 |
Correct |
6 ms |
768 KB |
Correct : C = 297 |
17 |
Partially correct |
6 ms |
768 KB |
Partially correct : C = 595 |
18 |
Correct |
6 ms |
640 KB |
Correct : C = 60 |
19 |
Correct |
6 ms |
768 KB |
Correct : C = 100 |
20 |
Correct |
6 ms |
768 KB |
Correct : C = 101 |
21 |
Correct |
6 ms |
640 KB |
Correct : C = 192 |
22 |
Correct |
7 ms |
768 KB |
Correct : C = 266 |
23 |
Correct |
6 ms |
768 KB |
Correct : C = 231 |
24 |
Correct |
6 ms |
640 KB |
Correct : C = 128 |
25 |
Correct |
5 ms |
512 KB |
Correct : C = 0 |
26 |
Correct |
6 ms |
768 KB |
Correct : C = 180 |
27 |
Correct |
7 ms |
768 KB |
Correct : C = 271 |