← 返回 openai 的题目列表Versioned User Follow Graph with Snapshots
类型:online_judge
Implement a system that maintains directed user-follow edges and supports queries against any historical snapshot.
Initially, the graph has no edges. Process Q commands in order. Every FOLLOW or UNFOLLOW operation creates a new snapshot version. Version 0 is the initial empty graph.
Commands
FOLLOW u v
Add directed edge u -> v to the current graph.
If the edge already exists, the state is unchanged, but a new version is still created.
UNFOLLOW u v
Remove directed edge u -> v from the current graph.
If the edge does not exist, the state is unchanged, but a new version is still created.
FOLLOWS version u v
Determine whether u follows v in historical version.
Print true or false.
FOLLOWERS version v
Print all users that follow v in version, in ascending order.
Print an empty line if there are no followers.
Input Format
The first line contains integer Q, followed by Q command lines. User IDs and version IDs are non-negative integers. Every queried version is guaranteed to exist.
Constraints
1 <= Q <= 2 * 10^5
0 <= u, v <= 10^9
All historical versions must remain queryable and immutable.
Example
Input
8
FOLLOW 1 2
FOLLOW 3 2
FOLLOWS 1 1 2
FOLLOWERS 2 2
UNFOLLOW 1 2
FOLLOWS 2 1 2
FOLLOWS 3 1 2
FOLLOWERS 3 2
Output
true
1 3
true
false
3
Example
Input
4
FOLLOW 1 2
FOLLOWS 1 1 2
UNFOLLOW 1 2
FOLLOWS 2 1 2
Output
true
false