Submission #1310595

#TimeUsernameProblemLanguageResultExecution timeMemory
1310595TymondParking (CEOI22_parking)C++20
20 / 100
63 ms141036 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; #define fi first #define se second #define vi vector<int> #define vll vector<long long> #define pii pair<int, int> #define pll pair<long long, long long> #define pb push_back #define mp make_pair #define eb emplace_back #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); mt19937_64 rng64(chrono::high_resolution_clock::now().time_since_epoch().count()); inline int rand(int l,int r){return uniform_int_distribution<int>(l, r)(rng);} inline ll rand(ll l,ll r){return uniform_int_distribution<ll>(l, r)(rng64);} #ifdef DEBUG auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";} auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";} #define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X) #else #define debug(...){} #endif struct custom_hash { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; struct pair_hash{ size_t operator()(const pair<int,int>&x)const{ return hash<long long>()(((long long)x.first)^(((long long)x.second)<<32)); } }; const int MAXN = 2e5 + 7; vi pos[MAXN]; vi vec[MAXN]; vi f = {}; vector<pii> ans = {}; bool ok = true; int n, m; void Move(int p, int q){ if(p == q){ return; } ans.pb(mp(p, q)); int x = vec[p].back(); if(pos[x][0] == p){ swap(pos[x][0], pos[x][1]); } pos[x][1] = q; vec[p].pop_back(); vec[q].pb(x); if(sz(f) && q == f.back()){ f.pop_back(); } if(sz(vec[p]) == 0){ f.pb(p); } } int eliminate(int v){ if(sz(vec[v]) == 0 || sz(vec[v]) == 2){ return 0; } int x = vec[v].back(); int nxt = pos[x][0] + pos[x][1] - v; //cerr << v << ' ' << nxt << ' ' << x << '\n'; if(vec[nxt].back() == vec[v].back()){ Move(nxt, v); if(sz(vec[nxt]) == 1){ return eliminate(nxt); } return 0; }else{ return v; } } void eliminate2(int v){ if(sz(vec[v]) == 0 || sz(vec[v]) == 2){ return; } int x = vec[v].back(); int nxt = pos[x][0] + pos[x][1] - v; // cerr << v << ' ' << nxt << ' ' << x << '\n'; // debug(ans); if(vec[nxt].back() == vec[v].back()){ Move(nxt, v); if(sz(vec[nxt]) == 1){ eliminate2(nxt); } }else{ int our = v; while(67){ // cerr << our << ' ' << nxt << ' ' << x << '\n'; if(vec[nxt].back() == vec[our].back()){ if(sz(f) == 0){ cout << "-1\n"; exit(0); } int xd = f.back(); Move(our, xd); Move(nxt, xd); if(sz(vec[nxt])){ eliminate2(nxt); } if(sz(vec[our])){ eliminate2(our); } return; }else{ x = vec[nxt].back(); our = nxt; nxt = pos[x][0] + pos[x][1] - our; } } } } int main(){ ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); cin >> n >> m; for(int i = 1; i <= m; i++){ int a, b; cin >> a >> b; if(a != 0){ pos[a].pb(i); vec[i].pb(a); }else{ f.pb(i); } if(b != 0){ pos[b].pb(i); vec[i].pb(b); } } // debug(ans); // debug(f); for(int i = 1; i <= m; i++){ if(sz(vec[i]) == 1){ eliminate(i); } } // cerr << "XD\n"; for(int i = 1; i <= m; i++){ if(sz(vec[i]) == 1){ eliminate2(i); } } // debug(ans); // debug(f); for(int i = 1; i <= n; i++){ if(pos[i][0] != pos[i][1] && vec[pos[i][0]].back() == i && vec[pos[i][1]].back() == i){ //jesteś w wierzchołku do którego wchodzi dwóch // cerr << i << '\n'; // debug(f); if(sz(f) == 0){ cout << "-1\n"; return 0; } int xd1 = pos[i][0]; int xd2 = pos[i][1]; Move(xd1, f.back()); Move(xd2, f.back()); xd1 = eliminate(xd1); xd2 = eliminate(xd2); //debug(ans); if(xd1 && sz(vec[xd1])){ eliminate2(xd1); } if(xd2 && sz(vec[xd2])){ eliminate2(xd2); } } } // cerr << "XD2\n"; //cykle for(int i = 1; i <= n; i++){ if(pos[i][0] == pos[i][1]){ continue; } if(sz(f) == 0){ cout << "-1\n"; return 0; } int xd = f.back(); int our = pos[i][0]; if(vec[our].back() != i){ our = pos[i][1]; } Move(our, xd); eliminate(xd); eliminate(our); } cout << sz(ans) << '\n'; for(auto ele : ans){ cout << ele.fi << ' ' << ele.se << '\n'; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...