#include <iostream> #include <set> #include <queue> #include <memory> namespace alc { using namespace std; struct Graph { struct Edge { static int encode(int a, int b) { if (a > b) swap(a, b); return (a << 16) | b; } static void decode(int mid, int &a, int &b) { a = mid >> 16; b = mid & ((1 << 16) - 1); } }; struct Vertex : vector< short > { }; struct Bfs { enum { UNVISITED = -1 }; Graph const &g; vector< short > parent; queue< short > q; Bfs(Graph const &g, int source) : g(g), parent(g.V.size(), UNVISITED) { parent[source] = source; q.push(source); } Bfs &run(int destination) { while (parent[destination] < 0) { int v = q.front(); q.pop(); Vertex const &s = g.V[v]; for (Vertex::const_iterator it = s.begin(); it != s.end(); ++it) { int u = *it; if (parent[u] == UNVISITED) { parent[u] = v; q.push(u); } } } return *this; } int distance(int destination) const { int result = 0; while (parent[destination] != destination) { destination = parent[destination]; ++result; } return result; } }; vector< Vertex > V; set< int > E; int marker; Graph(int n) : V(n), marker(0) { } void connect(int a, int b) { V[a].push_back(b); V[b].push_back(a); E.insert(Edge::encode(a, b)); } bool connected(int a, int b) const { return E.count(Edge::encode(a, b)); } }; } using namespace alc; struct Transformation : vector< int > { set< int > extra; Transformation(Graph &g, Graph const &d) { vector< shared_ptr< Graph::Bfs > > bfs(g.V.size()); for (set< int >::const_iterator e = d.E.begin(); e != d.E.end(); ++e) if (!g.E.count(*e)) { int a, b; Graph::Edge::decode(*e, a, b); if (!bfs[a]) { if (bfs[b]) std::swap(a, b); else bfs[a] = make_shared< Graph::Bfs >(g, a); } Graph::Bfs &bfsa = *bfs[a]; bfsa.run(b); int distance = bfsa.distance(b); if (distance > 2) { int id = bfsa.parent[b]; while ((id = bfsa.parent[id]) != a) { int mid = Graph::Edge::encode(b, id); if (!d.E.count(mid)) extra.insert(mid); this->push_back(mid); g.connect(b, id); } } this->push_back(Graph::Edge::encode(a, b)); g.connect(a, b); } } }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m, a, b; cin >> n; Graph g(n), f(n), full(n); cin >> m; while (m-- && cin >> a >> b) { g.connect(--a, --b); full.connect(a, b); } cin >> m; while (m-- && cin >> a >> b) { f.connect(--a, --b); if (!full.connected(a, b)) full.connect(a, b); } Transformation gToFull(g, full), fToFull(f, full); int extraToDel = 0, extraToAdd = 0; for (set< int >::iterator it = gToFull.extra.begin(); it != gToFull.extra.end(); ++it) extraToDel += !fToFull.extra.count(*it); for (set< int >::iterator it = fToFull.extra.begin(); it != fToFull.extra.end(); ++it) extraToAdd += !gToFull.extra.count(*it); cout << gToFull.size() + extraToDel + extraToAdd + fToFull.size() << '\n'; for (Transformation::iterator it = gToFull.begin(); it != gToFull.end(); ++it) { Graph::Edge::decode(*it, a, b); cout << "+ " << (a + 1) << ' ' << (b + 1) << '\n'; } for (Transformation::reverse_iterator it = gToFull.rbegin(); it != gToFull.rend(); ++it) if (gToFull.extra.count(*it) && !fToFull.extra.count(*it)) { Graph::Edge::decode(*it, a, b); cout << "- " << (a + 1) << ' ' << (b + 1) << '\n'; } for (Transformation::iterator it = fToFull.begin(); it != fToFull.end(); ++it) if (fToFull.extra.count(*it) && !gToFull.extra.count(*it)) { Graph::Edge::decode(*it, a, b); cout << "+ " << (a + 1) << ' ' << (b + 1) << '\n'; } for (Transformation::reverse_iterator it = fToFull.rbegin(); it != fToFull.rend(); ++it) { Graph::Edge::decode(*it, a, b); cout << "- " << (a + 1) << ' ' << (b + 1) << '\n'; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | #include <iostream> #include <set> #include <queue> #include <memory> namespace alc { using namespace std; struct Graph { struct Edge { static int encode(int a, int b) { if (a > b) swap(a, b); return (a << 16) | b; } static void decode(int mid, int &a, int &b) { a = mid >> 16; b = mid & ((1 << 16) - 1); } }; struct Vertex : vector< short > { }; struct Bfs { enum { UNVISITED = -1 }; Graph const &g; vector< short > parent; queue< short > q; Bfs(Graph const &g, int source) : g(g), parent(g.V.size(), UNVISITED) { parent[source] = source; q.push(source); } Bfs &run(int destination) { while (parent[destination] < 0) { int v = q.front(); q.pop(); Vertex const &s = g.V[v]; for (Vertex::const_iterator it = s.begin(); it != s.end(); ++it) { int u = *it; if (parent[u] == UNVISITED) { parent[u] = v; q.push(u); } } } return *this; } int distance(int destination) const { int result = 0; while (parent[destination] != destination) { destination = parent[destination]; ++result; } return result; } }; vector< Vertex > V; set< int > E; int marker; Graph(int n) : V(n), marker(0) { } void connect(int a, int b) { V[a].push_back(b); V[b].push_back(a); E.insert(Edge::encode(a, b)); } bool connected(int a, int b) const { return E.count(Edge::encode(a, b)); } }; } using namespace alc; struct Transformation : vector< int > { set< int > extra; Transformation(Graph &g, Graph const &d) { vector< shared_ptr< Graph::Bfs > > bfs(g.V.size()); for (set< int >::const_iterator e = d.E.begin(); e != d.E.end(); ++e) if (!g.E.count(*e)) { int a, b; Graph::Edge::decode(*e, a, b); if (!bfs[a]) { if (bfs[b]) std::swap(a, b); else bfs[a] = make_shared< Graph::Bfs >(g, a); } Graph::Bfs &bfsa = *bfs[a]; bfsa.run(b); int distance = bfsa.distance(b); if (distance > 2) { int id = bfsa.parent[b]; while ((id = bfsa.parent[id]) != a) { int mid = Graph::Edge::encode(b, id); if (!d.E.count(mid)) extra.insert(mid); this->push_back(mid); g.connect(b, id); } } this->push_back(Graph::Edge::encode(a, b)); g.connect(a, b); } } }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m, a, b; cin >> n; Graph g(n), f(n), full(n); cin >> m; while (m-- && cin >> a >> b) { g.connect(--a, --b); full.connect(a, b); } cin >> m; while (m-- && cin >> a >> b) { f.connect(--a, --b); if (!full.connected(a, b)) full.connect(a, b); } Transformation gToFull(g, full), fToFull(f, full); int extraToDel = 0, extraToAdd = 0; for (set< int >::iterator it = gToFull.extra.begin(); it != gToFull.extra.end(); ++it) extraToDel += !fToFull.extra.count(*it); for (set< int >::iterator it = fToFull.extra.begin(); it != fToFull.extra.end(); ++it) extraToAdd += !gToFull.extra.count(*it); cout << gToFull.size() + extraToDel + extraToAdd + fToFull.size() << '\n'; for (Transformation::iterator it = gToFull.begin(); it != gToFull.end(); ++it) { Graph::Edge::decode(*it, a, b); cout << "+ " << (a + 1) << ' ' << (b + 1) << '\n'; } for (Transformation::reverse_iterator it = gToFull.rbegin(); it != gToFull.rend(); ++it) if (gToFull.extra.count(*it) && !fToFull.extra.count(*it)) { Graph::Edge::decode(*it, a, b); cout << "- " << (a + 1) << ' ' << (b + 1) << '\n'; } for (Transformation::iterator it = fToFull.begin(); it != fToFull.end(); ++it) if (fToFull.extra.count(*it) && !gToFull.extra.count(*it)) { Graph::Edge::decode(*it, a, b); cout << "+ " << (a + 1) << ' ' << (b + 1) << '\n'; } for (Transformation::reverse_iterator it = fToFull.rbegin(); it != fToFull.rend(); ++it) { Graph::Edge::decode(*it, a, b); cout << "- " << (a + 1) << ' ' << (b + 1) << '\n'; } } |