QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#866792 | #9881. Diverge and Converge | qojszt | RE | 1ms | 3712kb | C++20 | 3.1kb | 2025-01-22 18:51:59 | 2025-01-22 18:51:59 |
Judging History
answer
#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
int n;
pair<int, int> e1[2010], e2[2010];
vector<int> tr1[1010], tr2[1010];
vector<pair<int, int> > op1;
vector<pair<int, int> > op2;
bool md[1010];
void del(vector<int>&rf, int v){
rf.erase(find(rf.begin(), rf.end(), v));
}
bool in(vector<int> *G, int elem, int u, int fa){
if (u == elem)return 1;
for (int v : G[u])if (v ^ fa)if (in(G, elem, v, u))return 1;
return 0;
}
bool same(const pair<int, int> &a, const pair<int, int> &b){
return a == b || (a.second == b.first && a.first == b.second);
}
template <typename _Tp>
void ins(vector<_Tp> &rf, int i, const vector<_Tp> &o){
rf.insert(rf.begin() + i, o.begin(), o.end());
}
void get(){
if (accumulate(md + 1, md + n + 1, 0) == 1)return;//only 1 point
vector<int> deg1(n + 1, 1e9), deg2(n + 1, 1e9);
for (int i = 1; i <= n; ++i)
if (md[i])deg1[i] = tr1[i].size(), deg2[i] = tr2[i].size();
vector<int> deg = deg1;
for (int i = 1; i <= n; ++i)deg[i] += deg2[i];
int u = min_element(deg.begin(), deg.end()) - deg.begin();
md[u] = 0;
if (deg1[u] == 1 && deg2[u] == 1){
int v1 = tr1[u][0], v2 = tr2[u][0];
del(tr1[v1], u);del(tr2[v2], u);
get();
op1.emplace_back(u, v1);
op2.emplace_back(u, v2);
return;
}
#define CHK(deg1, deg2, tr1, tr2, op1, op2)\
if (deg1[u] == 1 && deg2[u] == 2){\
int a = tr1[u][0], b = tr2[u][0], c = tr2[u][1];\
if (in(tr2, a, c, u))swap(b, c);/*a in subtree `b`, (u,b) on path u to a*/\
del(tr1[u], a);del(tr2[u], b);del(tr2[u], c);\
tr2[b].emplace_back(c);\
get();\
int i = find_if(op1.begin(), op1.end(), bind(same, make_pair(b, c), placeholders::_1)) - op1.begin();\
int d = op2[i].first, e = op2[i].second;\
\
op1.erase(op1.begin() + i);ins(op1, i, {make_pair(u, a), make_pair(u, b)});\
op2.erase(op2.begin() + i);ins(op2, i, {make_pair(u, b), make_pair(d, e)});\
/*swap((b,c),(d,e)) -> swap((u,a),(u,b)) and swap((u,c),(d,e))*/\
}
CHK(deg1, deg2, tr1, tr2, op1, op2)
CHK(deg2, deg1, tr2, tr1, op2, op1)
//for (int i = 1; i <= n; ++i)clog << md[i] << " "; clog << endl;
throw (-1);
}
int main(){
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
cin >> n;
for (int i = 1, u, v; i < n; ++i){
cin >> u >> v;
e1[i] = {u, v};
tr1[u].emplace_back(v);
tr1[v].emplace_back(u);
}
for (int i = 1, u, v; i < n; ++i){
cin >> u >> v;
e2[i] = {u, v};
tr2[u].emplace_back(v);
tr2[v].emplace_back(u);
}
fill_n(md + 1, n, 1);
get();
for (auto v : op1)cout << find_if(e1 + 1, e1 + n, bind(same, v, placeholders::_1)) - e1 << " ";
cout << endl;
for (auto v : op2)cout << find_if(e2 + 1, e2 + n, bind(same, v, placeholders::_1)) - e2 << " ";
return 0;
}
/*
4
1 2
2 3
3 4
1 2
2 4
2 3
3 1 2
2 1 3
*/
/*
2
1 2
2 1
1
1
*/
/*
1 2
1 3
2 4
2 5
3 6
3 7
1 5
1 6
1 7
5 2
6 3
7 4
1 2 3 4 5 6
1 2 6 4 5 3
*/
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3712kb
input:
4 1 2 2 3 3 4 1 2 2 4 2 3
output:
2 3 1 3 2 1
result:
ok Correct, N = 4
Test #2:
score: 0
Accepted
time: 1ms
memory: 3712kb
input:
2 1 2 2 1
output:
1 1
result:
ok Correct, N = 2
Test #3:
score: -100
Runtime Error
input:
7 1 2 1 3 2 4 2 5 3 6 3 7 1 5 1 6 1 7 5 2 6 3 7 4