Codeforces Round #554 Div.2 E - Neko and Flashback

逃离我推掉我的手 2021-11-08 17:20 357阅读 0赞

欧拉路径

神题啊神题!这道题的突破口就是后两个数组每个元素是一一对应的。

也就是说,对于一个p的排列,b’和c’取得每一个元素的下标在p中都是一样的。

根据b和c数组的性质可以得出,b[i] <= c[i]。

这也是我们输出-1的一个判断方法。

再来看b和c两个数组,他们是由原数组相邻两个数分别取min和max构造出来的,很容易看出b’和c’同一个位置的两个数一定在原数组中相邻。

我们可以根据这个相邻关系建图,a若与b在原数组中相邻,就连一条无向边,那么最后我们要构造的合法数组即为该无向图的一条欧拉路径。

因为数据比较大,所以我们要先离散化,再考虑有重边,我们可以用multiset来存图,跑过的边直接删除就行了。

  1. #include <bits/stdc++.h>
  2. #define INF 0x3f3f3f3f
  3. #define full(a, b) memset(a, b, sizeof a)
  4. using namespace std;
  5. typedef long long ll;
  6. inline int lowbit(int x){ return x & (-x); }
  7. inline int read(){
  8. int X = 0, w = 0; char ch = 0;
  9. while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
  10. while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
  11. return w ? -X : X;
  12. }
  13. inline int gcd(int a, int b){ return a % b ? gcd(b, a % b) : b; }
  14. inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
  15. template<typename T>
  16. inline T max(T x, T y, T z){ return max(max(x, y), z); }
  17. template<typename T>
  18. inline T min(T x, T y, T z){ return min(min(x, y), z); }
  19. template<typename A, typename B, typename C>
  20. inline A fpow(A x, B p, C lyd){
  21. A ans = 1;
  22. for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
  23. return ans;
  24. }
  25. const int N = 200005;
  26. int n, k, b[N], c[N], p[N], d[N];
  27. vector<int> rd;
  28. multiset<int> g[N];
  29. void dfs(int s){
  30. for(auto it = g[s].begin(); it != g[s].end(); it = g[s].begin()){
  31. int u = *it;
  32. g[s].erase(it), g[u].erase(g[u].lower_bound(s));
  33. dfs(u);
  34. }
  35. rd.push_back(s);
  36. }
  37. int main(){
  38. ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  39. n = read();
  40. for(int i = 1; i < n; i ++) b[i] = read(), p[++k] = b[i];
  41. for(int i = 1; i < n; i ++) c[i] = read(), p[++k] = c[i];
  42. for(int i = 1; i < n; i ++){
  43. if(b[i] > c[i]){
  44. printf("-1\n");
  45. return 0;
  46. }
  47. }
  48. sort(p + 1, p + k + 1);
  49. k = (int)(unique(p + 1, p + k + 1) - p - 1);
  50. for(int i = 1; i < n; i ++){
  51. b[i] = (int)(lower_bound(p + 1, p + k + 1, b[i]) - p);
  52. c[i] = (int)(lower_bound(p + 1, p + k + 1, c[i]) - p);
  53. }
  54. for(int i = 1; i < n; i ++){
  55. g[b[i]].insert(c[i]), g[c[i]].insert(b[i]);
  56. d[b[i]] ++, d[c[i]] ++;
  57. }
  58. int t = 0;
  59. for(int i = 1; i <= k; i ++){
  60. if(d[i] & 1) t ++;
  61. }
  62. if(t != 0 && t != 2){
  63. cout << "-1" << endl;
  64. return 0;
  65. }
  66. bool odd = false;
  67. for(int i = 1; i <= k; i ++){
  68. if(d[i] & 1){
  69. odd = true, dfs(i);
  70. break;
  71. }
  72. }
  73. if(!odd) dfs(1);
  74. if(rd.size() != n) cout << "-1" << endl;
  75. else{
  76. for(int i = rd.size() - 1; i >= 0; i --){
  77. cout << p[rd[i]];
  78. if(i != 0) cout << " ";
  79. }
  80. puts("");
  81. }
  82. return 0;
  83. }

转载于:https://www.cnblogs.com/onionQAQ/p/10820877.html

发表评论

表情:
评论列表 (有 0 条评论,357人围观)

还没有评论,来说两句吧...

相关阅读