#include <bits/stdc++.h> #define int long long #define pii pair<int,int> #define all(x) x.begin(),x.end() #define vi vector<int> #define vb vector<bool> #define vii vector<pii> #define siz(x) (int)x.size() #define pb push_back #define st first #define nd second #define rep(i,a,b) for(int i=a; i<=b; i++) using namespace std; const int inf = 1e9, maxn = 1e6, mod = 1e9+7; int32_t main(){ ios_base::sync_with_stdio(0);cin.tie(0); int n;cin>>n; int ms;cin>>ms; vector<set<int>>graf1(n+1); rep(i,1,ms){ int a,b; cin>>a>>b; graf1[a].insert(b); graf1[b].insert(a); } set<pair<int,int>>niema; rep(a,1,n){ rep(b,a+1,n){ if(!graf1[a].count(b)) niema.insert({a,b}); } } vii dod; rep(i,1,n+1){ vii ers; for(auto [a,b] : niema){ for(auto w : graf1[b])if(graf1[a].count(w)){ ers.pb({a,b}); graf1[a].insert(b); graf1[b].insert(a); dod.pb({a,b}); break; } } for(auto [a,b] : ers)niema.erase({a,b}); } int md;cin>>md; vector<set<int>>graf2(n+1); rep(i,1,md){ int a,b; cin>>a>>b; graf2[a].insert(b); graf2[b].insert(a); } vector<pair<int,pair<int,int>>> dousuniecia; rep(a,1,n){ vi odl(n+1,inf); queue<int>Q; Q.push(a); odl[a] = 0; while(siz(Q)){ int b = Q.front(); Q.pop(); for(auto w : graf2[b])if(odl[w] == inf){ odl[w] = odl[b]+1; Q.push(w); } } rep(b,a+1,n){ if(odl[b]>1){ dousuniecia.pb({odl[b],{a,b}}); } } } sort(all(dousuniecia));reverse(all(dousuniecia)); cout<<siz(dod)+siz(dousuniecia)<<'\n'; for(auto [a,b] : dod){ cout<<"+ "<<a<<' '<<b<<'\n'; } for(auto [d,x] : dousuniecia){ cout<<"- "<<x.st<<' '<<x.nd<<'\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 | #include <bits/stdc++.h> #define int long long #define pii pair<int,int> #define all(x) x.begin(),x.end() #define vi vector<int> #define vb vector<bool> #define vii vector<pii> #define siz(x) (int)x.size() #define pb push_back #define st first #define nd second #define rep(i,a,b) for(int i=a; i<=b; i++) using namespace std; const int inf = 1e9, maxn = 1e6, mod = 1e9+7; int32_t main(){ ios_base::sync_with_stdio(0);cin.tie(0); int n;cin>>n; int ms;cin>>ms; vector<set<int>>graf1(n+1); rep(i,1,ms){ int a,b; cin>>a>>b; graf1[a].insert(b); graf1[b].insert(a); } set<pair<int,int>>niema; rep(a,1,n){ rep(b,a+1,n){ if(!graf1[a].count(b)) niema.insert({a,b}); } } vii dod; rep(i,1,n+1){ vii ers; for(auto [a,b] : niema){ for(auto w : graf1[b])if(graf1[a].count(w)){ ers.pb({a,b}); graf1[a].insert(b); graf1[b].insert(a); dod.pb({a,b}); break; } } for(auto [a,b] : ers)niema.erase({a,b}); } int md;cin>>md; vector<set<int>>graf2(n+1); rep(i,1,md){ int a,b; cin>>a>>b; graf2[a].insert(b); graf2[b].insert(a); } vector<pair<int,pair<int,int>>> dousuniecia; rep(a,1,n){ vi odl(n+1,inf); queue<int>Q; Q.push(a); odl[a] = 0; while(siz(Q)){ int b = Q.front(); Q.pop(); for(auto w : graf2[b])if(odl[w] == inf){ odl[w] = odl[b]+1; Q.push(w); } } rep(b,a+1,n){ if(odl[b]>1){ dousuniecia.pb({odl[b],{a,b}}); } } } sort(all(dousuniecia));reverse(all(dousuniecia)); cout<<siz(dod)+siz(dousuniecia)<<'\n'; for(auto [a,b] : dod){ cout<<"+ "<<a<<' '<<b<<'\n'; } for(auto [d,x] : dousuniecia){ cout<<"- "<<x.st<<' '<<x.nd<<'\n'; } } |