#include<bits/stdc++.h>
using namespace std;
const int n_max = 3*1e4+7;
vector<int> graph_begin[n_max];
vector<int> graph_end[n_max];
vector<tuple<char, int, int>> ans;
int visited[n_max];
void bfs_init(){
queue<pair<int, int>> kolejka;
kolejka.push({1, 0});
while(!kolejka.empty()){
auto [v, lvl] = kolejka.front();
kolejka.pop();
if(visited[v] == 1){
continue;
}
visited[v] = 1;
for(auto u: graph_begin[v]){
kolejka.push({u, lvl + 1});
}
if(lvl > 1){
ans.push_back({'+', 1, v});
graph_begin[v].push_back(1);
}
}
}
void calculate(int v){
set<int> set_begin;
set<int> set_end;
for(auto u: graph_begin[v]){
set_begin.insert(u);
}
bool if_one = false;
for(auto u: graph_end[v]){
if(set_begin.find(u) == set_begin.end()){
set_end.insert(u);
}else{
set_begin.erase(u);
}
if(u == 1){
if_one = true;
}
}
if(set_begin.find(1) != set_begin.end()){
set_begin.erase(1);
}
if(set_end.find(1) != set_end.end()){
set_end.erase(1);
}
for(auto u: set_begin){
if(visited[u] == 3){
continue;
}
ans.push_back({'-', v, u});
}
for(auto u: set_end){
if(visited[u] == 3){
continue;
}
ans.push_back({'+', v, u});
}
if(!if_one){
ans.push_back({'-', 1, v});
}
}
void bfs_get_vertex(){
queue<pair<int, int>> kolejka;
stack<int> vertex;
kolejka.push({1, 1});
while(!kolejka.empty()){
auto [v, lvl] = kolejka.front();
kolejka.pop();
if(visited[v] == 2){
continue;
}
visited[v] = 2;
for(auto u: graph_end[v]){
kolejka.push({u, lvl + 1});
}
vertex.push(v);
}
while(!vertex.empty()){
auto v = vertex.top();
vertex.pop();
if(v == 1){
continue;
}
calculate(v);
visited[v] = 3;
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
int m;
cin >> m;
for(int i = 0; i < m; i++){
int a, b;
cin >> a >> b;
graph_begin[a].push_back(b);
graph_begin[b].push_back(a);
}
cin >> m;
for(int i = 0; i < m; i++){
int a, b;
cin >> a >> b;
graph_end[a].push_back(b);
graph_end[b].push_back(a);
}
bfs_init();
bfs_get_vertex();
cout << ans.size() << '\n';
for(auto [sign, v, u]: ans){
cout << sign << ' ' << v << ' ' << u << '\n';
}
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 | #include<bits/stdc++.h> using namespace std; const int n_max = 3*1e4+7; vector<int> graph_begin[n_max]; vector<int> graph_end[n_max]; vector<tuple<char, int, int>> ans; int visited[n_max]; void bfs_init(){ queue<pair<int, int>> kolejka; kolejka.push({1, 0}); while(!kolejka.empty()){ auto [v, lvl] = kolejka.front(); kolejka.pop(); if(visited[v] == 1){ continue; } visited[v] = 1; for(auto u: graph_begin[v]){ kolejka.push({u, lvl + 1}); } if(lvl > 1){ ans.push_back({'+', 1, v}); graph_begin[v].push_back(1); } } } void calculate(int v){ set<int> set_begin; set<int> set_end; for(auto u: graph_begin[v]){ set_begin.insert(u); } bool if_one = false; for(auto u: graph_end[v]){ if(set_begin.find(u) == set_begin.end()){ set_end.insert(u); }else{ set_begin.erase(u); } if(u == 1){ if_one = true; } } if(set_begin.find(1) != set_begin.end()){ set_begin.erase(1); } if(set_end.find(1) != set_end.end()){ set_end.erase(1); } for(auto u: set_begin){ if(visited[u] == 3){ continue; } ans.push_back({'-', v, u}); } for(auto u: set_end){ if(visited[u] == 3){ continue; } ans.push_back({'+', v, u}); } if(!if_one){ ans.push_back({'-', 1, v}); } } void bfs_get_vertex(){ queue<pair<int, int>> kolejka; stack<int> vertex; kolejka.push({1, 1}); while(!kolejka.empty()){ auto [v, lvl] = kolejka.front(); kolejka.pop(); if(visited[v] == 2){ continue; } visited[v] = 2; for(auto u: graph_end[v]){ kolejka.push({u, lvl + 1}); } vertex.push(v); } while(!vertex.empty()){ auto v = vertex.top(); vertex.pop(); if(v == 1){ continue; } calculate(v); visited[v] = 3; } } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; int m; cin >> m; for(int i = 0; i < m; i++){ int a, b; cin >> a >> b; graph_begin[a].push_back(b); graph_begin[b].push_back(a); } cin >> m; for(int i = 0; i < m; i++){ int a, b; cin >> a >> b; graph_end[a].push_back(b); graph_end[b].push_back(a); } bfs_init(); bfs_get_vertex(); cout << ans.size() << '\n'; for(auto [sign, v, u]: ans){ cout << sign << ' ' << v << ' ' << u << '\n'; } return 0; } |
English