답안 #719662

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
719662 2023-04-06T13:04:22 Z Arturgo Parking (CEOI22_parking) C++14
100 / 100
1167 ms 33684 KB
#include <bits/stdc++.h>
using namespace std;

vector<vector<int>> voitures;

vector<int> libres;
vector<vector<int>> positions;

vector<pair<int, int>> solution;

bool solve(int couleur) {
	if(positions[couleur][0] == positions[couleur][1])
		return false;
	
	if(voitures[positions[couleur][0]].size() == 2
	&& voitures[positions[couleur][1]].size() == 2
	&& voitures[positions[couleur][0]][1] == couleur
	&& voitures[positions[couleur][1]][1] == couleur) {
		// Branchement sortant
		
		if(libres.empty()) {
			cerr << "NOPE" << endl;
			cout << -1 << endl;
			exit(0);
		}
		
		int libre = libres.back();
		libres.pop_back();
		
		solution.push_back({positions[couleur][0], libre});
		solution.push_back({positions[couleur][1], libre});
			
		voitures[positions[couleur][0]].pop_back();
		voitures[positions[couleur][1]].pop_back();
		positions[couleur] = {libre, libre};
		voitures[libre] = {couleur, couleur};
		return true;
	}
	
	if(voitures[positions[couleur][0]].size() == 2
	&& voitures[positions[couleur][0]][1] == couleur) {
		if(voitures[positions[couleur][1]].size() != 1) return false;
		
		solution.push_back({
			positions[couleur][0], 
			positions[couleur][1]
		});
	
		voitures[positions[couleur][0]].pop_back();	
		positions[couleur] = {
			positions[couleur][1], 
			positions[couleur][1]
		};
		voitures[positions[couleur][0]] = {couleur, couleur};
		return true;
	}
	
	if(voitures[positions[couleur][0]].size() != 1) return false;
	if(voitures[positions[couleur][1]].back() != couleur) return false;
	
	voitures[positions[couleur][1]].pop_back();
	
	if(voitures[positions[couleur][1]].empty())
		libres.push_back(positions[couleur][1]);
	
	solution.push_back({
		positions[couleur][1],
		positions[couleur][0]
	});
	positions[couleur] = {
		positions[couleur][0],
		positions[couleur][0]
	};
	voitures[positions[couleur][0]] = {couleur, couleur};
	return true;
}

vector<int> branchements;
vector<bool> estPasse;

