Brexit Negotiations(Kattis-Northwestern Europe Regional Contest (NWERC) 2018)

Problem Description

As we all know, Brexit negotiations are on their way—but we still do not know whether they will actually finish in time.

The negotiations will take place topic-by-topic. To organise the negotiations in the most effective way, the topics will all be discussed and finalised in separate meetings, one meeting at a time.

This system exists partly because there are (non-cyclic) dependencies between some topics: for example, one cannot have a meaningful talk about tariffs before deciding upon the customs union. The EU can decide on any order in which to negotiate the topics, as long as the mentioned dependencies are respected and all topics are covered.

Each of the topics will be discussed at length using every available piece of data, including key results from past meetings. At the start of each meeting, the delegates will take one extra minute for each of the meetings that has already happened by that point, even unrelated ones, to recap the discussions and understand how their conclusions were reached. See Figure 1 for an example.

Nobody likes long meetings. The EU would like you to help order the meetings in a way such that the longest meeting takes as little time as possible.

Brexit Negotiations(Kattis-Northwestern Europe Regional Contest (NWERC) 2018)_第1张图片

Figure 1: Illustration of how time is spent in each meeting in a solution to Sample Input 2.

Input

The input consists of:

One line containing an integer n (1≤n≤4⋅105), the number of topics to be discussed. The topics are numbered from 1 to n.

n lines, describing the negotiation topics.

The ith such line starts with two integers eiei and di (1≤ei≤106, 0≤di

The remainder of the line has didi distinct integers bi,1,…,bi,di (1≤bi,j≤n and bi,j≠i for each j), the list of topics that need to be completed before topic i.

It is guaranteed that there are no cycles in the topic dependencies, and that the sum of didi over all topics is at most 4⋅105.

Output

Output the minimum possible length of the longest of all meetings, if meetings are arranged optimally according to the above rules.

Sample Example

Sample Input 1

3
10 0
10 0
10 0

Sample Output 1

12

Sample Input 2

6
2 2 4 3
4 1 5
1 2 2 4
3 1 5
2 0
4 1 3

Sample Output 2

8

题意:n 个会议,依次给出每个会议的持续时间、开这个会议前要开的会议编号,在开每个会议前,要回顾当前会议前的所有会议,每个会议回顾 1 个单位时间,问最长会议的最短时间

思路:

直接对 n 个会议的持续时间从小到大排序,从而保证最长的会议再最前,然后对于 n 个会议进行 dfs,在开当前会议 x 之前先对统计其依赖会议,最后输出这 n 个会议中的最大值即可

Source Program

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 400000+5;
const int dx[] = {0,0,-1,1,-1,-1,1,1};
const int dy[] = {-1,1,0,0,-1,1,-1,1};
using namespace std;

struct Node {
    int id;
    int time;
    bool operator < (const Node &rhs) {//按会议时间从大到小排序
        return time>rhs.time;
    }
}a[N];
int times[N];
vector G[N];
bool vis[N];
int res=-INF,extra=0;
void dfs(int x) {
    if(vis[x]==true)//第x个点被访问过
        return;

    if(vis[x]==false&&G[x].size()==0) {//第x个点未访问过且其没有依赖节点
        res=max(res,extra+times[x]);//统计最大值
        extra++;//额外时间+1
        vis[x]=true;
        return;
    }

    vis[x]=true;
    for(int i=0; i

 

你可能感兴趣的:(#,其它,OJ,#,搜索——深度优先搜索,DFS)