CodeForces-1634E.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main() {
  4. ios::sync_with_stdio(false);
  5. cin.tie(nullptr);
  6. int arrays;
  7. cin >> arrays;
  8. vector<vector<long long>> input(arrays);
  9. vector<long long> all_values;
  10. int edge_count = 0;
  11. for (int i = 0; i < arrays; ++i) {
  12. int length;
  13. cin >> length;
  14. input[i].resize(length);
  15. for (long long &x : input[i]) {
  16. cin >> x;
  17. all_values.push_back(x);
  18. }
  19. edge_count += length;
  20. }
  21. sort(all_values.begin(), all_values.end());
  22. all_values.erase(unique(all_values.begin(), all_values.end()), all_values.end());
  23. int value_count = static_cast<int>(all_values.size());
  24. vector<vector<pair<int, int>>> graph(arrays + value_count);
  25. vector<pair<int, int>> location(edge_count);
  26. vector<int> value_degree(value_count, 0);
  27. int edge = 0;
  28. for (int i = 0; i < arrays; ++i) {
  29. for (int j = 0; j < static_cast<int>(input[i].size()); ++j) {
  30. int value = lower_bound(all_values.begin(), all_values.end(), input[i][j]) - all_values.begin();
  31. int node = arrays + value;
  32. graph[i].push_back({node, edge});
  33. graph[node].push_back({i, edge});
  34. location[edge] = {i, j};
  35. ++value_degree[value];
  36. ++edge;
  37. }
  38. }
  39. for (int degree : value_degree) {
  40. if (degree & 1) {
  41. cout << "NO\n";
  42. return 0;
  43. }
  44. }
  45. vector<string> answer(arrays);
  46. for (int i = 0; i < arrays; ++i) answer[i].assign(input[i].size(), '?');
  47. vector<int> iterator(graph.size(), 0);
  48. vector<char> used(edge_count, false);
  49. for (int source = 0; source < static_cast<int>(graph.size()); ++source) {
  50. if (iterator[source] == static_cast<int>(graph[source].size())) continue;
  51. vector<int> stack{source};
  52. while (!stack.empty()) {
  53. int v = stack.back();
  54. while (iterator[v] < static_cast<int>(graph[v].size())
  55. && used[graph[v][iterator[v]].second]) ++iterator[v];
  56. if (iterator[v] == static_cast<int>(graph[v].size())) {
  57. stack.pop_back();
  58. continue;
  59. }
  60. auto [to, id] = graph[v][iterator[v]++];
  61. if (used[id]) continue;
  62. used[id] = true;
  63. auto [array, position] = location[id];
  64. answer[array][position] = (v < arrays ? 'L' : 'R');
  65. stack.push_back(to);
  66. }
  67. }
  68. cout << "YES\n";
  69. for (const string &line : answer) cout << line << '\n';
  70. return 0;
  71. }