fork(4) download
  1. #include <bits/stdc++.h>
  2.  
  3. #define clr(x) memset((x), 0, sizeof(x))
  4. #define all(x) (x).begin(), (x).end()
  5. #define pb push_back
  6. #define mp make_pair
  7. #define For(i, st, en) for(int i=(st); i<=(int)(en); i++)
  8. #define Ford(i, st, en) for(int i=(st); i>=(int)(en); i--)
  9. #define forn(i, n) for(int i=0; i<(int)(n); i++)
  10. #define ford(i, n) for(int i=(n)-1; i>=0; i--)
  11. #define fori(it, x) for (__typeof((x).begin()) it = (x).begin(); it != (x).end(); it++)
  12. #define in(x) int (x); input((x));
  13. #define x first
  14. #define y second
  15. #define less(a,b) ((a) < (b) - EPS)
  16. #define more(a,b) ((a) > (b) + EPS)
  17. #define eq(a,b) (fabs((a) - (b)) < EPS)
  18. #define remax(a, b) ((a) = (b) > (a) ? (b) : (a))
  19. #define remin(a, b) ((a) = (b) < (a) ? (b) : (a))
  20.  
  21. using namespace std;
  22.  
  23. template <typename T>
  24. T gcd(T a, T b) {
  25. while (b > 0) {
  26. a %= b;
  27. swap(a, b);
  28. }
  29. return a;
  30. }
  31.  
  32. typedef long double ld; typedef unsigned int uint; template <class _T> inline _T sqr(const _T& x) {return x * x;} template <class _T> inline string tostr(const _T& a) {ostringstream os(""); os << a; return os.str();} const ld PI = 3.1415926535897932384626433832795L; const double EPS = 1e-9; char TEMPORARY_CHAR;
  33.  
  34. typedef long long ll; typedef unsigned long long ull; typedef set < int > SI; typedef vector < int > VI; typedef vector < vector < int > > VVI; typedef map < string, int > MSI; typedef pair < int, int > PII; const int INF = 1e9; inline void input(int &a) {a = 0; while (((TEMPORARY_CHAR = getchar()) > '9' || TEMPORARY_CHAR < '0') && (TEMPORARY_CHAR != '-')){} char neg = 0; if (TEMPORARY_CHAR == '-') {neg = 1; TEMPORARY_CHAR = getchar();} while (TEMPORARY_CHAR <= '9' && TEMPORARY_CHAR >= '0') {a = 10 * a + TEMPORARY_CHAR - '0'; TEMPORARY_CHAR = getchar();} if (neg) a = -a;} inline void out(long long a) {if (!a) putchar('0'); if (a < 0) {putchar('-'); a = -a;} char s[20]; int i; for(i = 0; a; ++i) {s[i] = '0' + a % 10; a /= 10;} ford(j, i) putchar(s[j]);} inline int nxt() {in(ret); return ret;}
  35.  
  36. int main() {
  37. #ifdef LOCAL
  38. freopen("input.txt", "r", stdin);
  39. #else
  40.  
  41. #endif
  42. ios_base::sync_with_stdio(false);
  43.  
  44. int q;
  45. cin >> q;
  46.  
  47. map <string, string> edges;
  48.  
  49. set <string> nonBegin;
  50.  
  51. for (int i = 0; i < q; ++i) {
  52. string OLD, NEW;
  53. cin >> OLD >> NEW;
  54. edges[OLD] = NEW;
  55. nonBegin.insert(NEW);
  56. }
  57. vector <pair <string, string> > ans;
  58.  
  59. fori(p,edges) {
  60. if (nonBegin.count(p->first)) {
  61. continue;
  62. }
  63. string cur = p->first;
  64.  
  65. while (edges.count(cur)) {
  66. cur = edges[cur];
  67. }
  68.  
  69. ans.push_back(make_pair(p->first, cur));
  70. }
  71.  
  72. cout << ans.size() << endl;
  73.  
  74. fori(p, ans) {
  75. cout << p->first << " " << p->second << endl;
  76. }
  77.  
  78. #ifdef LOCAL
  79. cerr << "Time elapsed: " << (double)clock() / CLOCKS_PER_SEC * 1000 << " ms.\n";
  80. #endif // LOCAL
  81. return 0;
  82. }
Success #stdin #stdout 0s 3288KB
stdin
Standard input is empty
stdout
0