Submission #430990

# Submission time Handle Problem Language Result Execution time Memory
430990 2021-06-17T08:46:01 Z 8e7 Park (JOI17_park) C++14
100 / 100
349 ms 836 KB
#include "park.h"
//Challenge: Accepted
#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <random>
#include <time.h>
#include <assert.h>
#include <unordered_map>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace std;
//using namespace __gnu_pbds;
void debug() {cout << endl;}
template <class T, class ...U> void debug(T a, U ... b) { cout << a << " "; debug(b...);}
template <class T> void pary(T l, T r) {
	while (l != r) {cout << *l << " ";l++;}
	cout << endl;
}
#define ll long long
#define ld long double
#define maxn 1405
#define mod 1000000007
#define pii pair<long long, long long>
#define ff first
#define ss second

static int que[maxn];
vector<pii> ed;
vector<int> adj[maxn], vert;
int ord[maxn], par[maxn];
bool found[maxn], mark[maxn];
int qcnt = 0, mode = 0;
int query(int a, int b) {
	if (a > b) swap(a, b);
	if (a == b || (que[a] == 0) || (que[b] == 0)) return 0;
	qcnt++;
	return Ask(a, b, que);
}
vector<int> chain(vector<int> v) {
	if (v.size() <= 3) return v;
	int piv = v[1 + rand() % (v.size() - 2)];
	vector<int> lef, rig;
	lef.push_back(v[0]), rig.push_back(piv);
	for (int i:v) que[i] = 1;
	que[piv] = 0;
	for (int i = 1;i < v.size() - 1;i++) {
		if (v[i] != piv && query(v[0], v[i])) lef.push_back(v[i]);
		else if (v[i] != piv) rig.push_back(v[i]);
	}
	lef.push_back(piv), rig.push_back(v.back());
	lef = chain(lef), rig = chain(rig);
	vector<int> ret = lef;
	for (int i = 1;i < rig.size();i++) ret.push_back(rig[i]);
	return ret;
}
int ti = 0;
void dfs(int n, int par) {
	ord[ti++] = n;	
	for (int v:adj[n]) {
		if (v != par && !mark[v]) {
			dfs(v, n);
		}
	}
}
int tot;
int search(int ini, int x) {
	ti = 0;
	dfs(ini, -1);
	int low = 0, up = ti;
	//pary(ord, ord + ti);
	while (low + 1< up) {
		int mid = (low + up) / 2;
		for (int i = 0;i < ti;i++) que[ord[i]] = 1;
		for (int i = mid;i < ti;i++) que[ord[i]] = 0;
		if (query(x, ord[0])) up = mid;
		else low = mid;
	}
	return ord[low];
}
void getpath(vector<int> cand, vector<int> & ret, int x, int y) {
	if (cand.size() == 0) return;
	for (int i:cand) que[i] = 0; 
	bool res = query(x, y);
	if (res) {
		return;
	} else {
		for (int i:cand) que[i] = 1;
		if (cand.size() == 1) {
			ret.push_back(cand[0]);
			return;
		} else {
			vector<int> lef, rig;
			for (int i = 0;i < cand.size();i++) {
				if (i < cand.size() / 2) lef.push_back(cand[i]);
				else rig.push_back(cand[i]);
			}
			getpath(lef, ret, x, y), getpath(rig, ret, x, y);
		}
	}
}
void solve(int x, vector<int> v) {
	if (v.size() == 0) return;
	for (int i = 0;i < tot;i++) que[i] = 0;
	for (int i:v) que[i] = 1, mark[i] = 0;
	//pary(v.begin(), v.end());
	que[x] = 1;
	if (!query(x, v[0])) return;
	int y = search(v[0], x);
	//debug(x, y);
	ed.push_back({x, y});
	vector<vector<int> > rec;
	mark[y] = 1;
	for (int v:adj[y]) {
		if (mark[v] || v == x) continue;
		ti = 0, dfs(v, y);	
		vector<int> comp;
		for (int i = 0;i < ti;i++) comp.push_back(ord[i]);
		rec.push_back(comp);
	}
	for (auto i:rec) {
		solve(x, i);
	}
}
void Detect(int T, int N) {
	tot = N;
	if (T == 1) {
		for (int i = 0;i < N;i++) {
			for (int j = i + 1;j < N;j++) {
				que[i] = 1, que[j] = 1;
				if (query(i, j)) Answer(i, j);
				que[i] = 0, que[j] = 0;
			}
		}
		return;
	}
	srand(time(NULL));
	for (int i = 0;i < N;i++) vert.push_back(i);
	random_shuffle(vert.begin(), vert.end());
	found[vert[0]] = 1;
	for (int i = 1;i < N;i++) {
		if (found[vert[i]]) continue;
		for (int j = 0;j < N;j++) que[j] = 1;
		int anc = search(vert[0], vert[i]);	
		for (int j = 0;j < N;j++) que[j] = 0;
		vector<int> cand, path;
		for (int j = 0;j < N;j++) {
			if (!found[vert[j]] && j != i) cand.push_back(vert[j]), que[vert[j]] = 1;
		}
		que[vert[i]] = 1, que[anc] = 1;
		path.push_back(vert[i]);
		getpath(cand, path, vert[i], anc);
		path.push_back(anc);
		path = chain(path);
		for (int j:path) found[j] = 1;
		for (int j = 0;j < path.size() - 1;j++) {
			ed.push_back({path[j], path[j + 1]});
			adj[path[j]].push_back(path[j + 1]);
			adj[path[j + 1]].push_back(path[j]);
			par[path[j]] = path[j + 1];
		}	
	}
	par[vert[0]] = -1;
	ti = 0;
	dfs(vert[0], -1);
	vector<int> rev;
	for (int i = 0;i < N;i++) rev.push_back(ord[i]);
	reverse(rev.begin(), rev.end());
	for (int i = 0;i < N - 1;i++) {
		vector<int> sub;
		for (int j = i + 1;j < N;j++) {
			sub.push_back(rev[j]);
		}	
		for (int j = 0;j < N;j++) mark[j] = 1;
		//debug(49, rev[i], par[rev[i]]);
		solve(rev[i], sub);
	}
	//assert(ed.size() == N - 1);
	sort(ed.begin(), ed.end());
	ed.resize(int(unique(ed.begin(), ed.end()) - ed.begin()));
	for (auto i:ed) {
		if (i.ff > i.ss) swap(i.ff, i.ss);
		//debug(i.ff, i.ss);
		Answer(i.ff, i.ss);
	}
}
/*

4
10 14
0 1
0 2
0 5
1 2
1 3
1 5
2 4
2 6
2 7
3 6
3 9
5 9
7 8
7 9

4
8 7
0 1
0 2
0 3
0 4
0 5
0 6
0 7
*/

