QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#866795#9881. Diverge and ConvergeqojsztWA 1ms3712kbC++203.0kb2025-01-22 19:01:392025-01-22 19:01:39

Judging History

你现在查看的是最新测评结果

  • [2025-01-22 19:01:39]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3712kb
  • [2025-01-22 19:01:39]
  • 提交

answer

#include <bits/stdc++.h>
#define F first
#define S second
#define eb emplace_back
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.S == b.F && a.F == b.S);}
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.eb(u, v1);op2.eb(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[a], u);del(tr2[b], u);del(tr2[c], u);tr2[b].eb(c);tr2[c].eb(b);get();\
        int i = find_if(op1.begin(), op1.end(), bind(same, make_pair(b, c), placeholders::_1)) - op1.begin();\
        int d = op2[i].F, e = op2[i].S;\
        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))*/\
        return;\
    }

    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].eb(v);tr1[v].eb(u);}
    for (int i = 1, u, v; i < n; ++i){cin >> u >> v;e2[i] = {u, v};tr2[u].eb(v);tr2[v].eb(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
*/
/*
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
*/

详细

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: 3584kb

input:

2
1 2
2 1

output:

1 
1 

result:

ok Correct, N = 2

Test #3:

score: -100
Wrong Answer
time: 1ms
memory: 3712kb

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

output:

5 2 4 7 6 3 
5 2 4 4 3 6 

result:

wrong answer Integer 7 violates the range [1, 6]