답안 #1044415

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1044415 2024-08-05T09:32:39 Z ㅇㅇ(#11061) Parking (CEOI22_parking) C++17
100 / 100
1020 ms 42428 KB
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...)
#endif
using pii=array<int,2>;
const int N=200005;
int n,m,b[N],t[N];
vector<int> g[N],up[N],l[N];
bool vis[N];
vector<int> V,e;
bool ok=true,isCyc;
int outcnt;
vector<pii> ans;
vector<vector<int>> cycles;
void fail(){
	cout<<"-1\n";
	exit(0);
}
void upd(int i){
	vector<int> nl;
	for(int c: l[i]) if(b[c]==i||t[c]==i) nl.push_back(c);
	sort(nl.begin(),nl.end());
	nl.resize(unique(nl.begin(),nl.end())-nl.begin());
	for(int c: l[i]) if(b[c]==t[c]) nl.push_back(c);
	l[i]=nl;
	up[i].clear();
	for(int c: l[i]) if(b[c]==i&&t[c]) up[i].push_back(t[c]);
	g[i].clear();
	for(int c: l[i]) if(b[c]&&t[c]){
		if(b[c]!=i) g[i].push_back(b[c]);
		if(t[c]!=i) g[i].push_back(t[c]);
	}
}
void Do(int x,int y){
	assert(b[x]);
	if(t[x]){
		assert(!b[y]||t[x]==b[y]);
		if(!b[y]) b[y]=t[x];
		else t[y]=t[x];
		t[x]=0;
	} else{
		assert(!b[y]||b[x]==b[y]);
		if(!b[y]) b[y]=b[x];
		else t[y]=b[x];
		b[x]=0;
	}
	if(b[x]) upd(b[x]);
	if(t[x]) upd(t[x]);
	if(b[y]) upd(b[y]);
	if(t[y]) upd(t[y]);
	ans.push_back({x,y});
}
bool chk(int i){
	int x=l[i][0],y=l[i][1];
	if(!t[x]&&!t[y]){
		return true;
	} else if(!!t[x]+!!t[y]==1){
		if(t[x]) swap(x,y);
		if(i==t[y]){
			return true;
		}
	}
	return false;
}
void dfs(int u){
	V.push_back(u);
	vis[u]=1;
	isCyc&=g[u].size()==2;
	if(up[u].size()==2) outcnt++;
	for(int v: g[u]) if(!vis[v]) dfs(v);
}
int findDown(int i){
	if(b[l[i][0]]==i) return l[i][0];
	return l[i][1];
}
int findUp(int i){
	if(t[l[i][0]]==i) return l[i][0];
	return l[i][1];
}
void solvePath(){
	if(e.empty()) fail();
	int s=0;
	for(int u: V) if(g[u].size()==1) s=u;
	for(int u: V) vis[u]=0;
	V.clear();
	dfs(s);
	assert(up[s].size()==1);
	for(int s=0;s<(int)V.size();){
		int e1=s,e2=0;
		while(up[V[e1]].size()==1) e1++;
		e2=e1+1;
		while(e2+1<(int)V.size()&&up[V[e2]].size()==1) e2++;
		int x=l[V[e1]][0],y=l[V[e1]][1];
		Do(x,e[0]);
		Do(y,e[0]);
		for(int i=e1-1;i>s;i--) Do(findUp(V[i]),findDown(V[i]));
		int x1=l[V[s]][0],y1=l[V[s]][1];
		Do(x1,y1);
		e[0]=x1;
		for(int i=e1+1;i<e2;i++) Do(findUp(V[i]),findDown(V[i]));
		if(e2==(int)V.size()-1) break;
		s=e2;
	}
	int i=V.back(),x=l[i][0],y=l[i][1];
	Do(x,y);
	e.push_back(x);
	for(int x: e) assert(t[x]==0&&b[x]==0);
}
void solveCycle(){
	if(!outcnt){
		if(e.empty()) fail();
		int s=V[0],x=b[findUp(s)];
		V.clear();
		while(x!=s){
			V.push_back(x);
			x=b[findUp(x)];
		}
		x=findUp(s);
		Do(x,e[0]);
		for(int u: V) Do(findUp(u),findDown(u));
		Do(e[0],findDown(s));
		for(int x: e) assert(t[x]==0&&b[x]==0);
		return;
	}
	if(outcnt==1){
		if(e.empty()) fail();
		int s=0,t=0;
		for(int u: V){
			if(up[u].size()==0) s=u;
			if(up[u].size()==2) t=u;
		}
		int x=l[s][0],y=l[s][1];
		Do(x,e[0]);
		Do(y,e[0]);
		for(int i=b[x],j;i!=t;i=j){
			j=b[findUp(i)];
			Do(findUp(i),findDown(i));
		}
		for(int i=b[y],j;i!=t;i=j){
			j=b[findUp(i)];
			Do(findUp(i),findDown(i));
		}
		x=l[t][0],y=l[t][1];
		Do(x,y);
		e[0]=x;
		for(int x: e) assert(::t[x]==0&&b[x]==0);
		return;
	}
	if(e.size()<=1) fail();
	int s=0;
	for(int u: V) if(up[u].size()==0) s=u;
	int emp=e.back();
	e.pop_back();
	int x=l[s][0],y=l[s][1];
	Do(x,emp);
	Do(y,emp);
	int i;
	for(int i=b[x],j;up[i].size()!=1;i=j){
		j=b[findUp(i)];
		Do(findUp(i),findDown(i));
	}
	for(int i=b[y],j;up[i].size()!=1;i=j){
		j=b[findUp(i)];
		Do(findUp(i),findDown(i));
	}
	for(int u: V){
		vis[u]=0;
		if(l[u][0]!=l[u][1]) s=u;
	}
	V.clear();
	dfs(s);
	solvePath();
	for(int x: e) assert(t[x]==0&&b[x]==0);
}
int main(){
	//freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);
	ios::sync_with_stdio(false); cin.tie(0);
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		cin>>b[i]>>t[i];
		if(b[i]!=t[i]) ok=false;
		if(b[i]){
			l[b[i]].push_back(i);
		}
		if(t[i]){
			l[t[i]].push_back(i);
			if(b[i]!=t[i]){
				up[b[i]].push_back(t[i]);
				g[b[i]].push_back(t[i]);
				g[t[i]].push_back(b[i]);
			}
		}
	}
	if(ok){
		cout<<"0\n";
		return 0;
	}
	if(n==m) fail();
	for(int i=1;i<=m;i++) if(!b[i]) e.push_back(i);
	queue<int> que;
	for(int i=1;i<=n;i++){
		int x=l[i][0],y=l[i][1];
		if(x==y){
			vis[i]=1;
			continue;
		}
		if(chk(i)){
			vis[i]=1;
			que.push(i);
		}
	}
	while(que.size()){
		int i=que.front(); que.pop();
		int x=l[i][0],y=l[i][1];
		if(!t[x]&&!t[y]){
			Do(y,x);
			t[x]=i;
			b[y]=0;
			e.push_back(y);
		} else{
			if(t[x]) swap(x,y);
			Do(y,x);
			t[x]=i;
			t[y]=0;
			if(!vis[b[y]]&&chk(b[y])){
				vis[b[y]]=1;
				que.push(b[y]);
			}
		}
	}
	for(int s=1;s<=n;s++) if(!vis[s]){
		V.clear();
		isCyc=true;
		outcnt=0;
		dfs(s);
		if(!isCyc) solvePath();
		else cycles.push_back(V);
	}
	for(vector<int> cyc: cycles){
		for(int u: cyc) vis[u]=0;
		V.clear();
		isCyc=true;
		outcnt=0;
		dfs(cyc[0]);
		solveCycle();
	}
	cout<<ans.size()<<"\n";
	for(auto [x,y]: ans) cout<<x<<" "<<y<<"\n";
	return 0;
}

