# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
379217 | tqbfjotld | 로카히아 유적 (FXCUP4_lokahia) | C++17 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
int CollectRelics(int R1, int R2);
int FindBase(int N);
static void my_assert(int TF, const char* message){
if(!TF){ puts(message); exit(0); }
}
static int N, reach[202][202], call_cnt = 0;
int CollectRelics(int R1, int R2){
call_cnt++;
my_assert(call_cnt <= 300, "Wrong : Too Much Calls");
my_assert(0 <= R1 && 0 <= R2 && R1 < N && R2 < N && R1 != R2, "Wrong : Invalid Argument");
for(int i=0; i<N; i++){
if(reach[i][R1] && reach[i][R2]) return i;
}
return -1;
}
int main(){
my_assert(scanf("%d", &N) == 1, "Error: Invalid Input");
my_assert(1 <= N && N <= 200, "Error: Invalid Input");
int con[202][202] = {0,};
for(int i = 1; i < N; i++){
int s, e, m;
my_assert(scanf("%d%d%d", &s, &e, &m) == 3, "Error: Invalid Input");
my_assert(0 <= s && s < N && 0 <= e && e < N && s != e && 1 <= m && m <= N, "Error: Invalid Input");
con[s][e] = con[e][s] = m;
}
int near[202];
for(int i = 0; i < N; i++){
memset(near, 0, sizeof(near));
reach[i][i] = 1, near[i] = -1;
for(int j = 0; j < N; j++){
if(con[i][j] > 0 && near[j] == 0) near[j] = con[i][j];
}
for(int vis = 1; vis < N; vis++){
int nxt = -1;
for(int j = 0; j < N; j++){
if(near[j] > 0 && near[j] <= vis){
nxt = j;
break;
}
}
if(nxt < 0) break;
reach[i][nxt] = 1, near[nxt] = -1;
for(int j = 0; j < N; j++){
if(con[nxt][j] > 0 && near[j] == 0) near[j] = con[nxt][j];
}
}
}
int base = FindBase(N);
if(base == -1){
for(int i = 0; i < N; i++){
int cnt = 0;
for(int j = 0; j < N; j++) cnt += reach[i][j];
my_assert(cnt <= N / 2, "Wrong: Wrong Answer");
}
}
else if(0 <= base && base < N){
int cnt = 0;
for(int j = 0; j < N; j++) cnt += reach[base][j];
my_assert(cnt > N / 2, "Wrong: Wrong Answer");
}
else my_assert(0, "Wrong: Wrong Answer");
printf("Correct\n%d\n", call_cnt);
return 0;
}
#include <bits/stdc++.h>
using namespace std;
//#include "lokahia.h"
int X[205];
int retval[205];
int FindBase(int N){
memset(X,-1,sizeof(X));
for (int x = 0; x<N-1; x++){
retval[x+1] = CollectRelics(x,x+1);
if (retval[x+1]!=-1){
X[x+1] = X[x];
}
else{
X[x+1] = X[x+1];
}
}
int longest = -1;
int longpos = -1;
for (int x = 0; x<N; x++){
if (x-X[x]>longest){
longest = x-X[x];
longpos = x;
}
}
int fingroup = X[longpos];
int cur = N-1;
int compsize = longest+1;
int ans = -1;
for (int x = fingroup+1; X[x]==fingroup; x++){
ans = max(ans,retval[x]);
}
int queryCount = N-1;
while (cur!=-1){
if (X[X[cur]-1]==fingroup){
cur = X[cur]-1;
continue;
}
if (X[cur]==fingroup){
cur = X[cur]-1;
continue;
}
if (cur+1==fingroup){
cur = X[cur]-1;
continue;
}
if (queryCount==300) return -1;
int ret = CollectRelics(cur,fingroup);
queryCount++;
if (ret==-1){
cur = X[cur]-1;
continue;
}
else{
compsize += cur-X[cur]+1;
ans = max(ans,ret);
if (X[cur]==0) cur = -1;
else cur = X[X[cur]-1]-1;
}
}
return compsize>N/2?ans:-1;
}