voiddfs(int u){ vis[u] = true; v.push_back(a[u]); pos.push_back(u); for (int v: g[u]) { if (!vis[v]) { dfs(v); } } }
intmain(){ int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) { scanf("%d", &a[i]); p[a[i]] = 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(); pos.clear(); dfs(i); sort(v.begin(), v.end(), greater<int> ()); sort(pos.begin(), pos.end()); for (int j = 0; j < pos.size(); ++j) { ans[pos[j]] = v[j]; } } } for (int i = 1; i <= n; ++i) { printf("%d ", ans[i]); } return0; }