Codeforces-949C:Data Center Maintenance(Tarjan强联通分量)

C. Data Center Maintenance
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output
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 ci, 1 and ci, 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 uj (0 ≤ uj ≤ 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 uci, 1 ≠ uci, 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 uj = h - 1, then the new maintenance hour would become 0, otherwise it would become uj + 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.

Input
The first line of input contains three integers n, m and h (2 ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000, 2 ≤ h ≤ 100 000), the number of company data centers, number of clients and the day length of day measured in hours.

The second line of input contains n integers u1, u2, ..., un (0 ≤ uj < h), j-th of these numbers is an index of a maintenance hour for data center j.

Each of the next m lines contains two integers ci, 1 and ci, 2 (1 ≤ ci, 1, ci, 2 ≤ n, ci, 1 ≠ ci, 2), defining the data center indices containing the data of client i.

It is guaranteed that the given maintenance schedule allows each client to access at least one copy of his data at any moment of day.

Output
In the first line print the minimum possible number of data centers k (1 ≤ k ≤ n) that have to be included in an experiment in order to keep the data available for any client.

In the second line print k distinct integers x1, x2, ..., xk (1 ≤ xi ≤ n), the indices of data centers whose maintenance time will be shifted by one hour later. Data center indices may be printed in any order.

If there are several possible answers, it is allowed to print any of them. It is guaranteed that at there is at least one valid choice of data centers.

Examples
input
Copy
3 3 5
4 4 0
1 3
3 2
3 1
output
1

input
Copy
4 5 4
2 1 0 3
4 3
3 2
1 2
1 4
1 3
output
4
1 2 3 4 
Note
Consider the first sample test. The given answer is the only way to conduct an experiment involving the only data center. In such a scenario the third data center has a maintenance during the hour 1, and no two data centers storing the information of the same client have maintenance at the same hour.

On the other hand, for example, if we shift the maintenance time on hour later for the first data center, then the data of clients 1 and 3 will be unavailable during the hour 0.

题意:n个点,每个点有一个值a[i]。m个条件,每个条件有2个点x,y且a[x]!=a[y]。选择最少的k个点,使其值加1后,m个条件仍成立。

思路:对于m个条件,若(a[x]+1)%h=a[y],则加上x->y这条边;若(a[y]+1)%h=a[x],则加上y->x这条边。则我们只需找到最小的且出度为0的SCC即可。
 

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <vector>
#include <stack>

using namespace std;
const int maxn = 1e6 +100;

int a[maxn],dfn[maxn],low[maxn],scc[maxn],tot,sc=0;
int num[maxn],out[maxn];

vector<int> e[maxn];
stack<int> s;

void tarjan(int k)
{
    dfn[k]=low[k]=++tot;
    s.push(k);

    int len=e[k].size();
    for(int i=0;i<len;i++)
    {
        int v=e[k][i];
        if(dfn[v]==0)
        {
            tarjan(v);
            low[k]=min(low[k],low[v]);
        }
        else if(scc[v]==0)
            low[k]=min(low[k],dfn[v]);
    }

    if(dfn[k]==low[k])
    {
        sc++;
        while(1)
        {
            int x=s.top();s.pop();
            scc[x]=sc;
            num[sc]++;
            if(x==k)
                break;
        }
    }
}
int main() {

    int n,m,h;
    while(~scanf("%d%d%d",&n,&m,&h))
    {
        tot=0;
        sc=0;

        for(int i=0;i<maxn;i++)
            e[i].clear(),out[i]=0,scc[i]=0,dfn[i]=0,num[i]=0;

        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        for(int i=1;i<=m;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            if((a[x]+1)%h == a[y])
                e[x].push_back(y);
            if((a[y]+1)%h == a[x])
                e[y].push_back(x);
        }

        for(int i=1;i<=n;i++)
        {
            if(dfn[i]==0)
                tarjan(i);
        }
        for(int i=1;i<=n;i++)
        {
            int len=e[i].size();
            for(int j=0;j<len;j++)
            {
                int v=e[i][j];
                if(scc[i]!=scc[v])
                {
                    out[scc[i]]++;
                }
            }
        }

        int ans=maxn,id=0;
        for(int i=1;i<=sc;i++)
        {
            if(out[i]==0 && num[i]<ans)
            {
                ans=num[i];
                id=i;
            }
        }
        printf("%d\n",ans);
        for(int i=1;i<=n;i++)
            if(scc[i]==id)
                printf("%d ",i);

        printf("\n");
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值