QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#531977#5088. Two ChoreographiesTiga_Pilot_2TL 1ms4068kbC++204.5kb2024-08-24 23:28:182024-08-24 23:28:18

Judging History

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

  • [2024-08-24 23:28:18]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:4068kb
  • [2024-08-24 23:28:18]
  • 提交

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(sz(C),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 adj(n, vi()),adj2(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 {
            adj2[u].push_back(v);
        }
    }
    vector st(n, set<int>());
    rep(i,0,n) {
        st[uf.find(i)].insert(i);
    }
    rep(i,0,n) {
        if(sz(st[i])==0) continue;
        map<int,int> toId,toU;
        int id= 0;
        for(auto u: st[i]) {
            toId[u] = id;
            toU[id] = u;
            id++;
        }
        vector adji(id, vi());
        for(auto u: st[i]) {
            for(auto v: adj[u]) {
                adji[toId[u]].push_back(toId[v]);
                adji[toId[v]].push_back(toId[u]);
            }   
        }
        LCA lca(adji);
        map<int,pii> mp;
        auto crtAns = [&](vi& ans, int u, int v) -> void {
            vi tmp1, tmp2;
            int iu = toId[u], iv=toId[v];
            int lc = lca.lca(iu,iv);
            int x = iu;
            while(x!=lc) {
                tmp1.push_back(toU[x]);
                x = lca.pr[x];
            }
            x = iv;
            while(x!=lc) {
                tmp2.push_back(toU[x]);
                x = lca.pr[x];
            }
            rep(j,0,sz(tmp1)) {
                ans.push_back(tmp1[j]);
            }
            ans.push_back(toU[lc]);
            per(j,sz(tmp2)-1,-1) {
                ans.push_back(tmp2[j]);
            }
        };
        for(auto u: st[i]) {
            for(auto v: adj2[u]) {
                int iu = toId[u], iv=toId[v];
                int ds = lca.dist(iu,iv);
                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(j,0,sz(ans1)) {
                        cout <<ans1[j]+1 <<" \n"[j==sz(ans1)-1];
                    }
                    rep(j,0,sz(ans2)) {
                        cout <<ans2[j]+1 <<" \n"[j==sz(ans2)-1];
                    }
                    return;
                }
                mp[ds] = {u,v};
            }
        }
    }
    assert(false);
    cout <<"-1\n";
}

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

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

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

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:

3
2 5 6
1 2 5

result:

ok 

Test #4:

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

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:

4
16 2 4 6
4 2 16 1

result:

ok 

Test #5:

score: 0
Accepted
time: 1ms
memory: 4068kb

input:

201
1 7
1 114
1 119
2 49
2 93
4 197
5 139
6 1
6 27
7 39
7 121
8 127
9 130
9 145
11 106
11 136
11 193
12 2
12 116
13 55
13 69
13 105
13 187
13 196
14 144
14 177
15 127
15 134
15 145
15 155
15 184
15 199
16 96
16 177
17 20
21 100
22 68
22 71
22 81
22 142
23 148
23 190
24 12
24 81
24 89
25 158
25 159
2...

output:

5
48 39 7 1 119
48 39 7 1 114

result:

ok 

Test #6:

score: -100
Time Limit Exceeded

input:

8000
2 1508
2 3068
3 5268
3 5501
6 266
6 2737
6 3197
6 5863
6 6697
7 3492
9 427
9 794
9 3114
9 5509
10 2257
10 4348
11 1479
11 1957
11 2230
11 2500
11 3182
11 4399
11 5051
11 7727
12 7669
13 1403
13 5753
14 2871
14 6956
14 7959
15 6902
17 1630
17 3155
17 5950
18 7232
19 125
19 3280
19 5648
20 6879
2...

output:


result: