This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**
____ ____ ____ ____ ____ ____
||l |||e |||i |||n |||a |||d ||
||__|||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|/__\|
**/
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
void speed()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
const int maxn = 2e5 + 10;
int n, m, t[maxn][2];
vector < pair < int, int > > path, ans;
int found = -1;
void brute()
{
/**cout << "here " << path.size() << endl;
for (int i = 1; i <= m; i ++)
cout << t[i][0] << " " << t[i][1] << endl;*/
bool done = true;
for (int i = 1; i <= m; i ++)
{
if (t[i][0] != t[i][1])
{
done = false;
break;
}
}
if (done)
{
cout << path.size() << endl;
for (pair < int, int > cur : path)
cout << cur.first << " " << cur.second << endl;
exit(0);
///cout << "done" << endl;
if (found == -1 || path.size() < ans.size())
ans = path, found = 1;
return;
}
if (path.size() >= ans.size() && found != -1)
return;
for (int i = 1; i <= m; i ++)
{
if (t[i][0] == t[i][1])
continue;
if (t[i][0] == 0)
continue;
if (t[i][1] == 0)
{
for (int j = 1; j <= m; j ++)
{
if (i == j)
continue;
if (t[j][1] == 0 && t[j][0] == t[i][0])
{
path.push_back({i, j});
t[j][1] = t[j][0];
t[i][0] = 0;
brute();
path.pop_back();
t[j][1] = 0;
t[i][0] = t[j][0];
}
}
}
else
{
for (int j = 1; j <= m; j ++)
{
if (j == i)
continue;
if (t[j][1] == 0 && t[j][0] == t[i][1])
{
t[j][1] = t[i][1];
t[i][1] = 0;
path.push_back({i, j});
brute();
path.pop_back();
t[i][1] = t[j][1];
t[j][1] = 0;
}
}
for (int j = 1; j <= m; j ++)
{
if (j == i)
continue;
if (t[j][0] == 0)
{
path.push_back({i, j});
t[j][0] = t[i][1];
t[i][1] = 0;
brute();
t[i][1] = t[j][0];
t[j][0] = 0;
path.pop_back();
}
}
}
}
}
void solve()
{
cin >> n >> m;
for (int i = 1; i <= m; i ++)
{
cin >> t[i][0] >> t[i][1];
}
brute();
if (found == -1)
{
cout << -1 << endl;
return;
}
cout << ans.size() << endl;
for (pair < int, int > cur : ans)
cout << cur.first << " " << cur.second << endl;
}
int main()
{
solve();
return 0;
}
/**
1 2
1 0
1 0
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |