#include <iostream> #include <vector> #include <set> #include <algorithm> #include <iomanip> #define REP(x,n) for (int x=0;x<(n);++x) #define VAR(x,n) __typeof(n) x = (n) #define FOREACH(x,c) for(VAR(x, (c).begin()); x != (c).end(); ++x) #define CONTAINS(x,elem) ((x).find(elem) != (x).end()) #define MAX_RES 200001 const char TYPE_ADD = '+'; const char TYPE_REMOVE = '-'; // #define __DEBUG #ifdef __DEBUG #define DEBUG(x) x #else #define DEBUG(x) #endif using namespace std; struct Particle { int n; vector<set<int>> edges; Particle(int n): n(n) { edges.resize(n+1); } }; istream& operator>>(istream& is, Particle& p) { int edges,a,b; is>>edges; DEBUG(cerr << "got " << edges << " edges " << endl;) REP(x,edges) { is>>a>>b; DEBUG(cerr << " got edge " << a << "-" << b << endl;) p.edges[a].insert(b); p.edges[b].insert(a); } return is; } ostream& operator<<(ostream& os, const Particle& p) { REP(x, p.edges.size()) { if (p.edges[x].empty()) continue; os << x << ":"; FOREACH(it, p.edges[x]) { os << " " << *it; } os << endl; } return os; } bool canAlterEdge(const Particle& p, int a, int b) { FOREACH(it, p.edges[a]) { if (CONTAINS(p.edges[*it], b)) return true; } return false; } struct Move { char type; int a; int b; } moves[MAX_RES]; int movesCount = 0; ostream& operator<<(ostream& os, const Move& move) { return os << move.type << " " << move.a << " " << move.b; } void inline addConnection(Particle& particle, int source, int target) { DEBUG(cerr << "add connection " << source << "-" << target << endl;) moves[movesCount++] = {TYPE_ADD, source, target}; particle.edges[source].insert(target); particle.edges[target].insert(source); } set<int>::iterator inline removeConnection(Particle& particle, int source, set<int>::iterator targetIter) { DEBUG(cerr << "remove connection " << source << "-" << *targetIter << endl;) moves[movesCount++] = {TYPE_REMOVE, source, *targetIter}; particle.edges[*targetIter].erase(source); return particle.edges[source].erase(targetIter); } void makeInitialConnections(Particle& particle, int currentEdge) { FOREACH(it, particle.edges[currentEdge]) { if ((*it) != 1 && !CONTAINS(particle.edges[*it], 1)) { addConnection(particle, 1, *it); makeInitialConnections(particle, *it); } } } void makeAllInitialConnections(Particle& particle) { set<int> initialConnections = particle.edges[1]; FOREACH(it, initialConnections) { makeInitialConnections(particle, *it); } } void addRequiredConnections(Particle& source, Particle& target) { REP(x, target.n) { FOREACH(it, target.edges[x]) { if (!CONTAINS(source.edges[x], *it)) { addConnection(source, x, *it); } } } } //removes unwanted connections not touching the root (id=1) void removeOtherUnwantedConnections(Particle& source, Particle& target) { for(int x = source.n; x > 1 ; --x) { for(VAR(it, source.edges[x].begin()); it != source.edges[x].end(); ) { if (*it != 1 && !CONTAINS(target.edges[x], *it)) { it = removeConnection(source, x, it); } else { ++it; } } } } void makeBfs(Particle& particle, int startingEdge, vector<int>& resultDistances) { vector<int> queue; queue.reserve(particle.n+1); resultDistances[1] = 0; queue.push_back(1); int queueIndex = 0; while(queue.size() > queueIndex) { int element = queue[queueIndex++]; FOREACH(it, particle.edges[element]) { if (resultDistances[*it] == -1) { queue.push_back(*it); resultDistances[*it] = resultDistances[element] + 1; } } } DEBUG( cerr << "queue:" << endl; REP(x,queue.size()) cerr << x+1 << " " << resultDistances[x+1] << endl; ) } void removeUnwantedConnectionsFromRoot(Particle& source, Particle& target) { const int root = 1; vector<int> distance(source.n+1, -1); makeBfs(target, root, distance); Move* rootMoveStartPtr = &moves[movesCount]; Move* rootMoveEndPtr = rootMoveStartPtr; FOREACH(it, source.edges[root]) { if (!CONTAINS(target.edges[root], *it)) { *(rootMoveEndPtr++) = {TYPE_REMOVE, root, *it}; ++movesCount; DEBUG(cerr << "remove root connection " << root << "-" << *it << endl;) } } std::sort(rootMoveStartPtr, rootMoveEndPtr, [&distance](const Move& m1, const Move& m2){ return distance[m1.b] > distance[m2.b]; }); } void removeUnwantedConnections(Particle& source, Particle& target) { removeOtherUnwantedConnections(source, target); removeUnwantedConnectionsFromRoot(source, target); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin>>n; Particle source(n), target(n); DEBUG(cerr << "Reading source" << endl;) cin >> source; DEBUG(cerr << "Reading target" << endl;) cin >> target; DEBUG(cerr << "make all initial connections" << endl;) makeAllInitialConnections(source); DEBUG(cerr << "make required connections" << endl;) addRequiredConnections(source, target); DEBUG(cerr << "remove unnecessary connections" << endl;) removeUnwantedConnections(source, target); cout << movesCount << endl; Move* movePtr = moves; REP(x, movesCount) cout << *movePtr++ << endl; return 0; }
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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | #include <iostream> #include <vector> #include <set> #include <algorithm> #include <iomanip> #define REP(x,n) for (int x=0;x<(n);++x) #define VAR(x,n) __typeof(n) x = (n) #define FOREACH(x,c) for(VAR(x, (c).begin()); x != (c).end(); ++x) #define CONTAINS(x,elem) ((x).find(elem) != (x).end()) #define MAX_RES 200001 const char TYPE_ADD = '+'; const char TYPE_REMOVE = '-'; // #define __DEBUG #ifdef __DEBUG #define DEBUG(x) x #else #define DEBUG(x) #endif using namespace std; struct Particle { int n; vector<set<int>> edges; Particle(int n): n(n) { edges.resize(n+1); } }; istream& operator>>(istream& is, Particle& p) { int edges,a,b; is>>edges; DEBUG(cerr << "got " << edges << " edges " << endl;) REP(x,edges) { is>>a>>b; DEBUG(cerr << " got edge " << a << "-" << b << endl;) p.edges[a].insert(b); p.edges[b].insert(a); } return is; } ostream& operator<<(ostream& os, const Particle& p) { REP(x, p.edges.size()) { if (p.edges[x].empty()) continue; os << x << ":"; FOREACH(it, p.edges[x]) { os << " " << *it; } os << endl; } return os; } bool canAlterEdge(const Particle& p, int a, int b) { FOREACH(it, p.edges[a]) { if (CONTAINS(p.edges[*it], b)) return true; } return false; } struct Move { char type; int a; int b; } moves[MAX_RES]; int movesCount = 0; ostream& operator<<(ostream& os, const Move& move) { return os << move.type << " " << move.a << " " << move.b; } void inline addConnection(Particle& particle, int source, int target) { DEBUG(cerr << "add connection " << source << "-" << target << endl;) moves[movesCount++] = {TYPE_ADD, source, target}; particle.edges[source].insert(target); particle.edges[target].insert(source); } set<int>::iterator inline removeConnection(Particle& particle, int source, set<int>::iterator targetIter) { DEBUG(cerr << "remove connection " << source << "-" << *targetIter << endl;) moves[movesCount++] = {TYPE_REMOVE, source, *targetIter}; particle.edges[*targetIter].erase(source); return particle.edges[source].erase(targetIter); } void makeInitialConnections(Particle& particle, int currentEdge) { FOREACH(it, particle.edges[currentEdge]) { if ((*it) != 1 && !CONTAINS(particle.edges[*it], 1)) { addConnection(particle, 1, *it); makeInitialConnections(particle, *it); } } } void makeAllInitialConnections(Particle& particle) { set<int> initialConnections = particle.edges[1]; FOREACH(it, initialConnections) { makeInitialConnections(particle, *it); } } void addRequiredConnections(Particle& source, Particle& target) { REP(x, target.n) { FOREACH(it, target.edges[x]) { if (!CONTAINS(source.edges[x], *it)) { addConnection(source, x, *it); } } } } //removes unwanted connections not touching the root (id=1) void removeOtherUnwantedConnections(Particle& source, Particle& target) { for(int x = source.n; x > 1 ; --x) { for(VAR(it, source.edges[x].begin()); it != source.edges[x].end(); ) { if (*it != 1 && !CONTAINS(target.edges[x], *it)) { it = removeConnection(source, x, it); } else { ++it; } } } } void makeBfs(Particle& particle, int startingEdge, vector<int>& resultDistances) { vector<int> queue; queue.reserve(particle.n+1); resultDistances[1] = 0; queue.push_back(1); int queueIndex = 0; while(queue.size() > queueIndex) { int element = queue[queueIndex++]; FOREACH(it, particle.edges[element]) { if (resultDistances[*it] == -1) { queue.push_back(*it); resultDistances[*it] = resultDistances[element] + 1; } } } DEBUG( cerr << "queue:" << endl; REP(x,queue.size()) cerr << x+1 << " " << resultDistances[x+1] << endl; ) } void removeUnwantedConnectionsFromRoot(Particle& source, Particle& target) { const int root = 1; vector<int> distance(source.n+1, -1); makeBfs(target, root, distance); Move* rootMoveStartPtr = &moves[movesCount]; Move* rootMoveEndPtr = rootMoveStartPtr; FOREACH(it, source.edges[root]) { if (!CONTAINS(target.edges[root], *it)) { *(rootMoveEndPtr++) = {TYPE_REMOVE, root, *it}; ++movesCount; DEBUG(cerr << "remove root connection " << root << "-" << *it << endl;) } } std::sort(rootMoveStartPtr, rootMoveEndPtr, [&distance](const Move& m1, const Move& m2){ return distance[m1.b] > distance[m2.b]; }); } void removeUnwantedConnections(Particle& source, Particle& target) { removeOtherUnwantedConnections(source, target); removeUnwantedConnectionsFromRoot(source, target); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin>>n; Particle source(n), target(n); DEBUG(cerr << "Reading source" << endl;) cin >> source; DEBUG(cerr << "Reading target" << endl;) cin >> target; DEBUG(cerr << "make all initial connections" << endl;) makeAllInitialConnections(source); DEBUG(cerr << "make required connections" << endl;) addRequiredConnections(source, target); DEBUG(cerr << "remove unnecessary connections" << endl;) removeUnwantedConnections(source, target); cout << movesCount << endl; Move* movePtr = moves; REP(x, movesCount) cout << *movePtr++ << endl; return 0; } |