This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <unordered_set>
#include <queue>
#include <deque>
#include <string>
#include <sstream>
#include <iomanip>
#include <map>
#include <unordered_map>
#include <stack>
#include <cstdio>
#include <climits>
#include <tuple>
#include <ctime>
#include <cstring>
#include <numeric>
#include <functional>
#include <chrono>
#include <cassert>
#include <bitset>
//#include <bit>
//#include <ranges>
//#include <numbers>
#define itn int
#define sacnf scanf
#define sz(a) ((int)((a).size()))
// printf("%.10f\n", ans);
using ll = long long;
using namespace std;
const ll mod = 1e9 + 7;
const int N = 1e5 + 5;
int a, b, m;
vector<bool> used;
vector<int> order;
vector<pair<int, int>> edges;
vector<vector<int>> gp, rev_gp, tgp, rev_tgp;
void dfs1(int v)
{
used[v] = 1;
for (auto u : tgp[v]) if (!used[u]) dfs1(u);
order.push_back(v);
}
void dfs2(int v)
{
used[v] = 1;
for (auto u : rev_tgp[v]) if (!used[u]) dfs2(u);
}
int cnt()
{
used.assign(a + b, 0);
for (int i = 0; i < a + b; i++) if (!used[i]) dfs1(i);
used.assign(a + b, false);
reverse(order.begin(), order.end());
int c = 0;
for (auto v : order)
{
if (!used[v])
{
dfs2(v);
c++;
}
}
order.clear();
return c;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> a >> b >> m;
edges = vector<pair<int, int>>(m);
gp = rev_gp = vector<vector<int>>(a + b);
for (int i = 0; i < m; i++) cin >> edges[i].first >> edges[i].second;
for (int i = 0; i < a - 1; i++) gp[i].push_back(i + 1), rev_gp[i + 1].push_back(i);
for (int i = 0; i < b - 1; i++) gp[a + i].push_back(a + i + 1), rev_gp[a + i + 1].push_back(a + i);
int mn = INT_MAX;
vector<int> ans;
for (int i = 0; i < (1 << m); i++)
{
vector<int> vec;
for (int j = 0; j < m; j++) vec.push_back((i >> j) & 1);
reverse(vec.begin(), vec.end());
tgp = gp;
rev_tgp = rev_gp;
for (int j = 0; j < m; j++)
{
int u = edges[j].first, v = edges[j].second;
u--, v--;
v += a;
if (vec[j]) swap(u, v);
tgp[u].push_back(v);
rev_tgp[v].push_back(u);
}
int c = cnt();
if (c < mn)
{
mn = c;
ans = vec;
}
}
cout << mn << "\n";
for (int& i : ans) cout << i << " ";
return 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... |