• CF 793 div2 E 题解


    793div2 E

    可上 CF 看本题解

    建模不多说,你会把排列拆成若干个轮换,然后对于长为 \(k\) 的轮换,会且仅会用 \(k-1\) 次交换(因为题目保证用的次数是最少的)。

    把这些交换抓出来建图,会得到一个森林,你需要给每条边定向,使得每棵树的拓扑序都可以和原轮换循环同构。

    考虑如果满足一个 \(i\to p_i\) 的置换,那么你操作的顺序一定是在树上从 \(i\) 开始走然后走到 \(p_i\),这样的路径是唯一的。设经过了 \(m\) 条边分别是 \(e_1,e_2,\cdots,e_m\),这 \(m\) 条边会有严格拓扑序 \(\mathrm{opt}_{e_1}<\mathrm{opt}_{e_2}<\cdots<\mathrm{opt}_{e_m}\)

    而这 \(m\) 条边有这些拓扑序恰是满足 \(i\to p_i\) 置换的充要条件。

    充分:按这个拓扑序 \(i\) 可以走到 \(p_i\)

    必要:树上路径唯一。

    所以对于每个 \(i\to p_i\),在树上抓出这些路径然后加上有向边,最后拓扑排序即可。

    这样做是对的,因为一条边是交换两个数字,最多只会影响两个置换,所以会且恰好会被两条路径覆盖。

    总时间复杂度是 \(O(n)\) 的。

    关于怎么抓出路径,无根转有根后暴力跳 \(\mathrm{lca}\) 即可。

    贴个代码以免有人说我在口胡

    // Problem: E. Unordered Swaps
    // From: Codeforces - Codeforces Round #793 (Div. 2)
    // URL: https://codeforces.com/contest/1682/problem/E
    // Time: 2022-05-22 22:36
    // Author: lingfunny
    
    #include <bits/stdc++.h>
    #define LL long long
    using namespace std;
    const int mxn = 2e5+10;
    
    int n, m, a[mxn], p[mxn], dep[mxn], in[mxn];
    
    typedef pair<int, int> edge;
    vector <edge> G[mxn];
    edge fa[mxn];
    vector <int> nodes, g[mxn];
    
    inline void adde(int u, int v) { ++in[v]; g[u].push_back(v); }
    
    void dfs(int u, int f) {
    	dep[u] = dep[f] + 1; nodes.push_back(u);
    	for(auto [id, v]: G[u]) if(v != f) fa[v] = {id, u}, dfs(v, u);
    }
    
    inline void solve(int u) {
    	vector <int>().swap(nodes);
    	dfs(u, 0);
    	for(int x: nodes) {
    		int y = p[x];	// x -> y
    		vector <int> fx, fy;
    		while(dep[x] > dep[y]) fx.push_back(fa[x].first), x = fa[x].second;
    		while(dep[y] > dep[x]) fy.push_back(fa[y].first), y = fa[y].second;
    		while(x != y) {
    			fx.push_back(fa[x].first), x = fa[x].second;
    			fy.push_back(fa[y].first), y = fa[y].second;
    		}
    		reverse(fy.begin(), fy.end());
    		fx.insert(fx.end(), fy.begin(), fy.end());
    		for(int i = 1; i < (int)fx.size(); ++i)
    		adde(fx[i-1], fx[i]);
    	}
    }
    
    signed main() {
    	scanf("%d%d", &n, &m);
    	for(int i = 1; i <= n; ++i) scanf("%d", p+i);
    	for(int i = 1; i <= m; ++i) {
    		int x, y; scanf("%d%d", &x, &y);
    		G[x].push_back({i, y});
    		G[y].push_back({i, x});
    	}
    	for(int i = 1; i <= n; ++i) if(!dep[i]) solve(i);
    	queue <int> q;
    	for(int i = 1; i <= m; ++i) if(!in[i]) q.push(i);
    	while(q.size()) {
    		int u = q.front(); q.pop();
    		printf("%d ", u);
    		for(int v: g[u]) if(--in[v] == 0) q.push(v);
    	}
    	return 0;
    }
    
  • 相关阅读:
    Writing and deploying a custom report in Visual Studio for Dynamics 365 /PowerApps
    Integrating Dynamics 365 CE with PowerApps
    Creating a console application to import data to Dynamics 365 / Power Apps
    DNS仍然是整个互联网中最脆弱的一环
    域名解析TTL值设置为多少合适?
    DNS TTL 字段就是骗你的
    DNS域名轮询业务监控
    域名到站点的负载均衡技术一览
    nginx+iis实现负载均衡
    用Node.js基于Express框架重写博客程序,从此告别ASP.NET。
  • 原文地址:https://www.cnblogs.com/lingfunny/p/16361143.html
Copyright © 2020-2023  润新知