#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
vector<pair<int, int>>ans;
void solve(vector<vector<int>> &pol, bool fl)
{
int n=pol.size();
vector<pair<int, int>>add, ers;
queue<int>kol;
vector<char>odw(n);
odw[1]=1;
for (auto i : pol[1])
{
kol.push(i);
odw[i]=1;
}
while (!kol.empty())
{
int f=kol.front();
kol.pop();
for (auto i : pol[f])
{
if (!odw[i])
{
odw[i]=1;
kol.push(i);
add.push_back({fl? -1 : 1, i});
}
if (i != 1 && f > i) ers.push_back({fl? f : -f, i});
}
}
if (!fl)
{
ans.insert(ans.end(), add.begin(), add.end());
ans.insert(ans.end(), ers.begin(), ers.end());
}
if (fl)
{
ans.insert(ans.end(), ers.begin(), ers.end());
reverse(add.begin(), add.end());
ans.insert(ans.end(), add.begin(), add.end());
}
}
//~ int los(int a, int b, mt19937 &rng)
//~ {
//~ if (a == b) return a;
//~ return rng()%(b-a)+a;
//~ }
//~ bool check(int a, int b, vector<set<int>>&pol)
//~ {
//~ for (auto i : pol[a])
//~ {
//~ if (pol[i].find(b) != pol[i].end()) return true;
//~ }
//~ return false;
//~ }
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n; cin>>n;
vector<vector<int>>a(n+1), b(n+1);
int m; cin>>m;
for (int i=0; i<m; i++)
{
int x, y; cin>>x>>y;
//~ int x, y;
//~ if (i<n-1) x=los(1, i+1, rng), y=i+2;
//~ else x=los(1, n, rng), y=los(1, n, rng);
a[x].push_back(y);
a[y].push_back(x);
}
cin>>m;
//~ m=n-1+rng()%100;
for (int i=0; i<m; i++)
{
int x, y; cin>>x>>y;
//~ int x, y;
//~ if (i<n-1) x=los(1, i+1, rng), y=i+2;
//~ else x=los(1, n, rng), y=los(1, n, rng);
b[x].push_back(y);
b[y].push_back(x);
}
solve(a, 0);
solve(b, 1);
cout<<ans.size()<<"\n";
//~ vector<set<int>>pol(n+1);
//~ for (int i=1; i<=n; i++)
//~ {
//~ pol[i].insert(a[i].begin(), a[i].end());
//~ }
for (auto i : ans)
{
if (i.f > 0)
{
cout<<"+ "<<i.f<<" "<<i.s<<"\n";
//~ if (!check(i.f, i.s, pol))
//~ {
//~ cout<<"BLAD\n";
//~ return 0;
//~ }
//~ pol[i.f].insert(i.s);
//~ pol[i.s].insert(i.f);
}
else
{
cout<<"- "<<-i.f<<" "<<i.s<<"\n";
//~ if (!check(-i.f, i.s, pol))
//~ {
//~ cout<<"BLAD\n";
//~ return 0;
//~ }
//~ pol[-i.f].erase(i.s);
//~ pol[i.s].erase(-i.f);
}
}
//~ cout<<"GIT "<<argv[1]<<endl;
}
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 | #include <bits/stdc++.h> #define f first #define s second using namespace std; vector<pair<int, int>>ans; void solve(vector<vector<int>> &pol, bool fl) { int n=pol.size(); vector<pair<int, int>>add, ers; queue<int>kol; vector<char>odw(n); odw[1]=1; for (auto i : pol[1]) { kol.push(i); odw[i]=1; } while (!kol.empty()) { int f=kol.front(); kol.pop(); for (auto i : pol[f]) { if (!odw[i]) { odw[i]=1; kol.push(i); add.push_back({fl? -1 : 1, i}); } if (i != 1 && f > i) ers.push_back({fl? f : -f, i}); } } if (!fl) { ans.insert(ans.end(), add.begin(), add.end()); ans.insert(ans.end(), ers.begin(), ers.end()); } if (fl) { ans.insert(ans.end(), ers.begin(), ers.end()); reverse(add.begin(), add.end()); ans.insert(ans.end(), add.begin(), add.end()); } } //~ int los(int a, int b, mt19937 &rng) //~ { //~ if (a == b) return a; //~ return rng()%(b-a)+a; //~ } //~ bool check(int a, int b, vector<set<int>>&pol) //~ { //~ for (auto i : pol[a]) //~ { //~ if (pol[i].find(b) != pol[i].end()) return true; //~ } //~ return false; //~ } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin>>n; vector<vector<int>>a(n+1), b(n+1); int m; cin>>m; for (int i=0; i<m; i++) { int x, y; cin>>x>>y; //~ int x, y; //~ if (i<n-1) x=los(1, i+1, rng), y=i+2; //~ else x=los(1, n, rng), y=los(1, n, rng); a[x].push_back(y); a[y].push_back(x); } cin>>m; //~ m=n-1+rng()%100; for (int i=0; i<m; i++) { int x, y; cin>>x>>y; //~ int x, y; //~ if (i<n-1) x=los(1, i+1, rng), y=i+2; //~ else x=los(1, n, rng), y=los(1, n, rng); b[x].push_back(y); b[y].push_back(x); } solve(a, 0); solve(b, 1); cout<<ans.size()<<"\n"; //~ vector<set<int>>pol(n+1); //~ for (int i=1; i<=n; i++) //~ { //~ pol[i].insert(a[i].begin(), a[i].end()); //~ } for (auto i : ans) { if (i.f > 0) { cout<<"+ "<<i.f<<" "<<i.s<<"\n"; //~ if (!check(i.f, i.s, pol)) //~ { //~ cout<<"BLAD\n"; //~ return 0; //~ } //~ pol[i.f].insert(i.s); //~ pol[i.s].insert(i.f); } else { cout<<"- "<<-i.f<<" "<<i.s<<"\n"; //~ if (!check(-i.f, i.s, pol)) //~ { //~ cout<<"BLAD\n"; //~ return 0; //~ } //~ pol[-i.f].erase(i.s); //~ pol[i.s].erase(-i.f); } } //~ cout<<"GIT "<<argv[1]<<endl; } |
English