Advertisement
7oSkaaa

G

Jul 2nd, 2022
743
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define cin_2d(vec, n, m) for(int i = 0; i < n; i++) for(int j = 0; j < m && cin >> vec[i][j]; j++);
  6. #define cout_2d(vec, n, m) for(int i = 0; i < n; i++, cout << "\n") for(int j = 0; j < m && cout << vec[i][j] << " "; j++);
  7. #define cout_map(mp) for(auto& [f, s] : mp) cout << f << "  " << s << "\n";
  8. #define Time cerr << "Time Taken: " << (float)clock() / CLOCKS_PER_SEC << " Secs" << "\n";
  9. #define fixed(n) fixed << setprecision(n)
  10. #define ceil(n, m) (((n) / (m)) + ((n) % (m) ? 1 : 0))
  11. #define fill(vec, value) memset(vec, value, sizeof(vec));
  12. #define Num_of_Digits(n) ((int)log10(n) + 1)
  13. #define mod_combine(a, b, m) (((a % m) * (b % m)) % m)
  14. #define all(vec) vec.begin(), vec.end()
  15. #define rall(vec) vec.rbegin(), vec.rend()
  16. #define sz(x) int(x.size())
  17. #define debug(x) cout << #x << ": " << (x) << "\n";
  18. #define fi first
  19. #define se second
  20. #define ll long long
  21. #define ull unsigned long long
  22. #define Mod  1'000'000'007
  23. #define OO 2'000'000'000
  24. #define EPS 1e-9
  25. #define PI acos(-1)
  26. template < typename T = int > using Pair = pair < T, T >;
  27.  
  28. template < typename T = int > istream& operator >> (istream &in, vector < T > &v) {
  29.     for (auto &x: v) in >> x;
  30.     return in;
  31. }
  32.  
  33. template < typename T = int > ostream& operator << (ostream &out, const vector < T > &v) {
  34.     for (const T &x: v) out << x << ' ';
  35.     return out;
  36. }
  37.  
  38. void AhMeD_HoSSaM(){
  39.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  40.     #ifndef ONLINE_JUDGE
  41.         freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
  42.     #endif
  43. }
  44.  
  45. constexpr int N = 1e5, H = 128;
  46. int n;
  47. vector < vector < int > > adj;
  48. vector < int > deg;
  49. string res;
  50.  
  51. int Hash(char a, char b){
  52.     return a * H + b;
  53. }
  54.  
  55. void dfs(int u){
  56.     int v;
  57.     while(!adj[u].empty()){
  58.         v = adj[u].back();
  59.         adj[u].pop_back();
  60.         dfs(v);
  61.     }
  62.     res += char(u % H);
  63. }
  64.  
  65. void Solve(){
  66.     cin >> n;
  67.     adj = vector < vector < int > > (N);
  68.     deg = vector < int > (N);
  69.     vector < string > words(n - 2);
  70.     set < int > keys;
  71.     int Root = 0, Degrees = 0, Minus_Degrees = 0;
  72.     for(auto& s : words){
  73.         cin >> s;
  74.         int u = Hash(s[0], s[1]), v = Hash(s[1], s[2]);
  75.         adj[u].push_back(v), deg[v]--, deg[u]++;
  76.         keys.insert(u), keys.insert(v), Root = u;
  77.     }
  78.     for(auto& k : keys){
  79.         if(deg[k] > 0)
  80.             Root = k, Degrees += deg[k];
  81.         else if(deg[k] < 0)
  82.             Minus_Degrees += deg[k];
  83.     }
  84.     dfs(Root);
  85.     res += char(Root / H);
  86.     reverse(all(res));
  87.     if(sz(res) != n || Degrees > 1 || Minus_Degrees < -1)
  88.         return cout << "NO\n", void();
  89.     cout << "YES\n";
  90.     cout << res << '\n';
  91. }
  92.  
  93. int main(){
  94.     AhMeD_HoSSaM();
  95.     int t = 1;
  96.     //cin >> t;
  97.     while(t--)
  98.         Solve();
  99.     return 0;
  100. }
Advertisement
RAW Paste Data Copied
Advertisement