답안 #1044372

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1044372 2024-08-05T09:05:35 Z 김은성(#11005) Parking (CEOI22_parking) C++17
60 / 100
221 ms 61816 KB
#include <bits/stdc++.h>
using namespace std;
bool ch[500009];
vector<pair<int, int> > graph[500009];
int b0[500009], t0[500009], b[500009], t[500009];
unordered_set<int> emp;
vector<int> color[500009];
vector<pair<int, int> > ans;
vector<int> comp, cols;
vector<pair<int, int> > res;
bool done[500009];
bool solo[500009];
int needed = 0;
void dfs(int v){
	ch[v] = 1;
	cols.push_back(v);
	for(auto [e, u]: graph[v]){
		comp.push_back(e);
		if(!ch[u]){
			dfs(u);
		}
	}
}
void makemove(int occ, int e){
	//printf("from=%d to=%d\n", occ, e);
	needed = min((int)emp.size(), needed);
	ans.push_back(make_pair(occ, e));
	if(t[occ]){
		color[t[occ]].erase(find(color[t[occ]].begin(), color[t[occ]].end(), occ));
		color[b[occ]].push_back(occ);
		if(b[e]){
			t[e] = t[occ];
			t[occ] = 0;
		}
		else{
			b[e] = t[occ];
			t[occ] = 0;
			emp.erase(e);
			color[b[e]].push_back(e);
		}
	}
	else{
		color[b[occ]].erase(find(color[b[occ]].begin(), color[b[occ]].end(), occ));
		emp.insert(occ);
		if(b[e]){
			t[e] = b[occ];
			b[occ] = 0;
		}
		else{
			b[e] = b[occ];
			b[occ] = 0;
			emp.erase(e);
			color[b[e]].push_back(e);
		}
	}
	needed = min((int)emp.size(), needed);
}
int main(){
	int n, m, i, j, ans2 = 0;
	bool flag = 0;
	scanf("%d %d", &n, &m);
	//assert(m<=4);
	for(i=1; i<=m; i++){
		scanf("%d %d", &b[i], &t[i]);
		b0[i] = b[i];
		t0[i] = t[i];
		if(b[i] != t[i])
			flag = 1, ans2++;
		if(b[i] + t[i] == 0)
			emp.insert(i);
		if(t[i])
			color[t[i]].push_back(i);
		else if(b[i])
			color[b[i]].push_back(i);
	}
	if(n==m){
		if(flag)
			printf("-1\n");
		else
			printf("0\n");
		return 0;
	}
	while(1){
		queue<int> q;
		for(int c=1; c<=n; c++){
			if(color[c].size() >= 2){
				if(!t[color[c][0]] || !t[color[c][1]]){
					q.push(c);
				}
			}
		}
		while(!q.empty()){
			int c = q.front();
			q.pop();
			assert(color[c].size() >= 2);
				assert(!t[color[c][0]] || !t[color[c][1]]);
					if(!t[color[c][0]]){
						makemove(color[c][1], color[c][0]);
						done[c] = 1;
						int temp = b[color[c][1]];
						if(temp && !done[temp] && color[temp].size() >= 2){
							q.push(temp);
						}
					}
					else{
						makemove(color[c][0], color[c][1]);
						done[c] = 1;
						int temp = b[color[c][0]];
						if(temp && !done[temp] && color[temp].size() >= 2){
							q.push(temp);
						}
					}
		}
		vector<bool> tch(n+1);
		for(i=1; i<=m; i++){
			if(b[i] && t[i]){
				tch[b[i]] = 1;
				tch[t[i]] = 1;
			}
		}
		for(i=1; i<=n; i++){
			if(!tch[i])
				break;
		}
		if(i==n+1)
			break;
	}
	for(i=1; i<=m; i++){
		if(!t[i])
			solo[b[i]] = 1;
	}
	//simulation
	for(i=m+1; i<=2*m; i++)
		emp.insert(i);
	m *= 2;
	for(i=1; i<=m; i++){
		if(!b[i] || !t[i])
			continue;
		graph[b[i]].push_back(make_pair(i, t[i]));
		graph[t[i]].push_back(make_pair(i, b[i]));
	}
	for(i=1; i<=n; i++){
		assert(graph[i].size());
		if(done[i] || ch[i] || graph[i][0].second == i)
			continue;
		comp.clear();
		cols.clear();
		int emp0 = emp.size();
		needed = emp0;
		//printf("i=%d\n", i);
		dfs(i);
		sort(comp.begin(), comp.end());
		comp.erase(unique(comp.begin(), comp.end()), comp.end());
		deque<int> qa;
		for(int c: cols){
			if(color[c].size() >= 2){
				if(!t[color[c][0]] || !t[color[c][1]])
					qa.push_front(c);
				else
					qa.push_back(c);
			}
		}
		//printf("emp.size=%d\n", emp.size());
		if(qa.empty()){
			int occ = comp[0], e = *emp.begin();
			//printf("occ=%d e=%d\n", occ, e);
			makemove(occ, e);
			for(int c: cols){
				if(color[c].size() >= 2){
					if(!t[color[c][0]] || !t[color[c][1]])
						qa.push_front(c);
					else
						qa.push_back(c);
				}
			}
		}
		//printf("qa.size=%d\n", qa.size());
		while(!qa.empty()){
			//printf("qa.front=%d\n", qa.front());
			if(done[qa.front()]){
				qa.pop_front();
				continue;
			}
			int from = color[qa.front()][0], to = color[qa.front()][1];
			//printf("from=%d to=%d\n", from, to);
			if(t[to])
				swap(from, to);
			if(t[to]){
				if(emp.empty()){
					printf("-1\n");
					return 0;
				}
				int e = *emp.begin();
				//printf("from=%d to=%d e=%d\n", from, to, e);
				makemove(from, e);
				makemove(to, e);
			}
			else{
				makemove(from, to);
			}
			done[qa.front()] = 1;
			qa.pop_front();
			for(int c: {b[from], b[to]}){
				if(c && color[c].size() == 2){
					if(!t[color[c][0]] || !t[color[c][1]])
						qa.push_front(c);
					else
						qa.push_back(c);
				}
			}
		}
		res.push_back(make_pair(emp0 - needed, i));
		//printf("i=%d emp0-needed=%d\n", i, emp0-needed);
	}
	sort(res.begin(), res.end(), [](pair<int, int> &u, pair<int, int> &v){return u.first < v.first;});
	m /= 2;
	memset(ch, 0, sizeof(ch));
	for(i=1; i<=n; i++)
		graph[i].clear();
	for(i=1; i<=m; i++){
		b[i]= b0[i];
		t[i] =t0[i];
	}
	emp.clear();
	for(i=1; i<=n; i++)
		color[i].clear();
	ans.clear();
	comp.clear();
	cols.clear();
	memset(done, 0, sizeof(done));
	
	for(i=1; i<=m; i++){
		b[i] = b0[i];
		t[i] = t0[i];
		if(b[i] != t[i])
			flag = 1, ans2++;
		if(b[i] + t[i] == 0)
			emp.insert(i);
		if(t[i])
			color[t[i]].push_back(i);
		else if(b[i])
			color[b[i]].push_back(i);
	}
	if(n==m){
		if(flag)
			printf("-1\n");
		else
			printf("0\n");
		return 0;
	}
	while(1){
		queue<int> q;
		for(int c=1; c<=n; c++){
			if(color[c].size() >= 2){
				if(!t[color[c][0]] || !t[color[c][1]]){
					q.push(c);
				}
			}
		}
		while(!q.empty()){
			int c = q.front();
			q.pop();
			assert(color[c].size() >= 2);
				assert(!t[color[c][0]] || !t[color[c][1]]);
					if(!t[color[c][0]]){
						makemove(color[c][1], color[c][0]);
						done[c] = 1;
						int temp = b[color[c][1]];
						if(temp && !done[temp] && color[temp].size() >= 2){
							q.push(temp);
						}
					}
					else{
						makemove(color[c][0], color[c][1]);
						done[c] = 1;
						int temp = b[color[c][0]];
						if(temp && !done[temp] && color[temp].size() >= 2){
							q.push(temp);
						}
					}
		}
		vector<bool> tch(n+1);
		for(i=1; i<=m; i++){
			if(b[i] && t[i]){
				tch[b[i]] = 1;
				tch[t[i]] = 1;
			}
		}
		for(i=1; i<=n; i++){
			if(!tch[i])
				break;
		}
		if(i==n+1)
			break;
	}
	for(i=1; i<=m; i++){
		if(!t[i])
			solo[b[i]] = 1;
	}
	for(i=1; i<=m; i++){
		if(!b[i] || !t[i])
			continue;
		graph[b[i]].push_back(make_pair(i, t[i]));
		graph[t[i]].push_back(make_pair(i, b[i]));
	}
	for(auto [trash, i]: res){
		//printf("i=%d\n", i);
		assert(graph[i].size());
		if(done[i] || ch[i] || graph[i][0].second == i)
			continue;
		comp.clear();
		cols.clear();
		int emp0 = emp.size();
		needed = emp0;
		//printf("i=%d\n", i);
		dfs(i);
		sort(comp.begin(), comp.end());
		comp.erase(unique(comp.begin(), comp.end()), comp.end());
		deque<int> qa;
		for(int c: cols){
			if(color[c].size() >= 2){
				if(!t[color[c][0]] || !t[color[c][1]])
					qa.push_front(c);
				else
					qa.push_back(c);
			}
		}
		//printf("emp.size=%d\n", emp.size());
		if(qa.empty()){
			int occ = comp[0], e = *emp.begin();
			//printf("occ=%d e=%d\n", occ, e);
			makemove(occ, e);
			for(int c: cols){
				if(color[c].size() >= 2){
					if(!t[color[c][0]] || !t[color[c][1]])
						qa.push_front(c);
					else
						qa.push_back(c);
				}
			}
		}
		//printf("qa.size=%d\n", qa.size());
		while(!qa.empty()){
			//printf("qa.front=%d\n", qa.front());
			if(done[qa.front()]){
				qa.pop_front();
				continue;
			}
			int from = color[qa.front()][0], to = color[qa.front()][1];
			//printf("from=%d to=%d\n", from, to);
			if(t[to])
				swap(from, to);
			if(t[to]){
				if(emp.empty()){
					printf("-1\n");
					return 0;
				}
				int e = *emp.begin();
				//printf("from=%d to=%d e=%d\n", from, to, e);
				makemove(from, e);
				makemove(to, e);
			}
			else{
				makemove(from, to);
			}
			done[qa.front()] = 1;
			qa.pop_front();
			for(int c: {b[from], b[to]}){
				if(c && color[c].size() == 2){
					if(!t[color[c][0]] || !t[color[c][1]])
						qa.push_front(c);
					else
						qa.push_back(c);
				}
			}
		}
		//res.push_back(make_pair(emp0 - needed, i));
	}
	printf("%d\n", ans.size());
	for(auto [u, v]: ans){
		printf("%d %d\n", u, v);
	}
	return 0;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:379:11: warning: format '%d' expects argument of type 'int', but argument 2 has type 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wformat=]
  379 |  printf("%d\n", ans.size());
      |          ~^     ~~~~~~~~~~
      |           |             |
      |           int           std::vector<std::pair<int, int> >::size_type {aka long unsigned int}
      |          %ld
Main.cpp:59:15: warning: unused variable 'j' [-Wunused-variable]
   59 |  int n, m, i, j, ans2 = 0;
      |               ^
Main.cpp:61:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   61 |  scanf("%d %d", &n, &m);
      |  ~~~~~^~~~~~~~~~~~~~~~~
Main.cpp:64:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   64 |   scanf("%d %d", &b[i], &t[i]);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 33060 KB Output is correct
2 Correct 5 ms 32856 KB Output is correct
3 Correct 5 ms 32860 KB Output is correct
4 Correct 5 ms 32860 KB Output is correct
5 Correct 4 ms 32860 KB Output is correct
6 Correct 4 ms 32348 KB Output is correct
7 Correct 4 ms 32860 KB Output is correct
8 Correct 4 ms 32348 KB Output is correct
9 Correct 6 ms 32600 KB Output is correct
10 Correct 5 ms 32856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 114 ms 53012 KB Output is correct
2 Correct 137 ms 54288 KB Output is correct
3 Correct 106 ms 51780 KB Output is correct
4 Correct 81 ms 51580 KB Output is correct
5 Correct 124 ms 54520 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 33152 KB Output is correct
2 Correct 5 ms 32604 KB Output is correct
3 Correct 5 ms 32600 KB Output is correct
4 Correct 5 ms 33112 KB Output is correct
5 Correct 5 ms 32604 KB Output is correct
6 Correct 5 ms 33112 KB Output is correct
7 Correct 5 ms 33112 KB Output is correct
8 Correct 6 ms 33116 KB Output is correct
9 Correct 5 ms 33116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 33152 KB Output is correct
2 Correct 5 ms 32604 KB Output is correct
3 Correct 5 ms 32600 KB Output is correct
4 Correct 5 ms 33112 KB Output is correct
5 Correct 5 ms 32604 KB Output is correct
6 Correct 5 ms 33112 KB Output is correct
7 Correct 5 ms 33112 KB Output is correct
8 Correct 6 ms 33116 KB Output is correct
9 Correct 5 ms 33116 KB Output is correct
10 Correct 198 ms 61304 KB Output is correct
11 Correct 47 ms 40108 KB Output is correct
12 Correct 38 ms 40028 KB Output is correct
13 Correct 221 ms 61816 KB Output is correct
14 Correct 36 ms 38596 KB Output is correct
15 Correct 127 ms 57724 KB Output is correct
16 Correct 211 ms 61564 KB Output is correct
17 Correct 136 ms 58228 KB Output is correct
18 Correct 188 ms 61816 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 33112 KB Output is correct
2 Correct 5 ms 33116 KB Output is correct
3 Correct 5 ms 33112 KB Output is correct
4 Correct 5 ms 33116 KB Output is correct
5 Correct 5 ms 32604 KB Output is correct
6 Correct 6 ms 33116 KB Output is correct
7 Correct 5 ms 33116 KB Output is correct
8 Correct 5 ms 33112 KB Output is correct
9 Correct 5 ms 33116 KB Output is correct
10 Correct 5 ms 32856 KB Output is correct
11 Correct 6 ms 33280 KB Output is correct
12 Correct 8 ms 33112 KB Output is correct
13 Correct 7 ms 33116 KB Output is correct
14 Correct 6 ms 32604 KB Output is correct
15 Correct 5 ms 33116 KB Output is correct
16 Correct 6 ms 33052 KB Output is correct
17 Correct 6 ms 33116 KB Output is correct
18 Correct 7 ms 33184 KB Output is correct
19 Correct 6 ms 33116 KB Output is correct
20 Incorrect 7 ms 33044 KB Output isn't correct
21 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 33060 KB Output is correct
2 Correct 5 ms 32856 KB Output is correct
3 Correct 5 ms 32860 KB Output is correct
4 Correct 5 ms 32860 KB Output is correct
5 Correct 4 ms 32860 KB Output is correct
6 Correct 4 ms 32348 KB Output is correct
7 Correct 4 ms 32860 KB Output is correct
8 Correct 4 ms 32348 KB Output is correct
9 Correct 6 ms 32600 KB Output is correct
10 Correct 5 ms 32856 KB Output is correct
11 Correct 114 ms 53012 KB Output is correct
12 Correct 137 ms 54288 KB Output is correct
13 Correct 106 ms 51780 KB Output is correct
14 Correct 81 ms 51580 KB Output is correct
15 Correct 124 ms 54520 KB Output is correct
16 Correct 6 ms 33152 KB Output is correct
17 Correct 5 ms 32604 KB Output is correct
18 Correct 5 ms 32600 KB Output is correct
19 Correct 5 ms 33112 KB Output is correct
20 Correct 5 ms 32604 KB Output is correct
21 Correct 5 ms 33112 KB Output is correct
22 Correct 5 ms 33112 KB Output is correct
23 Correct 6 ms 33116 KB Output is correct
24 Correct 5 ms 33116 KB Output is correct
25 Correct 198 ms 61304 KB Output is correct
26 Correct 47 ms 40108 KB Output is correct
27 Correct 38 ms 40028 KB Output is correct
28 Correct 221 ms 61816 KB Output is correct
29 Correct 36 ms 38596 KB Output is correct
30 Correct 127 ms 57724 KB Output is correct
31 Correct 211 ms 61564 KB Output is correct
32 Correct 136 ms 58228 KB Output is correct
33 Correct 188 ms 61816 KB Output is correct
34 Correct 6 ms 33112 KB Output is correct
35 Correct 5 ms 33116 KB Output is correct
36 Correct 5 ms 33112 KB Output is correct
37 Correct 5 ms 33116 KB Output is correct
38 Correct 5 ms 32604 KB Output is correct
39 Correct 6 ms 33116 KB Output is correct
40 Correct 5 ms 33116 KB Output is correct
41 Correct 5 ms 33112 KB Output is correct
42 Correct 5 ms 33116 KB Output is correct
43 Correct 5 ms 32856 KB Output is correct
44 Correct 6 ms 33280 KB Output is correct
45 Correct 8 ms 33112 KB Output is correct
46 Correct 7 ms 33116 KB Output is correct
47 Correct 6 ms 32604 KB Output is correct
48 Correct 5 ms 33116 KB Output is correct
49 Correct 6 ms 33052 KB Output is correct
50 Correct 6 ms 33116 KB Output is correct
51 Correct 7 ms 33184 KB Output is correct
52 Correct 6 ms 33116 KB Output is correct
53 Incorrect 7 ms 33044 KB Output isn't correct
54 Halted 0 ms 0 KB -