Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define sz(x) ((int)x.size ())
- const int N = 200;
- const int inf = 1e9 + 7;
- int n, m, k;
- vector<int> gr[N], S, A, B;
- int used[N], in[N], w[N];
- int timer = 1;
- vector<int> g[N];
- int was[N], s, t;
- struct edge {
- int fr, to, f;
- };
- vector<edge> e;
- void make (int a, int b, int c) {
- g[a].push_back(sz(e));
- e.push_back({a, b, c});
- g[b].push_back(sz(e));
- e.push_back({b, a, 0});
- }
- int go (int v, int flow = inf) {
- was[v] = timer;
- if (v == t) return flow;
- for (auto i : g[v])
- if (e[i].f && was[e[i].to] != timer) {
- int push = go (e[i].to, min (flow, e[i].f));
- if (push) {
- e[i].f -= push;
- e[i^1].f += push;
- return push;
- }
- }
- return 0;
- }
- int get() {
- timer++;
- int flow = 0;
- int cur;
- while (cur = go(s)) {
- flow += cur;
- timer++;
- }
- return flow;
- }
- void dfs (int v, int c) {
- used[v] = 1 + c;
- if (!c) A.push_back(v);
- else B.push_back(v);
- for (auto x : gr[v])
- if (used[x] == 0)
- dfs (x, c ^ 1);
- }
- vector<int> get (vector<int>& p, vector<int>& A, int pos) {
- vector<int> ans(sz(A));
- for (int i = 0; i < sz(A); i++) {
- for (auto x : gr[A[i]])
- if (w[x] >= 0 && p[w[x]] == pos)
- ans[i] = 1;
- }
- return ans;
- }
- bool go (int pos, vector<int>& p, vector<int>& ans) {
- if (pos == k) {
- int ok = 0;
- vector<int> T;
- for (int i = 0; i < k; i++) {
- in[S[i]] = (p[i] == 1);
- if (p[i] == 2) T.push_back(S[i]);
- }
- for (int i = 0; i < k; i++) {
- if (p[i] == 0)
- for (auto x : gr[S[i]])
- ok |= in[x];
- }
- if (ok) return false;
- auto Al = get (p, A, 0);
- auto Ar = get (p, A, 1);
- auto Bl = get (p, B, 0);
- auto Br = get (p, B, 1);
- vector<int> AlBr, ArBl;
- for (int i = 0; i < sz(A); i++) {
- if (Al[i]) AlBr.push_back(A[i]);
- if (Ar[i]) ArBl.push_back(A[i]);
- }
- for (int i = 0; i < sz(B); i++) {
- if (Br[i]) AlBr.push_back(B[i]);
- if (Bl[i]) ArBl.push_back(B[i]);
- }
- e.clear();
- s = 2 * n;
- t = s + 1;
- for (int i = 0; i <= t; i++) g[i].clear();
- for (int i = 0; i < n; i++)
- if (used[i] != -1) {
- make (i, n + i, 1);
- for (auto x : gr[i])
- if (used[x] != -1)
- make (n + i, x, 1);
- }
- for (auto x : AlBr) make (s, x, 1);
- for (auto x : ArBl) make (x + n, t, 1);
- if (get() < sz(S) - sz(T)) {
- for (int i = 0; i < sz(e); i += 2)
- if (e[i].fr == s && !e[i].f) ans.push_back(e[i].to);
- for (auto x : T) ans.push_back(x);
- return true;
- }
- return false;
- }
- for (int i = 0; i < 3; i++) {
- p[pos] = i;
- if (go (pos + 1, p, ans)) return true;
- }
- return false;
- }
- int main() {
- memset (w, -1, sizeof(w));
- cin >> n >> m >> k;
- S.resize(k);
- for (int i = 0; i < m; i++) {
- int x, y;
- cin >> x >> y;
- x--; y--;
- gr[x].push_back(y);
- gr[y].push_back(x);
- }
- vector<int> t(k);
- for (int i = 0; i < k; i++) cin >> S[i], used[--S[i]] = -1, w[S[i]] = i;
- for (int i = 0; i < n; i++)
- if (used[i] == 0)
- dfs (i, 0);
- vector<int> ans;
- if (go (0, t, ans))
- for (auto x : ans) cout << x + 1 << " ";
- else cout << "No solution";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement