답안 #1022544

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1022544 2024-07-13T17:25:50 Z Tob Park (JOI17_park) C++14
100 / 100
133 ms 3444 KB
#include <bits/stdc++.h>
#include "park.h"

#define F first
#define S second
#define all(x) x.begin(), x.end()
#define pb push_back
#define FIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)

using namespace std;

typedef long long ll;
typedef pair <ll, ll> pii;
/*
#define NMAX  1400
#define DMAX  7
#define QMAX  45000

static int T,N,M,Asktotal,Answertotal;
static int edge_list[NMAX][DMAX];
static int checked[NMAX][DMAX];
static int degree[NMAX];
static int visited[NMAX];
static void WA(int wa_num) {
	cout << "Wrong Answer " << wa_num << "\n";
	exit(0);
}
void Detect(int T, int N);

void Answer(int A, int B) {
	int i;
	if(A < 0||A >= B||B >= N) WA(1);
	for(i = 0 ; i < degree[A] ; i++) {
		if(edge_list[A][i] == B) {
			if(checked[A][i] == 1) WA(3);
			Answertotal++;
			checked[A][i] = 1;
			return;
		}
	}
	WA(2);
}
static void dfs(int now, int Place[]) {
	visited[now] = 1;
	int i;
	for(i = 0 ; i < degree[now] ; i++) {
		if(visited[edge_list[now][i]] == 0 && Place[edge_list[now][i]] == 1) dfs(edge_list[now][i], Place);
	}
}
int Ask(int A, int B, int Place[]) {
	int i;
	Asktotal++;
	if(Asktotal>QMAX) WA(5);
	if(A < 0||A >= B||B >= N) WA(4);
	if(Place[A] != 1) WA(4);
	if(Place[B] != 1) WA(4);
	for(i = 0 ; i < N ; i++) {
		if(Place[i]<0||Place[i]>1) WA(4);
		visited[i] = 0;
	}
	dfs(A, Place);
	return visited[B];
}*/

//-----------------------------

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int val(int x) {
	uniform_int_distribution <int> rnd(0, x-1);
	return rnd(rng);
}

static int Place[1400];
int n, tim, m;
int bio[1400], dis[1400], st[1400], en[1400], wh[1400];
bool ban[1400][1400];
vector <int> adj[1400];

void dfs(int x, int p = -1) {
	for (auto y : adj[x]) {
		if (y == p) continue;
		dis[y] = dis[x] + 1;
		dfs(y, x);
	}
}

void dfs2(int x, int p = -1) {
	wh[tim] = x;
	st[x] = tim++;
	for (auto y : adj[x]) {
		if (y == p) continue;
		dfs2(y, x);
	}
	en[x] = tim-1;
}

void ans(int x, int y) {
	if (x > y) swap(x, y);
	if (ban[x][y]) return;
	ban[x][y] = 1;
	Answer(x, y);
	m++;
	if (m >= n) return;
	adj[x].pb(y);
	adj[y].pb(x);
	bio[x] = bio[y] = 1;
}
int ask(int x, int y) {
	if (x > y) swap(x, y);
	return Ask(x, y, Place);
}

void rek(int x, int y) {
	for (int i = 0; i < n; i++) Place[i] = 0;
	Place[x] = Place[y] = 1;
	if (ask(x, y)) {
		ans(x, y);
		return;
	}
	vector <int> v;
	for (int i = 0; i < n; i++) {
		if (i == x || i == y || bio[i]) continue;
		Place[i] = 1;
		v.pb(i);
	}
	int lo = 0, hi = n-3;
	while (lo < hi) {
		int mid = (lo + hi + 1) / 2;
		for (int i = mid; i < v.size(); i++) Place[v[i]] = 0;
		if (ask(x, y)) hi = mid-1;
		else lo = mid;
		for (int i = mid; i < v.size(); i++) Place[v[i]] = 1;
	}
	rek(x, v[lo]);
	rek(v[lo], y);
}

void rec(int x, int y, vector <int> u) {
	if (u.empty()) return;
	for (int i = 0; i < n; i++) Place[i] = 0;
	Place[x] = 1;
	for (int i = 0; i < u.size(); i++) Place[u[i]] = 1;
	if (!ask(x, y)) return;
	int lo = 0, hi = u.size()-1;
	while (lo < hi) {
		int mid = (lo + hi) / 2;
		for (int i = 0; i < n; i++) Place[i] = 0;
		Place[x] = 1;
		for (int i = 0; i <= mid; i++) Place[u[i]] = 1;
		if (ask(x, y)) hi = mid;
		else lo = mid+1;
	}
	int z = u[lo];
	ans(x, z);
	for (auto it : adj[z]) {
		if (st[it] < st[z]) continue;
		vector <int> uu;
		for (int i = st[it]; i <= en[it]; i++) uu.pb(wh[i]);
		rec(x, it, uu);
	}
	vector <int> uu;
	for (auto i : u) if (st[i] < st[z] || st[i] > en[z]) uu.pb(i);
	rec(x, y, uu);
}

