답안 #298239

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
298239 2020-09-12T16:01:51 Z rqi 통행료 (IOI18_highway) C++14
100 / 100
421 ms 29908 KB
#include "highway.h"
#include <bits/stdc++.h>
using namespace std;

typedef vector<int> vi;
typedef long long ll;
typedef pair<int, int> pi;
typedef vector<pi> vpi;

#define pb push_back
#define mp make_pair
#define f first
#define s second
#define sz(x) (int)(x).size()
#define ins insert

const int MOD = 1000000007;
const int mx = 90005;
int N, M;
ll A, B;
int D;
ll reg;
vi U, V;
vi adj[mx]; //node, edge
int dist[mx][2];
void genDist(int node, int typ){
	queue<int> q;
	dist[node][typ] = 0;
	q.push(node);
	while(sz(q)){
		int a = q.front();
		q.pop();
		for(auto u: adj[a]){
			if(dist[u][typ] > dist[a][typ]+1){
				dist[u][typ] = dist[a][typ]+1;
				q.push(u);
			}
		}
	}
}

vi curw;

vpi tadj[mx];
vi ord; //nodes, ordered by postorder
int tdist[mx];

vi children[mx];
pi par[mx];

void genTDist(int node){
	queue<int> q;
	tdist[node] = 0;
	q.push(node);
	while(sz(q)){
		int a = q.front();
		q.pop();
		for(auto u: tadj[a]){
			if(tdist[u.f] > tdist[a]+1){
				tdist[u.f] = tdist[a]+1;
				q.push(u.f);
			}
		}
	}
}

void genPO(int node){
	for(auto u: children[node]){
		genPO(u);
	}
	ord.pb(node);
}

int treeAns(vector<pair<pi, int>> eds, int R){
	set<int> trash;
	for(auto u: eds){
		trash.ins(u.f.f); trash.ins(u.f.s);
	}
	trash.ins(R);

	vi nodes;
	for(auto u: trash) nodes.pb(u);

	// cout << "nodes: ";
	// for(auto u: nodes) cout << u << " ";
	// cout << "\n";
	// cout << sz(nodes) << "\n";

	ord.clear();
	for(auto u: nodes){
		tadj[u].clear();
		children[u].clear();
		par[u] = mp(-1, -1);
	}
	for(auto u: eds){
		tadj[u.f.f].pb(mp(u.f.s, u.s));
		tadj[u.f.s].pb(mp(u.f.f, u.s));
	}
	for(auto u: nodes) tdist[u] = MOD;
	genTDist(R);
	
	//for(auto u: nodes) cout << u << " " << tdist[u] << "\n";
	vi tw = curw;
	for(auto u: nodes){
		if(u == R) continue;
		bool found = 0;
		for(auto x: tadj[u]){
			if(tdist[x.f]+1 != tdist[u]) continue;
			if(!found){
				found = 1;
				children[x.f].pb(u);
				par[u] = x;
				//cout << "ED: " << x.f << " " << x.s << " " << u << "\n";
			}
			else{
				//cout << x.s << "\n";
				tw[x.s] = 1;
			}
		}
	}
	genPO(R);
	// cout << "ord: ";
	// for(auto u: ord) cout << u << " ";
	// cout << "\n";
	
	// cout << "tw: ";
	// for(auto u: tw) cout << u << " ";
	// cout << "\n";

	int lo = 0;
	int hi = sz(ord)-1;
	while(lo < hi){
		int mid = (lo+hi)/2;
		vi w = tw;
		for(int i = 0; i <= mid; i++){
			w[par[ord[i]].s] = 1;
		}
		if(ask(w) != reg){
			hi = mid;
		}
		else{
			lo = mid+1;
		}
	}

	return ord[lo];
}

