#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double db;
typedef pair<int,int> pii;
typedef vector<int> vi;
#define pb push_back
#define fi first
#define se second
#define rep(i,b,e) for(int i=(b); i<(e); i++)
#define each(a,x) for(auto &a : (x))
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
template <class T, class U> auto &operator<<(ostream &out, pair<T, U> a) { return out << "(" << a.first << ", " << a.second << ")"; }
template <class T, class = class enable_if<!is_same<T, string>(), class T::iterator>::type> auto &operator<<(ostream &out, T a) {
out << "{";
bool fi = 1;
for(auto b : a) {
if(fi) {out<<b; fi=0;}
else out<<", "<<b;
}
return out << "}";
}
void dump(){
cerr << "\e"<<"\n";
}
template <class T, class... Ts> void dump(T a, Ts... x) {
cerr << a << ", ";
dump(x...);
}
#define debug(...) cerr << "\e"<<__func__<<":"<<__LINE__<<"\t"<<"[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__)
//#define debug(...) ;
const int N = 3e4+10;
vector<vi> g[2];
int dist[2][N];
void dfs(int v, int d, int num){
dist[num][v] = d;
each(u,g[num][v])
if(dist[num][u]==-1)
dfs(u,d+1,num);
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n,m[2];
cin >> n;
set<pii> kraw[2];
vector<pair<char,pii>> wynik;
rep(u,0,2){
g[u].resize(N);
cin >> m[u];
rep(i,0,m[u]){
int a,b;
cin >> a >> b;
g[u][a].pb(b);
g[u][b].pb(a);
if(a > b) swap(a,b);
kraw[u].insert({a,b});
}
}
rep(u,0,2){
rep(i,0,n+5)
dist[u][i] = -1;
dfs(1,0,u);
}
vi toVisit;
for(int i=2; i<=n; ++i) toVisit.pb(i);
sort(all(toVisit), [&](const int &a, const int &b){ return dist[0][a]<=dist[0][b];});
// Robimy pajeczyne
each(v,toVisit){
// cout << "V: " << v << " dist:" << dist[0][v] << endl;;
if(kraw[0].find({1,v})==kraw[0].end())
wynik.pb({'+',{1,v}});
}
// Odpinamy gosci starych (a wszystkich, czemu nie)
for(auto &&[a,b] : kraw[0])
if(a!=1)
wynik.pb({'-',{a,b}});
// Podpinamy gosci nowych
for(auto &&[a,b] : kraw[1])
if(a!=1)
wynik.pb({'+',{a,b}});
// Usuwamy pajeczynke
sort(all(toVisit), [&](const int &a, const int &b){ return dist[1][a]>=dist[1][b];});
each(v,toVisit)
if(kraw[1].find({1,v})==kraw[1].end())
wynik.pb({'-',{1,v}});
cout << wynik.size() << "\n";
for(auto &[c,ab] : wynik){
auto &[a,b] = ab;
cout << c << " " << a << " " << b << '\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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double db; typedef pair<int,int> pii; typedef vector<int> vi; #define pb push_back #define fi first #define se second #define rep(i,b,e) for(int i=(b); i<(e); i++) #define each(a,x) for(auto &a : (x)) #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() template <class T, class U> auto &operator<<(ostream &out, pair<T, U> a) { return out << "(" << a.first << ", " << a.second << ")"; } template <class T, class = class enable_if<!is_same<T, string>(), class T::iterator>::type> auto &operator<<(ostream &out, T a) { out << "{"; bool fi = 1; for(auto b : a) { if(fi) {out<<b; fi=0;} else out<<", "<<b; } return out << "}"; } void dump(){ cerr << "\e"<<"\n"; } template <class T, class... Ts> void dump(T a, Ts... x) { cerr << a << ", "; dump(x...); } #define debug(...) cerr << "\e"<<__func__<<":"<<__LINE__<<"\t"<<"[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__) //#define debug(...) ; const int N = 3e4+10; vector<vi> g[2]; int dist[2][N]; void dfs(int v, int d, int num){ dist[num][v] = d; each(u,g[num][v]) if(dist[num][u]==-1) dfs(u,d+1,num); } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n,m[2]; cin >> n; set<pii> kraw[2]; vector<pair<char,pii>> wynik; rep(u,0,2){ g[u].resize(N); cin >> m[u]; rep(i,0,m[u]){ int a,b; cin >> a >> b; g[u][a].pb(b); g[u][b].pb(a); if(a > b) swap(a,b); kraw[u].insert({a,b}); } } rep(u,0,2){ rep(i,0,n+5) dist[u][i] = -1; dfs(1,0,u); } vi toVisit; for(int i=2; i<=n; ++i) toVisit.pb(i); sort(all(toVisit), [&](const int &a, const int &b){ return dist[0][a]<=dist[0][b];}); // Robimy pajeczyne each(v,toVisit){ // cout << "V: " << v << " dist:" << dist[0][v] << endl;; if(kraw[0].find({1,v})==kraw[0].end()) wynik.pb({'+',{1,v}}); } // Odpinamy gosci starych (a wszystkich, czemu nie) for(auto &&[a,b] : kraw[0]) if(a!=1) wynik.pb({'-',{a,b}}); // Podpinamy gosci nowych for(auto &&[a,b] : kraw[1]) if(a!=1) wynik.pb({'+',{a,b}}); // Usuwamy pajeczynke sort(all(toVisit), [&](const int &a, const int &b){ return dist[1][a]>=dist[1][b];}); each(v,toVisit) if(kraw[1].find({1,v})==kraw[1].end()) wynik.pb({'-',{1,v}}); cout << wynik.size() << "\n"; for(auto &[c,ab] : wynik){ auto &[a,b] = ab; cout << c << " " << a << " " << b << '\n'; } return 0; } |
English