Problem:
Problem Statement

We have N boxes, numbered 1 through N. At first, box 1 contains one red ball, and each of the other boxes contains one white ball.

Snuke will perform the following M operations, one by one. In the i-th operation, he randomly picks one ball from box x_i, then he puts it into box y_i.

Find the number of boxes that may contain the red ball after all operations are performed.

Constraints

2≤N≤10^5

1≤M≤10^5

1≤x_i,y_i≤N

x_i≠y_i

Just before the i-th operation is performed, box x_i contains at least 1 ball.

Input

The input is given from Standard Input in the following format:

N M
x_1 y_1
:
x_M y_M

Output

Print the number of boxes that may contain the red ball after all operations are performed.

Sample Input 1

3 2
1 2
2 3

Sample Output 1

2

Just after the first operation, box 1 is empty, box 2 contains one red ball and one white ball, and box 3 contains one white ball.

Now, consider the second operation. If Snuke picks the red ball from box 2, the red ball will go into box 3. If he picks the white ball instead, the red ball will stay in box 2.
Thus, the number of boxes that may contain the red ball after all operations, is 2.

Sample Input 2

3 3
1 2
2 3
2 3

Sample Output 2

1

All balls will go into box 3.

Sample Input 3

4 4
1 2
2 3
4 1
3 4

Sample Output 3

3

Solution in C++ (GCC 9.2.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const ll mod = 1e9+7;

struct UnionFind{
    vector<int> branch;
    UnionFind(int N = 0) : branch(N,-1){}

    int find(int x){
        if(branch[x] < 0) return x;
        else return branch[x] = find(branch[x]);//メモ化、要素を入れて根が返ってくる
    }

    bool unite(int x, int y){
        int rx,ry;
        rx = find(x);
        ry = find(y);
        if(rx == ry) return false;//連結できたかどうか
        if(-branch[rx] < -branch[ry]) swap(rx,ry);
        branch[rx] += branch[ry];//木のサイズを負の値で管理
        branch[ry] = rx;
        return true;
    }

    bool same(int x, int y){
        if(find(x) == find(y)) return true;
        else return false;
    }

    int size(int x){
        return -branch[find(x)];
    }
};

int main(){
    int n,m;
    cin >> n >> m;
    UnionFind tree(n);
    int a = -1;
    bool first = true;
    vector<int> mp(n,1);
    for(int i = 0; i < m; i++){
        int x,y;
        cin >> x >> y;
        x--;y--;
        if(x == 0 && first){
            a = y;
            first = false;
            mp[x]--;
            mp[y]++;
            continue;
        } else if(y == 0 && first){
            a = x;
            first = false;
            mp[y]--;
            mp[x]++;
            continue;
        }
        mp[x]--;
        mp[y]++; 
        if(first) continue;
        if(tree.same(x,a) || tree.same(y,a)){
            tree.unite(x,y);
        }
    }
    int ans = tree.size(a);
    for(int i = 0; i < n; i++){
        if(mp[i] == 0 && tree.same(a,i)) ans--;
    }
    cout << ans << endl;
    return 0;
}