QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#531589#5088. Two ChoreographiesTiga_Pilot_2WA 0ms3688kbC++203.4kb2024-08-24 21:15:312024-08-24 21:15:32

Judging History

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

  • [2024-08-24 21:15:32]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3688kb
  • [2024-08-24 21:15:31]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define per(i, a, b) for(int i = a; i > (b); --i)
#define ar array
#define sz(x) (int) (x).size()
#define pii pair<int,int>
#define fi first
#define se second
typedef long long ll;
typedef pair<ll,ll> pll;
typedef pair<double,double> pdd;
typedef pair<double,int> pdi;
typedef vector<int> vi;
#define all(x) (x).begin(), (x).end()

template<typename T>
void min_self(T& A, T B) {
    A = min(A,B);
}
template<typename T>
void max_self(T& A, T B) {
    A = max(A,B);
}

const int mxn=1e5;
int n;

struct UF {
	vi e;
	UF(int n) : e(n, -1) {}
	bool sameSet(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;
	}
};

vector<vi> treeJump(vi& P){
	int on = 1, d = 1;
	while(on < sz(P)) on *= 2, d++;
	vector<vi> jmp(d, P);
	rep(i,1,d) rep(j,0,sz(P))
		jmp[i][j] = jmp[i-1][jmp[i-1][j]];
	return jmp;
}

int jmp(vector<vi>& tbl, int nod, int steps){
	rep(i,0,sz(tbl))
		if(steps&(1<<i)) nod = tbl[i][nod];
	return nod;
}

int lca(vector<vi>& tbl, vi& depth, int a, int b) {
	if (depth[a] < depth[b]) swap(a, b);
	a = jmp(tbl, a, depth[a] - depth[b]);
	if (a == b) return a;
	for (int i = sz(tbl); i--;) {
		int c = tbl[i][a], d = tbl[i][b];
		if (c != d) a = c, b = d;
	}
	return tbl[0][a];
}


void solve() {
    cin >>n;
    UF uf(n);
    vector<pii> exc;
    vector adj(n, vi());
    rep(i,0,n*2-3) {
        int u,v; cin >>u >>v; u--,v--;
        if(uf.join(u,v)) {
            adj[u].push_back(v);
            adj[v].push_back(u);
        } else {
            exc.push_back({u,v});
        }
    }
    vi par,dp;
    par.resize(n,0);
    dp.resize(n,0);
    function<void(int,int)> dfs;
    dfs = [&](int u, int pr) -> void {
        for(int v: adj[u]) {
            if(v==pr) continue;
            par[v] = u;
            dp[v] = dp[u]+1;
            dfs(v,u);
        }
    };
    dfs(0,-1);
    auto tbl = treeJump(par);
    map<int,pii> mp;
    auto crtAns = [&](vi& ans, int u, int v) -> void {
        vi tmp1, tmp2;
        int lc = lca(tbl, dp, u,v);
        int x = u;
        while(x!=lc) {
            tmp1.push_back(x);
            x = par[x];
        }
        x = v;
        while(x!=lc) {
            tmp2.push_back(x);
            x = par[x];
        }
        rep(i,0,sz(tmp1)) {
            ans.push_back(tmp1[i]);
        }
        ans.push_back(lc);
        per(i,sz(tmp2)-1,-1) {
            ans.push_back(tmp2[i]);
        }
    };
    for(auto [u,v]: exc) {
        int ds = dp[u]+dp[v]-dp[lca(tbl, dp,u,v)]*2;
        if(mp.count(ds)) {
            cout <<ds+1 <<"\n";
            vi ans1,ans2;
            crtAns(ans1, u,v);
            auto [u2,v2] = mp[ds];
            crtAns(ans2, u2, v2);
            rep(i,0,sz(ans1)) {
                cout <<ans1[i]+1 <<" \n"[i==sz(ans1)-1];
            }
            rep(i,0,sz(ans2)) {
                cout <<ans2[i]+1 <<" \n"[i==sz(ans2)-1];
            }
            return;
        }
        mp[ds] = {u,v};
    }
    cout <<"-1\n";
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    solve();
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3652kb

input:

4
1 2
1 3
1 4
2 3
2 4

output:

3
2 1 4
2 1 3

result:

ok 

Test #2:

score: 0
Accepted
time: 0ms
memory: 3688kb

input:

5
1 2
1 3
1 4
1 5
2 3
2 5
3 4

output:

3
2 1 5
2 1 3

result:

ok 

Test #3:

score: 0
Accepted
time: 0ms
memory: 3652kb

input:

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

output:

4
4 3 1 2
6 5 2 1

result:

ok 

Test #4:

score: -100
Wrong Answer
time: 0ms
memory: 3596kb

input:

40
1 16
1 40
2 4
2 16
2 36
3 25
3 38
4 1
4 13
5 11
5 27
6 4
7 5
7 11
8 10
8 14
8 24
9 34
10 20
12 35
13 2
14 10
14 20
15 18
15 28
15 31
16 6
16 13
17 5
17 11
17 27
18 9
19 1
19 4
19 16
20 24
21 12
21 33
21 35
22 38
23 12
23 21
25 28
25 31
25 34
25 38
26 14
26 20
27 7
27 11
28 3
28 31
29 16
29 19
30 ...

output:

1
14 1 10
7 1 11

result:

wrong answer Integer 1 violates the range [3, 40]