void Detect(int T, int N) {
	n = N;
	if (N == 2) {
		Answer(0, 1);
		return;
	}
	bio[0] = 1;
	while (1) {
		vector <int> v;
		for (int i = 0; i < n; i++) if (!bio[i]) v.pb(i);
		if (v.empty()) break;
//		shuffle(all(v), rng);
		int x = v[0];
		
		dis[0] = 0;
		dfs(0);
		int lo = 0, hi = 0;
		for (int i = 0; i < n; i++) if (bio[i]) hi = max(hi, dis[i]);
		while (lo < hi) {
			int mid = (lo + hi + 1) / 2;
			for (int i = 0; i < n; i++) Place[i] = (!bio[i] || dis[i] < mid);
			if (!ask(0, x)) lo = mid;
			else hi = mid-1;
		}
		int d = lo;
		if (!d) {
			rek(0, x);
			continue;
		}
		v.clear();
		for (int i = 0; i < n; i++) if (bio[i] && dis[i] == d) v.pb(i);
		lo = 0; hi = v.size()-1;
		while (lo < hi) {
			int mid = (lo + hi) / 2;
			for (int i = 0; i < n; i++) Place[i] = (!bio[i] || dis[i] <= d);
			for (int i = mid+1; i < v.size(); i++) Place[v[i]] = 0;
			if (ask(0, x)) hi = mid;
			else lo = mid+1;
		}
		rek(v[lo], x);
	}
	for (int i = 0; i < n; i++) {
		for (auto x : adj[i]) {
			tim = 0;
			dfs2(x, i);
			for (auto y : adj[x]) {
				if (y == i) continue;
				vector <int> uu;
				for (int i = st[y]; i <= en[y]; i++) uu.pb(wh[i]);
				rec(i, y, uu);
			}
		}
	}
}

//-----------------------------
/*
int main(int argc, char **argv) {
	int i;
	cin >> T >> N >> M;
	Answertotal = 0;
	for(i = 0 ; i < M ; i++) {
		int ea,eb;
		cin >> ea >> eb;
		checked[ea][degree[ea]] =  0;
		checked[eb][degree[eb]] =  0;
		edge_list[ea][degree[ea]++] = eb;
		edge_list[eb][degree[eb]++] = ea;
	}
	Detect(T, N);
	if(Answertotal<M) WA(6);
	cout << "Accepted\n";
	return 0;
}*/

Compilation message

park.cpp: In function 'void rek(int, int)':
park.cpp:129:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  129 |   for (int i = mid; i < v.size(); i++) Place[v[i]] = 0;
      |                     ~~^~~~~~~~~~
park.cpp:132:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  132 |   for (int i = mid; i < v.size(); i++) Place[v[i]] = 1;
      |                     ~~^~~~~~~~~~
park.cpp: In function 'void rec(int, int, std::vector<int>)':
park.cpp:142:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  142 |  for (int i = 0; i < u.size(); i++) Place[u[i]] = 1;
      |                  ~~^~~~~~~~~~
park.cpp: In function 'void Detect(int, int)':
park.cpp:201:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  201 |    for (int i = mid+1; i < v.size(); i++) Place[v[i]] = 0;
      |                        ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 4 ms 860 KB Output is correct
3 Correct 6 ms 860 KB Output is correct
4 Correct 14 ms 860 KB Output is correct
5 Correct 53 ms 904 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 89 ms 2384 KB Output is correct
2 Correct 133 ms 2604 KB Output is correct
3 Correct 90 ms 3444 KB Output is correct
4 Correct 57 ms 2400 KB Output is correct
5 Correct 53 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 88 ms 2224 KB Output is correct
2 Correct 94 ms 2080 KB Output is correct
3 Correct 98 ms 2084 KB Output is correct
4 Correct 92 ms 2000 KB Output is correct
5 Correct 96 ms 2072 KB Output is correct
6 Correct 92 ms 2108 KB Output is correct
7 Correct 105 ms 2188 KB Output is correct
8 Correct 99 ms 2204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 1692 KB Output is correct
2 Correct 84 ms 2132 KB Output is correct
3 Correct 129 ms 2128 KB Output is correct
4 Correct 73 ms 2140 KB Output is correct
5 Correct 80 ms 2384 KB Output is correct
6 Correct 72 ms 2384 KB Output is correct
7 Correct 66 ms 2428 KB Output is correct
8 Correct 81 ms 2128 KB Output is correct
9 Correct 73 ms 2312 KB Output is correct
10 Correct 93 ms 2216 KB Output is correct
11 Correct 87 ms 2372 KB Output is correct
12 Correct 92 ms 2352 KB Output is correct
13 Correct 57 ms 2136 KB Output is correct
14 Correct 105 ms 2328 KB Output is correct
15 Correct 61 ms 2320 KB Output is correct
16 Correct 87 ms 2132 KB Output is correct
17 Correct 51 ms 2388 KB Output is correct
18 Correct 94 ms 2384 KB Output is correct
19 Correct 57 ms 2392 KB Output is correct
20 Correct 76 ms 2136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 119 ms 2132 KB Output is correct
2 Correct 108 ms 2132 KB Output is correct
3 Correct 97 ms 2376 KB Output is correct
4 Correct 90 ms 2392 KB Output is correct
5 Correct 108 ms 2128 KB Output is correct
6 Correct 62 ms 2472 KB Output is correct
7 Correct 105 ms 2388 KB Output is correct
8 Correct 79 ms 2196 KB Output is correct
9 Correct 87 ms 2352 KB Output is correct
10 Correct 96 ms 2332 KB Output is correct
11 Correct 85 ms 2140 KB Output is correct
12 Correct 88 ms 2200 KB Output is correct
13 Correct 81 ms 2132 KB Output is correct
14 Correct 105 ms 2132 KB Output is correct
15 Correct 100 ms 2388 KB Output is correct
16 Correct 84 ms 2128 KB Output is correct
17 Correct 57 ms 2432 KB Output is correct
18 Correct 80 ms 2384 KB Output is correct