(树) Codeforces Round #417 (Div. 2) D. Sagheer and Kindergarten

(树) Codeforces Round #417 (Div. 2) D. Sagheer and Kindergarten

Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys:

  • Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing if and only if he is granted all the toys in his lovely set.
  • If a child starts playing, then sooner or later he gives the toys back. No child keeps the toys forever.
  • Children request toys at distinct moments of time. No two children request a toy at the same time.
  • If a child is granted a toy, he never gives it back until he finishes playing with his lovely set.
  • If a child is not granted a toy, he waits until he is granted this toy. He can't request another toy while waiting.
  • If two children are waiting for the same toy, then the child who requested it first will take the toy first.

Children don't like to play with each other. That's why they never share toys. When a child requests a toy, then granting the toy to this child depends on whether the toy is free or not. If the toy is free, Sagheer will give it to the child. Otherwise, the child has to wait for it and can't request another toy.

Children are smart and can detect if they have to wait forever before they get the toys they want. In such case they start crying. In other words, a crying set is a set of children in which each child is waiting for a toy that is kept by another child in the set.

Now, we have reached a scenario where all the children made all the requests for their lovely sets, except for one child x that still has one last request for his lovely set. Some children are playing while others are waiting for a toy, but no child is crying, and no one has yet finished playing. If the child x is currently waiting for some toy, he makes his last request just after getting that toy. Otherwise, he makes the request right away. When child x will make his last request, how many children will start crying?

You will be given the scenario and q independent queries. Each query will be of the form x y meaning that the last request of the child x is for the toy y. Your task is to help Sagheer find the size of the maximal crying set when child x makes his last request.

Input

The first line contains four integers nmkq (1 ≤ n, m, k, q ≤ 105) — the number of children, toys, scenario requests and queries.

Each of the next k lines contains two integers ab (1 ≤ a ≤ n and 1 ≤ b ≤ m) — a scenario request meaning child a requests toy b. The requests are given in the order they are made by children.

Each of the next q lines contains two integers xy (1 ≤ x ≤ n and 1 ≤ y ≤ m) — the request to be added to the scenario meaning child x will request toy y just after getting the toy he is waiting for (if any).

It is guaranteed that the scenario requests are consistent and no child is initially crying. All the scenario requests are distinct and no query coincides with a scenario request.

Output

For each query, print on a single line the number of children who will start crying when child x makes his last request for toy y. Please answer all queries independent of each other.

Example

Input
3 3 5 1
1 1
2 2
3 3
1 2
2 3
3 1
Output
3
Input
5 4 7 2
1 1
2 2
2 1
5 1
3 3
4 4
4 1
5 3
5 4
Output
0
2

Note

In the first example, child 1 is waiting for toy 2, which child 2 has, while child 2 is waiting for top 3, which child 3 has. When child 3 makes his last request, the toy he requests is held by child 1. Each of the three children is waiting for a toy held by another child and no one is playing, so all the three will start crying.

In the second example, at the beginning, child i is holding toy i for 1 ≤ i ≤ 4. Children 1 and 3 have completed their lovely sets. After they finish playing, toy 3 will be free while toy 1 will be taken by child 2 who has just completed his lovely set. After he finishes, toys 1 and 2 will be free and child 5 will take toy 1. Now:

  • In the first query, child 5 will take toy 3 and after he finishes playing, child 4 can play.
  • In the second query, child 5 will request toy 4 which is held by child 4. At the same time, child 4 is waiting for toy 1 which is now held by child 5. None of them can play and they will start crying.

题意:

 一共n个人,m个玩具,然后读入是x和y,表示x要玩y。然后在玩y的人玩的时间不知道(当他得到了所有想要的玩具之后就开始玩,玩完之后将玩具还回),如果x要玩y........这样形成了一个环,这个环上所有人都会哭。比如1要玩2在玩的玩具,2要玩3的,3要玩1的,就形成了个环,就全都哭了。然后前面k个操作,保证没人哭,后面q个操作,问如果有这个操作,有多少个人哭了

解题思路:

每个人最多只会等1个人玩某个玩具,从某个人出发连一条指向等的人的有向边。最终形成了一个有许多有根树的森林。每个结点最多1个出度,用欧拉路的方式给每个结点的出入标id,就方便判断某两点是否之前在一棵树上,以及在树的路径中经过的结点个数。这样每次询问,如果最后一个玩y的人在以x为根节点的子树上,连接这两个人就形成了一个环,输出这棵子树上结点个数即可。

 1 #include <iostream>
 2 #include <string>
 3 #include <algorithm>
 4 #include <cstring>
 5 #include <cstdio>
 6 #include <cmath>
 7 #include <queue>
 8 #include <set>
 9 #include <map>
10 #include <list>
11 #include <vector>
12 #include <stack>
13 #define mp make_pair
14 //#define P make_pair
15 #define MIN(a,b) (a>b?b:a)
16 //#define MAX(a,b) (a>b?a:b)
17 typedef long long ll;
18 typedef unsigned long long ull;
19 const int MAX=1e5+5;
20 const int MAX_V=1e3+5;
21 const ll INF=4e18+5;
22 const double M=4e18;
23 using namespace std;
24 const int MOD=1e9+7;
25 typedef pair<ll,int> pii;
26 const double eps=0.000000001;
27 #define rank rankk
28 int n,m,k,q,idx;
29 int fa[MAX],who[MAX],in[MAX],out[MAX];
30 vector <int> edge[MAX];
31 void dfs(int x)
32 {
33     in[x]=++idx;
34     for(int i=0;i<edge[x].size();i++)
35         dfs(edge[x][i]);
36     out[x]=idx;
37 }
38 int main()
39 {
40     scanf("%d%d%d%d",&n,&m,&k,&q);
41     for(int i=1;i<=k;i++)
42     {
43         int x,y;
44         scanf("%d%d",&x,&y);
45         if(who[y])//如果有人在x前一个玩y
46             fa[x]=who[y];//x得等到前一个玩y的结束
47         who[y]=x;
48     }
49     for(int i=1;i<=n;i++)
50         edge[fa[i]].push_back(i);
51     for(int i=1;i<=n;i++)
52         if(!in[i])
53             dfs(i);
54     while(q--)
55     {
56         int x,y;
57         scanf("%d%d",&x,&y);
58         y=who[y];//最后一个玩y的人
59         if(in[x]<=in[y]&&out[x]>=in[y])
60             printf("%d
",out[x]-in[x]+1);
61         else
62             printf("0
");
63     }
64 }