Submission #290651

# Submission time Handle Problem Language Result Execution time Memory
290651 2020-09-04T09:23:44 Z maximath_1 Park (JOI17_park) C++11
100 / 100
847 ms 1272 KB
#include "park.h"
#include <vector>
#include <queue>
#include <numeric>
#include <algorithm>
#include <iostream>
using namespace std;

int n;
vector<int> graph, pend;
vector<pair<int, int> > edge;

void ans(pair<int, int> i){
	int u = i.first, v = i.second;
	if(u > v) swap(u, v);
	Answer(u, v);
}

vector<int> subarray(int l, int r, vector<int> a){
	vector<int> rs;
	for(int i = l; i <= r; i ++)
		rs.push_back(a[i]);
	return rs;
}

bool is_con(int nw, vector<int> g){
	vector<int> que(n, 0);
	for(int i : g) que[i] = 1;
	que[nw] = 1;
	return Ask(min(g[0], nw), max(g[0], nw), que.data());
}

bool is_con_via(int nw, vector<int> g, vector<int> p){
	vector<int> que(n, 0);
	for(int i : g) que[i] = 1;
	for(int i : p) que[i] = 1;
	que[nw] = 1;
	return Ask(min(g[0], nw), max(g[0], nw), que.data());
}

void connect(int nd, vector<int> g){
	if(g.empty()) return;
	vector<vector<int> > adj(n);
	vector<bool> active(n, 0);
	for(int i : g) active[i] = 1;
	for(auto e : edge){
		if(active[e.first] && active[e.second]){
			adj[e.first].push_back(e.second);
			adj[e.second].push_back(e.first);
		}
	}

	vector<int> ord;
	vector<bool> vis(n, 0);
	ord.push_back(g[0]); vis[g[0]] = 1;
	queue<int> bfs; bfs.push(g[0]);

	for(; bfs.size(); bfs.pop()){
		int nw = bfs.front();
		for(int nx : adj[nw]) if(!vis[nx]){
			vis[nx] = 1;
			bfs.push(nx);
			ord.push_back(nx);
		}
	}

	if(ord.size() != g.size()){
		vector<int> g1, g2;
		for(int i : g)
			if(vis[i]) g1.push_back(i);
			else g2.push_back(i);
		connect(nd, g1);
		connect(nd, g2);
		return;
	}

	if(!is_con(nd, subarray(0, (int)ord.size() - 1, ord)))
		return;

	int id = (int)ord.size() - 1;
	for(int lf = 0, rg = id, md; lf <= rg;){
		md = (lf + rg) / 2;
		if(is_con(nd, subarray(0, md, ord))){
			id = md;
			rg = md - 1;
		}
		else
			lf = md + 1;
	}

	edge.push_back({nd, ord[id]});
	g.erase(find(g.begin(), g.end(), ord[id]));
	connect(nd, g);
}

vector<int> path;
void find_path(int l, int r){
	vector<int> c;
	if(l == 0) c = graph;
	else c = {l};

	if(is_con(r, c)) path.push_back(r);
	else{
		int id = (int)pend.size() - 1;
		for(int lf = 0, rg = id, md; lf <= rg;){
			md = (lf + rg) / 2;
			if(is_con_via(r, c, subarray(0, md, pend))){
				id = md;
				rg = md - 1;
			}else
				lf = md + 1;
		}
		find_path(l, pend[id]);
		find_path(pend[id], r);
	}
}

void Detect(int T, int N){
	n = N;
	graph.push_back(0);
	pend.resize(N - 1); 
	iota(pend.begin(), pend.end(), 1);

	for(; pend.size();){
		int nw = pend.back();
		path.clear();
		find_path(0, nw);
		for(int u : path){
			connect(u, graph);
			graph.push_back(u);
			pend.erase(find(pend.begin(), pend.end(), u));
		}
	}

	for(pair<int, int> i : edge) ans(i);
	return;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 27 ms 512 KB Output is correct
3 Correct 25 ms 512 KB Output is correct
4 Correct 49 ms 512 KB Output is correct
5 Correct 102 ms 632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 745 ms 888 KB Output is correct
2 Correct 667 ms 1272 KB Output is correct
3 Correct 649 ms 888 KB Output is correct
4 Correct 746 ms 924 KB Output is correct
5 Correct 743 ms 1016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 707 ms 1068 KB Output is correct
2 Correct 747 ms 880 KB Output is correct
3 Correct 759 ms 872 KB Output is correct
4 Correct 745 ms 880 KB Output is correct
5 Correct 780 ms 992 KB Output is correct
6 Correct 770 ms 980 KB Output is correct
7 Correct 741 ms 1072 KB Output is correct
8 Correct 761 ms 992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 371 ms 668 KB Output is correct
2 Correct 747 ms 816 KB Output is correct
3 Correct 759 ms 808 KB Output is correct
4 Correct 717 ms 928 KB Output is correct
5 Correct 718 ms 1084 KB Output is correct
6 Correct 732 ms 1004 KB Output is correct
7 Correct 677 ms 984 KB Output is correct
8 Correct 711 ms 1008 KB Output is correct
9 Correct 690 ms 948 KB Output is correct
10 Correct 769 ms 1060 KB Output is correct
11 Correct 754 ms 1148 KB Output is correct
12 Correct 738 ms 1000 KB Output is correct
13 Correct 657 ms 1076 KB Output is correct
14 Correct 767 ms 988 KB Output is correct
15 Correct 661 ms 928 KB Output is correct
16 Correct 761 ms 864 KB Output is correct
17 Correct 747 ms 760 KB Output is correct
18 Correct 770 ms 948 KB Output is correct
19 Correct 680 ms 956 KB Output is correct
20 Correct 705 ms 1084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 812 ms 1152 KB Output is correct
2 Correct 814 ms 1128 KB Output is correct
3 Correct 765 ms 1084 KB Output is correct
4 Correct 764 ms 1028 KB Output is correct
5 Correct 847 ms 1132 KB Output is correct
6 Correct 757 ms 908 KB Output is correct
7 Correct 846 ms 1228 KB Output is correct
8 Correct 734 ms 976 KB Output is correct
9 Correct 760 ms 944 KB Output is correct
10 Correct 754 ms 940 KB Output is correct
11 Correct 739 ms 992 KB Output is correct
12 Correct 764 ms 952 KB Output is correct
13 Correct 697 ms 872 KB Output is correct
14 Correct 819 ms 1116 KB Output is correct
15 Correct 757 ms 992 KB Output is correct
16 Correct 753 ms 852 KB Output is correct
17 Correct 748 ms 760 KB Output is correct
18 Correct 767 ms 1104 KB Output is correct