CF949C 数据中心维护

1 CF949C 数据中心维护

2 题目描述

时间限制 \(1s\) | 空间限制 \(512M\)

BigData Inc. is a corporation that has n data centers indexed from 1 to n that are located all over the world. These data centers provide storage for client data (you can figure out that client data is really big!).

Main feature of services offered by BigData Inc. is the access availability guarantee even under the circumstances of any data center having an outage. Such a guarantee is ensured by using the two-way replication. Two-way replication is such an approach for data storage that any piece of data is represented by two identical copies that are stored in two different data centers.

For each of m company clients, let us denote indices of two different data centers storing this client data as \(c_{i, 1}\) and \(c_{i, 2}\).

In order to keep data centers operational and safe, the software running on data center computers is being updated regularly. Release cycle of BigData Inc. is one day meaning that the new version of software is being deployed to the data center computers each day.

Data center software update is a non-trivial long process, that is why there is a special hour-long time frame that is dedicated for data center maintenance. During the maintenance period, data center computers are installing software updates, and thus they may be unavailable. Consider the day to be exactly \(h\) hours long. For each data center there is an integer \(u_j (0 ≤ u_j ≤ h - 1)\) defining the index of an hour of day, such that during this hour data center \(j\) is unavailable due to maintenance.

Summing up everything above, the condition \(u_{c_{i, 1}} ≠ u_{c_{i, 2}}\) should hold for each client, or otherwise his data may be unaccessible while data centers that store it are under maintenance.

Due to occasional timezone change in different cities all over the world, the maintenance time in some of the data centers may change by one hour sometimes. Company should be prepared for such situation, that is why they decided to conduct an experiment, choosing some non-empty subset of data centers, and shifting the maintenance time for them by an hour later (i.e. if \(u_j = h - 1\), then the new maintenance hour would become 0, otherwise it would become \(u_j + 1\)). Nonetheless, such an experiment should not break the accessibility guarantees, meaning that data of any client should be still available during any hour of a day after the data center maintenance times are changed.

Such an experiment would provide useful insights, but changing update time is quite an expensive procedure, that is why the company asked you to find out the minimum number of data centers that have to be included in an experiment in order to keep the data accessibility guarantees.

数据范围:\(2 ≤ n ≤ 100000, 1 ≤ m ≤ 100000, 2 ≤ h ≤ 100000\)

3 题解

我们可以将题目转换为:延迟任意一个数据中心的维护时间,使得为了满足要求而延迟的数据中心个数最少。容易想到:对于某个顾客的两个文件备份,其中某一个文件所处的数据中心如果延迟后维护时间正好与第二个备份所处的数据中心维护时间相同,那么第一个数据中心延迟后第二个数据中心就必须延迟,反之亦然。因此,我们可以将这种强制关系看作一条边,\(x\)\(y\) 连边就意味着当 \(x\) 维护时,\(y\) 一定需要维护。容易得出,在同一个强连通分量中的数据中心必须同时延迟,否则任何一个数据中心不延迟都会导致不满足需求。因此,我们可以计算一下所有强连通分量所包含的节点个数,然后将每个强连通分量缩为一个点。此时,如果在新图上某一个点的出度为 \(0\),那么只延迟该强连通分量中的所有数据中心就可以解决需求。所以,我们考虑所有出度为 \(0\) 的强连通分量并输出其中最少的点数量即可。

4 代码(空格警告):

#include <iostream>
using namespace std;
const int N = 2e5+10;
int n, m, h, tot, c1, c2, cnt, top, num, minn, ans;
int u[N], head[N], last[N], ver[N];
int stack[N], ins[N], c[N], dfn[N], low[N], siz[N], deg[N];
void add(int x, int y)
{
    ver[++tot] = y;
    last[tot] = head[x];
    head[x] = tot;
}
void tarjan(int x)
{
    dfn[x] = low[x] = ++cnt;
    stack[++top] = x, ins[x] = 1;
    for (int i = head[x]; i; i = last[i])
    {
        int y = ver[i];
        if (!dfn[y])
        {
            tarjan(y);
            low[x] = min(low[x], low[y]);
        }
        else if (ins[y]) low[x] = min(low[x], dfn[y]);
    }
    if (dfn[x] == low[x])
    {
        int y;
        num++;
        do
        {
            y = stack[top--], ins[y] = 0;
            c[y] = num, siz[num]++;
        } while (x != y);
    }
}
int main()
{
    minn = 0x3f3f3f3f;
    cin >> n >> m >> h;
    for (int i = 1; i <= n; i++) cin >> u[i];
    for (int i = 1; i <= m; i++)
    {
        cin >> c1 >> c2;
        if ((u[c1] + 1) % h == u[c2] % h) add(c1, c2);
        if ((u[c2] + 1) % h == u[c1] % h) add(c2, c1);
    }
    for (int i = 1; i <= n; i++) if (!dfn[i]) tarjan(i);
    for (int x = 1; x <= n; x++)
    {
        for (int i = head[x]; i; i = last[i])
        {
            int y = ver[i];
            if (c[x] == c[y]) continue;
            deg[c[x]]++;
        }
    }
    for (int i = 1; i <= n; i++)
    {
        if (!deg[c[i]] && minn >= siz[c[i]])
        {
            minn = siz[c[i]];
            ans = c[i];
        }
    }
    cout << minn << '\n';
    for (int i = 1; i <= n; i++) if (c[i] == ans) cout << i << " ";
    return 0;
}

欢迎关注我的公众号:智子笔记

posted @ 2021-04-11 22:56  David24  阅读(111)  评论(0编辑  收藏  举报