Codeforces 757F Team Rocket Rises Again 支配树

Team Rocket Rises Again

直接求支配树就好啦。

#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define ull unsigned long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
#define fio ios::sync_with_stdio(false); cin.tie(0);

using namespace std;

const int N = 2e5 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double PI = acos(-1);

template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < 0) a += mod;}
template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;}

int n, m, s, deg[N];
int pa[N][20], fa[N], depth[N];
int ans, son[N];
LL d[N];
vector<PLI> G[N];
vector<int> dG[N];
vector<int> rG[N];
vector<int> tG[N];

int getLca(int u, int v) {
    if(depth[u] < depth[v]) swap(u, v);
    int dis = depth[u] - depth[v];
    for(int i = 19; i >= 0; i--)
        if(dis >> i & 1) u = pa[u][i];
    if(u == v) return u;
    for(int i = 19; i >= 0; i--)
        if(pa[u][i] != pa[v][i])
            u = pa[u][i], v = pa[v][i];
    return pa[u][0];
}

void dfs(int u) {
    son[u] = 1;
    for(auto& v : tG[u]) {
        dfs(v);
        son[u] += son[v];
    }
    if(u != s) chkmax(ans, son[u]);
}

int main() {
    memset(d, 0x3f, sizeof(d));
    scanf("%d%d%d", &n, &m, &s);
    for(int i = 1; i <= m; i++) {
        int u, v, w;
        scanf("%d%d%d", &u, &v, &w);
        G[u].push_back(mk(w, v));
        G[v].push_back(mk(w, u));
    }
    priority_queue<PLI, vector<PLI>, greater<PLI> > que;
    d[s] = 0;
    que.push(mk(0, s));
    while(!que.empty()) {
        int u = que.top().se;
        LL dis = que.top().fi;
        que.pop();
        if(dis < d[u]) continue;
        for(auto& e : G[u]) {
            if(chkmin(d[e.se], dis + e.fi)) {
                que.push(mk(d[e.se], e.se));
            }
        }
    }
    for(int u = 1; u <= n; u++) {
        for(auto& e : G[u]) {
            if(d[u] + e.fi == d[e.se]) {
                dG[u].push_back(e.se);
                rG[e.se].push_back(u);
                deg[e.se]++;
            }
        }
    }
    queue<int> que2;
    for(int i = 1; i <= n; i++)
        if(d[i] < INF && !deg[i]) que2.push(i);
    depth[s] = 1;
    while(!que2.empty()) {
        int u = que2.front(); que2.pop();
        if(u != s) {
            int lca = -1;
            for(auto& v : rG[u]) {
                if(~lca) lca = getLca(lca, v);
                else lca = v;
            }
            fa[u] = pa[u][0] = lca;
            for(int i = 1; i < 20; i++)
                pa[u][i] = pa[pa[u][i - 1]][i - 1];
            depth[u] = depth[fa[u]] + 1;
            tG[fa[u]].push_back(u);
        }
        for(auto& v : dG[u]) {
            deg[v]--;
            if(!deg[v]) que2.push(v);
        }
    }
    dfs(s);
    printf("%d\n", ans);
    return 0;
}

/*
*/

 

转载于:https://www.cnblogs.com/CJLHY/p/10986326.html

Codeforces 2123F 问题中,目标是通过重新排列数组 $ a $ 来最小化不动点的数量。所谓“不动点”是指在重新排列后的数组中满足 $ a_i = i $ 的位置。该问题要设计一种策略,以最优方式重新排列数组元素,使得这样的不动点数量最少。 为了解决这个问题,可以采用贪心算法和图论思想相结合的策略: - 首先,观察到如果某个值 $ i $ 出现了多次(即 $ a_i = i $),那么这些重复的值必须被移动到其他位置,以消除不动点。 - 对于那些没有出现在其索引上的值(例如 $ a_i \neq i $),可以通过交换操作将其移动到合适的位置,从而避免产生新的不动点。 一个有效的解决方案可以基于以下步骤: 1. 构建一个映射表,记录每个值出现的位置。 2. 找出所有当前值等于其索引的位置(即当前的不动点)。 3. 尝试通过交换来消除这些不动点。优先考虑将这些值移动到未被占用的位置,并确保不会引入新的不动点。 4. 在无法完全消除所有不动点的情况下,选择最优的交换策略以尽可能减少不动点的数量。 以下是 Python 中的一个示例实现,用于解决此类问题的基本思路: ```python def minimize_fixed_points(n, a): pos = {} fixed_points = [] # 记录每个值的出现位置,并找出初始的不动点 for i in range(n): if a[i] == i + 1: fixed_points.append(i) if a[i] not in pos: pos[a[i]] = [] pos[a[i]].append(i) # 如果没有重复的值,则可以直接交换以消除所有不动点 result = a[:] for i in fixed_points: found = False for val in pos: if val != i + 1 and len(pos[val]) > 0: j = pos[val].pop() result[i], result[j] = result[j], result[i] found = True break if not found: # 特殊情况处理:当只剩下一个值时 for j in range(n): if j != i and result[j] != j + 1: result[i], result[j] = result[j], result[i] break return result # 示例输入 n = int(input()) a = list(map(int, input().split())) result = minimize_fixed_points(n, a) print(' '.join(map(str, result))) ``` 此代码实现了上述逻辑,并尝试通过交换来最小化不动点的数量。对于大多数情况,它能够有效消除所有不动点;在某些特殊情况下(例如所有值都唯一且存在多个不动点),则需要特别处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值