Compilation message

Main.cpp: In function 'void solveCycle()':
Main.cpp:160:6: warning: unused variable 'i' [-Wunused-variable]
  160 |  int i;
      |      ^
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 14684 KB Output is correct
2 Correct 2 ms 14684 KB Output is correct
3 Correct 2 ms 14684 KB Output is correct
4 Correct 2 ms 14680 KB Output is correct
5 Correct 2 ms 14684 KB Output is correct
6 Correct 2 ms 14680 KB Output is correct
7 Correct 2 ms 14684 KB Output is correct
8 Correct 2 ms 14684 KB Output is correct
9 Correct 2 ms 14680 KB Output is correct
10 Correct 2 ms 14680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 384 ms 25176 KB Output is correct
2 Correct 559 ms 27652 KB Output is correct
3 Correct 171 ms 22752 KB Output is correct
4 Correct 141 ms 22484 KB Output is correct
5 Correct 570 ms 27400 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 14684 KB Output is correct
2 Correct 2 ms 14684 KB Output is correct
3 Correct 2 ms 14684 KB Output is correct
4 Correct 3 ms 14940 KB Output is correct
5 Correct 3 ms 14684 KB Output is correct
6 Correct 3 ms 14680 KB Output is correct
7 Correct 3 ms 14864 KB Output is correct
8 Correct 2 ms 14936 KB Output is correct
9 Correct 3 ms 14936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 14684 KB Output is correct
2 Correct 2 ms 14684 KB Output is correct
3 Correct 2 ms 14684 KB Output is correct
4 Correct 3 ms 14940 KB Output is correct
5 Correct 3 ms 14684 KB Output is correct
6 Correct 3 ms 14680 KB Output is correct
7 Correct 3 ms 14864 KB Output is correct
8 Correct 2 ms 14936 KB Output is correct
9 Correct 3 ms 14936 KB Output is correct
10 Correct 199 ms 40836 KB Output is correct
11 Correct 36 ms 23124 KB Output is correct
12 Correct 80 ms 35156 KB Output is correct
13 Correct 231 ms 40668 KB Output is correct
14 Correct 75 ms 34212 KB Output is correct
15 Correct 93 ms 34388 KB Output is correct
16 Correct 215 ms 40496 KB Output is correct
17 Correct 120 ms 35084 KB Output is correct
18 Correct 199 ms 40744 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14936 KB Output is correct
2 Correct 2 ms 14940 KB Output is correct
3 Correct 3 ms 14684 KB Output is correct
4 Correct 2 ms 14680 KB Output is correct
5 Correct 2 ms 14684 KB Output is correct
6 Correct 3 ms 14936 KB Output is correct
7 Correct 2 ms 14684 KB Output is correct
8 Correct 3 ms 14940 KB Output is correct
9 Correct 4 ms 14936 KB Output is correct
10 Correct 3 ms 14684 KB Output is correct
11 Correct 5 ms 14940 KB Output is correct
12 Correct 4 ms 14940 KB Output is correct
13 Correct 2 ms 14684 KB Output is correct
14 Correct 3 ms 14684 KB Output is correct
15 Correct 3 ms 14940 KB Output is correct
16 Correct 3 ms 14940 KB Output is correct
17 Correct 3 ms 14836 KB Output is correct
18 Correct 4 ms 14940 KB Output is correct
19 Correct 3 ms 14972 KB Output is correct
20 Correct 3 ms 14972 KB Output is correct
21 Correct 2 ms 14684 KB Output is correct
22 Correct 4 ms 15192 KB Output is correct
23 Correct 2 ms 14940 KB Output is correct
24 Correct 3 ms 14940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 14684 KB Output is correct
2 Correct 2 ms 14684 KB Output is correct
3 Correct 2 ms 14684 KB Output is correct
4 Correct 2 ms 14680 KB Output is correct
5 Correct 2 ms 14684 KB Output is correct
6 Correct 2 ms 14680 KB Output is correct
7 Correct 2 ms 14684 KB Output is correct
8 Correct 2 ms 14684 KB Output is correct
9 Correct 2 ms 14680 KB Output is correct
10 Correct 2 ms 14680 KB Output is correct
11 Correct 384 ms 25176 KB Output is correct
12 Correct 559 ms 27652 KB Output is correct
13 Correct 171 ms 22752 KB Output is correct
14 Correct 141 ms 22484 KB Output is correct
15 Correct 570 ms 27400 KB Output is correct
16 Correct 2 ms 14684 KB Output is correct
17 Correct 2 ms 14684 KB Output is correct
18 Correct 2 ms 14684 KB Output is correct
19 Correct 3 ms 14940 KB Output is correct
20 Correct 3 ms 14684 KB Output is correct
21 Correct 3 ms 14680 KB Output is correct
22 Correct 3 ms 14864 KB Output is correct
23 Correct 2 ms 14936 KB Output is correct
24 Correct 3 ms 14936 KB Output is correct
25 Correct 199 ms 40836 KB Output is correct
26 Correct 36 ms 23124 KB Output is correct
27 Correct 80 ms 35156 KB Output is correct
28 Correct 231 ms 40668 KB Output is correct
29 Correct 75 ms 34212 KB Output is correct
30 Correct 93 ms 34388 KB Output is correct
31 Correct 215 ms 40496 KB Output is correct
32 Correct 120 ms 35084 KB Output is correct
33 Correct 199 ms 40744 KB Output is correct
34 Correct 3 ms 14936 KB Output is correct
35 Correct 2 ms 14940 KB Output is correct
36 Correct 3 ms 14684 KB Output is correct
37 Correct 2 ms 14680 KB Output is correct
38 Correct 2 ms 14684 KB Output is correct
39 Correct 3 ms 14936 KB Output is correct
40 Correct 2 ms 14684 KB Output is correct
41 Correct 3 ms 14940 KB Output is correct
42 Correct 4 ms 14936 KB Output is correct
43 Correct 3 ms 14684 KB Output is correct
44 Correct 5 ms 14940 KB Output is correct
45 Correct 4 ms 14940 KB Output is correct
46 Correct 2 ms 14684 KB Output is correct
47 Correct 3 ms 14684 KB Output is correct
48 Correct 3 ms 14940 KB Output is correct
49 Correct 3 ms 14940 KB Output is correct
50 Correct 3 ms 14836 KB Output is correct
51 Correct 4 ms 14940 KB Output is correct
52 Correct 3 ms 14972 KB Output is correct
53 Correct 3 ms 14972 KB Output is correct
54 Correct 2 ms 14684 KB Output is correct
55 Correct 4 ms 15192 KB Output is correct
56 Correct 2 ms 14940 KB Output is correct
57 Correct 3 ms 14940 KB Output is correct
58 Correct 497 ms 38504 KB Output is correct
59 Correct 270 ms 40720 KB Output is correct
60 Correct 1020 ms 35776 KB Output is correct
61 Correct 470 ms 39424 KB Output is correct
62 Correct 49 ms 24848 KB Output is correct
63 Correct 253 ms 40724 KB Output is correct
64 Correct 125 ms 35864 KB Output is correct
65 Correct 268 ms 39992 KB Output is correct
66 Correct 317 ms 40904 KB Output is correct
67 Correct 117 ms 36216 KB Output is correct
68 Correct 303 ms 42428 KB Output is correct
69 Correct 321 ms 41576 KB Output is correct
70 Correct 126 ms 35668 KB Output is correct
71 Correct 114 ms 35472 KB Output is correct
72 Correct 198 ms 36180 KB Output is correct
73 Correct 330 ms 41916 KB Output is correct
74 Correct 174 ms 35972 KB Output is correct
75 Correct 296 ms 41152 KB Output is correct
76 Correct 388 ms 41600 KB Output is correct
77 Correct 311 ms 40900 KB Output is correct
78 Correct 120 ms 35436 KB Output is correct
79 Correct 277 ms 40120 KB Output is correct
80 Correct 153 ms 36628 KB Output is correct
81 Correct 377 ms 41720 KB Output is correct