void explore(int couleur, int der = -1) {
	if(estPasse[couleur]) return;
	estPasse[couleur] = true;
	
	vector<int> ps = positions[couleur];
	
	if(voitures[positions[couleur][0]].size() == 2
	&& voitures[positions[couleur][1]].size() == 2
	&& voitures[positions[couleur][0]][1] == couleur
	&& voitures[positions[couleur][1]][1] == couleur) {
		branchements.push_back(couleur);
	}
	
	vector<int> couls1 = voitures[positions[couleur][0]];
	vector<int> couls2 = voitures[positions[couleur][1]];
	
	for(int val : couls2) couls1.push_back(val);
	
	for(int val : couls1) {
		if(val != couleur && val != der) {
			explore(val, couleur);
			return;
		}
	}
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	
	int nbCouleurs, nbPlaces;
	cin >> nbCouleurs >> nbPlaces;
	
	voitures.resize(nbPlaces);
	positions.resize(nbCouleurs);
	estPasse = vector<bool>(nbCouleurs, false);
	
	for(int iPlace = 0;iPlace < nbPlaces;iPlace++) {
		int a, b;
		cin >> a >> b;
		
		if(a != 0) {
			voitures[iPlace].push_back(a - 1);
			positions[a - 1].push_back(iPlace);
		}
		
		if(b != 0) {
			voitures[iPlace].push_back(b - 1);
			positions[b - 1].push_back(iPlace);
		}
		
		if(voitures[iPlace].empty()) {
			libres.push_back(iPlace);
		}
	}
	
	vector<int> aTraiter;
	
	vector<int> toSolve;
	
	for(int iPlace = 0;iPlace < nbPlaces;iPlace++) {
		toSolve.push_back(iPlace);
		if(voitures[iPlace].size() == 1) {
			aTraiter.push_back(iPlace);
			explore(voitures[iPlace].back());
		}
	}
	
	for(int iCouleur = 0;iCouleur < nbCouleurs;iCouleur++) {
		if(!estPasse[iCouleur]) {
			explore(iCouleur, -1);
		}
	}
	
	reverse(branchements.begin(), branchements.end());
	
	while(true) {
		cerr << libres.size() << endl;
		while(!aTraiter.empty()) {
			int place = aTraiter.back();
			aTraiter.pop_back();
			
			vector<int> ps = positions[voitures[place].back()];
			if(solve(voitures[place].back())) {	
				for(int pos : ps) {
					if(voitures[pos].size() == 1) aTraiter.push_back(pos);
				}
			}
		}
		
		/*for(pair<int, int> sol : solution) {
			cerr << 2 + sol.first << " " << 2 + sol.second << endl;
		}*/
		
		if(!branchements.empty()) {
			//cerr << "BRANCHE" << endl;
			int couleur = branchements.back();
			branchements.pop_back();
			
			vector<int> ps = positions[couleur];
			solve(couleur);
			
			for(int pos : ps) {
				if(voitures[pos].size() == 1) aTraiter.push_back(pos);
			}
		}
		else if(!toSolve.empty()) {
			int place = toSolve.back();
			toSolve.pop_back();
			
			if(voitures[place].size() < 2) continue;
			if(voitures[place][0] == voitures[place][1]) continue;
			if(libres.empty()) {
				cout << -1 << endl;
				exit(0);
			}
			
			int libre = libres.back();
			libres.pop_back();
			
			solution.push_back({place, libre});
			
			for(int& pos : positions[voitures[place][1]]) {
				if(pos == place) pos = libre;
			}
			
			voitures[libre].push_back(voitures[place][1]);
			voitures[place].pop_back();
						
			aTraiter.push_back(place);
		}
		else {
			break;
		}
	}
	
	cout << solution.size() << "\n";
	
	for(pair<int, int> dep : solution) {
		cout << 1 + dep.first << " " << 1 + dep.second << "\n";
	}
		
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 276 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 652 ms 19236 KB Output is correct
2 Correct 770 ms 20724 KB Output is correct
3 Correct 595 ms 16244 KB Output is correct
4 Correct 603 ms 15596 KB Output is correct
5 Correct 705 ms 21236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 340 KB Output is correct
2 Correct 6 ms 340 KB Output is correct
3 Correct 1 ms 324 KB Output is correct
4 Correct 5 ms 468 KB Output is correct
5 Correct 1 ms 452 KB Output is correct
6 Correct 1 ms 448 KB Output is correct
7 Correct 6 ms 468 KB Output is correct
8 Correct 2 ms 324 KB Output is correct
9 Correct 4 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 340 KB Output is correct
2 Correct 6 ms 340 KB Output is correct
3 Correct 1 ms 324 KB Output is correct
4 Correct 5 ms 468 KB Output is correct
5 Correct 1 ms 452 KB Output is correct
6 Correct 1 ms 448 KB Output is correct
7 Correct 6 ms 468 KB Output is correct
8 Correct 2 ms 324 KB Output is correct
9 Correct 4 ms 468 KB Output is correct
10 Correct 1018 ms 32096 KB Output is correct
11 Correct 1167 ms 26552 KB Output is correct
12 Correct 248 ms 25160 KB Output is correct
13 Correct 846 ms 29604 KB Output is correct
14 Correct 273 ms 25988 KB Output is correct
15 Correct 218 ms 25064 KB Output is correct
16 Correct 896 ms 32452 KB Output is correct
17 Correct 219 ms 24688 KB Output is correct
18 Correct 838 ms 31768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 468 KB Output is correct
2 Correct 1 ms 596 KB Output is correct
3 Correct 4 ms 468 KB Output is correct
4 Correct 3 ms 340 KB Output is correct
5 Correct 6 ms 340 KB Output is correct
6 Correct 4 ms 452 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 4 ms 468 KB Output is correct
9 Correct 4 ms 480 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 4 ms 468 KB Output is correct
12 Correct 3 ms 468 KB Output is correct
13 Correct 1 ms 328 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 4 ms 468 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 4 ms 468 KB Output is correct
19 Correct 5 ms 468 KB Output is correct
20 Correct 4 ms 468 KB Output is correct
21 Correct 1 ms 456 KB Output is correct
22 Correct 4 ms 468 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 3 ms 460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 276 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 652 ms 19236 KB Output is correct
12 Correct 770 ms 20724 KB Output is correct
13 Correct 595 ms 16244 KB Output is correct
14 Correct 603 ms 15596 KB Output is correct
15 Correct 705 ms 21236 KB Output is correct
16 Correct 3 ms 340 KB Output is correct
17 Correct 6 ms 340 KB Output is correct
18 Correct 1 ms 324 KB Output is correct
19 Correct 5 ms 468 KB Output is correct
20 Correct 1 ms 452 KB Output is correct
21 Correct 1 ms 448 KB Output is correct
22 Correct 6 ms 468 KB Output is correct
23 Correct 2 ms 324 KB Output is correct
24 Correct 4 ms 468 KB Output is correct
25 Correct 1018 ms 32096 KB Output is correct
26 Correct 1167 ms 26552 KB Output is correct
27 Correct 248 ms 25160 KB Output is correct
28 Correct 846 ms 29604 KB Output is correct
29 Correct 273 ms 25988 KB Output is correct
30 Correct 218 ms 25064 KB Output is correct
31 Correct 896 ms 32452 KB Output is correct
32 Correct 219 ms 24688 KB Output is correct
33 Correct 838 ms 31768 KB Output is correct
34 Correct 4 ms 468 KB Output is correct
35 Correct 1 ms 596 KB Output is correct
36 Correct 4 ms 468 KB Output is correct
37 Correct 3 ms 340 KB Output is correct
38 Correct 6 ms 340 KB Output is correct
39 Correct 4 ms 452 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 4 ms 468 KB Output is correct
42 Correct 4 ms 480 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 4 ms 468 KB Output is correct
45 Correct 3 ms 468 KB Output is correct
46 Correct 1 ms 328 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 1 ms 340 KB Output is correct
49 Correct 4 ms 468 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
51 Correct 4 ms 468 KB Output is correct
52 Correct 5 ms 468 KB Output is correct
53 Correct 4 ms 468 KB Output is correct
54 Correct 1 ms 456 KB Output is correct
55 Correct 4 ms 468 KB Output is correct
56 Correct 1 ms 340 KB Output is correct
57 Correct 3 ms 460 KB Output is correct
58 Correct 878 ms 30904 KB Output is correct
59 Correct 902 ms 33684 KB Output is correct
60 Correct 841 ms 28724 KB Output is correct
61 Correct 873 ms 30604 KB Output is correct
62 Correct 1111 ms 27392 KB Output is correct
63 Correct 793 ms 29536 KB Output is correct
64 Correct 214 ms 26084 KB Output is correct
65 Correct 875 ms 31568 KB Output is correct
66 Correct 928 ms 32968 KB Output is correct
67 Correct 205 ms 24720 KB Output is correct
68 Correct 780 ms 30096 KB Output is correct
69 Correct 726 ms 29140 KB Output is correct
70 Correct 232 ms 24960 KB Output is correct
71 Correct 218 ms 26052 KB Output is correct
72 Correct 226 ms 25420 KB Output is correct
73 Correct 923 ms 32312 KB Output is correct
74 Correct 240 ms 25136 KB Output is correct
75 Correct 886 ms 31432 KB Output is correct
76 Correct 959 ms 32052 KB Output is correct
77 Correct 886 ms 31200 KB Output is correct
78 Correct 227 ms 25500 KB Output is correct
79 Correct 863 ms 31420 KB Output is correct
80 Correct 197 ms 24864 KB Output is correct
81 Correct 874 ms 31220 KB Output is correct