void find_pair(int _N, vi _U, vi _V, int _A, int _B) {
	N = _N;
	U = _U;
	V = _V;
	M = sz(U);
	A = _A;
	B = _B;

	reg = ask(vi(M, 0));

	//perhaps random shuffle?
	int lo = 1;
	int hi = M;
	while(lo < hi){
		int mid = (lo+hi)/2;
		vi w(M, 0);
		for(int i = 0; i < mid; i++) w[i] = 1;
		ll res = ask(w);
		if(res == reg){
			lo = mid+1;
		}
		else hi = mid;
	}

	for(int i = lo; i < M; i++){
		adj[U[i]].pb(V[i]);
		adj[V[i]].pb(U[i]);
	}

	//cout << lo << "\n";

	for(int j = 0; j < 2; j++){
		for(int i = 0; i < N; i++){
			dist[i][j] = MOD;
		}
	}

	genDist(U[lo-1], 0);
	genDist(V[lo-1], 1);

	//cout << dist[3][0] << " " << dist[3][1] << "\n";

	vector<pair<pi, int>> tr[2];

	for(int i = lo; i < M; i++){
		int typ1 = -1;
		int typ2 = -1;
		int typ;
		if(dist[U[i]][0] < dist[U[i]][1]){
			typ1 = 0;
		}
		else if(dist[U[i]][0] > dist[U[i]][1]){
			typ1 = 1;
		}
		if(dist[V[i]][0] < dist[V[i]][1]){
			typ2 = 0;
		}
		else if(dist[V[i]][0] > dist[V[i]][1]){
			typ2 = 1;
		}

		if(typ1 != typ2) continue;

		typ = typ1;
		if(typ == -1) continue;

		tr[typ].pb(mp(mp(U[i], V[i]), i));
	}

	curw = vi(M, 0);
	for(int i = 0; i < lo-1; i++){
		curw[i] = 1;
	}

	int a = treeAns(tr[0], U[lo-1]);
	int b = treeAns(tr[1], V[lo-1]);
	//cout << a << " " << b << "\n";
	answer(a, b);
}

# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 6656 KB Output is correct
2 Correct 5 ms 6656 KB Output is correct
3 Correct 5 ms 6656 KB Output is correct
4 Correct 5 ms 6656 KB Output is correct
5 Correct 5 ms 6656 KB Output is correct
6 Correct 5 ms 6656 KB Output is correct
7 Correct 5 ms 6656 KB Output is correct
8 Correct 5 ms 6656 KB Output is correct
9 Correct 5 ms 6708 KB Output is correct
10 Correct 5 ms 6656 KB Output is correct
11 Correct 5 ms 6724 KB Output is correct
12 Correct 5 ms 6656 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 6912 KB Output is correct
2 Correct 29 ms 8892 KB Output is correct
3 Correct 262 ms 24136 KB Output is correct
4 Correct 249 ms 23764 KB Output is correct
5 Correct 303 ms 26452 KB Output is correct
6 Correct 219 ms 21032 KB Output is correct
7 Correct 197 ms 20016 KB Output is correct
8 Correct 285 ms 26636 KB Output is correct
9 Correct 216 ms 20340 KB Output is correct
10 Correct 217 ms 23316 KB Output is correct
11 Correct 158 ms 16272 KB Output is correct
12 Correct 273 ms 24888 KB Output is correct
13 Correct 299 ms 27692 KB Output is correct
14 Correct 317 ms 27564 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 9152 KB Output is correct
2 Correct 38 ms 11448 KB Output is correct
3 Correct 42 ms 8888 KB Output is correct
4 Correct 153 ms 23700 KB Output is correct
5 Correct 150 ms 23916 KB Output is correct
6 Correct 154 ms 27652 KB Output is correct
7 Correct 108 ms 11784 KB Output is correct
8 Correct 155 ms 25524 KB Output is correct
9 Correct 123 ms 16088 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 6784 KB Output is correct
2 Correct 23 ms 8192 KB Output is correct
3 Correct 225 ms 22224 KB Output is correct
4 Correct 219 ms 23480 KB Output is correct
5 Correct 117 ms 14420 KB Output is correct
6 Correct 112 ms 14356 KB Output is correct
7 Correct 189 ms 19856 KB Output is correct
8 Correct 130 ms 15304 KB Output is correct
9 Correct 275 ms 24164 KB Output is correct
10 Correct 222 ms 21704 KB Output is correct
11 Correct 309 ms 27172 KB Output is correct
12 Correct 260 ms 25908 KB Output is correct
13 Correct 263 ms 23692 KB Output is correct
14 Correct 168 ms 17176 KB Output is correct
15 Correct 126 ms 14532 KB Output is correct
16 Correct 112 ms 12780 KB Output is correct
17 Correct 272 ms 25232 KB Output is correct
18 Correct 298 ms 28044 KB Output is correct
19 Correct 113 ms 13516 KB Output is correct
20 Correct 136 ms 15088 KB Output is correct
21 Correct 164 ms 20404 KB Output is correct
22 Correct 125 ms 15580 KB Output is correct
23 Correct 202 ms 23180 KB Output is correct
24 Correct 213 ms 23440 KB Output is correct
25 Correct 287 ms 29908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 8696 KB Output is correct
2 Correct 27 ms 8664 KB Output is correct
3 Correct 332 ms 26632 KB Output is correct
4 Correct 298 ms 22352 KB Output is correct
5 Correct 319 ms 25020 KB Output is correct
6 Correct 350 ms 25696 KB Output is correct
7 Correct 320 ms 23640 KB Output is correct
8 Correct 343 ms 26952 KB Output is correct
9 Correct 149 ms 12552 KB Output is correct
10 Correct 190 ms 16620 KB Output is correct
11 Correct 201 ms 17212 KB Output is correct
12 Correct 297 ms 20932 KB Output is correct
13 Correct 318 ms 23620 KB Output is correct
14 Correct 355 ms 23360 KB Output is correct
15 Correct 370 ms 26112 KB Output is correct
16 Correct 227 ms 17104 KB Output is correct
17 Correct 173 ms 21748 KB Output is correct
18 Correct 134 ms 15496 KB Output is correct
19 Correct 248 ms 24076 KB Output is correct
20 Correct 161 ms 18728 KB Output is correct
21 Correct 385 ms 29600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 8568 KB Output is correct
2 Correct 32 ms 8896 KB Output is correct
3 Correct 289 ms 23484 KB Output is correct
4 Correct 297 ms 22828 KB Output is correct
5 Correct 301 ms 24632 KB Output is correct
6 Correct 349 ms 25620 KB Output is correct
7 Correct 311 ms 27540 KB Output is correct
8 Correct 325 ms 26596 KB Output is correct
9 Correct 305 ms 24544 KB Output is correct
10 Correct 312 ms 23804 KB Output is correct
11 Correct 296 ms 23396 KB Output is correct
12 Correct 311 ms 24860 KB Output is correct
13 Correct 149 ms 12704 KB Output is correct
14 Correct 149 ms 12492 KB Output is correct
15 Correct 212 ms 19544 KB Output is correct
16 Correct 177 ms 16628 KB Output is correct
17 Correct 202 ms 17764 KB Output is correct
18 Correct 187 ms 15708 KB Output is correct
19 Correct 285 ms 20760 KB Output is correct
20 Correct 345 ms 24196 KB Output is correct
21 Correct 330 ms 24936 KB Output is correct
22 Correct 335 ms 24512 KB Output is correct
23 Correct 351 ms 23944 KB Output is correct
24 Correct 339 ms 24404 KB Output is correct
25 Correct 331 ms 25124 KB Output is correct
26 Correct 348 ms 24884 KB Output is correct
27 Correct 166 ms 18816 KB Output is correct
28 Correct 216 ms 23284 KB Output is correct
29 Correct 250 ms 26616 KB Output is correct
30 Correct 217 ms 25456 KB Output is correct
31 Correct 221 ms 23572 KB Output is correct
32 Correct 217 ms 23844 KB Output is correct
33 Correct 253 ms 26820 KB Output is correct
34 Correct 196 ms 21880 KB Output is correct
35 Correct 178 ms 22428 KB Output is correct
36 Correct 210 ms 25068 KB Output is correct
37 Correct 210 ms 23528 KB Output is correct
38 Correct 222 ms 24836 KB Output is correct
39 Correct 421 ms 29652 KB Output is correct
40 Correct 390 ms 29204 KB Output is correct
41 Correct 402 ms 29828 KB Output is correct
42 Correct 409 ms 26248 KB Output is correct