QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#531566#5088. Two ChoreographiesTiga_Pilot_2RE 0ms3824kbC++203.5kb2024-08-24 21:10:272024-08-24 21:10:31

Judging History

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

  • [2024-08-24 21:10:31]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:3824kb
  • [2024-08-24 21:10:27]
  • 提交

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;
	}
};

template<class T>
struct RMQ {
	vector<vector<T>> jmp;
	RMQ(const vector<T>& V) : jmp(1, V) {
		for (int pw = 1, k = 1; pw * 2 <= sz(V); pw *= 2, ++k) {
			jmp.emplace_back(sz(V) - pw * 2 + 1);
			rep(j,0,sz(jmp[k]))
				jmp[k][j] = min(jmp[k - 1][j], jmp[k - 1][j + pw]);
		}
	}
	T query(int a, int b) {
		// assert(a < b); // or return inf if a == b
		int dep = 31 - __builtin_clz(b - a);
		return min(jmp[dep][a], jmp[dep][b - (1 << dep)]);
	}
};

struct LCA {
	int T = 0;
	vi time, path, ret,depth,pr;
	RMQ<int> rmq;

	LCA(vector<vi>& C) : time(sz(C)), depth(sz(C),0),pr(n,0), rmq((dfs(C,0,-1), ret)) {}
	void dfs(vector<vi>& C, int v, int par) {
		time[v] = T++;
		for (int y : C[v]) if (y != par) {
			path.push_back(v), ret.push_back(time[v]);
            depth[y] = depth[v]+1;
            pr[y] = v;
			dfs(C, y, v);
		}
	}

	int lca(int a, int b) {
		if (a == b) return a;
		tie(a, b) = minmax(time[a], time[b]);
		return path[rmq.query(a, b)];
	}
	int dist(int a,int b){return depth[a] + depth[b] - 2*depth[lca(a,b)];}
};

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});
        }
    }
    LCA lca(adj);
    map<int,pii> mp;
    auto crtAns = [&](vi& ans, int u, int v) -> void {
        vi tmp1, tmp2;
        int lc = lca.lca(u,v);
        int x = u;
        while(x!=lc) {
            tmp1.push_back(x);
            x = lca.pr[x];
        }
        x = v;
        while(x!=lc) {
            tmp2.push_back(x);
            x = lca.pr[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 = lca.dist(u,v);
        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};
    }
}

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

详细

Test #1:

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

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

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

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
Runtime Error

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:


result: