#include<bits/stdc++.h> using namespace std; #define pb push_back #define eb emplace_back //mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); #define mod 998244353 #define ll long long #define inf 0x3f3f3f3f #define INF 0x3f3f3f3f3f3f3f3f inline int read() { char ch=getchar(); int nega=1; while(!isdigit(ch)) {if(ch=='-') nega=-1; ch=getchar();} int ans=0; while(isdigit(ch)) {ans=ans*10+ch-48;ch=getchar();} if(nega==-1) return -ans; return ans; } void print(vector<int> x){for(int i=0;i<(int)x.size();i++) printf("%d%c",x[i]," \n"[i==(int)x.size()-1]);} #define N 30005 int n; int dep[N]; vector<int> G[N]; struct Node { int op,u,v; Node(int a=0,int b=0,int c=0) {op=a,u=b,v=c;} }; vector<Node> ans; vector<Node> get() { int m; cin>>m; for(int i=1;i<=n;i++) G[i].clear(); ans.clear(); for(int i=1;i<=n;i++) dep[i]=-1; for(int i=1;i<=m;i++) { int u=read(),v=read(); G[u].push_back(v),G[v].push_back(u); } queue<int> q; q.push(1),dep[1]=1; while(!q.empty()) { int u=q.front(); q.pop(); for(int v:G[u]) { if(dep[v]==-1) { if(u!=1) { ans.emplace_back(1,1,v); ans.emplace_back(-1,u,v); } dep[v]=dep[u]+1,q.push(v); } else if(dep[v]==dep[u]) { if(u<v) ans.emplace_back(-1,u,v); } else if(dep[v]>dep[u]) ans.emplace_back(-1,u,v); } } return ans; } signed main() { cin>>n; vector<Node> ans1=get(); vector<Node> ans2=get(); reverse(ans2.begin(),ans2.end()); cout<<ans1.size()+ans2.size()<<endl; for(auto [op,u,v]:ans1) { if(op==1) printf("+ %d %d\n",u,v); else printf("- %d %d\n",u,v); } for(auto [op,u,v]:ans2) { if(op==1) printf("- %d %d\n",u,v); else printf("+ %d %d\n",u,v); } 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 | #include<bits/stdc++.h> using namespace std; #define pb push_back #define eb emplace_back //mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); #define mod 998244353 #define ll long long #define inf 0x3f3f3f3f #define INF 0x3f3f3f3f3f3f3f3f inline int read() { char ch=getchar(); int nega=1; while(!isdigit(ch)) {if(ch=='-') nega=-1; ch=getchar();} int ans=0; while(isdigit(ch)) {ans=ans*10+ch-48;ch=getchar();} if(nega==-1) return -ans; return ans; } void print(vector<int> x){for(int i=0;i<(int)x.size();i++) printf("%d%c",x[i]," \n"[i==(int)x.size()-1]);} #define N 30005 int n; int dep[N]; vector<int> G[N]; struct Node { int op,u,v; Node(int a=0,int b=0,int c=0) {op=a,u=b,v=c;} }; vector<Node> ans; vector<Node> get() { int m; cin>>m; for(int i=1;i<=n;i++) G[i].clear(); ans.clear(); for(int i=1;i<=n;i++) dep[i]=-1; for(int i=1;i<=m;i++) { int u=read(),v=read(); G[u].push_back(v),G[v].push_back(u); } queue<int> q; q.push(1),dep[1]=1; while(!q.empty()) { int u=q.front(); q.pop(); for(int v:G[u]) { if(dep[v]==-1) { if(u!=1) { ans.emplace_back(1,1,v); ans.emplace_back(-1,u,v); } dep[v]=dep[u]+1,q.push(v); } else if(dep[v]==dep[u]) { if(u<v) ans.emplace_back(-1,u,v); } else if(dep[v]>dep[u]) ans.emplace_back(-1,u,v); } } return ans; } signed main() { cin>>n; vector<Node> ans1=get(); vector<Node> ans2=get(); reverse(ans2.begin(),ans2.end()); cout<<ans1.size()+ans2.size()<<endl; for(auto [op,u,v]:ans1) { if(op==1) printf("+ %d %d\n",u,v); else printf("- %d %d\n",u,v); } for(auto [op,u,v]:ans2) { if(op==1) printf("- %d %d\n",u,v); else printf("+ %d %d\n",u,v); } return 0; } |