text
stringlengths
49
983k
#include <bits/stdc++.h> using namespace std; long long read() { long long a = 0, b = getchar(), c = 1; while (!isdigit(b)) c = b == '-' ? -1 : 1, b = getchar(); while (isdigit(b)) a = a * 10 + b - '0', b = getchar(); return a * c; } priority_queue<int> q[1000005]; int n, m, a[1000005], f[1000005]; int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); } int main() { n = read(), m = read(); for (int i = 1; i <= n; i++) a[i] = read(), f[i] = i; for (int i = 0; i < m; i++) { int x = read(), y = read(); f[find(x)] = find(y); } for (int i = 1; i <= n; i++) find(i), q[f[i]].push(a[i]); for (int i = 1; i <= n; i++) printf("%d ", q[f[i]].top()), q[f[i]].pop(); return 0; }
#include <bits/stdc++.h> using namespace std; long long int modexpo(long long int x, long long int y) { if (y == 0) return 1; if (y % 2) { long long int viky = modexpo(x, y / 2); return (((x * viky) % 1000000007) * viky) % 1000000007; } else { long long int viky = modexpo(x, y / 2); return (viky * viky) % 1000000007; } } long long int ap[1000011], ap1[1000011]; vector<long long int> v[1000011]; long long int visited[1000011]; long long int kk = 0, ans[1000011]; void dfs(long long int s) { visited[s] = 1; ap[kk] = ans[s]; ap1[kk] = s; kk++; for (auto x : v[s]) { if (!visited[x]) { dfs(x); } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int t, i, j, l, r, q, k, x, m, n; cin >> n >> m; for (i = 1; i <= n; i++) { cin >> ans[i]; } for (i = 1; i <= m; i++) { cin >> l >> r; v[l].push_back(r); v[r].push_back(l); } for (i = 1; i <= n; i++) { if (!visited[i]) { kk = 0; dfs(i); sort(ap, ap + kk); reverse(ap, ap + kk); sort(ap1, ap1 + kk); for (j = 0; j < kk; j++) { ans[ap1[j]] = ap[j]; } } } for (i = 1; i <= n; i++) { cout << ans[i] << " "; } return 0; }
#include <bits/stdc++.h> using namespace std; vector<vector<int> > g; vector<int> used; vector<vector<int> > ccs; void dfs(int first, int cc) { stack<int> s; s.push(first); used[first] = 1; while (!s.empty()) { int v = s.top(); s.pop(); if (used[v] == 2) { continue; } used[v] = 2; ccs[cc].push_back(v); for (int i = 0; i < g[v].size(); ++i) { if (used[g[v][i]] != 2) { s.push(g[v][i]); used[g[v][i]] = 1; } } } } int maxx(int fd, int sd) { return (fd > sd) ? fd : sd; } int minn(int fd, int sd) { return (fd < sd) ? fd : sd; } int main() { long n, k; scanf("%ld %ld", &n, &k); vector<long> a(n); g.resize(n); for (int i = 0; i < n; ++i) { scanf("%ld", &a[i]); } for (int i = 0; i < k; ++i) { long c, d; scanf("%ld %ld", &c, &d); c--; d--; g[c].push_back(d); g[d].push_back(c); } int cc = -1; used.resize(g.size(), 0); ccs.resize(n); for (int i = 0; i < n; ++i) { if (used[i] == 0) { cc++; dfs(i, cc); } } vector<int> answer(n, 0); { for (int curCC = 0; curCC <= cc; ++curCC) { sort(ccs[curCC].begin(), ccs[curCC].end()); vector<long> subanswer; for (int i = 0; i < ccs[curCC].size(); ++i) { subanswer.push_back(a[ccs[curCC][i]]); } sort(subanswer.begin(), subanswer.end()); for (int i = 0; i < ccs[curCC].size(); ++i) { answer[ccs[curCC][i]] = subanswer[subanswer.size() - i - 1]; } } } for (int i = 0; i < n; ++i) { printf("%ld ", answer[i]); } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; const long long mod = 1e9 + 7; struct Edge { int v, next; } edge[N << 1]; int head[N], tot, a[N], n, m, cnt; vector<int> bcc[N], ran[N]; void add(int u, int v) { edge[tot].v = v; edge[tot].next = head[u]; head[u] = tot++; } bool vis[N]; void dfs(int u) { vis[u] = true; bcc[cnt].push_back(a[u]); ran[cnt].push_back(u); for (int i = head[u]; ~i; i = edge[i].next) if (!vis[edge[i].v]) dfs(edge[i].v); } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); memset(head, -1, sizeof(head)); for (int i = 1; i <= m; ++i) { int u, v; scanf("%d%d", &u, &v); add(u, v); add(v, u); } for (int i = 1; i <= n; ++i) { if (vis[i]) continue; ++cnt; dfs(i); } for (int i = 1; i <= cnt; ++i) { sort(bcc[i].begin(), bcc[i].end()); sort(ran[i].begin(), ran[i].end()); int sz = bcc[i].size() - 1; for (int j = 0, k = sz; j <= sz; ++j, --k) a[ran[i][j]] = bcc[i][k]; } for (int i = 1; i < n; ++i) printf("%d ", a[i]); printf("%d\n", a[n]); return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T, size_t size> ostream &operator<<(ostream &os, const array<T, size> &arr) { os << '{'; string sep; for (const auto &x : arr) os << sep << x, sep = ", "; return os << '}'; } template <class T> ostream &operator<<(ostream &os, vector<T> V) { os << "[ "; for (auto v : V) os << v << " "; return os << "]"; } template <class T> ostream &operator<<(ostream &os, set<T> S) { os << "{ "; for (auto s : S) os << s << " "; return os << "}"; } template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) { return os << "(" << P.first << "," << P.second << ")"; } template <class L, class R> ostream &operator<<(ostream &os, map<L, R> M) { os << "{ "; for (auto m : M) os << "(" << m.first << ":" << m.second << ") "; return os << "}"; } const int MAX = 1000010; const int MOD2 = 998244353; const int MOD1 = 1000000007; const int INF = 0x3f; const long long int LLINF = 0x3f3f3f3f3f3f3f3fll; const long double EPS = 1e-7; struct UF { vector<int> e; UF(int n) : e(n, -1) {} bool same_set(int a, int b) { return find(a) == find(b); } int size(int x) { return -e[find(x)]; } int find(int x) { return e[x] < 0 ? x : e[x] = find(e[x]); } bool join(int a, int b) { a = find(a); b = find(b); if (a == b) return false; if (e[a] > e[b]) swap(a, b); e[a] += e[b]; e[b] = a; return true; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout << fixed << setprecision(25); cerr << fixed << setprecision(10); int n, m; cin >> n >> m; vector<int> p(n + 1); for (int i = 1; i <= n; i++) cin >> p[i]; UF uf(n + 1); for (int i = 1; i <= m; i++) { int x, y; cin >> x >> y; uf.join(x, y); } map<int, set<int>> setnum_pos; map<int, set<int>> setnum_val; for (int i = 1; i <= n; i++) { setnum_pos[uf.find(i)].insert(-i); setnum_val[uf.find(i)].insert(p[i]); } vector<int> ans(n + 1); for (auto [setnum, pos_set] : setnum_pos) { for (auto idx : pos_set) { ans[-idx] = *setnum_val[setnum].begin(); setnum_val[setnum].erase(ans[-idx]); } } for (int i = 1; i <= n; i++) cout << ans[i] << " "; cout << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const double eps = 1e-8; const int mod = 1000000007; const double pi = acos(-1); inline void gn(long long &x) { int sg = 1; char c; while (((c = getchar()) < '0' || c > '9') && c != '-') ; c == '-' ? (sg = -1, x = 0) : (x = c - '0'); while ((c = getchar()) >= '0' && c <= '9') x = x * 10 + c - '0'; x *= sg; } inline void gn(int &x) { long long t; gn(t); x = t; } inline void gn(unsigned long long &x) { long long t; gn(t); x = t; } int gcd(int a, int b) { return a ? gcd(b % a, a) : b; } long long powmod(long long a, long long x, long long mod) { long long t = 1ll; while (x) { if (x & 1) t = t * a % mod; a = a * a % mod; x >>= 1; } return t; } const int maxn = 1e6 + 100; vector<int> v[maxn]; int f[maxn]; int ite[maxn] = {0}; int find(int x) { if (x == f[x]) return x; return find(f[x]); } void add(int a, int b) { a = find(a); b = find(b); f[a] = b; } bool cmp(int &a, int &b) { return b < a; } int numb[maxn]; int main() { int n, m; cin >> n >> m; for (int i = (1); i <= (n); i++) gn(numb[i]), f[i] = i; int a, b; for (int i = (1); i <= (m); i++) { cin >> a >> b; add(a, b); } for (int i = (1); i <= (n); i++) v[find(f[i])].push_back(numb[i]); for (int i = (1); i <= (n); i++) sort(v[i].begin(), v[i].end(), cmp); for (int i = (1); i <= (n); i++) { int f = find(i); printf("%d ", v[f][ite[f]++]); } return 0; }
#include <bits/stdc++.h> using namespace std; int n, q; int ans[1000005], a[1000005], p[1000005], ace[1000005]; vector<int> gr[1000005], pos_gr[1000005]; int f(int u) { if (p[u] == u) return u; return p[u] = f(p[u]); } void m(int u, int v) { p[f(u)] = f(v); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> q; int u, v; for (int i = 1; i <= n; i++) { cin >> a[i]; p[i] = i; } for (int i = 1; i <= q; i++) { cin >> u >> v; m(u, v); } for (int i = 1; i <= n; i++) ace[i] = f(i); for (int i = 1; i <= n; i++) gr[ace[i]].push_back(a[i]); for (int i = n; i >= 1; i--) pos_gr[ace[i]].push_back(i); for (int i = 1; i <= n; i++) { sort(gr[i].begin(), gr[i].end()); for (int j = gr[i].size() - 1; j >= 0; j--) ans[pos_gr[i][j]] = gr[i][j]; } for (int i = 1; i <= n; i++) printf("%d ", ans[i]); }
#include <bits/stdc++.h> void dfs(const std::vector<std::vector<int>>& edges, int v, std::vector<int>* el, std::vector<bool>* used) { (*used)[v] = true; el->push_back(v); for (auto next : edges[v]) { if (!(*used)[next]) { dfs(edges, next, el, used); } } } int main() { std::ios_base::sync_with_stdio(false); int n, m; std::cin >> n >> m; std::vector<int> elements(n); std::vector<std::vector<int>> edges(n); for (int i = 0; i < n; ++i) { std::cin >> elements[i]; } for (int i = 0; i < m; ++i) { int a, b; std::cin >> a >> b; --a; --b; edges[a].push_back(b); edges[b].push_back(a); } std::vector<std::vector<int>> components; std::vector<bool> used(n, false); for (int i = 0; i < n; ++i) { if (!used[i]) { components.emplace_back(); dfs(edges, i, &components.back(), &used); } } for (auto comp : components) { std::sort(comp.begin(), comp.end()); std::vector<int> els; for (auto pos : comp) { els.push_back(elements[pos]); } std::sort(els.rbegin(), els.rend()); for (int index = 0; index < comp.size(); ++index) { auto pos = comp[index]; elements[pos] = els[index]; } } for (auto q : elements) { std::cout << q << " "; } std::cout << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; struct Edge { int v, next; } edge[N << 1]; int head[N], tot, a[N], n, m, cnt; vector<int> bcc[N], ran[N]; void add(int u, int v) { edge[tot].v = v; edge[tot].next = head[u]; head[u] = tot++; } bool vis[N]; void dfs(int u) { vis[u] = true; bcc[cnt].push_back(a[u]); ran[cnt].push_back(u); for (int i = head[u]; ~i; i = edge[i].next) if (!vis[edge[i].v]) dfs(edge[i].v); } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); memset(head, -1, sizeof(head)); for (int i = 1; i <= m; ++i) { int u, v; scanf("%d%d", &u, &v); add(u, v); add(v, u); } for (int i = 1; i <= n; ++i) { if (vis[i]) continue; ++cnt; dfs(i); } for (int i = 1; i <= cnt; ++i) { sort(bcc[i].begin(), bcc[i].end()); sort(ran[i].begin(), ran[i].end()); int sz = bcc[i].size() - 1; for (int j = 0, k = sz; j <= sz; ++j, --k) a[ran[i][j]] = bcc[i][k]; } for (int i = 1; i < n; ++i) printf("%d ", a[i]); printf("%d\n", a[n]); return 0; }
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using vi = vector<long long>; using vb = vector<bool>; using pii = pair<long long, long long>; using mii = map<long long, long long>; const long long inf = (long long)1e16; const long long upper = (long long)3e5 + 50; const long long M = (long long)1e9 + 7; const long long mod = (long long)998244353; const double eps = 1e-8; long long n, m; long long parent[(long long)1e6 + 1], sz[(long long)1e6 + 1]; long long perm[(long long)1e6 + 1]; void make_set(long long x) { parent[x] = x; sz[x] = 1; } long long find_set(long long x) { if (parent[x] == x) return x; return parent[x] = find_set(parent[x]); } void merge(long long a, long long b) { a = find_set(a), b = find_set(b); if (a == b) return; if (sz[a] < sz[b]) swap(a, b); parent[b] = a; sz[a] += sz[b]; } vector<long long> vals_of_set[(long long)1e6 + 1]; vector<long long> idx_of_set[(long long)1e6 + 1]; long long ans[(long long)1e6 + 1]; signed main() { long long begtime = clock(); ; cin.tie(0), cout.tie(0), ios_base::sync_with_stdio(false); cin >> n >> m; for (long long i = 1; i <= n; i += 1) { cin >> perm[i]; } for (long long i = 1; i <= n; i += 1) { make_set(i); } for (long long i = 1; i <= m; i += 1) { long long a, b; cin >> a >> b; merge(a, b); } for (long long i = 1; i <= n; i += 1) { long long s = find_set(i); idx_of_set[s].push_back(i); vals_of_set[s].push_back(perm[i]); } for (long long i = 1; i <= n; i += 1) { sort(vals_of_set[i].begin(), vals_of_set[i].end()); reverse(vals_of_set[i].begin(), vals_of_set[i].end()); } for (long long i = 1; i <= n; i += 1) { for (long long j = 0; j < (long long)idx_of_set[i].size(); j += 1) { ans[idx_of_set[i][j]] = vals_of_set[i][j]; } } for (long long i = 1; i <= n; i += 1) { cout << ans[i] << ' '; } long long endtime = clock(); cerr << '\n' << "Time elapsed: " << (endtime - begtime) * 1000 / CLOCKS_PER_SEC << " ms"; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 2000210; inline int scan() { char c = getchar(); int x = 0; while (c < '0' || c > '9') { c = getchar(); } while (c >= '0' && c <= '9') { x = (x << 1) + (x << 3) + c - '0'; c = getchar(); } return x; } long long int n, m; long long int a[N]; long long int parent[N]; long long int sz[N]; void reset() { for (long long int i = 0; i <= n; i++) parent[i] = i, sz[i] = 1; } long long int findparent(long long int x) { if (x != parent[x]) return findparent(parent[x]); return parent[x]; } void merge(long long int x, long long int y) { x = findparent(x); y = findparent(y); if (sz[x] < sz[y]) swap(x, y); sz[x] += sz[y]; parent[y] = x; } vector<long long int> g[N]; vector<long long int> l[N]; bool comp1(long long int x, long long int y) { return x < y; } bool comp2(long long int x, long long int y) { return x > y; } long long int minparent = INT_MAX, maxparent = INT_MIN; int main() { scanf("%lld %lld", &n, &m); for (long long int i = 1; i <= n; i++) scanf("%lld", &a[i]); reset(); while (m--) { long long int a, b; scanf("%lld %lld", &a, &b); merge(a, b); } for (long long int i = 1; i <= n; i++) { g[findparent(i)].push_back(i); l[findparent(i)].push_back(a[i]); minparent = min(minparent, parent[i]); maxparent = max(maxparent, parent[i]); } for (long long int i = minparent; i <= maxparent; i++) { sort(g[i].begin(), g[i].end(), comp1); sort(l[i].begin(), l[i].end(), comp2); } for (long long int i = minparent; i <= maxparent; i++) { for (long long int j = 0; j < g[i].size(); j++) { a[g[i][j]] = l[i][j]; } } for (long long int i = 1; i <= n; i++) cout << a[i] << " "; return 0; }
#include <bits/stdc++.h> using namespace std; int get_parent(vector<int> &parents, int i) { if (parents[i] == i) { return i; } return parents[i] = get_parent(parents, parents[i]); } int main() { cin.sync_with_stdio(false); int n, m; cin >> n >> m; vector<int> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } vector<int> parents(n); for (int i = 0; i < n; ++i) { parents[i] = i; } for (int i = 0; i < m; ++i) { int i1, i2; cin >> i1 >> i2; --i1, --i2; parents[get_parent(parents, i1)] = get_parent(parents, i2); } map<int, vector<int> > groups; for (int i = 0; i < n; ++i) { int root = get_parent(parents, i); if (groups.find(root) == groups.end()) { groups[root] = vector<int>(); } groups[root].emplace_back(i); } for (int i = 0; i < groups.size(); ++i) { vector<int> group_elements; for (int ai : groups[i]) { group_elements.push_back(a[ai]); } sort(group_elements.rbegin(), group_elements.rend()); int idx = 0; for (int ai : groups[i]) { a[ai] = group_elements[idx++]; } } for (int ai : a) { cout << ai << " "; } return 0; }
#include <bits/stdc++.h> using namespace std; const long long MAXN = 1e6 + 10, inf = 1e18; long long n, m, a[MAXN]; bool mark[MAXN]; vector<long long> tmp, adj[MAXN]; vector<vector<long long>> com; void Read_input() { cin >> n >> m; for (long long i = 1; i <= n; i++) cin >> a[i]; for (long long i = 1; i <= m; i++) { long long x, y; cin >> x >> y; adj[x].push_back(y); adj[y].push_back(x); } } void dfs(long long v) { tmp.push_back(v); mark[v] = true; for (auto i : adj[v]) if (!mark[i]) dfs(i); } bool cmp(long long x, long long y) { return x > y; } void Solve() { for (long long i = 1; i <= n; i++) if (!mark[i]) { dfs(i); com.push_back(tmp); tmp.clear(); } for (long long i = 0; i < com.size(); i++) { sort(com[i].begin(), com[i].end()); vector<long long> tm; for (auto j : com[i]) tm.push_back(a[j]); sort(tm.begin(), tm.end(), cmp); for (long long j = 0; j < com[i].size(); j++) a[com[i][j]] = tm[j]; } for (long long i = 1; i <= n; i++) cout << a[i] << " "; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); Read_input(), Solve(); return 0; }
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const int maxn = 1005; int a[1000100], rans[1000100], p[1000100]; bool leap[1000100]; struct Node { int id, root; Node(int id = 0, int root = 0) : id(id), root(root) {} bool operator<(const Node& r) const { if (root == r.root) return id < r.id; return root < r.root; } }; void init(int n) { for (int i = 1; i <= n; i++) p[i] = i; } int find(int x) { return x == p[x] ? x : p[x] = find(p[x]); } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); init(n); for (int i = 1; i <= m; i++) { int u, v; scanf("%d%d", &u, &v); int x = find(u); int y = find(v); if (x != y) p[x] = y; } vector<Node> vt; for (int i = 1; i <= n; i++) { int root = find(i); vt.push_back(Node(i, root)); } sort(vt.begin(), vt.end()); for (int i = 0, r; i < vt.size(); i = r) { vector<int> a1, a2; a1.push_back(a[vt[i].id]); a2.push_back(vt[i].id); int l = i; r = i; while (r < n && vt[r].root == vt[l].root) { if (l != r) { a1.push_back(a[vt[r].id]); a2.push_back(vt[r].id); } r++; } sort(a1.begin(), a1.end()); for (int j = 0; j < a2.size(); j++) rans[a2[j]] = a1[a2.size() - 1 - j]; } for (int i = 1; i <= n; i++) printf("%d ", rans[i]); return 0; }
#include <bits/stdc++.h> using namespace std; void dfs(long long curr, const vector<vector<long long>> &g, vector<long long> &temp, vector<bool> &visited) { visited[curr] = true; temp.push_back(curr); for (auto i : g[curr]) if (!visited[i]) dfs(i, g, temp, visited); } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, m; cin >> n >> m; vector<long long> permutation(n); for (long long &i : permutation) cin >> i; vector<vector<long long>> g(n); vector<bool> visited(n, false); for (long long i = 0; i < m; i++) { long long u, v; cin >> u >> v; u--; v--; g[u].push_back(v); g[v].push_back(u); } vector<vector<long long>> groups; vector<long long> ans(n); for (long long i = 0; i < n; i++) { if (!visited[i]) { vector<long long> temp; dfs(i, g, temp, visited); vector<long long> old = temp; sort(old.begin(), old.end()); sort(temp.begin(), temp.end(), [&](long long a, long long b) { return permutation[a] > permutation[b]; }); for (long long j = 0; j < (long long)old.size(); j++) { ans[old[j]] = permutation[temp[j]]; } groups.push_back(temp); } } for (auto i : ans) cout << i << " "; cout << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; int par[1000005]; int find(int z) { if (par[z] == z) return z; else return par[z] = find(par[z]); } void Union(int a, int b) { int p1 = find(a); int p2 = find(b); if (p1 > p2) swap(p1, p2); par[p2] = p1; } int main() { int n, m; cin >> n >> m; pair<int, int> arr[n + 1]; for (int i = 1; i <= n; i++) { int p; cin >> p; arr[i].first = p; arr[i].second = i; } for (int i = 1; i <= n; i++) par[i] = i; for (int i = 1; i <= m; i++) { int x, y; cin >> x >> y; Union(x, y); } vector<int> val[n + 1]; vector<int> ind[n + 1]; set<int> p; for (int i = 1; i <= n; i++) { val[find(i)].push_back(arr[i].first); ind[find(i)].push_back(arr[i].second); p.insert(find(i)); } int ans[n + 1]; set<int> vis; for (int i = 1; i <= n; i++) { if (p.count(i) && !vis.count(i)) { vis.insert(i); sort(val[i].rbegin(), val[i].rend()); int j = 0; while (j < val[i].size()) { ans[ind[i][j]] = val[i][j]; j++; } } } for (int i = 1; i <= n; i++) cout << ans[i] << " "; }
#include <bits/stdc++.h> using namespace std; const long double pi = acos(-1.0); template <class T> T sqr(T a) { return a * a; } const int maxn = 1e6 + 10; int p[maxn]; vector<int> g[maxn]; bool used[maxn]; vector<int> comp; void dfs(int v) { used[v] = true; comp.push_back(v); for (auto &u : g[v]) { if (!used[u]) dfs(u); } } int main() { ios_base::sync_with_stdio(false); int n, m; cin >> n >> m; for (int i = 0; i < n; i++) { cin >> p[i]; } for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; --a, --b; g[a].push_back(b); g[b].push_back(a); } for (int i = 0; i < n; i++) { if (!used[i]) { dfs(i); sort(comp.begin(), comp.end()); vector<int> c; for (int x : comp) c.push_back(p[x]); sort(c.rbegin(), c.rend()); for (int j = 0; j < comp.size(); j++) p[comp[j]] = c[j]; comp.clear(); } } for (int i = 0; i < n; i++) cout << p[i] << ' '; return 0; }
#include <bits/stdc++.h> using namespace std; const long long nmax = 1e9 + 7; const long long Mod = 998244353; const double PI = 2 * asin(1); int N, M, P[1000005], Group[1000005], Ans[1000005], Next[1000005]; vector<int> Num[1000005]; int main() { cin >> N >> M; for (int i = 1; i <= N; i++) cin >> P[i]; for (int i = 1; i <= N; i++) Group[i] = i; for (int i = 0; i < M; i++) { int A, B; cin >> A >> B; int GA = Group[A], GB = Group[B]; while (GA != Group[GA]) GA = Group[GA]; while (GB != Group[GB]) GB = Group[GB]; int G = min(GA, GB); Group[A] = G; Group[B] = G; Group[max(GA, GB)] = G; } for (int i = 1; i <= N; i++) { int G = Group[i]; while (G != Group[G]) G = Group[G]; Num[G].push_back(i); } for (int i = 1; i <= N; i++) { if (Num[i].size() == 0) continue; int S = Num[i].size(); for (int j = 0; j < S; j++) { Next[j] = P[Num[i][j]]; } sort(Next, Next + S, greater<int>()); for (int j = 0; j < S; j++) { Ans[Num[i][j]] = Next[j]; } } for (int i = 1; i <= N; i++) { cout << Ans[i]; if (i < N) cout << " "; else cout << endl; } return 0; }
#include <bits/stdc++.h> struct Edge { int u, v; }; inline bool operator<(const Edge& a, const Edge& b) { return a.u < b.u || (a.u == b.u && a.v < b.v); } inline bool operator==(const Edge& a, const Edge& b) { return !(a < b) && !(b < a); } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0); std::cerr.tie(0); int nItems, nEdges; std::cin >> nItems >> nEdges; std::vector<int> arr(nItems); for (auto& it : arr) std::cin >> it; std::vector<Edge> list; for (int i = 0; i < nEdges; ++i) { int a, b; std::cin >> a >> b; --a, --b; if (a > b) std::swap(a, b); list.push_back(Edge{a, b}); } std::sort(list.begin(), list.end()); list.erase(std::unique(list.begin(), list.end()), list.end()); std::vector<std::vector<int>> edges(nItems); for (auto& it : list) { edges[it.u].push_back(it.v); edges[it.v].push_back(it.u); } std::vector<int> part(nItems, 0); int nParts = 0; for (int i = 0; i < nItems; ++i) { if (part[i] == 0) { part[i] = ++nParts; std::queue<int> queue; queue.push(i); while (!queue.empty()) { auto curr = queue.front(); queue.pop(); for (auto next : edges[curr]) { if (part[next] == 0) { part[next] = part[curr]; queue.push(next); } } } } } std::vector<std::vector<int>> parts(1 + nParts); for (int i = 0; i < nItems; ++i) { parts[part[i]].push_back(arr[i]); } for (auto& it : parts) { std::sort(it.begin(), it.end(), std::greater<int>()); } for (int i = nItems - 1; i >= 0; --i) { int v = parts[part[i]].back(); parts[part[i]].pop_back(); arr[i] = v; } for (auto& it : arr) { std::cout << it << ' '; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); int n, m; cin >> n >> m; vector<int> a(n); vector<vector<int> > g(n); for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; x--, y--; g[x].push_back(y); g[y].push_back(x); } vector<set<int> > s(n); vector<int> set_id(n, -1); for (int i = 0; i < n; i++) { if (set_id[i] == -1) { set_id[i] = i; queue<int> q; q.push(i); while (!q.empty()) { int v = q.front(); q.pop(); s[i].insert(a[v]); for (size_t j = 0; j < g[v].size(); j++) { int u = g[v][j]; if (set_id[u] == -1) { set_id[u] = i; q.push(u); } } } } } for (int i = 0; i < n; i++) { auto it = s[set_id[i]].end(); --it; cout << *it << " "; s[set_id[i]].erase(it); } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 7; bool mark[maxn]; vector<long long int> v[maxn], o[maxn], a[maxn]; long long counter, ar[maxn]; void dfs(int u) { mark[u] = 1; if (v[u].size() == 0) { counter--; return; } o[counter].push_back(u); a[counter].push_back(ar[u]); for (int i = 0; i < v[u].size(); i++) { int z = v[u][i]; if (!mark[z]) { dfs(z); } } } int main() { long long n, m, mac = -1; cin >> n >> m; for (int i = 1; i <= n; i++) cin >> ar[i]; for (int i = 0; i < m; i++) { long long x, y; cin >> x >> y; v[x].push_back(y); v[y].push_back(x); } for (int i = 1; i < n + 1; i++) { if (!mark[i]) { dfs(i); mac = max(mac, counter); counter++; } } for (long long i = 0; i < mac + 1; i++) { sort(o[i].begin(), o[i].end()); sort(a[i].begin(), a[i].end()); long long j = 0; for (long long k = a[i].size() - 1; k > -1; k--) { ar[o[i][j]] = a[i][k]; j++; } } for (int i = 1; i < n + 1; i++) cout << ar[i] << ' '; return 0; }
#include <bits/stdc++.h> using namespace std; vector<int> ans[1000005]; vector<int> root; int pos[1000005], a[1000005], fa[1000005]; bool vis[1000005]; int get_fa(int u) { if (fa[u] == u) return u; return fa[u] = get_fa(fa[u]); } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); fa[i] = i; } for (int i = 1; i <= m; i++) { int u, v; scanf("%d%d", &u, &v); u = get_fa(u), v = get_fa(v); if (u != v) fa[u] = v; } for (int i = 1; i <= n; i++) { int u = get_fa(i); ans[u].push_back(a[i]); if (!vis[u]) { root.push_back(u); vis[u] = true; } } for (int i = 0; i < root.size(); i++) { sort(ans[root[i]].rbegin(), ans[root[i]].rend()); } for (int i = 1; i < n; i++) { printf("%d ", ans[get_fa(i)][pos[get_fa(i)]++]); } printf("%d\n", ans[get_fa(n)][pos[get_fa(n)]]); return 0; }
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; long long ceil(long long x, long long y) { if (x % y) { return x / y + 1; } else { return x / y; } } struct dsu { vector<long long> par, size; void init(long long n) { par.resize(n + 1); size.resize(n + 1); for (long long i = 0; i <= n; i++) { par[i] = i; size[i] = 1; } } long long find(long long x) { if (par[x] == x) { return x; } else { return par[x] = find(par[x]); } } void join(long long x, long long y) { long long parx = find(x), pary = find(y); if (parx == pary) { return; } if (size[parx] > size[pary]) { size[parx] += size[pary]; par[pary] = parx; } else { size[pary] += size[parx]; par[parx] = pary; } } }; const long long maxn = 1e6 + 5; vector<long long> adj[maxn]; bool vis[maxn]; vector<long long> a; void DFS(long long u) { a.push_back(u); vis[u] = true; for (auto it : adj[u]) { if (!vis[it]) { DFS(it); } } } void solve() { long long n, m; cin >> n >> m; long long p[n]; for (long long i = 0; i < n; i++) { cin >> p[i]; p[i]--; } for (long long i = 0; i < n; i++) { adj[i].clear(); vis[i] = false; } for (long long i = 0; i < m; i++) { long long u, v; cin >> u >> v; u--; v--; adj[u].push_back(v); adj[v].push_back(u); } for (long long i = 0; i < n; i++) { if (!vis[i]) { a.clear(); DFS(i); vector<long long> b; b.clear(); for (long long i = 0; i < a.size(); i++) { b.push_back(p[a[i]]); } sort(b.begin(), b.end()); reverse(b.begin(), b.end()); sort(a.begin(), a.end()); for (long long i = 0; i < a.size(); i++) { p[a[i]] = b[i]; } } } for (long long i = 0; i < n; i++) { cout << p[i] + 1 << " "; } cout << endl; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); long long ttt = 1; while (ttt--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; const int Max = 1e6 + 10; int num[Max]; int fa[Max]; priority_queue<int> q[Max]; inline int Find(int x) { if (x == fa[x]) return x; else return fa[x] = Find(fa[x]); } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%d", &num[i]); fa[i] = i; } int u, v; for (int i = 0; i < m; i++) { scanf("%d%d", &u, &v); fa[Find(u)] = Find(v); } for (int i = 1; i <= n; i++) { Find(i); q[fa[i]].push(num[i]); } for (int i = 1; i <= n; i++) { cout << q[fa[i]].top() << ' '; q[fa[i]].pop(); } return 0; }
#include <bits/stdc++.h> using namespace std; int k[1000001], p[1000001], cur[1000001]; vector<int> th[1000001]; int find(int x) { return (x == p[x]) ? x : (p[x] = find(p[x])); } int main() { int n, m, i, a, b; scanf("%d%d", &n, &m); for (i = 1; i <= n; i++) scanf("%d", k + i), p[i] = i; while (m--) scanf("%d%d", &a, &b), p[find(a)] = find(b); for (i = 1; i <= n; i++) th[find(i)].push_back(k[i]); for (i = 1; i <= n; i++) sort(th[i].begin(), th[i].end()), cur[i] = th[i].size(); for (i = 1; i <= n; i++) printf("%d ", th[find(i)][--cur[find(i)]]); }
#include <bits/stdc++.h> using namespace std; const int N = 1000020; int a[N]; int n, m; int fa[N], siz[N]; vector<int> v[N]; int find(int x) { if (fa[x] == x) return x; return fa[x] = find(fa[x]); } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); fa[i] = i; } for (int i = 1; i <= m; i++) { int x, y; scanf("%d%d", &x, &y); fa[find(x)] = find(y); } for (int i = 1; i <= n; i++) v[find(i)].push_back(a[i]); for (int i = 1; i <= n; i++) { sort(v[i].begin(), v[i].end()); siz[i] = v[i].size(); } for (int i = 1; i <= n; i++) printf("%d ", v[find(i)][--siz[find(i)]]); return 0; }
#include <bits/stdc++.h> using namespace std; int nextInt() { int x = 0; int c; while ('0' > (c = getchar())) ; x = c - '0'; while ('0' <= (c = getchar())) x = (x << 1) + (x << 3) + c - '0'; return x; } void putInt(int x) { static char buf[22] = {}; if (x == 0) { putchar('0'); return; } int i = 22; do buf[--i] = x % 10 + '0'; while (x /= 10); while (i < 22) putchar(buf[i++]); } const int INF = (int)1e9; const double PI = 2 * acos(0.0); const double eps = 1e-9; const int NPOS = -1; const int N = 1000009; int n, m, arr[N], x, y; vector<int> sorted, numbers; vector<int> adj[N]; bool vis[N]; void dfs(int s) { vis[s] = true; numbers.push_back(s); sorted.push_back(arr[s]); for (int u : adj[s]) { if (vis[u]) continue; dfs(u); } } void do_the_thing() { sort(((sorted).begin()), ((sorted).end()), greater<int>()); sort(((numbers).begin()), ((numbers).end())); for (int i = int(0); i < int(numbers.size()); ++i) arr[numbers[i]] = sorted[i]; sorted.clear(); numbers.clear(); } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n = nextInt(); m = nextInt(); for (int i = int(1); i < int(n + 1); ++i) arr[i] = nextInt(); for (int i = int(0); i < int(m); ++i) { x = nextInt(); y = nextInt(); adj[x].push_back(y); adj[y].push_back(x); } for (int i = int(1); i < int(n + 1); ++i) { if (vis[i]) continue; dfs(i); do_the_thing(); } for (int i = int(1); i < int(n + 1); ++i) putInt(arr[i]), putchar(' '); return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 2000000; int parent[maxn]; int FIND(int x) { if (parent[x] != x) parent[x] = FIND(parent[x]); return parent[x]; } vector<int> ele[maxn]; int arr[maxn]; int main() { for (int i = 0; i < maxn; i++) parent[i] = i; int n, m; scanf("%d", &n); scanf("%d", &m); for (int i = 1; i <= n; i++) scanf("%d", &arr[i]); for (int i = 1; i <= m; i++) { int a, b; scanf("%d", &a); scanf("%d", &b); int pa = FIND(a); int push_back = FIND(b); if (pa != push_back) parent[push_back] = pa; } for (int i = 1; i <= n; i++) { int p = FIND(i); ele[p].push_back(arr[i]); } for (int i = 1; i <= n; i++) sort(ele[i].begin(), ele[i].end()); for (int i = 1; i <= n; i++) { int p = FIND(i); printf("%d ", ele[p].back()); ele[p].pop_back(); } return 0; }
#include <bits/stdc++.h> using namespace std; const long long int N = 1e6 + 5; const int BL = 1; const int WH = -1; long long int dfs_num[N]; vector<pair<long long int, long long int> > adj[N]; long long int dfs_low[N]; long long int dfs_counter; stack<long long int> dfs_scc; set<long long int> in_stack; long long int cnt = 0; long long int A[N]; vector<long long int> pos[N], val[N]; void SCC(long long int u) { dfs_low[u] = dfs_num[u] = dfs_counter++; dfs_scc.push(u); in_stack.insert(u); for (vector<pair<long long int, long long int> >::iterator v = adj[u].begin(); v != adj[u].end(); ++v) { if (dfs_num[v->first] == WH) SCC(v->first); if (in_stack.find(v->first) != in_stack.end()) dfs_low[u] = min(dfs_low[u], dfs_num[v->first]); } if (dfs_num[u] == dfs_low[u]) { while (!dfs_scc.empty()) { val[cnt].push_back(A[dfs_scc.top()]); pos[cnt].push_back(dfs_scc.top()); in_stack.erase(dfs_scc.top()); dfs_scc.pop(); } cnt++; } } bool cmpr1(long long int p, long long int q) { return (p < q); } bool cmpr2(long long int p, long long int q) { return (p > q); } int main() { long long int m, n; ios::sync_with_stdio(false); cin >> n >> m; for (long long int i = 0; i < n; i++) cin >> A[i]; for (long long int i = 0; i < m; i++) { long long int ai, bi; cin >> ai >> bi; ai--; bi--; adj[ai].push_back(make_pair(bi, 1)); adj[bi].push_back(make_pair(ai, 1)); } memset(dfs_num, WH, sizeof(dfs_num)); dfs_counter = 0; for (long long int i = 0; i < n; i++) { if (dfs_num[i] == WH) SCC(i); } long long int B[N]; for (long long int i = 0; i < cnt; i++) { sort(pos[i].begin(), pos[i].end(), cmpr1); sort(val[i].begin(), val[i].end(), cmpr2); for (vector<long long int>::iterator it = pos[i].begin(), ti = val[i].begin(); it != pos[i].end() && ti != val[i].end(); ++it, ++ti) B[*it] = *ti; } for (long long int i = 0; i < n; i++) cout << B[i] << " "; cout << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 << std::endl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } const int N = 1000000; int dsu[N + 1]; int find(int pos) { if (dsu[pos] == pos) return pos; return dsu[pos] = find(dsu[pos]); } int unio(int a, int b) { int pa = find(a); int pb = find(b); if (pa == pb) return 0; dsu[pa] = pb; return 1; } vector<int> x[N + 1], y[N + 1]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); for (int i = 0; i <= N; i++) dsu[i] = i; int n, m; cin >> n >> m; vector<int> vals(n + 1); for (int i = 1; i <= n; i++) cin >> vals[i]; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; unio(a, b); } for (int i = 1; i <= n; i++) { x[find(i)].push_back(-vals[i]); y[find(i)].push_back(i); } for (int i = 1; i <= n; i++) sort(x[i].begin(), x[i].end()), sort(y[i].begin(), y[i].end()); for (int i = 1; i <= n; i++) { for (int j = 0; j < (int)(x[i].size()); j++) { vals[y[i][j]] = -x[i][j]; } } for (int i = 1; i <= n; i++) cout << vals[i] << " "; cout << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; int ara[1000010]; vector<int> edge[1000010]; int ans[1000010]; int visited[1000010]; vector<int> pos; vector<int> val; bool cmp(int a, int b) { return a > b; } void dfs(int root) { visited[root] = 1; pos.push_back(root); val.push_back(ara[root]); int sz = edge[root].size(); for (int i = 0; i < sz; i++) { if (!visited[edge[root][i]]) dfs(edge[root][i]); } } int main() { int n, m; scanf("%d %d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%d", &ara[i]); } int a, b; for (int i = 1; i <= m; i++) { scanf("%d %d", &a, &b); edge[a].push_back(b); edge[b].push_back(a); } for (int i = 1; i <= n; i++) { if (!visited[i]) { val.clear(); pos.clear(); dfs(i); sort(val.begin(), val.end(), cmp); sort(pos.begin(), pos.end()); int sz = val.size(); for (int i = 0; i < sz; i++) { ans[pos[i]] = val[i]; } } } for (int i = 1; i <= n; i++) { printf("%d", ans[i]); if (i == n) printf("\n"); else printf(" "); } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 2000210; inline int scan() { char c = getchar(); int x = 0; while (c < '0' || c > '9') { c = getchar(); } while (c >= '0' && c <= '9') { x = (x << 1) + (x << 3) + c - '0'; c = getchar(); } return x; } long long int n, m; long long int a[N]; long long int parent[N]; long long int sz[N]; void reset() { for (long long int i = 0; i <= n; i++) parent[i] = i, sz[i] = 1; } long long int findparent(long long int x) { if (x != parent[x]) return findparent(parent[x]); return parent[x]; } void merge(long long int x, long long int y) { x = findparent(x); y = findparent(y); if (sz[x] < sz[y]) swap(x, y); sz[x] += sz[y]; parent[y] = x; } vector<long long int> g[N]; vector<long long int> l[N]; bool comp1(long long int x, long long int y) { return x < y; } bool comp2(long long int x, long long int y) { return x > y; } long long int minparent = INT_MAX, maxparent = INT_MIN; int main() { scanf("%lld %lld", &n, &m); for (long long int i = 1; i <= n; i++) scanf("%lld", &a[i]); reset(); while (m--) { long long int a, b; scanf("%lld %lld", &a, &b); merge(a, b); } for (long long int i = 1; i <= n; i++) { g[findparent(i)].push_back(i); l[findparent(i)].push_back(a[i]); minparent = min(minparent, parent[i]); maxparent = max(maxparent, parent[i]); } for (long long int i = 1; i <= n; i++) { if (findparent(i) == i) { sort(l[i].begin(), l[i].end(), comp2); for (long long int j = 0; j < g[i].size(); j++) { a[g[i][j]] = l[i][j]; } } } for (long long int i = 1; i <= n; i++) printf("%lld ", a[i]); return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 5; int n, m; int a[maxn]; int fa[maxn]; int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); } set<int> S[maxn]; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= n; i++) fa[i] = i; while (m--) { int x, y; scanf("%d%d", &x, &y); fa[find(x)] = find(y); } for (int i = 1; i <= n; i++) S[find(i)].insert(a[i]); for (int i = 1; i <= n; i++) { int t = find(i); printf("%d%c", *--S[t].end(), " \n"[i == n]); S[t].erase(--S[t].end()); } return 0; }
#include <bits/stdc++.h> const int mod = 1e9 + 7; const int MOD = 998244353; inline int add(int x, int y) { x += y; if (x >= mod) x -= mod; return x; } inline int sub(int x, int y) { x -= y; if (x < 0) x += mod; return x; } inline int mul(int x, int y) { return (x * 1ll * y) % mod; } using namespace std; const long long int maxn = 1e6 + 10; const long long int inf = 1e9; const double pi = acos(-1); long long int n, m, u, v, a, b, c, d, p, q, x, y, z, minn, maxx; long long int parent[maxn], rnk[maxn], perm[maxn]; set<long long int> st[maxn]; long long int find_set(long long int v) { if (v == parent[v]) return v; return parent[v] = find_set(parent[v]); } void init_set() { for (long long int v = 1; v <= n; v++) { parent[v] = v; rnk[v] = 1; } } void union_sets(long long int a, long long int b) { a = find_set(a); b = find_set(b); if (a != b) { if (rnk[a] < rnk[b]) { parent[a] = b; rnk[b] += rnk[a]; st[b].insert(st[a].begin(), st[a].end()); st[a].clear(); } else { parent[b] = a; rnk[a] += rnk[b]; st[a].insert(st[b].begin(), st[b].end()); st[b].clear(); } } } signed main() { ios_base::sync_with_stdio(false); cin.tie(); cout.tie(); cin >> n >> m; init_set(); for (long long int i = 1; i <= n; i++) { cin >> perm[i]; st[i].insert(perm[i]); } for (long long int i = 1; i <= m; i++) { cin >> u >> v; union_sets(u, v); } for (long long int i = 1; i <= n; i++) { parent[i] = find_set(i); x = parent[i]; cout << *st[x].rbegin() << " "; auto it = st[x].end(); it--; st[x].erase(it); } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; vector<int> ans[N]; vector<int> root; int pos[N], par[N], a[N], vis[N]; int Find(int n) { if (n == par[n]) return n; return par[n] = Find(par[n]); } int main() { int n, m, u, v; scanf("%d %d", &n, &m); for (int i = 1; i <= n; ++i) { scanf("%d", a + i); par[i] = i; } while (m--) { scanf("%d %d", &u, &v); u = Find(u), v = Find(v); if (u != v) par[u] = v; } for (int i = 1; i <= n; ++i) { int u = Find(i); ans[u].push_back(a[i]); if (!vis[u]) { root.push_back(u); vis[u] = true; } } for (int i = 0; i < root.size(); ++i) sort(ans[root[i]].rbegin(), ans[root[i]].rend()); for (int i = 1; i < n; ++i) printf("%d ", ans[Find(i)][pos[Find(i)]++]); printf("%d\n", ans[Find(n)][pos[Find(n)]]); return 0; }
#include <bits/stdc++.h> using namespace std; unsigned long long pw[67]; void pre() { pw[0] = 1; for (int i = 1; i < 64; i++) pw[i] = 2 * pw[i - 1]; } bool vis[1000005]; vector<int> v[1000005]; vector<int> foo; void dfs(int start) { vis[start] = true; foo.push_back(start); for (int i = 0; i < v[start].size(); i++) { if (!vis[v[start][i]]) { dfs(v[start][i]); } } } int main() { ios_base::sync_with_stdio(false); pre(); vector<int> bar; int ans[1000005]; int n, m, i, p, q, s[1000005]; for (i = 0; i < 1000005; i++) { vis[i] = false; } cin >> n >> m; for (i = 1; i <= n; i++) { cin >> s[i]; } for (i = 0; i < m; i++) { cin >> p >> q; v[p].push_back(q); v[q].push_back(p); } for (i = 1; i <= n; i++) { if (!vis[i]) { foo.erase(foo.begin(), foo.end()); bar.erase(bar.begin(), bar.end()); dfs(i); sort(foo.begin(), foo.end()); for (int j = 0; j < foo.size(); j++) bar.push_back(s[foo[j]]); sort(bar.begin(), bar.end()); reverse(bar.begin(), bar.end()); for (int j = 0; j < foo.size(); j++) ans[foo[j]] = bar[j]; } } for (i = 1; i <= n; i++) cout << ans[i] << " "; cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const int N = 1e6 + 1; int par[N]; int root(int v) { return par[v] < 0 ? v : (par[v] = root(par[v])); } void merge(int x, int y) { if ((x = root(x)) == (y = root(y))) return; if (par[y] < par[x]) swap(x, y); par[x] += par[y]; par[y] = x; } bool same(int x, int y) { return (root(x) == root(y)); } int group[N]; vector<int> vec[N]; int counter[N]; int a[N]; int main() { memset(par, -1, sizeof(par)); int u, v; int n, m; scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) { scanf("%d", a + i); } for (int i = 0; i < m; i++) { scanf("%d %d", &u, &v); u--; v--; merge(u, v); } int r; for (int i = n - 1; i >= 0; i--) { r = root(i); vec[r].push_back(a[i]); group[i] = r; } for (int i = 0; i < n; i++) { if (!vec[i].empty()) { sort(vec[i].begin(), vec[i].end()); reverse(vec[i].begin(), vec[i].end()); } } for (int i = 0; i < n; i++) { r = group[i]; printf("%d ", vec[r][counter[r]]); counter[r]++; } return 0; }
#include <bits/stdc++.h> using namespace std; long long com = 0; long long n, m; void DFS_visit(vector<long long> adj[], long long vis[], long long comp[], long long u) { vis[u] = 1; comp[u] = com; for (long long i = 0; i < adj[u].size(); i++) { if (vis[adj[u][i]] == 0) DFS_visit(adj, vis, comp, adj[u][i]); } return; } void DFS(vector<long long> adj[], long long vis[], long long comp[]) { for (long long i = 0; i < n; i++) { if (vis[i] == 0) { com++; DFS_visit(adj, vis, comp, i); } } return; } int main() { cin >> n >> m; long long arr[n], ans[n]; for (long long i = 0; i < n; i++) { cin >> arr[i]; } vector<long long> adj[n]; long long a, b; for (long long i = 0; i < m; i++) { cin >> a >> b; a--, b--; adj[a].push_back(b); adj[b].push_back(a); } long long comp[n], vis[n]; for (long long i = 0; i < n; i++) { comp[i] = 0; vis[i] = 0; } DFS(adj, vis, comp); vector<long long> nodes[com + 1]; for (long long i = 0; i < n; i++) { nodes[comp[i]].push_back(arr[i]); } for (long long i = 1; i <= com; i++) { sort(nodes[i].begin(), nodes[i].end()); } long long ind[com + 1]; for (long long i = 0; i < com + 1; i++) { ind[i] = nodes[i].size() - 1; } for (long long i = 0; i < n; i++) { ans[i] = nodes[comp[i]][ind[comp[i]]]; ind[comp[i]]--; } for (long long i = 0; i < n; i++) { cout << ans[i] << ' '; } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int D = 1000005, MOD = 1000000007; int fa[D], a[D], t[D]; int head[D]; int to[D], linkto[D], tot = 0; int tmp[D], el[D]; int getfa(int x) { if (fa[x] == x) return fa[x]; else return fa[x] = getfa(fa[x]); } int samefa(int x, int y) { return getfa(x) == getfa(y); } void merge(int x, int y) { int fax = getfa(x), fay = getfa(y); fa[fax] = fay; } void ins(int pos, int ele) { to[tot] = ele; linkto[tot] = head[pos]; head[pos] = tot++; } bool cmp0(const int& a, const int& b) { return a < b; } bool cmp1(const int& a, const int& b) { return t[a] > t[b]; } int main() { int i, x, y, n, m, j; for (i = 0; i < D; i++) fa[i] = i; memset(head, -1, sizeof(head)); scanf("%d%d", &n, &m); for (i = 0; i < n; i++) { scanf("%d", a + i); t[i] = a[i]; } for (i = 0; i < m; i++) { scanf("%d%d", &x, &y); x--, y--; merge(x, y); } for (i = 0; i < n; i++) { fa[i] = getfa(i); ins(fa[i], i); } for (i = 0; i < n; i++) { int total = 0; for (j = head[i]; j != -1; j = linkto[j]) { tmp[total] = el[total] = to[j]; total++; } sort(tmp, tmp + total, cmp0); sort(el, el + total, cmp1); for (j = 0; j < total; j++) a[tmp[j]] = t[el[j]]; } for (i = 0; i < n; i++) printf("%d ", a[i]); printf("\n"); return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; void Open() {} const int N = 1000050; int id1[N]; vector<int> id2[N]; int a[N], pa[N]; int ans[N]; int find(int x) { return pa[x] == x ? x : pa[x] = find(pa[x]); } int cmp(int i, int j) { return a[i] > a[j]; } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &a[i]), pa[i] = i; for (int i = 0; i < m; i++) { int u, v; scanf("%d%d", &u, &v); u = find(u), v = find(v); pa[u] = v; } for (int i = 1; i <= n; i++) id2[find(i)].push_back(i); for (int i = 1; i <= n; i++) { int t = 0; for (int j = 0; j < id2[i].size(); j++) { id1[t++] = id2[i][j]; } if (t == 0) continue; sort(id1, id1 + t, cmp); for (int j = 0; j < t; j++) { ans[id2[i][j]] = a[id1[j]]; } } for (int i = 1; i <= n; i++) printf("%d%c", ans[i], " \n"[i == n]); return 0; }
#include <bits/stdc++.h> using namespace std; long long int gcd(long long int a, long long int b) { if (b == 0) return a; return gcd(b, a % b); } long long int lcm(long long int a, long long int b) { return a * b / gcd(a, b); } long long int fexp(long long int a, long long int b) { long long int ans = 1; while (b) { if (b & 1) ans = ans * a % 1000000007; b /= 2; a = a * a % 1000000007; } return ans; } long long int inverse(long long int a, long long int p) { return fexp(a, p - 2); } void ingraph(vector<vector<long long int> >& graph, long long int m) { long long int x, y; for (long long int i = 0; i < m; i++) { cin >> x >> y; x--, y--; graph[x].push_back(y); graph[y].push_back(x); } } bool visited[1000001] = {0}; vector<long long int> dfs(vector<vector<long long int> >& graph, long long int v) { stack<long long int> st; st.push(v); long long int sz = 0; visited[v] = 1; vector<long long int> res; while (!st.empty()) { long long int u = st.top(); res.push_back(u); visited[u] = 1; st.pop(); for (long long int i = 0; i < graph[u].size(); i++) { if (!visited[graph[u][i]]) { st.push(graph[u][i]); visited[graph[u][i]] = 1; } } sz++; } return res; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int t; t = 1; while (t--) { long long int n, m; cin >> n >> m; vector<vector<long long int> > graph(n); vector<long long int> vec(n), rev(n); for (long long int i = 0; i < n; i++) { cin >> vec[i]; vec[i]--; rev[vec[i]] = i; } ingraph(graph, m); vector<long long int> fin, res; for (long long int i = 0; i < n; i++) { if (!visited[i]) { fin = dfs(graph, i); for (long long int j = 0; j < fin.size(); j++) res.push_back(vec[fin[j]]); sort(fin.begin(), fin.end()); sort(res.rbegin(), res.rend()); for (long long int j = 0; j < fin.size(); j++) { vec[fin[j]] = res[j] + 1; } res.clear(); } } for (long long int i = 0; i < n; i++) cout << vec[i] << " "; } }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 5; int fa[maxn]; void init(int n) { for (int i = 1; i <= n; ++i) fa[i] = i; } int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); } void unite(int u, int v) { int fx = find(u); int fy = find(v); if (fx == fy) return; fa[fx] = fy; } priority_queue<int, vector<int>, less<int> > q[maxn]; int x[maxn]; int main() { int n, m; scanf("%d%d", &n, &m); init(n); for (int i = 1; i <= n; ++i) { scanf("%d", &x[i]); } for (int i = 1; i <= m; ++i) { int u, v; scanf("%d%d", &u, &v); unite(u, v); } for (int i = 1; i <= n; ++i) { find(i); q[fa[i]].push(x[i]); } for (int i = 1; i <= n; ++i) { printf("%d%c", q[fa[i]].top(), " \n"[i == n]); q[fa[i]].pop(); } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; int a[N], vis[N]; vector<vector<int>> adj(N); int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; for (int i = 0; i < n; ++i) cin >> a[i + 1]; int x, y; for (int i = 0; i < m; ++i) { cin >> x >> y; adj[x].push_back(y), adj[y].push_back(x); } for (int i = 1; i <= n; ++i) { if (vis[i]) continue; vis[i] = 1; queue<int> q; q.push(i); vector<int> pos, val; pos.push_back(i), val.push_back(a[i]); while (!q.empty()) { int p = q.front(); q.pop(); for (int tt : adj[p]) if (!vis[tt]) { vis[tt] = 1; q.push(tt); pos.push_back(tt), val.push_back(a[tt]); } } sort(pos.begin(), pos.end()), sort(val.rbegin(), val.rend()); for (int j = 0; j < (int)pos.size(); ++j) a[pos[j]] = val[j]; } for (int i = 0; i < n; ++i) cout << a[i + 1] << ' '; return 0; }
#include <bits/stdc++.h> using namespace std; int nums[1000010], dsu[1000010], n, m; vector<int> parts[1000010]; vector<int> idxs[1000010]; int get_par(int pos) { if (dsu[pos] == pos) return pos; int r = get_par(dsu[pos]); dsu[pos] = r; return r; } int main() { scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) dsu[i] = i; for (int i = 0; i < n; i++) { scanf("%d", nums + i); } for (int i = 0; i < m; i++) { int a, b; scanf("%d %d", &a, &b); a--; b--; a = get_par(a); b = get_par(b); dsu[a] = b; } for (int i = 0; i < n; i++) { parts[get_par(i)].push_back(nums[i]); idxs[get_par(i)].push_back(i); } for (int i = 0; i < n; i++) { sort(parts[i].begin(), parts[i].end(), greater<int>()); for (int j = 0; j < parts[i].size(); j++) { nums[idxs[i][j]] = parts[i][j]; } } for (int i = 0; i < n; i++) printf("%d ", nums[i]); printf("\n"); }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 2; priority_queue<int> Q[maxn]; int num[maxn]; int fa[maxn]; int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); } int main() { int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) { scanf("%d", &num[i]); fa[i] = i; } for (int i = 0; i < m; i++) { int u, v; scanf("%d %d", &u, &v); fa[find(u)] = find(v); } for (int i = 1; i <= n; i++) { find(i); Q[fa[i]].push(num[i]); } for (int i = 1; i <= n; i++) { printf("%d ", Q[fa[i]].top()); Q[fa[i]].pop(); } return 0; }
#include <bits/stdc++.h> using namespace std; const long long MAX = 1000005; long long n, m; long long a[MAX]; vector<long long> g[MAX]; long long visited[MAX]; vector<long long> k; void dfs(long long node) { visited[node] = 1; k.push_back(node); for (auto i : g[node]) { if (!visited[i]) dfs(i); } } int main() { cin >> n >> m; for (long long i = 0; i < n; i++) cin >> a[i + 1]; for (long long i = 0; i < m; i++) { long long x, y; cin >> x >> y; if (x != y) { g[x].push_back(y); g[y].push_back(x); } } for (long long i = 1; i <= n; i++) { if (!visited[i]) { k.clear(); dfs(i); sort(k.begin(), k.end()); vector<long long> num; for (long long i = 0; i < k.size(); i++) num.push_back(a[k[i]]); sort(num.begin(), num.end(), greater<long long>()); for (long long i = 0; i < num.size(); i++) a[k[i]] = num[i]; } } for (long long i = 0; i < n; i++) cout << a[i + 1] << " "; }
#include <bits/stdc++.h> using namespace std; vector<int> b; list<int> b1v; vector<list<int>> aa; void doRec(int vv) { if (b[vv] > 0) return; b[vv] = 1; b1v.push_back(vv); if (!aa[vv].empty()) for (auto it = aa[vv].begin(); it != aa[vv].end(); it++) doRec(*it); } int main() { int n, m; cin >> n >> m; vector<int> p(n + 1); aa.resize(n + 1); b.resize(n + 1, 0); for (int i = 1; i < n + 1; i++) cin >> p[i]; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; aa[a].push_back(b); aa[b].push_back(a); } for (auto d = p.begin() + 1; d != p.end(); d++) if (b[*d] == 0) { b1v.clear(); doRec(*d); vector<int> su; vector<int> p1; for (auto bb = b1v.begin(); bb != b1v.end(); bb++) { p1.push_back(p[*bb]); su.push_back(*bb); } sort(su.begin(), su.end()); sort(p1.rbegin(), p1.rend()); int pInd = 0; for (auto bb = su.begin(); bb != su.end(); bb++) p[*bb] = p1[pInd++]; } for (int i = 1; i < n + 1; i++) cout << p[i] << " "; cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; vector<int> v[1000006]; vector<int> vv[1000006], brr[1000006]; int z, arr[1000006], vis[1000006]; void dfs(int u, int s) { vis[u] = 1; v[s].push_back(arr[u]); brr[s].push_back(u); for (int i = 0; i < vv[u].size(); i++) { int V = vv[u][i]; if (!vis[V]) dfs(V, s); } } int main() { int n, m; scanf("%d %d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &arr[i]); while (m--) { int x, y; scanf("%d %d", &x, &y); vv[x].push_back(y); vv[y].push_back(x); } for (int i = 1; i <= n; i++) { if (!vis[i]) dfs(i, ++z); } for (int i = 1; i <= z; i++) { sort(v[i].begin(), v[i].end()); sort(brr[i].begin(), brr[i].end()); } for (int i = 1; i <= z; i++) { for (int j = 0, k = v[i].size() - 1; j < brr[i].size(); j++, k--) { arr[brr[i][j]] = v[i][k]; } } for (int i = 1; i <= n; i++) printf("%d ", arr[i]); printf("\n"); }
#include <bits/stdc++.h> using namespace std; class Graph { public: int num_vertices; vector<int> vertex; vector<int> *adj; Graph(int n) { num_vertices = n; adj = new vector<int>[n + 1]; vertex.resize(n + 1); } void addVertex(int i, int x) { vertex[i] = x; } void addEdge(int x, int y) { adj[x].push_back(y); adj[y].push_back(x); } void DFS_visit(int current, vector<bool> &visited, vector<int> &index, vector<int> &value) { visited[current] = true; index.push_back(current); value.push_back(vertex[current]); for (int neighbor : adj[current]) { if (!visited[neighbor]) { DFS_visit(neighbor, visited, index, value); } } } void DFS() { vector<bool> visited(num_vertices + 1, false); vector<int> index; vector<int> value; for (int i = 1; i < num_vertices + 1; i++) { if (!visited[i]) { index.clear(); value.clear(); DFS_visit(i, visited, index, value); sort(index.begin(), index.end()); sort(value.rbegin(), value.rend()); for (int j = 0; j < index.size(); j++) vertex[index[j]] = value[j]; } } } }; int main() { ios::sync_with_stdio(false); int n, m, x, y; cin >> n >> m; Graph G(n); for (int i = 1; i <= n; i++) { cin >> x; G.addVertex(i, x); } for (int i = 0; i < m; i++) { cin >> x >> y; G.addEdge(x, y); } G.DFS(); copy(G.vertex.begin() + 1, G.vertex.end(), ostream_iterator<int>(cout, " ")); cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int sort_arra[1000010]; int starting_permutation[1000010]; int vertexlist[1000010]; vector<int> adjancylist[1000010]; vector<int> arra; void dfs(int i) { int x; arra.push_back(i); vertexlist[i] = 1; x = adjancylist[i].size(); for (int j = 0; j < x; ++j) if (vertexlist[adjancylist[i][j]] == 0) dfs(adjancylist[i][j]); } int main() { int n, m, first, second, x; scanf("%d %d", &n, &m); for (int i = 0; i < n; ++i) scanf("%d", starting_permutation + i); for (int i = 0; i < m; ++i) { scanf("%d %d", &first, &second); adjancylist[first - 1].push_back(second - 1); adjancylist[second - 1].push_back(first - 1); } for (int i = 0; i < n; ++i) if (vertexlist[i] == 0) { arra.clear(); dfs(i); sort(arra.begin(), arra.end()); x = arra.size(); for (int j = 0; j < x; ++j) sort_arra[j] = starting_permutation[arra[j]]; sort(sort_arra, sort_arra + x, greater<int>()); for (int j = 0; j < x; ++j) starting_permutation[arra[j]] = sort_arra[j]; } for (int i = 0; i < n; ++i) printf("%d ", starting_permutation[i]); printf("\n"); }
#include <bits/stdc++.h> using namespace std; vector<int> value, idx; vector<bool> dfs_num; vector<int> vi; vector<vector<int> > AdjList; void cc(int p) { dfs_num[p] = true; value.push_back(vi[p]); idx.push_back(p); int q; for (int i = 0; i < AdjList[p].size(); ++i) { q = AdjList[p][i]; if (!dfs_num[q]) cc(q); } } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0), cout.precision(15); int n, m, a, b, change = 1; cin >> n >> m; vi.assign(n, 0); AdjList.assign(n, vector<int>(0)); for (int i = 0; i < n; ++i) cin >> vi[i]; for (int i = 0; i < m; ++i) { cin >> a >> b; --a, --b; AdjList[a].push_back(b); AdjList[b].push_back(a); } dfs_num.assign(n, false); for (int i = 0; i < n; ++i) { if (!dfs_num[i]) { value.clear(); idx.clear(); cc(i); sort(value.begin(), value.end(), greater<int>()); sort(idx.begin(), idx.end()); for (int j = 0; j < idx.size(); ++j) { vi[idx[j]] = value[j]; } } } for (int i = 0; i < vi.size(); ++i) { if (i) cout << " "; cout << vi[i]; } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int A[1000010]; int dsu[1000010]; int s[1000010]; vector<int> V[1000010]; int st[1000010]; int root(int x) { while (x != dsu[x]) { dsu[x] = dsu[dsu[x]]; x = dsu[x]; } return x; } void onion(int x, int y) { x = root(x); y = root(y); if (x == y) return; if (s[x] < s[y]) { dsu[y] = x; s[x] += s[y]; } else { dsu[x] = y; s[y] += s[x]; } return; } int main() { int n, m; cin >> n >> m; for (int i = 0; i < n; i++) { cin >> A[i]; dsu[i] = i; s[i] = 1; } for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--, b--; onion(a, b); } for (int i = 0; i < n; i++) { V[root(i)].push_back(A[i]); } for (int i = 0; i < n; i++) { if (root(i) == i) { sort(V[i].begin(), V[i].end(), greater<int>()); } } for (int i = 0; i < n; i++) { cout << V[root(i)][st[root(i)]] << ' '; st[root(i)]++; } return 0; }
#include <bits/stdc++.h> using namespace std; void Print() { cout << endl; } template <typename T1, typename... T> void Print(const T1 &t1, const T &...t) { cout << t1 << " "; Print(t...); } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { os << "(" << p.first << ", " << p.second << ")"; return os; } long long gcd(long long a, long long b) { if (b) { long long r = a % b; a = b, b = r; } return a; } tuple<long long, long long, long long> extend_gcd(long long a, long long b) { if (b == 0) return make_tuple(a, 1, 0); else { long long g, nx, ny; std::tie(g, nx, ny) = extend_gcd(b, a % b); return make_tuple(g, ny, nx - (a / b) * ny); } } long long fastpow(long long a, long long e, long long mod) { if (e == 0) return 1; if (e == 1) return a % mod; long long t = fastpow(a, e / 2, mod); t = t * t % mod; if (e & 1) return t * a % mod; else return t % mod; } const double PI = 3.14159265358979323846; const int MOD = (int)1e9 + 7; const int N = 1000005; int p[N], ans[N]; bool vis[N]; vector<int> edges[N]; priority_queue<int> vals; priority_queue<int, std::vector<int>, std::greater<int> > idx; void dfs(int v, int r) { vis[v] = true; idx.push(v); vals.push(p[v]); for (auto to : edges[v]) { if (!vis[to]) dfs(to, r); } if (v == r) { while (!idx.empty()) { int tidx = idx.top(); idx.pop(); int tval = vals.top(); vals.pop(); ans[tidx] = tval; } } } int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); int n, m; cin >> n >> m; for (int i = 1; i <= n; ++i) { cin >> p[i]; } for (int i = 0; i < m; ++i) { int a, b; cin >> a >> b; if (a == b) continue; edges[a].push_back(b); edges[b].push_back(a); } for (int i = 1; i <= n; ++i) { if (!vis[i]) dfs(i, i); } for (int i = 1; i <= n; ++i) cout << ans[i] << ' '; cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int n, m; int a[1000005]; vector<int> G[1000005]; int ans[1000005]; vector<int> fir; vector<int> sec; bool vis[1000005]; void dfs(int u) { if (vis[u]) return; vis[u] = 1; fir.push_back(u); sec.push_back(a[u]); for (int i = 0; i < (int)G[u].size(); i++) { int v = G[u][i]; dfs(v); } } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 0; i < m; i++) { int x, y; scanf("%d%d", &x, &y); G[x].push_back(y); G[y].push_back(x); } for (int i = 1; i <= n; i++) { if (vis[i]) continue; fir.clear(); sec.clear(); dfs(i); sort(fir.begin(), fir.end()); sort(sec.begin(), sec.end()); int k = 0; for (int j = (int)sec.size() - 1; j >= 0; j--) { ans[fir[k++]] = sec[j]; } } for (int i = 1; i <= n; i++) { if (i != 1) printf(" "); printf("%d", ans[i]); } printf("\n"); return 0; }
#include <bits/stdc++.h> using namespace std; long long int modp(long long int a, long long int p, long long int n) { long long int d[100]; long long int i, j, k, l; if (n == 0) return 1; for (i = 0; n >= 1; i++) { d[i] = n % 2; n /= 2; } l = i; long long int e = 1; if (d[0] == 1) e *= (a % p); for (i = 1; i < l; i++) { a *= a; a = a % p; if (d[i] == 1) { e *= a; e = e % p; } } return e % p; } long long int modInverse(long long int n, long long int p) { return modp(n, p, p - 2); } long long int p1 = 1e9 + 7, p2 = 998244353; long long int l, r, mid, ans; long long int n, i, j, k, g, m; long long int x, y, n1, n2, h, z, c; long long int aa = 1; vector<long long int> v(1000001), parent(1000001); void make_set(long long int x) { parent[x] = x; } long long int find_set(long long int x) { if (parent[x] == x) return x; return parent[x] = find_set(parent[x]); } void union_set(long long int a, long long int b) { a = find_set(a); b = find_set(b); if (a != b) { if (b < a) swap(a, b); parent[b] = a; } } void solve() { cin >> n >> m; for (i = 0; i < n; i++) { cin >> v[i]; } for (i = 0; i < n; i++) { make_set(i); } for (i = 0; i < m; i++) { cin >> x >> y; x--; y--; union_set(x, y); } vector<vector<long long int> > w(n), w2(n); vector<long long int> ans(n); for (i = 0; i < n; i++) { parent[i] = find_set(i); w[parent[i]].push_back(v[i]); w2[parent[i]].push_back(i); } for (i = 0; i < n; i++) { sort(w[i].begin(), w[i].end(), greater<long long int>()); l = w[i].size(); for (j = 0; j < l; j++) { ans[w2[i][j]] = w[i][j]; } } for (i = 0; i < n; i++) cout << ans[i] << " "; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int t = 1; long long int ans = 0; for (int i = 0; i < t; i++) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; struct ss { int num, id; } e[1000005] = {0}; int pre[1000005]; int findn(int x) { if (pre[x] == x) return x; pre[x] = findn(pre[x]); return pre[x]; } bool cmp(ss a, ss b) { if (pre[a.id] == pre[b.id]) { return a.num > b.num; } return pre[a.id] < pre[b.id]; } int x, y, xi, yi, k[1000005] = {0}, sub[1000005] = {0}; int n, m, a[1000005]; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); e[i].num = a[i]; e[i].id = i; pre[i] = i; } for (int i = 0; i < m; i++) { scanf("%d%d", &x, &y); xi = findn(x), yi = findn(y); if (xi == yi) { ; } else { pre[xi] = yi; } } for (int i = 1; i <= n; i++) { pre[i] = findn(i); } sort(e + 1, e + n + 1, cmp); for (int i = 1; i <= n; i++) { if (pre[e[i].id] != pre[e[i - 1].id]) { k[pre[e[i].id]] = i; } } int pp; for (int i = 1; i <= n; i++) { pp = pre[i]; if (i != 1) printf(" "); printf("%d", e[k[pp] + (sub[pp])].num); sub[pp]++; } }
#include <bits/stdc++.h> using namespace std; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 << std::endl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } vector<vector<int> > op; vector<int> ha; int vis[1000005]; int av[1000005]; int mi = 1e9; map<int, int> pos; int dfs(int start) { vis[start] = 1; if (av[start] == 0 && start < mi) mi = start; for (int i = 0; i < op[start].size(); i++) { if (vis[op[start][i]] == 0) dfs(op[start][i]); } vis[start] = 0; return (0); } int main(void) { int n, m; scanf("%d", &n), scanf("%d", &m); vector<int> arr; for (int i = 0; i < n; i++) { int a; scanf("%d", &a); arr.push_back(a); } for (int i = 0; i < n; i++) { vector<int> a; op.push_back(a); } for (int i = 0; i < m; i++) { int a, b; scanf("%d", &a), scanf("%d", &b); op[a - 1].push_back(b - 1); op[b - 1].push_back(a - 1); } for (int i = 0; i < n; i++) pos[arr[i]] = i; vector<int> brr; for (int i = 0; i < n; i++) brr.push_back(-1 * arr[i]); sort(brr.begin(), brr.end()); for (int i = 0; i < n; i++) brr[i] = brr[i] * -1; for (int i = 0; i < n; i++) { int ind = pos[brr[i]]; mi = ind; if (vis[ind] == 0) dfs(ind); int temp = arr[ind]; arr[ind] = arr[mi]; arr[mi] = temp; pos[arr[ind]] = ind; pos[arr[mi]] = mi; av[mi] = 1; } for (int i = 0; i < n; i++) printf("%d ", arr[i]); printf("\n"); return (0); }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 7; int n, m, arr[maxn], ans[maxn], ind[maxn]; bool mrk[maxn]; vector<int> a, b, g[maxn]; void input() { cin >> n >> m; for (int i = 1; i <= n; i++) { cin >> arr[i]; } for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } } void dfs(int v) { mrk[v] = true; a.push_back(v); b.push_back(arr[v]); for (auto u : g[v]) { if (mrk[u] == false) { dfs(u); } } } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); input(); for (int i = 1; i <= n; i++) { if (mrk[i] == false) { a.clear(); b.clear(); dfs(i); sort(a.begin(), a.end(), greater<int>()); sort(b.begin(), b.end()); for (int i = 0; i < a.size(); i++) arr[a[i]] = b[i]; } } for (int i = 1; i <= n; i++) cout << arr[i] << " "; }
#include <bits/stdc++.h> using namespace std; const int MAXX = 1e6 + 10; int n, m, a[MAXX], ans[MAXX], ptr; vector<int> adj[MAXX], vec, vec_pos; bool vis[MAXX]; void dfs(int v) { vis[v] = 1; vec.push_back(a[v]), vec_pos.push_back(v); for (auto x : adj[v]) if (!vis[x]) dfs(x); } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n >> m; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < m; i++) { int v, u; cin >> v >> u, v--, u--; adj[v].push_back(u), adj[u].push_back(v); } for (int i = 0; i < n; i++) { if (vis[i]) continue; dfs(i); sort(vec.begin(), vec.end(), greater<int>()); sort(vec_pos.begin(), vec_pos.end()); ptr = 0; for (auto x : vec_pos) ans[x] = vec[ptr++]; vec.clear(); vec_pos.clear(); } for (int i = 0; i < n; i++) cout << ans[i] << " "; return cout << endl, 0; }
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const double eps = 1e-8; const int mod = 1000000007; const double pi = acos(-1); inline void gn(long long &x) { int sg = 1; char c; while (((c = getchar()) < '0' || c > '9') && c != '-') ; c == '-' ? (sg = -1, x = 0) : (x = c - '0'); while ((c = getchar()) >= '0' && c <= '9') x = x * 10 + c - '0'; x *= sg; } inline void gn(int &x) { long long t; gn(t); x = t; } inline void gn(unsigned long long &x) { long long t; gn(t); x = t; } int gcd(int a, int b) { return a ? gcd(b % a, a) : b; } long long powmod(long long a, long long x, long long mod) { long long t = 1ll; while (x) { if (x & 1) t = t * a % mod; a = a * a % mod; x >>= 1; } return t; } const int maxn = 1e6 + 100; vector<int> v[maxn]; int f[maxn]; int ite[maxn] = {0}; int find(int x) { if (x == f[x]) return x; int t = find(f[x]); f[x] = t; return t; } void add(int a, int b) { a = find(a); b = find(b); f[a] = b; } bool cmp(int &a, int &b) { return b < a; } int numb[maxn]; int main() { int n, m; gn(n); gn(m); for (int i = (1); i <= (n); i++) gn(numb[i]), f[i] = i; int a, b; for (int i = (1); i <= (m); i++) { gn(a); gn(b); add(a, b); } for (int i = (1); i <= (n); i++) v[find(f[i])].push_back(numb[i]); for (int i = (1); i <= (n); i++) sort(v[i].begin(), v[i].end(), cmp); for (int i = (1); i <= (n); i++) { int f = find(i); printf("%d ", v[f][ite[f]++]); } return 0; }
#include <bits/stdc++.h> using namespace std; vector<int> parent; vector<int> s_tree; int find(int a) { if (parent[a] != a) { return parent[a] = find(parent[a]); } return a; } void merge(int a, int b) { a = find(a); b = find(b); if (a == b) return; if (s_tree[a] < s_tree[b]) { parent[a] = b; s_tree[b] += s_tree[a]; } else { parent[b] = a; s_tree[a] += s_tree[b]; } } int main() { int n, m; cin >> n >> m; vector<vector<int> > graph(n); vector<int> perm(n); vector<int> inverse(n); parent = vector<int>(n); s_tree = vector<int>(n, 1); vector<int> indices(n); vector<int> algo(n); for (int i = 0; i < n; ++i) { cin >> perm[i]; perm[i]--; inverse[perm[i]] = i; parent[i] = i; } for (int i = 0; i < m; ++i) { int a, b; cin >> a >> b; merge(a - 1, b - 1); } vector<int> next(n, -1); vector<int> follow(n); for (int i = n - 1; i >= 0; --i) { int a = find(i); follow[i] = next[a]; next[a] = i; } vector<int> sol(n); for (int i = n - 1; i >= 0; --i) { int a = find(inverse[i]); sol[next[a]] = i; next[a] = follow[next[a]]; } for (int i = 0; i < n; ++i) { cout << sol[i] + 1 << ((i == n - 1) ? '\n' : ' '); } }
#include <bits/stdc++.h> using namespace std; int n, m, a, b; int p[10 * 100005], used[10 * 100005]; vector<int> g[10 * 100005]; vector<int> v, pv; void dfs(int node) { v.push_back(node); pv.push_back(p[node]); used[node] = 1; for (int i = 0; i < g[node].size(); i++) { int to = g[node][i]; if (!used[to]) { dfs(to); } } } int main() { scanf("%d", &n); scanf("%d", &m); for (int i = 1; i <= n; i++) { scanf("%d", &p[i]); } while (m--) { scanf("%d", &a); scanf("%d", &b); g[a].push_back(b); g[b].push_back(a); } for (int i = 1; i <= n; i++) { if (!used[i]) { v.clear(); pv.clear(); dfs(i); sort(v.begin(), v.end()); sort(pv.begin(), pv.end()); reverse(pv.begin(), pv.end()); for (int j = 0; j < v.size(); j++) { p[v[j]] = pv[j]; } } } for (int i = 1; i <= n; i++) { printf("%d ", p[i]); } printf("\n"); return 0; }
#include <bits/stdc++.h> using namespace std; const int M = 1000005; int n, m, a[M], f[M]; struct node { int s, v; } p[M]; int tot, Next[M], head[M], vet[M]; void add(int x, int y) { Next[tot] = head[x], vet[tot] = y; head[x] = tot++; } int find(int x) { if (f[x] != x) f[x] = find(f[x]); return f[x]; } bool cmp(node x, node y) { return x.v < y.v; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); f[i] = i; } for (int i = 1; i <= m; i++) { int x, y; scanf("%d%d", &x, &y); x = find(x), y = find(y); if (x != y) f[y] = x; } for (int i = 1; i <= n; i++) { int x = find(i); p[i].s = x, p[i].v = a[i]; } sort(p + 1, p + n + 1, cmp); tot = 0; for (int i = 1; i <= n; i++) head[i] = -1; for (int i = 1; i <= n; i++) { add(p[i].s, p[i].v); } for (int i = 1; i <= n; i++) { if (i > 1) printf(" "); int x = find(i); int j = head[x]; int v = vet[j]; printf("%d", v); head[x] = Next[j]; } printf("\n"); return 0; }
#include <bits/stdc++.h> using namespace std; long long n, m, a, b; long long p[1000111]; long long par[1000111]; long long c; vector<long long> g[1000111]; bool used[1000111]; priority_queue<long long> curs[1000111]; void dfs(long long x) { used[x] = true; par[x] = c; curs[c].push(p[x]); for (long long i = 0; i <= (long long)g[x].size() - 1; ++i) { if (!used[g[x][i]]) dfs(g[x][i]); } } int main() { cin.sync_with_stdio(0); cin.tie(0); cin >> n >> m; for (long long i = 1; i <= n; ++i) { cin >> p[i]; } for (long long i = 1; i <= m; ++i) { cin >> a >> b; g[a].push_back(b); g[b].push_back(a); } for (long long i = 1; i <= n; ++i) { if (!used[i]) { c = i; dfs(i); } } for (long long i = 1; i <= n; ++i) { cout << curs[par[i]].top() << ' '; curs[par[i]].pop(); } }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 10; int n, m, c[maxn], fa[maxn], x, y; int getfather(int x) { if (x == fa[x]) return x; return fa[x] = getfather(fa[x]); } priority_queue<int> q[maxn]; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%d", c + i); fa[i] = i; } while (m--) { scanf("%d%d", &x, &y); int fx = getfather(x); int fy = getfather(y); if (fx != fy) fa[fx] = fy; } for (int i = 1; i <= n; i++) getfather(i); for (int i = 1; i <= n; i++) { q[fa[i]].push(c[i]); } for (int i = 1; i <= n; i++) { printf("%d ", q[fa[i]].top()); q[fa[i]].pop(); } puts(""); return 0; }
#include <bits/stdc++.h> using namespace std; vector<long long int> v[1000009]; vector<long long int> v1, v2; long long int visit[1000009]; long long int a[1000009]; void dfs(int x) { visit[x] = 1; for (int i = 0; i < v[x].size(); i++) { if (visit[v[x][i]] == 0) { v1.push_back(v[x][i]); v2.push_back(a[v[x][i]]); dfs(v[x][i]); } } } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); long long int i, j, k, l, n, m; cin >> n >> m; for (i = 1; i <= n; i++) { cin >> a[i]; } for (i = 0; i < m; i++) { cin >> j >> k; v[j].push_back(k); v[k].push_back(j); } for (i = 1; i <= n; i++) { if (visit[i] == 0) { v1.push_back(i); v2.push_back(a[i]); dfs(i); } sort(v2.rbegin(), v2.rend()); sort(v1.begin(), v1.end()); for (j = 0; j < v2.size(); j++) { a[v1[j]] = v2[j]; } v1.clear(); v2.clear(); } for (i = 1; i <= n; i++) cout << a[i] << " "; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 10; int movex[] = {0, 0, 1, -1}; int movey[] = {1, -1, 0, 0}; int parent[N], arr[N]; int find_set(int v) { if (v == parent[v]) return v; return parent[v] = find_set(parent[v]); } void union_sets(int a, int b) { a = find_set(a); b = find_set(b); if (a != b) parent[b] = a; } map<int, vector<int>> mp; int main() { static const int _ = []() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); return 0; }(); int t = 1; while (t--) { long long n, m; cin >> n >> m; for (int i = 1; i <= n; i++) cin >> arr[i]; for (int i = 1; i <= n; i++) parent[i] = i; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; union_sets(a, b); } for (int i = 1; i <= n; i++) { mp[find_set(i)].emplace_back(arr[i]); } for (auto &i : mp) { sort(i.second.begin(), i.second.end()); } vector<int> ans; for (int i = 1; i <= n; i++) { int groupi = find_set(i); int curr = mp[groupi].back(); ans.push_back(curr); mp[groupi].pop_back(); } for (auto a : ans) cout << a << " "; } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 6; int Root[N], n, m, a, b; int Find(int u) { if (Root[u] == u) return u; return Root[u] = Find(Root[u]); } priority_queue<pair<int, int>> Q[N]; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.precision(10), cout << fixed; cin >> n >> m; vector<int> v(n); for (int i = 0; i < n; ++i) cin >> v[i], Root[i] = i; while (m--) { cin >> a >> b; a--; b--; if (a == b) continue; a = Find(a); b = Find(b); Root[b] = Root[a]; } for (int i = 0; i < n; ++i) Q[Find(i)].push({v[i], i}); map<int, int> New; for (int i = 0, Family, index; i < n; ++i) { Family = Find(i); if (Q[Family].size()) { if (New.find(Q[Family].top().first) != New.end()) index = New[Q[Family].top().first]; else index = Q[Family].top().second; if (Q[Family].top().first > v[i] && index > i) { New[v[index]] = i; New[v[i]] = index; swap(v[index], v[i]); Q[Family].pop(); } else Q[Family].pop(); } } for (auto i : v) cout << i << " "; return 0; }
#include <bits/stdc++.h> using namespace std; const int mx = 1e6 + 5; int n, m; int fa[mx]; int a[mx], siz[mx]; vector<int> fat[mx]; inline int f(int x) { return fa[x] == x ? x : fa[x] = f(fa[x]); } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); fa[i] = i; } for (int i = 0; i < m; i++) { int x, y; scanf("%d%d", &x, &y); fa[f(x - 1)] = f(y - 1); } for (int i = 0; i < n; i++) fat[f(i)].push_back(a[i]); for (int i = 0; i < n; i++) { if (f(i) == i) sort(fat[i].begin(), fat[i].end()); siz[i] = fat[i].size() - 1; } for (int i = 0; i < n; i++) { printf("%d ", fat[f(i)][siz[f(i)]]); siz[f(i)]--; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; int p[n]; for (int i = 0; i < n; i++) { cin >> p[i]; } vector<int> v[n]; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; v[x - 1].push_back(y - 1); v[y - 1].push_back(x - 1); } int vis[n]; for (int i = 0; i < n; i++) { vis[i] = 0; } for (int i = 0; i < n; i++) { if (vis[i] == 0) { queue<int> q; q.push(i); vis[i] = 1; priority_queue<int> q1; priority_queue<int, vector<int>, greater<int> > q2; while (!q.empty()) { int x = q.front(); q.pop(); q1.push(p[x]); q2.push(x); for (int j = 0; j < v[x].size(); j++) { if (vis[v[x][j]] == 0) { q.push(v[x][j]); vis[v[x][j]] = 1; } } } while (!q1.empty()) { p[q2.top()] = q1.top(); q1.pop(); q2.pop(); } } } for (int i = 0; i < n; i++) { cout << p[i]; if (i != n - 1) { cout << " "; } } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int mx = 1e6 + 5; int n, m; int fa[mx]; int a[mx], siz[mx]; vector<int> fat[mx]; inline int f(int x) { return fa[x] == x ? x : fa[x] = f(fa[x]); } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); fa[i] = i; } for (int i = 0; i < m; i++) { int x, y; scanf("%d%d", &x, &y); fa[f(x - 1)] = f(y - 1); } for (int i = n - 1; i >= 0; i--) fat[f(i)].push_back(a[i]); for (int i = 0; i < n; i++) { sort(fat[i].begin(), fat[i].end()); siz[i] = fat[i].size() - 1; } for (int i = 0; i < n; i++) { printf("%d ", fat[fa[i]][siz[fa[i]]]); siz[fa[i]]--; } return 0; }
#include <bits/stdc++.h> long long mpow(long long a, long long n, long long mod) { long long ret = 1; long long b = a; while (n) { if (n & 1) ret = (ret * b) % mod; b = (b * b) % mod; n >>= 1; } return (long long)ret; } using namespace std; using namespace std; int a[(int)(1e6 + 5)], belong[(int)(1e6 + 5)], c = 1; vector<int> G[(int)(1e6 + 5)]; vector<int> comp[(int)(1e6 + 5)]; bool vis[(int)(1e6 + 5)]; void dfs(int x) { vis[x] = 1; comp[c].push_back(a[x]); belong[x] = c; for (int i = 0; i < G[x].size(); i++) { int to = G[x][i]; if (!vis[to]) { dfs(to); } } } void solve() { int n, m; scanf("%d", &n); scanf("%d", &m); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } for (int i = 1; i <= m; i++) { int x, y; scanf("%d", &x); scanf("%d", &y); G[x].push_back(y); G[y].push_back(x); } for (int i = 1; i <= n; i++) { if (!vis[i]) { dfs(i); sort(comp[c].begin(), comp[c].end()); c++; } } for (int i = 1; i <= n; i++) { a[i] = comp[belong[i]].back(); comp[belong[i]].pop_back(); printf("%d", a[i]); printf(" "); } } int main() { int t = 1; for (int i = 1; i <= t; i++) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; long long n, m, i, a[1000006], pa[1000006], s[1000006], c, d, p, q, j, k; vector<long long> v1, v2; vector<long long> v[1000006]; long long daddy(long long z) { if (z != pa[z]) pa[z] = daddy(pa[z]); return pa[z]; } int main() { cin >> n >> m; for (i = 1; i <= n; i++) { scanf("%lld", &a[i]); pa[i] = i; s[i] = 1; } for (i = 1; i <= m; i++) { scanf("%lld%lld", &c, &d); p = daddy(c); q = daddy(d); if (s[p] > s[q]) { s[p] += s[q]; pa[q] = p; } else { s[q] += s[p]; pa[p] = q; } } for (i = 1; i <= n; i++) { v[daddy(i)].push_back(i); } for (i = 1; i <= n; i++) { if (v[i].size() != 0) { for (j = 0; j < v[i].size(); j++) { v1.push_back(a[v[i][j]]); v2.push_back(v[i][j]); } } sort(v1.begin(), v1.end()); reverse(v1.begin(), v1.end()); for (j = 0; j < v1.size(); j++) { a[v2[j]] = v1[j]; } v1.clear(); v2.clear(); } for (i = 1; i <= n; i++) printf("%lld ", a[i]); return 0; }
#include <bits/stdc++.h> using namespace std; const int MAX = 1000006; int rnk[MAX], parent[MAX], ans[MAX], arr[MAX]; vector<int> comp[MAX]; priority_queue<int> q[MAX]; int find(int x) { if (x == parent[x]) return x; return parent[x] = find(parent[x]); } void merge(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (rnk[x] > rnk[y]) parent[y] = x; else { parent[x] = y; if (rnk[x] == rnk[y]) rnk[y]++; } } int main() { int n, m, a, b; scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); parent[i] = i; rnk[i] = 0; } for (int i = 0; i < m; i++) { scanf("%d%d", &a, &b); a--, b--; merge(a, b); } int maxc = 0; for (int i = 0; i < n; i++) { comp[find(i)].push_back(i); maxc = max(maxc, find(i)); q[find(i)].push(arr[i]); } for (int i = 0; i <= maxc; i++) { for (int j = 0; j < comp[i].size(); j++) { ans[comp[i][j]] = q[i].top(); q[i].pop(); } } for (int i = 0; i < n; i++) printf("%d ", ans[i]); return 0; }
#include <bits/stdc++.h> using namespace std; vector<int> v[1000010], u[1000010], poz[1000010]; int n, m, x, y, i, j, k, a[1000010]; bitset<1000010> viz; bool comp(int a, int b) { return a > b; } void dfs(int i) { viz[i] = 1; u[k].push_back(a[i]); poz[k].push_back(i); for (auto it : v[i]) if (!viz[it]) dfs(it); } int main() { scanf("%d%d", &n, &m); for (i = 1; i <= n; i++) scanf("%d", a + i); for (i = 1; i <= m; i++) { scanf("%d%d", &x, &y); v[x].push_back(y); v[y].push_back(x); } for (i = 1; i <= n; i++) if (!viz[i]) { k++; dfs(i); } for (i = 1; i <= k; i++) { sort(u[i].begin(), u[i].end(), comp); sort(poz[i].begin(), poz[i].end()); int p = u[i].size(); for (j = 0; j < p; j++) a[poz[i][j]] = u[i][j]; } for (i = 1; i <= n; i++) printf("%d ", a[i]); return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 10; int n, m, moalefe, c[maxn], arr[maxn]; vector<int> g[maxn], A[maxn]; bool visited[maxn]; void dfs(int u, int x) { A[x].push_back(arr[u]); c[u] = x; visited[u] = true; for (auto v : g[u]) { if (!visited[v]) dfs(v, x); } } int main() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); cin >> n >> m; for (int i = 1; i <= n; i++) cin >> arr[i]; for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } for (int i = 1; i <= n; i++) { if (!visited[i]) dfs(i, ++moalefe); } for (int i = 1; i <= moalefe; i++) sort(A[i].begin(), A[i].end()); for (int i = 1; i <= n; i++) { cout << A[c[i]].back() << " "; A[c[i]].pop_back(); } cout << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; const long long int N = 1e6 + 10; const long long int inf = 1e15; const long long int M = 1e9 + 7; long long int n, k, m, x, y, a[N]; vector<long long int> v[N]; bool cmp(long long int A, long long int B) { return a[A] < a[B]; } long long int parent[N], ind[N]; long long int find_set(long long int v) { if (v == parent[v]) return v; return parent[v] = find_set(parent[v]); } void make_set(long long int v) { parent[v] = v; ind[v] = rand(); } void union_sets(long long int a, long long int b) { a = find_set(a); b = find_set(b); if (a != b) { if (ind[a] < ind[b]) swap(a, b); parent[b] = a; } } void solve() { cin >> n >> m; for (long long int i = 1; i <= n; i++) { cin >> a[i], make_set(i); } for (long long int i = 1; i <= m; i++) { cin >> x >> y; union_sets(x, y); } set<long long int> s; for (long long int i = 1; i <= n; i++) { v[find_set(i)].push_back(i); s.insert(find_set(i)); } for (auto second : s) { priority_queue<long long int> q; for (auto i : v[second]) q.push(a[i]); for (auto i : v[second]) { a[i] = q.top(); q.pop(); } } for (long long int i = 1; i <= n; i++) cout << a[i] << " "; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); solve(); long long int x = 0; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 10; int n, m, a[N], visit[N]; vector<int> adj[N]; vector<int> p; void dfs(int node) { if (visit[node]) return; p.push_back(node); visit[node] = 1; for (int x : adj[node]) dfs(x); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m; for (int i = 0; i < (int)(n); ++i) cin >> a[i], --a[i]; for (int i = 0; i < (int)(m); ++i) { int u, v; cin >> u >> v; --u, --v; adj[u].push_back(v); adj[v].push_back(u); } for (int i = 0; i < (int)(n); ++i) { if (!visit[i]) { p.clear(); dfs(i); vector<int> p2; for (int x : p) p2.push_back(a[x]); sort(p2.begin(), p2.end()); sort(p.begin(), p.end()); for (int x : p) { a[x] = p2.back(); p2.pop_back(); } } } for (int i = 0; i < (int)(n); ++i) cout << a[i] + 1 << " "; cout << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; vector<long long> val[1000111]; vector<long long> pos[1000111]; long long root[1000111]; long long ans[1000111]; long long arr[1000111]; long long rooting(long long x) { if (root[x] == x) return x; else return root[x] = rooting(root[x]); } int main() { long long n, m; scanf("%lld %lld", &n, &m); for (long long i = 1; i <= n; i++) { scanf("%lld", arr + i); root[i] = i; } for (long long i = 0; i < m; i++) { long long a, b; scanf("%lld %lld", &a, &b); long long ra = rooting(a); long long rb = rooting(b); root[ra] = rb; } for (long long i = 1; i <= n; i++) { int x = rooting(i); pos[x].push_back(i); val[x].push_back(arr[i]); } for (long long i = 1; i <= n; i++) { if (val[i].size() > 0) { sort(val[i].begin(), val[i].end()); reverse(val[i].begin(), val[i].end()); for (long long j = 0; j < val[i].size(); j++) { ans[pos[i][j]] = val[i][j]; } } } for (long long i = 1; i <= n; i++) printf("%lld ", ans[i]); return 0; }
#include <bits/stdc++.h> using namespace std; vector<int> grp, value, gr[1000000 + 5]; int vis[1000000 + 5] = {}, val[1000000 + 5]; void dfs(int v) { vis[v] = 1; grp.push_back(v); value.push_back(val[v]); for (int i = 0; i < gr[v].size(); i++) { if (!vis[gr[v][i]]) dfs(gr[v][i]); } } int main() { ios::sync_with_stdio(0); cin.tie(0); int i, j, n, m, a, b, ans[1000000 + 5]; cin >> n >> m; for (i = 1; i <= n; i++) { cin >> val[i]; } for (i = 0; i < m; i++) { cin >> a >> b; gr[a].push_back(b); gr[b].push_back(a); } for (i = 1; i <= n; i++) { if (!vis[i]) { grp.clear(); value.clear(); dfs(i); sort(grp.begin(), grp.end()); sort(value.begin(), value.end()); reverse(value.begin(), value.end()); for (j = 0; j < value.size(); j++) ans[grp[j]] = value[j]; } } for (i = 1; i <= n; i++) cout << ans[i] << ' '; cout << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const double eps = 1e-8; const int mod = 1000000007; const double pi = acos(-1); inline void gn(long long& x) { int sg = 1; char c; while (((c = getchar()) < '0' || c > '9') && c != '-') ; c == '-' ? (sg = -1, x = 0) : (x = c - '0'); while ((c = getchar()) >= '0' && c <= '9') x = x * 10 + c - '0'; x *= sg; } inline void gn(int& x) { long long t; gn(t); x = t; } inline void gn(unsigned long long& x) { long long t; gn(t); x = t; } int gcd(int a, int b) { return a ? gcd(b % a, a) : b; } long long powmod(long long a, long long x, long long mod) { long long t = 1ll; while (x) { if (x & 1) t = t * a % mod; a = a * a % mod; x >>= 1; } return t; } const int maxn = 1e6 + 100; vector<int> v[maxn]; int f[maxn]; int ite[maxn] = {0}; int find(int x) { if (x == f[x]) return x; return find(f[x]); } void add(int a, int b) { a = find(a); b = find(b); f[a] = b; } bool cmp(int a, int b) { return b < a; } int numb[maxn]; int main() { int n, m; cin >> n >> m; for (int i = (1); i <= (n); i++) gn(numb[i]), f[i] = i; int a, b; for (int i = (1); i <= (m); i++) { cin >> a >> b; add(a, b); } for (int i = (1); i <= (n); i++) v[find(f[i])].push_back(numb[i]); for (int i = (1); i <= (n); i++) sort(v[i].begin(), v[i].end(), cmp); for (int i = (1); i <= (n); i++) { int f = find(i); printf("%d ", v[f][ite[f]++]); } return 0; }
#include <bits/stdc++.h> using namespace std; vector<int> num, con, a; vector<vector<int> > g(1000001); bool used[1000001]; void dfs(int v) { num.push_back(v); con.push_back(a[v - 1]); used[v] = true; if (g[v].empty()) return; for (int i = 0; i < g[v].size(); i++) if (!used[g[v][i]]) dfs(g[v][i]); return; } int main() { int n, m; cin >> n >> m; int x, y; a.assign(n, 0); for (int i = 0; i < n; i++) scanf("%d", &a[i]); for (int i = 0; i < m; i++) { scanf("%d%d", &x, &y); g[x].push_back(y); g[y].push_back(x); } for (int i = 1; i <= n; i++) { num.resize(0); con.resize(0); if (used[i]) continue; dfs(a[i - 1]); sort(num.begin(), num.end()); sort(con.rbegin(), con.rend()); for (int j = 0; j < num.size(); j++) { a[num[j] - 1] = con[j]; } } for (int i = 0; i < n; i++) cout << a[i] << ' '; return 0; }
#include <bits/stdc++.h> using namespace std; const long long N = 1e6 + 5; long long a[N], vis[N]; vector<vector<long long>> adj(N); int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, m; cin >> n >> m; for (long long i = 0; i < n; ++i) cin >> a[i + 1]; long long x, y; for (long long i = 0; i < m; ++i) { cin >> x >> y; adj[x].push_back(y), adj[y].push_back(x); } for (long long i = 1; i <= n; ++i) { if (vis[i]) continue; vis[i] = 1; queue<long long> q; q.push(i); vector<long long> pos, val; pos.push_back(i), val.push_back(a[i]); while (!q.empty()) { long long p = q.front(); q.pop(); for (long long tt : adj[p]) if (!vis[tt]) { vis[tt] = 1; q.push(tt); pos.push_back(tt), val.push_back(a[tt]); } } sort(pos.begin(), pos.end()), sort(val.rbegin(), val.rend()); for (long long j = 0; j < (long long)pos.size(); ++j) a[pos[j]] = val[j]; } for (long long i = 0; i < n; ++i) cout << a[i + 1] << ' '; return 0; }
#include <bits/stdc++.h> using namespace std; long long mod(long long n) { return n % (long long)1000000007; } vector<long long> adj[1000005]; long long vis[1000005]; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } vector<long long> s; void dfs(long long n) { s.push_back(n); vis[n] = 1; for (auto i : adj[n]) { if (vis[i] == 0) { dfs(i); } } } int main() { long long n, m; cin >> n >> m; long long p[n + 1]; for (long long i = 1; i < n + 1; i = i + 1) { cin >> p[i]; } for (long long i = 1; i < m + 1; i = i + 1) { long long x, y; cin >> x >> y; adj[x].push_back(y); adj[y].push_back(x); } long long f[n + 1]; for (long long i = 1; i < n + 1; i = i + 1) { if (vis[i] == 0) { vector<long long> ans; s.clear(); dfs(i); sort(s.begin(), s.end()); for (long long i = 0; i < s.size(); i = i + 1) { ans.push_back(p[s[i]]); } sort(ans.begin(), ans.end()); for (long long i = 0; i < s.size(); i = i + 1) { f[s[i]] = ans[s.size() - i - 1]; } } } for (long long i = 1; i < n + 1; i = i + 1) { cout << f[i] << " "; } }
#include <bits/stdc++.h> using namespace std; vector<vector<int> > adj_list; int par[1000010]; int n, m; int numb[1000010]; int x, y; vector<int> group[1000010]; vector<int> pos_group[1000010]; int part[1000010]; int ans[1000010]; int anc(int p) { if (par[p] == p) return p; return par[p] = anc(par[p]); } void join(int p, int q) { par[anc(p)] = anc(q); } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> m; for (int i = 1; i <= n; ++i) { cin >> numb[i]; par[i] = i; } for (int i = 1; i <= m; ++i) { cin >> x >> y; join(x, y); } for (int i = 1; i <= n; ++i) { part[i] = anc(i); } for (int i = 1; i <= n; ++i) { group[part[i]].push_back(numb[i]); } for (int i = n; i >= 1; --i) { pos_group[part[i]].push_back(i); } for (int i = 1; i <= n; ++i) { sort(group[i].begin(), group[i].end()); for (int j = group[i].size() - 1; j >= 0; --j) { ans[pos_group[i][j]] = group[i][j]; } } for (int i = 1; i <= n; ++i) { cout << ans[i] << endl; } }
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f, mod = 1000000007; const double pi = 3.1415926535897932, eps = 1e-6; int n, m, p[1000005], fa[1000005], a[1000005], b[1000005], siz[1000005], lst[1000005]; vector<int> v[1000005]; int find(int x) { if (fa[x] == x) return x; return fa[x] = find(fa[x]); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (siz[x] > siz[y]) swap(x, y); fa[x] = fa[y]; siz[y] += siz[x]; siz[x] = 0; } bool cmp(int x, int y) { return x > y; } int main() { scanf("%d%d", &n, &m); for (int(i) = (1); (i) <= (n); (i)++) fa[i] = i, siz[i] = 1; for (int(i) = (1); (i) <= (n); (i)++) scanf("%d", p + i); for (int(i) = (1); (i) <= (m); (i)++) scanf("%d%d", a + i, b + i), unite(a[i], b[i]); for (int(i) = (1); (i) <= (n); (i)++) fa[i] = find(i), v[fa[i]].push_back(p[i]); for (int(i) = (1); (i) <= (n); (i)++) if (!v[i].empty()) sort(v[i].begin(), v[i].end(), cmp); for (int(i) = (1); (i) <= (n); (i)++) printf("%d ", v[fa[i]][lst[fa[i]]]), lst[fa[i]]++; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = (int)1e6 + 5; vector<int> t[N]; int n, m, a[N]; bool used[N]; void bfs(int); int main() { cin >> n >> m; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0, a, b; i < m; i++) { cin >> a >> b; a--; b--; t[a].push_back(b); t[b].push_back(a); } for (int i = 0; i < n; i++) { if (!used[i]) bfs(i); } for (int i = 0; i < n; i++) cout << a[i] << " "; return 0; } void bfs(int start) { vector<int> buf, ind; queue<int> q; int v, to; int *p; used[start] = 1; q.push(start); ind.push_back(start); buf.push_back(a[start]); while (!q.empty()) { v = q.front(); q.pop(); for (int i = 0; i < t[v].size(); i++) { to = t[v][i]; if (!used[to]) { used[to] = 1; q.push(to); ind.push_back(to); buf.push_back(a[to]); } } } sort(buf.begin(), buf.end()); sort(ind.begin(), ind.end()); for (int i = 0; i < ind.size(); i++) a[ind[i]] = buf[ind.size() - 1 - i]; return; }
#include <bits/stdc++.h> using namespace std; int n, m; bool vis[1000010]; int p[1000010]; vector<int> g[1000010]; vector<int> col[1000010]; vector<int> num[1000010]; int ans[1000010]; void dfs(int v, int k) { col[k].push_back(v); num[k].push_back(p[v]); vis[v] = 1; for (int to : g[v]) if (!vis[to]) dfs(to, k); } int main() { scanf("%d %d", &n, &m); for (int i = 0; i < (n); i++) scanf("%d", &p[i]); for (int i = 0; i < (m); i++) { int a, b; scanf("%d %d", &a, &b); --a; --b; g[a].push_back(b); g[b].push_back(a); } int now = 0; for (int i = 0; i < (n); i++) if (!vis[i]) { dfs(i, now++); } for (int i = 0; i < (now); i++) { sort((col[i]).begin(), (col[i]).end()); sort((num[i]).begin(), (num[i]).end()); reverse((num[i]).begin(), (num[i]).end()); for (int j = 0; j < (num[i].size()); j++) { ans[col[i][j]] = num[i][j]; } } for (int i = 0; i < (n); i++) printf("%d%c", ans[i], i == n - 1 ? '\n' : ' '); return 0; }
#include <bits/stdc++.h> using namespace std; int A[1000005]; vector<int> G[1000005]; int B[1000005]; vector<int> v; vector<int> num; int vis[1000005]; void dfs(int u) { vis[u] = 1; v.push_back(u); for (int i = 0; i < G[u].size(); i++) if (!vis[G[u][i]]) dfs(G[u][i]); } int main() { int N, M; scanf("%d%d", &N, &M); for (int i = 1; i <= N; i++) scanf("%d", A + i); for (int i = 0; i < M; i++) { int u, v; scanf("%d%d", &u, &v); G[u].push_back(v); G[v].push_back(u); } for (int i = 1; i <= N; i++) { if (!vis[i]) { v.clear(); num.clear(); dfs(i); for (int i = 0; i < v.size(); i++) num.push_back(A[v[i]]); sort(num.rbegin(), num.rend()); sort(v.begin(), v.end()); for (int i = v.size() - 1; i >= 0; i--) { B[v[i]] = num[i]; } } } for (int i = 1; i <= N; i++) printf("%d ", B[i]); printf("\n"); return 0; }
#include <bits/stdc++.h> using namespace std; using pii = pair<int, int>; const int N = 1e6; int32_t n, m, p[N + 1]; vector<int32_t> adj[N + 1], qi, q; bool v[N + 1]; void dfs(int i) { if (v[i]) return; v[i] = true; qi.push_back(i); q.push_back(p[i]); for (int j : adj[i]) dfs(j); } int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout << fixed; cin >> n >> m; for (int i = 1; i <= n; i++) cin >> p[i]; for (int i = 1; i <= m; i++) { int a, b; cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } for (int i = 1; i <= n; i++) { if (!v[i]) { dfs(i); sort(q.begin(), q.end(), greater<int>()); sort(qi.begin(), qi.end()); for (size_t i = 0; i < q.size(); i++) p[qi[i]] = q[i]; q.clear(); qi.clear(); } } for (int i = 1; i <= n; i++) { cout << p[i] << (i == n ? '\n' : ' '); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; vector<vector<int>> ady(n + 1); vector<int> val(n + 1); for (int i = 1; i <= n; i++) { cin >> val[i]; } for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; ady[a].push_back(b); ady[b].push_back(a); } vector<int> comp(n + 1); queue<int> q; int actual = 1; for (int i = 1; i <= n; i++) { if (comp[i]) continue; comp[i] = actual; q.push(i); while (!q.empty()) { int curr = q.front(); q.pop(); for (auto a : ady[curr]) { if (comp[a]) continue; comp[a] = actual; q.push(a); } } actual++; } vector<vector<int>> nums(actual); for (int i = 1; i <= n; i++) { nums[comp[i]].push_back(val[i]); } for (int i = 1; i < actual; i++) { sort(nums[i].begin(), nums[i].end()); } vector<int> ans(n + 1); for (int i = 1; i <= n; i++) { ans[i] = nums[comp[i]].back(); nums[comp[i]].pop_back(); } for (int i = 1; i <= n; i++) cout << ans[i] << ' '; cout << '\n'; }
#include <bits/stdc++.h> using namespace std; void readi(int &x) { int v = 0, f = 1; char c = getchar(); while (!isdigit(c) && c != '-') c = getchar(); if (c == '-') f = -1; else v = v * 10 + c - '0'; while (isdigit(c = getchar())) v = v * 10 + c - '0'; x = v * f; } void readll(long long &x) { long long v = 0ll, f = 1ll; char c = getchar(); while (!isdigit(c) && c != '-') c = getchar(); if (c == '-') f = -1; else v = v * 10 + c - '0'; while (isdigit(c = getchar())) v = v * 10 + c - '0'; x = v * f; } void readc(char &x) { char c; while ((c = getchar()) == ' ') ; x = c; } void writes(string s) { puts(s.c_str()); } void writeln() { writes(""); } void writei(int x) { if (!x) putchar('0'); char a[25]; int top = 0; while (x) { a[++top] = (x % 10) + '0'; x /= 10; } while (top) { putchar(a[top]); top--; } } void writell(long long x) { if (!x) putchar('0'); char a[25]; int top = 0; while (x) { a[++top] = (x % 10) + '0'; x /= 10; } while (top) { putchar(a[top]); top--; } } int n, m, i, j, f[1000005], p[1000005]; vector<int> s[1000005], t[1000005]; int find(int x) { if (f[x] == x) return x; return f[x] = find(f[x]); } int main() { readi(n); readi(m); for (i = 1; i <= n; i++) { readi(p[i]); f[i] = i; } for (i = 1; i <= m; i++) { int x, y; readi(x); readi(y); if (find(x) != find(y)) { f[find(x)] = find(y); } } for (i = 1; i <= n; i++) { s[find(i)].push_back(i); t[find(i)].push_back(p[i]); } for (i = 1; i <= n; i++) { if (!t[i].empty()) { stable_sort((t[i]).begin(), (t[i]).end(), greater<int>()); for (j = 0; j < s[i].size(); j++) { p[s[i][j]] = t[i][j]; } } } for (i = 1; i <= n; i++) writei(p[i]), printf(" "); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1000001; int vis[N]; int arr[N]; vector<int> adj[N]; void BFS(int src) { queue<int> q; priority_queue<int> val; priority_queue<int> index; q.push(src); vis[src] = 1; while (!q.empty()) { int u = q.front(); q.pop(); index.push(-u); val.push(arr[u]); for (int v : adj[u]) { if (vis[v] == 0) { vis[v] = 1; q.push(v); } } } while (!index.empty()) { int idx = index.top() * -1; int num = val.top(); index.pop(); val.pop(); arr[idx] = num; } } int main() { int t, n, m, a, b; cin >> n >> m; for (int i = 1; i <= n; i++) cin >> arr[i]; for (int i = 1; i <= m; i++) { cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } for (int i = 1; i <= n; i++) if (vis[i] == 0) BFS(i); for (int i = 1; i <= n; i++) cout << arr[i] << ' '; cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; vector<int> padre; vector<int> tama; int encontrar(int a) { if (padre[a] == a) { return a; } else { padre[a] = encontrar(padre[a]); return padre[a]; } } void unir(int a, int b) { a = encontrar(a); b = encontrar(b); if (a == b) { return; } if (tama[a] >= tama[b]) { padre[b] = a; tama[a] = tama[a] + tama[b]; return; } padre[a] = b; tama[b] = tama[b] + tama[a]; return; } int main() { int n, m; cin >> n >> m; vector<int> permutacion(n); vector<vector<int> > grupos(n + 1); vector<int> actual(n + 1, 0); tama.resize(n, 1); padre.resize(n); for (int i = 0; i < n; i++) { padre[i] = i; } for (int i = 0; i < n; i++) { cin >> permutacion[i]; } while (m--) { int a, b; cin >> a >> b; a--; b--; unir(a, b); } for (int i = 0; i < n; i++) { grupos[encontrar(i)].push_back(permutacion[i]); } for (int i = 0; i < n; i++) { sort(grupos[i].begin(), grupos[i].end(), greater<int>()); } for (int i = 0; i < n; i++) { int grupo = encontrar(i); cout << grupos[grupo][actual[grupo]] << " "; actual[grupo]++; } cout << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; vector<int> g[(int)1e6 + 6]; bool visited[(int)1e6 + 6]; int a[(int)1e6 + 6]; vector<int> s, t; void dfs(int i) { if (!visited[i]) { s.push_back(a[i]); t.push_back(i); visited[i] = true; for (auto j : g[i]) dfs(j); } return; } int main() { std::ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } for (int i = 1; i <= n; i++) { dfs(i); sort(s.begin(), s.end(), greater<int>()); sort(t.begin(), t.end()); auto it = s.begin(); for (auto i : t) { a[i] = *it; it++; } s.clear(); t.clear(); } for (int i = 1; i <= n; i++) cout << a[i] << ' '; cout << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; int v[1000005], t[1000005]; set<int> s[1000005]; int findt(int nod) { if (t[nod] == nod) return nod; return t[nod] = findt(t[nod]); } int main() { int n, m, x, y, i, aux; scanf("%d%d", &n, &m); for (i = 1; i <= n; i++) scanf("%d", &v[i]), t[i] = i; for (i = 1; i <= m; i++) { scanf("%d%d", &x, &y); t[findt(x)] = findt(y); } for (i = 1; i <= n; i++) s[findt(i)].insert(v[i]); for (i = 1; i <= n; i++) { aux = findt(i); printf("%d ", *(--s[aux].end())); s[aux].erase(--s[aux].end()); } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; vector<int> g[N], pos1, pos2; int n, m, p[N], x, y, ans[N]; bool used[N]; void dfs(int v) { used[v] = true; for (int i = 0; i < g[v].size(); i++) { int to = g[v][i]; if (!used[to]) { dfs(to); } } pos1.push_back(v); pos2.push_back(p[v]); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 1; i <= n; i++) { cin >> p[i]; } for (int i = 1; i <= m; i++) { cin >> x >> y; g[x].push_back(y); g[y].push_back(x); } for (int i = 1; i <= n; i++) { if (!used[i]) { dfs(i); sort(pos1.rbegin(), pos1.rend()); sort(pos2.begin(), pos2.end()); for (int j = 0; j < pos1.size(); j++) { ans[pos1[j]] = pos2[j]; } pos1.clear(); pos2.clear(); } } for (int i = 1; i <= n; i++) { cout << ans[i] << " "; } }
#include <bits/stdc++.h> using namespace std; int n, m; int a[1000007], ans[1000007]; vector<int> adj[1000007]; vector<int> temp1, temp2; bool visited[1000007]; void dfs(int i) { visited[i] = true; temp1.push_back(i); temp2.push_back(a[i]); for (auto j : adj[i]) if (!visited[j]) dfs(j); } void func() { for (int i = 1; i <= n; i++) if (!visited[i]) { dfs(i); sort(temp1.begin(), temp1.end()); sort(temp2.begin(), temp2.end()); for (int j = 0; j < temp2.size(); j++) ans[temp1[j]] = temp2[temp2.size() - j - 1]; temp1.clear(); temp2.clear(); } for (int i = 1; i <= n; i++) printf("%d ", ans[i]); printf("\n"); } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); while (m--) { int u, v; scanf("%d%d", &u, &v); adj[u].push_back(v); adj[v].push_back(u); } func(); return 0; }
#include <bits/stdc++.h> using namespace std; int p[1000001], a[1000001], b[1000001], ans[1000001], vis[1000001], cnt; vector<int> ve[1000001]; void dfs(int n); void solve(); bool cmp(int a, int b); int main() { int n, m, num1, num2; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &p[i]); for (int i = 1; i <= m; i++) { scanf("%d%d", &num1, &num2); ve[num1].push_back(num2); ve[num2].push_back(num1); } for (int i = 1; i <= n; i++) if (!vis[i]) { cnt = 0; dfs(i); solve(); } for (int i = 1; i <= n; i++) { if (i != 1) printf(" "); printf("%d", ans[i]); } cout << endl; return 0; } void dfs(int n) { vis[n] = 1; cnt++; a[cnt] = p[n]; b[cnt] = n; int m = ve[n].size(); for (int i = 0; i < m; i++) { if (!vis[ve[n][i]]) dfs(ve[n][i]); } } void solve() { sort(a + 1, a + cnt + 1, cmp); sort(b + 1, b + cnt + 1); for (int i = 1; i <= cnt; i++) ans[b[i]] = a[i]; } bool cmp(int a, int b) { return a > b; }
#include <bits/stdc++.h> using namespace std; vector<vector<long long> > adj; vector<bool> visited; void dfs(long long r, vector<long long>& temp) { for (long long i = 0; i < adj[r].size(); i++) { long long c = adj[r][i]; if (visited[c]) continue; temp.push_back(c); visited[c] = true; dfs(c, temp); } } int main() { ios_base::sync_with_stdio(false); long long n, m; cin >> n >> m; long long p[n]; for (long long i = 0; i < n; i++) { cin >> p[i]; } adj.resize(n); for (long long i = 0; i < m; i++) { long long u, v; cin >> u >> v; u--; v--; adj[u].push_back(v); adj[v].push_back(u); } visited.resize(n, false); vector<long long> res(n); for (long long i = 0; i < n; i++) { if (visited[i]) continue; visited[i] = true; vector<long long> temp; temp.push_back(i); dfs(i, temp); vector<long long> val; sort(temp.begin(), temp.end()); for (long long i = 0; i < temp.size(); i++) { val.push_back(p[temp[i]]); } sort(val.begin(), val.end(), greater<long long>()); for (long long i = 0; i < temp.size(); i++) { p[temp[i]] = val[i]; } } for (long long i = 0; i < n; i++) { cout << p[i] << ' '; } cout << endl; return 0; }