#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <set>
#include <stack>
using namespace std;
struct odp{
pair <int, int> krawedz;
char operacja;
};
//dfs z preordem
void dfs(int v, vector<vector<int>> &graf, vector<bool> &odwiedzone, stack<int> &preorder){
odwiedzone[v] = true;
preorder.push(v);
for (auto p: graf[v]){
if (!odwiedzone[p]){
dfs(p, graf, odwiedzone, preorder);
}
}
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int n, ms, md;
cin >> n >> ms;
vector<vector<int>> graf(n+1);
set<pair<int, int>> graf1;
for (int i = 0; i < ms; i++){
int a, b;
cin >> a >> b;
graf[a].push_back(b);
graf[b].push_back(a);
if (a < b){
graf1.insert({a, b});
}else{
graf1.insert({b, a});
}
}
vector<vector<int>> graf_docelowo(n+1);
set<pair<int, int>> graf_docelowo1;
cin >> md;
for (int i = 0; i < md; i++){
int a, b;
cin >> a >> b;
graf_docelowo[a].push_back(b);
graf_docelowo[b].push_back(a);
if (a < b){
graf_docelowo1.insert({a, b});
}else{
graf_docelowo1.insert({b, a});
}
}
vector<odp> wynik;
//bfs
vector<bool> odwiedzone(n+1, false);
odwiedzone[1] = true;
queue<int> kolejka;
for (auto p: graf[1]){
kolejka.push(p);
odwiedzone[p] = true;
}
while (!kolejka.empty()){
int aktualny = kolejka.front();
kolejka.pop();
for (auto p: graf[aktualny]){
if (!odwiedzone[p]){
kolejka.push(p);
odwiedzone[p] = true;
wynik.push_back({{1, p}, '+'});
}
}
}
for (auto p: graf1){
if (p.first != 1 && graf_docelowo1.count(p) == 0){
wynik.push_back({p, '-'});
}
}
for (auto p: graf_docelowo1){
if (p.first != 1 && graf1.count(p) == 0){
wynik.push_back({p, '+'});
}
}
odwiedzone = vector<bool>(n+1, false);
stack<int> preorder;
dfs(1, graf_docelowo, odwiedzone, preorder);
while(!preorder.empty()){
int aktualny = preorder.top();
preorder.pop();
if (aktualny != 1 && graf_docelowo1.count({1, aktualny}) == 0){
wynik.push_back({{1, aktualny}, '-'});
}
}
cout << wynik.size() << "\n";
for (auto p: wynik){
cout << p.operacja << " " << p.krawedz.first << " " << p.krawedz.second << "\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 | #include <iostream> #include <vector> #include <algorithm> #include <queue> #include <set> #include <stack> using namespace std; struct odp{ pair <int, int> krawedz; char operacja; }; //dfs z preordem void dfs(int v, vector<vector<int>> &graf, vector<bool> &odwiedzone, stack<int> &preorder){ odwiedzone[v] = true; preorder.push(v); for (auto p: graf[v]){ if (!odwiedzone[p]){ dfs(p, graf, odwiedzone, preorder); } } } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, ms, md; cin >> n >> ms; vector<vector<int>> graf(n+1); set<pair<int, int>> graf1; for (int i = 0; i < ms; i++){ int a, b; cin >> a >> b; graf[a].push_back(b); graf[b].push_back(a); if (a < b){ graf1.insert({a, b}); }else{ graf1.insert({b, a}); } } vector<vector<int>> graf_docelowo(n+1); set<pair<int, int>> graf_docelowo1; cin >> md; for (int i = 0; i < md; i++){ int a, b; cin >> a >> b; graf_docelowo[a].push_back(b); graf_docelowo[b].push_back(a); if (a < b){ graf_docelowo1.insert({a, b}); }else{ graf_docelowo1.insert({b, a}); } } vector<odp> wynik; //bfs vector<bool> odwiedzone(n+1, false); odwiedzone[1] = true; queue<int> kolejka; for (auto p: graf[1]){ kolejka.push(p); odwiedzone[p] = true; } while (!kolejka.empty()){ int aktualny = kolejka.front(); kolejka.pop(); for (auto p: graf[aktualny]){ if (!odwiedzone[p]){ kolejka.push(p); odwiedzone[p] = true; wynik.push_back({{1, p}, '+'}); } } } for (auto p: graf1){ if (p.first != 1 && graf_docelowo1.count(p) == 0){ wynik.push_back({p, '-'}); } } for (auto p: graf_docelowo1){ if (p.first != 1 && graf1.count(p) == 0){ wynik.push_back({p, '+'}); } } odwiedzone = vector<bool>(n+1, false); stack<int> preorder; dfs(1, graf_docelowo, odwiedzone, preorder); while(!preorder.empty()){ int aktualny = preorder.top(); preorder.pop(); if (aktualny != 1 && graf_docelowo1.count({1, aktualny}) == 0){ wynik.push_back({{1, aktualny}, '-'}); } } cout << wynik.size() << "\n"; for (auto p: wynik){ cout << p.operacja << " " << p.krawedz.first << " " << p.krawedz.second << "\n"; } } |
English