← 返回 google 的题目列表Earliest Time When Everyone Become Friends
类型:online_judge
You are given N people labeled 0..N-1 and a list of social event logs logs. Each log contains a timestamp t, two people a and b, and an operation op:
FRIEND: a and b become friends at time t
UNFRIEND: a and b unfriend at time t
Friendship is undirected. At any time, the current friendship graph is obtained by applying all FRIEND operations and removing edges for UNFRIEND operations up to that time.
Return the earliest timestamp t* such that after processing all logs at time t*, all N people are in a single connected component (i.e., the graph is fully connected). If it never happens, return -1.
Input (assumed)
Line 1: integers N and M (number of logs)
Next M lines: t a b op
Output
One line: the earliest t*, or -1.
Constraints
1 <= N <= 2*10^5
1 <= M <= 2*10^5
logs are not guaranteed to be sorted; multiple logs may share the same timestamp.
Example Input:
4 4
1 0 1 FRIEND
2 1 2 FRIEND
3 2 3 FRIEND
4 1 2 UNFRIEND
Output:
3
Example
Input
4 4
1 0 1 FRIEND
2 1 2 FRIEND
3 2 3 FRIEND
4 1 2 UNFRIEND
Output
3