Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <cmath>
- #include <algorithm>
- #include <iomanip>
- //#include <ext\pb_ds\assoc_container.hpp>
- //#pragma comment(linker, "/stack:200000000")
- //#pragma GCC optimize("Ofast")
- //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
- #define x first
- #define y second
- #define endl '\n'
- #define int long long
- using namespace std;
- //using namespace __gnu_pbds;
- //typedef tree <int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> tree_set;
- typedef pair <int, int> pii;
- typedef long double ld;
- const int N = 1e5 + 5;
- vector <int> po;
- vector <int> g[N];
- bool used[N];
- int ans[N];
- bool cmp(int a, int b)
- {
- return po[a] > po[b];
- }
- void dfs(int v)
- {
- used[v] = true;
- for (auto el : g[v])
- if (!used[el])
- {
- ans[el] = v + 1;
- dfs(el);
- }
- }
- main()
- {
- ios_base :: sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- int t;
- cin >> t;
- for (int _ = 0; _ < t; ++_)
- {
- int n, m;
- cin >> n >> m;
- po.clear();
- po.resize(n);
- for (int i = 0; i < m; ++i)
- {
- int a, b;
- cin >> a >> b;
- g[--a].push_back(--b);
- g[b].push_back(a);
- ++po[a];
- ++po[b];
- }
- for (int i = 0; i < n; ++i)
- sort(g[i].begin(), g[i].end(), cmp);
- int root = -1;
- for (int i = 0; i < n; ++i)
- if (po[i] == n - 1)
- {
- root = i;
- break;
- }
- if (root == -1)
- {
- cout << "No\n";
- for (int i = 0; i < n; ++i)
- g[i].clear();
- continue;
- }
- cout << "Yes\n";
- dfs(root);
- for (int i = 0; i < n; ++i)
- {
- cout << ans[i] << ' ';
- g[i].clear();
- used[i] = false;
- ans[i] = 0;
- }
- cout << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement