#include <bits/stdc++.h> using namespace std; #define endl "\n" #define ll long long #define ld long double #define ull unsigned long long void dbg_out() { cout << endl; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); } #define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__) void solve() { ll n, m1, m2, u, v; cin >> n >> m1; map<pair<ll,ll>, bool> mp1,mp2; for(ll i = 0; i < m1; i++){ cin >> u >> v; mp1[{u, v}] = true; mp1[{v, u}] = true; } cin >> m2; for(ll i = 0; i < m2; i++){ cin >> u >> v; mp2[{u, v}] = true; mp2[{v, u}] = true; } //dodawanie od 1 do wszystkich innych vector<pair<ll,ll>> dodawanie,usuwanie; for(ll i = 2; i <= n; i++){ if(mp1[{1, i}]) continue; mp1[{1, i}] = true; mp1[{i, 1}] = true; dodawanie.push_back({1, i}); } for(auto el : mp2){ if(mp1[el.first]) continue; mp1[{el.first.first, el.first.second}] = true; mp1[{el.first.second, el.first.first}] = true; dodawanie.push_back(el.first); } for(auto el : mp1){ if(mp2[el.first] || el.second == false) continue; mp1[{el.first.first, el.first.second}] = false; mp1[{el.first.second, el.first.first}] = false; usuwanie.push_back(el.first); } cout << dodawanie.size() + usuwanie.size() << endl; for(auto el : dodawanie) cout << "+ " << el.first << " " << el.second << endl; for(auto el : usuwanie) cout << "- " << el.first << " " << el.second << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // #ifndef ONLINE_JUDGE // freopen("../../in.in", "r", stdin); // freopen("../../out.out", "w", stdout); // #endif solve(); }
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 | #include <bits/stdc++.h> using namespace std; #define endl "\n" #define ll long long #define ld long double #define ull unsigned long long void dbg_out() { cout << endl; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); } #define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__) void solve() { ll n, m1, m2, u, v; cin >> n >> m1; map<pair<ll,ll>, bool> mp1,mp2; for(ll i = 0; i < m1; i++){ cin >> u >> v; mp1[{u, v}] = true; mp1[{v, u}] = true; } cin >> m2; for(ll i = 0; i < m2; i++){ cin >> u >> v; mp2[{u, v}] = true; mp2[{v, u}] = true; } //dodawanie od 1 do wszystkich innych vector<pair<ll,ll>> dodawanie,usuwanie; for(ll i = 2; i <= n; i++){ if(mp1[{1, i}]) continue; mp1[{1, i}] = true; mp1[{i, 1}] = true; dodawanie.push_back({1, i}); } for(auto el : mp2){ if(mp1[el.first]) continue; mp1[{el.first.first, el.first.second}] = true; mp1[{el.first.second, el.first.first}] = true; dodawanie.push_back(el.first); } for(auto el : mp1){ if(mp2[el.first] || el.second == false) continue; mp1[{el.first.first, el.first.second}] = false; mp1[{el.first.second, el.first.first}] = false; usuwanie.push_back(el.first); } cout << dodawanie.size() + usuwanie.size() << endl; for(auto el : dodawanie) cout << "+ " << el.first << " " << el.second << endl; for(auto el : usuwanie) cout << "- " << el.first << " " << el.second << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // #ifndef ONLINE_JUDGE // freopen("../../in.in", "r", stdin); // freopen("../../out.out", "w", stdout); // #endif solve(); } |