#include <bits/stdc++.h> #define FOR(i,p,k) for(int i=(p);i<=(k);++i) #define REP(i,n) FOR(i,0,(n)-1) #define ssize(x) (int((x).size())) #define all(x) (x).begin(),(x).end() #define rall(x) (x).rbegin(),(x).rend() #define gc getchar_unlocked #define pc putchar_unlocked #define fi first #define se second using namespace std; typedef pair<int, int> pii; void wczytaj(int &a){ int c = gc(); while(c < '0' || c > '9') c = gc(); for(a = 0; c >= '0' && c <= '9'; c = gc()) a = 10*a+c-'0'; } void wypisz(int a){ vector<char> vec; do{ vec.emplace_back('0'+a%10); } while(a /= 10); for(int i = ssize(vec); ~--i;) pc(vec[i]); } struct operacja{ int a, b, c; }; void solve(){ int n; wczytaj(n); auto xd = [&](){ int m; wczytaj(m); vector<vector<int>> g(n+1); while(m--){ int a, b; wczytaj(a), wczytaj(b); g[a].emplace_back(b); g[b].emplace_back(a); } vector<operacja> ret; vector<int> gl(n+1, 0); queue<int> q; gl[1] = 1, q.emplace(1); while(ssize(q)){ int w = q.front(); q.pop(); if(gl[w] > 2) ret.push_back({1, w, 1}); for(int i : g[w]) if(!gl[i]) gl[i] = gl[w]+1, q.emplace(i); } FOR(i, 1, n) for(int j : g[i]) if(i!=1 && j!=1 && i>j) ret.push_back({i, j, -1}); return ret; }; vector<operacja> wyn = xd(); auto dwa = xd(); reverse(all(dwa)); for(auto [a, b, c] : dwa) wyn.emplace_back(a, b, -c); wypisz(ssize(wyn)), pc('\n'); for(auto [a, b, c] : wyn){ pc(c>0 ? '+' : '-'), pc(' '); wypisz(a), pc(' '); wypisz(b), pc('\n'); } } int main(){ solve(); 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 | #include <bits/stdc++.h> #define FOR(i,p,k) for(int i=(p);i<=(k);++i) #define REP(i,n) FOR(i,0,(n)-1) #define ssize(x) (int((x).size())) #define all(x) (x).begin(),(x).end() #define rall(x) (x).rbegin(),(x).rend() #define gc getchar_unlocked #define pc putchar_unlocked #define fi first #define se second using namespace std; typedef pair<int, int> pii; void wczytaj(int &a){ int c = gc(); while(c < '0' || c > '9') c = gc(); for(a = 0; c >= '0' && c <= '9'; c = gc()) a = 10*a+c-'0'; } void wypisz(int a){ vector<char> vec; do{ vec.emplace_back('0'+a%10); } while(a /= 10); for(int i = ssize(vec); ~--i;) pc(vec[i]); } struct operacja{ int a, b, c; }; void solve(){ int n; wczytaj(n); auto xd = [&](){ int m; wczytaj(m); vector<vector<int>> g(n+1); while(m--){ int a, b; wczytaj(a), wczytaj(b); g[a].emplace_back(b); g[b].emplace_back(a); } vector<operacja> ret; vector<int> gl(n+1, 0); queue<int> q; gl[1] = 1, q.emplace(1); while(ssize(q)){ int w = q.front(); q.pop(); if(gl[w] > 2) ret.push_back({1, w, 1}); for(int i : g[w]) if(!gl[i]) gl[i] = gl[w]+1, q.emplace(i); } FOR(i, 1, n) for(int j : g[i]) if(i!=1 && j!=1 && i>j) ret.push_back({i, j, -1}); return ret; }; vector<operacja> wyn = xd(); auto dwa = xd(); reverse(all(dwa)); for(auto [a, b, c] : dwa) wyn.emplace_back(a, b, -c); wypisz(ssize(wyn)), pc('\n'); for(auto [a, b, c] : wyn){ pc(c>0 ? '+' : '-'), pc(' '); wypisz(a), pc(' '); wypisz(b), pc('\n'); } } int main(){ solve(); return 0; } |