Compilation message

park.cpp: In function 'std::vector<int> chain(std::vector<int>)':
park.cpp:48:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   48 |  for (int i = 1;i < v.size() - 1;i++) {
      |                 ~~^~~~~~~~~~~~~~
park.cpp:55:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   55 |  for (int i = 1;i < rig.size();i++) ret.push_back(rig[i]);
      |                 ~~^~~~~~~~~~~~
park.cpp: In function 'void getpath(std::vector<int>, std::vector<int>&, int, int)':
park.cpp:95:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   95 |    for (int i = 0;i < cand.size();i++) {
      |                   ~~^~~~~~~~~~~~~
park.cpp:96:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   96 |     if (i < cand.size() / 2) lef.push_back(cand[i]);
      |         ~~^~~~~~~~~~~~~~~~~
park.cpp: In function 'void Detect(int, int)':
park.cpp:157:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  157 |   for (int j = 0;j < path.size() - 1;j++) {
      |                  ~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 5 ms 368 KB Output is correct
3 Correct 5 ms 332 KB Output is correct
4 Correct 5 ms 332 KB Output is correct
5 Correct 6 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 192 ms 664 KB Output is correct
2 Correct 149 ms 708 KB Output is correct
3 Correct 145 ms 668 KB Output is correct
4 Correct 209 ms 632 KB Output is correct
5 Correct 197 ms 664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 262 ms 528 KB Output is correct
2 Correct 298 ms 736 KB Output is correct
3 Correct 302 ms 648 KB Output is correct
4 Correct 290 ms 580 KB Output is correct
5 Correct 297 ms 612 KB Output is correct
6 Correct 303 ms 612 KB Output is correct
7 Correct 311 ms 632 KB Output is correct
8 Correct 316 ms 580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 146 ms 556 KB Output is correct
2 Correct 307 ms 816 KB Output is correct
3 Correct 306 ms 640 KB Output is correct
4 Correct 245 ms 564 KB Output is correct
5 Correct 263 ms 688 KB Output is correct
6 Correct 264 ms 580 KB Output is correct
7 Correct 284 ms 632 KB Output is correct
8 Correct 269 ms 636 KB Output is correct
9 Correct 255 ms 648 KB Output is correct
10 Correct 261 ms 640 KB Output is correct
11 Correct 283 ms 592 KB Output is correct
12 Correct 275 ms 648 KB Output is correct
13 Correct 205 ms 580 KB Output is correct
14 Correct 255 ms 580 KB Output is correct
15 Correct 209 ms 668 KB Output is correct
16 Correct 293 ms 604 KB Output is correct
17 Correct 232 ms 640 KB Output is correct
18 Correct 276 ms 632 KB Output is correct
19 Correct 228 ms 580 KB Output is correct
20 Correct 269 ms 636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 310 ms 580 KB Output is correct
2 Correct 317 ms 680 KB Output is correct
3 Correct 321 ms 660 KB Output is correct
4 Correct 275 ms 580 KB Output is correct
5 Correct 349 ms 596 KB Output is correct
6 Correct 235 ms 836 KB Output is correct
7 Correct 282 ms 724 KB Output is correct
8 Correct 279 ms 692 KB Output is correct
9 Correct 290 ms 808 KB Output is correct
10 Correct 323 ms 604 KB Output is correct
11 Correct 289 ms 728 KB Output is correct
12 Correct 271 ms 676 KB Output is correct
13 Correct 289 ms 664 KB Output is correct
14 Correct 336 ms 696 KB Output is correct
15 Correct 313 ms 656 KB Output is correct
16 Correct 289 ms 580 KB Output is correct
17 Correct 205 ms 700 KB Output is correct
18 Correct 299 ms 712 KB Output is correct