QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#371264 | #7798. Colorful Village | kevinyang# | RE | 0ms | 0kb | C++17 | 3.3kb | 2024-03-30 03:39:40 | 2024-03-30 03:39:42 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
struct SCC {
int ind, top; vector<int> id, low, stk; vector<vector<int>> components;
int getTo(int e) { return e; }
template <class T> int getTo(const pair<int, T> &e) { return e.first; }
template <class Digraph> void dfs(const Digraph &G, int v) {
id[stk[top++] = v] = -1; int mn = low[v] = ind++; for (auto &&e : G[v]) {
int w = getTo(e); if (id[w] == -2) dfs(G, w);
mn = min(mn, low[w]);
}
if (mn < low[v]) { low[v] = mn; return; }
int w; components.emplace_back(); do {
id[w = stk[--top]] = components.size() - 1; low[w] = INT_MAX;
components.back().push_back(w);
} while (w != v);
}
template <class Digraph> SCC(const Digraph &G)
: ind(0), top(0), id(G.size(), -2), low(G.size()), stk(G.size()) {
for (int v = 0; v < int(G.size()); v++) if (id[v] == -2) dfs(G, v);
}
template <class Digraph>
SCC(const Digraph &G, vector<pair<int, int>> &condensationEdges) : SCC(G) {
vector<int> last(components.size(), -1);
for (auto &&comp : components) for (int v : comp) for (auto &&e : G[v]) {
int w = getTo(e); if (id[v] != id[w] && last[id[w]] != id[v])
condensationEdges.emplace_back(last[id[w]] = id[v], id[w]);
}
}
};
const int mxn = 400005;
vector<int>centroids;
vector<vector<int>>adj(mxn);
vector<int>sz(mxn);
vector<int>a(mxn);
vector<int>b(mxn);
vector<int>c(mxn);
int n;
void dfs(int u, int p){
sz[u] = 1;
int mx = 0;
for(int nxt: adj[u]){
if(nxt==p)continue;
dfs(nxt,u);
sz[u]+=sz[nxt];
mx = max(mx,sz[nxt]);
}
mx = max(mx,n-sz[u]);
if(mx<=n/2){
centroids.push_back(u);
}
}
vector<vector<int>>g(mxn);
void dfs1(int u, int p){
for(int nxt: adj[u]){
if(nxt==p)continue;
g[nxt].push_back(u);
g[u+n].push_back(nxt+n);
dfs1(nxt,u);
}
}
vector<int>ans;
bool solve(int x){
for(int i = 1; i<=2*n; i++){
g[i].clear();
}
dfs1(x,0);
for(int i = 1; i<=n/2; i++){
g[a[i]].push_back(b[i]+n);
g[b[i]+n].push_back(a[i]);
g[a[i]+n].push_back(b[i]);
g[b[i]].push_back(a[i]+n);
}
SCC scc(g);
for(int i = 1; i<=n; i++){
if(scc.id[i]==scc.id[i+n]){
return false;
}
}
vector<int>order(2*n+1);
for(int i = 0; i<scc.components.size(); i++){
for(auto nxt: scc.components[i]){
order[nxt] = i;
}
}
for(int i = 1; i<=n; i++){
if(order[i] < order[i+n]){
ans.push_back(i);
}
}
assert(ans.size() == n/2);
return true;
}
void reset(int n){
for(int i = 1; i<=2*n; i++){
adj[i].clear();
g[i].clear();
a[i] = b[i] = c[i] = sz[i] = 0;
}
centroids.clear();
ans.clear();
}
signed main(){
cin.tie(nullptr)->sync_with_stdio(false);
int t;
cin >> t;
while(t--){
cin >> n; n*=2;
int m = n/2;
for(int i = 1; i<=n; i++){
cin >> c[i];
if(a[c[i]]){
b[c[i]] = i;
}
else{
a[c[i]] = i;
}
}
for(int i = 1; i<n; i++){
int x,y;
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
dfs(1,0);
bool found = false;
for(int nxt: centroids){
bool res = solve(nxt);
if(res){
for(int i = 0; i<m; i++){
cout << ans[i];
if(i+1<m)cout << ' ';
}
cout << '\n';
found = true;
break;
}
}
if(!found){
cout << "-1\n";
}
reset(n);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
input:
2 4 1 3 1 3 4 4 2 2 1 6 5 3 2 4 7 1 7 5 5 8 2 5 3 1 1 2 2 3 3 1 2 2 3 3 4 4 5 5 6