Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int kMaxN = 5e5 + 7;
  6.  
  7. vector<int> adj[kMaxN];
  8. set<int> adj2[kMaxN];
  9. pair<int, int> degree[kMaxN];
  10. vector<pair<int, int> > adj3[kMaxN];
  11. int par[kMaxN];
  12. vector<pair<int, int> > edges;
  13. int pos[kMaxN];
  14.  
  15. bool cmp(pair<int, int> lvalue, pair<int, int> rvalue){
  16. if(lvalue.first != rvalue.first)
  17. return lvalue.first < rvalue.first;
  18.  
  19. return lvalue.second > rvalue.second;
  20. }
  21.  
  22. void solve(){
  23. int n, m;
  24. cin >> n >> m;
  25.  
  26. for(int i = 1; i <= n; ++i){
  27. adj[i].clear();
  28. adj2[i].clear();
  29. adj3[i].clear();
  30. }
  31.  
  32. edges.clear();
  33. for(int i = 1; i <= m; ++i){
  34. int u, v;
  35. cin >> u >> v;
  36. edges.push_back({u, v});
  37.  
  38. adj[u].push_back(v);
  39. adj[v].push_back(u);
  40. }
  41.  
  42. for(int i = 1; i <= n; ++i)
  43. degree[i] = {adj[i].size(), i};
  44.  
  45. sort(degree + 1, degree + 1 + n);
  46. reverse(degree + 1, degree + 1 + n);
  47.  
  48. for(int i = 1; i <= n; ++i)
  49. pos[degree[i].second] = i;
  50.  
  51. for(pair<int, int> edge: edges){
  52. int u = edge.first, v = edge.second;
  53. //cout << u << " " << v << " edge" << endl;
  54. adj3[u].push_back({pos[v], v});
  55. adj3[v].push_back({pos[v], u});
  56. adj2[u].insert(v);
  57. adj2[v].insert(u);
  58. }
  59.  
  60.  
  61.  
  62. for(int i = 1; i <= n; ++i){
  63. sort(adj3[i].begin(), adj3[i].end());
  64. //reverse(adj3[i].begin(), adj3[i].end());
  65. }
  66.  
  67.  
  68.  
  69. int root = degree[1].second, cnt = m;
  70. par[root] = 0;
  71. for(int i = 2; i <= n; ++i){
  72. int u = degree[i].second;
  73.  
  74. if(adj3[u].empty()){
  75. cout << "No\n";
  76. return;
  77. }
  78. if(adj3[u][0].second != root){
  79. cout << "No\n";
  80. return;
  81. }
  82.  
  83. int curr = root;
  84. for(int j = 1; j < adj3[u].size(); ++j){
  85.  
  86. if(!adj2[curr].count(u)){
  87. cout << "No\n";
  88. return;
  89. }
  90. --cnt;
  91. if(pos[adj3[u][j].second] > pos[u]){
  92. ++cnt;
  93. break;
  94. }
  95. curr = adj3[u][j].second;
  96. }
  97. if(!adj2[curr].count(u)){
  98. cout << "No\n";
  99. return;
  100. }
  101. par[u] = curr;
  102. --cnt;
  103. }
  104.  
  105. if(cnt){
  106. cout << "No\n";
  107. return;
  108. }
  109.  
  110. cout << "Yes\n";
  111. for(int i = 1; i <= n; ++i)
  112. cout << par[i] << " ";
  113. cout << "\n";
  114. }
  115.  
  116. int main(){
  117. //ios::sync_with_stdio(false);
  118. //cin.tie(NULL);
  119.  
  120. int t;
  121. cin >> t;
  122.  
  123. while(t--)
  124. solve();
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement