#include<bits/stdc++.h>
using namespace std;
const int maxn = 30000;
bitset<maxn*maxn>adj;
bitset<maxn*maxn>goal;
vector<vector<int>>adjj;
vector<vector<int>>goall;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
int m;
cin >> m;
adjj.resize(n);
goall.resize(n);
for(int i = 0; i < m; i++){
int a, b;
cin >> a >> b;
a--; b--;
adj.set(maxn*a+b);
adj.set(maxn*b+a);
adjj[a].push_back(b);
adjj[b].push_back(a);
}
cin >> m;
for(int i = 0; i < m; i++){
int a, b;
cin >> a >> b;
a--; b--;
goal.set(maxn*a+b);
goal.set(maxn*b+a);
goall[a].push_back(b);
goall[b].push_back(a);
}
queue<int>q;
for(int x : adjj[0])q.push(x);
for(int i = 1; i < n; i++){
if(adj[i])q.push(i);
}
vector<pair<char, pair<int,int>>>last;
vector<pair<char, pair<int,int>>>ans;
while(!q.empty()){
int x = q.front();
q.pop();
if(!adj[maxn*x]){
ans.push_back({'+', {1, x+1}});
adjj[x].push_back(0);
adjj[0].push_back(x);
adj.set(maxn*x);
adj.set(x);
}
for(int u : adjj[x]){
if(u == 0)continue;
if(!adj[u])q.push(u);
}
}
for(int x : goall[0])q.push(x);
while(!q.empty()){
int x = q.front();
q.pop();
if(!goal[x*maxn]){
last.push_back({'-', {1, x+1}});
goal[x*maxn] = true;
goal[x] = true;
goall[x].push_back(0);
goall[0].push_back(x);
}
for(int u : goall[x]){
if(u == 0)continue;
if(!goal[u])q.push(u);
}
}
reverse(last.begin(), last.end());
for(int i = 0; i < n; i++){
for(int j = i + 1; j < n; j++){
if(goal[i*maxn+j] && !adj[i*maxn+j]){
ans.push_back({'+', {i+1, j+1}});
adj[i*maxn+j] = true;
adj[j*maxn+i] = true;
}
if(!goal[i*maxn+j] && adj[i*maxn+j]){
ans.push_back({'-', {i+1, j+1}});
adj[i*maxn+j] = true;
adj[j*maxn+i] = true;
}
}
}
for(auto x : last)ans.push_back(x);
cout << ans.size() << '\n';
for(auto p : ans){
cout << p.first << ' ' << p.second.first << ' ' << p.second.second << '\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 | #include<bits/stdc++.h> using namespace std; const int maxn = 30000; bitset<maxn*maxn>adj; bitset<maxn*maxn>goal; vector<vector<int>>adjj; vector<vector<int>>goall; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; int m; cin >> m; adjj.resize(n); goall.resize(n); for(int i = 0; i < m; i++){ int a, b; cin >> a >> b; a--; b--; adj.set(maxn*a+b); adj.set(maxn*b+a); adjj[a].push_back(b); adjj[b].push_back(a); } cin >> m; for(int i = 0; i < m; i++){ int a, b; cin >> a >> b; a--; b--; goal.set(maxn*a+b); goal.set(maxn*b+a); goall[a].push_back(b); goall[b].push_back(a); } queue<int>q; for(int x : adjj[0])q.push(x); for(int i = 1; i < n; i++){ if(adj[i])q.push(i); } vector<pair<char, pair<int,int>>>last; vector<pair<char, pair<int,int>>>ans; while(!q.empty()){ int x = q.front(); q.pop(); if(!adj[maxn*x]){ ans.push_back({'+', {1, x+1}}); adjj[x].push_back(0); adjj[0].push_back(x); adj.set(maxn*x); adj.set(x); } for(int u : adjj[x]){ if(u == 0)continue; if(!adj[u])q.push(u); } } for(int x : goall[0])q.push(x); while(!q.empty()){ int x = q.front(); q.pop(); if(!goal[x*maxn]){ last.push_back({'-', {1, x+1}}); goal[x*maxn] = true; goal[x] = true; goall[x].push_back(0); goall[0].push_back(x); } for(int u : goall[x]){ if(u == 0)continue; if(!goal[u])q.push(u); } } reverse(last.begin(), last.end()); for(int i = 0; i < n; i++){ for(int j = i + 1; j < n; j++){ if(goal[i*maxn+j] && !adj[i*maxn+j]){ ans.push_back({'+', {i+1, j+1}}); adj[i*maxn+j] = true; adj[j*maxn+i] = true; } if(!goal[i*maxn+j] && adj[i*maxn+j]){ ans.push_back({'-', {i+1, j+1}}); adj[i*maxn+j] = true; adj[j*maxn+i] = true; } } } for(auto x : last)ans.push_back(x); cout << ans.size() << '\n'; for(auto p : ans){ cout << p.first << ' ' << p.second.first << ' ' << p.second.second << '\n'; } return 0; } |
English