Problem:
F: コマの配置
問題
N
行
N
列の盤面が与えられる。盤面の左から
x
番目、盤面の上から
y
番目のマスを
(x,y)
と表す。この盤面にはコマを置けるマスの候補
(a_i,b_i)
が
M
個存在する。
Q
個のクエリが与えられる。クエリではマス
(x_i,y_i)
が与えられ、コマを置けるマスならばコマを置けないマスに変化し、コマを置けないマスならばコマを置けるマスへ変化する。この変化した後の盤面において、コマを
N
個置くことで、どの行・列についてもコマがちょうど1個存在するようにできるのであれば
Yes
、置けないのであれば
No
を出力しろ。
入力形式
N
M
a_1
b_1
a_2
b_2
...
a_M
b_M
Q
x_1
y_1
x_2
y_2
...
x_M
y_M
制約
1\leq N,M,Q\leq 5000
i\neq j
ならば
(a_i,b_i)\neq (a_j,b_j)
1\leq a_i,b_i,x_i,y_i\leq N
入力はすべて整数
出力形式
各クエリについて
Yes
か
No
を出力しろ。
入力例1
5 4
4 1
1 2
2 3
5 4
1
3 5
出力例1
Yes
コマを置けるマスをo、置けないマスをxとしたときの盤面を以下に示す。oのマスにコマを１つずつ置けば条件を満たす。
xxxox
oxxxx
xoxxx
xxxxo
xxoxx

Submitted solutions:

code_snippet: // AOJ #3198 Arrangement of pieces
// 2025.3.19

#include <bits/stdc++.h>
using namespace std;

#define gc() getchar_unlocked()
#define pc(c) putchar_unlocked(c)

int Cin() {  // 整数の入力
	int n = 0; int c = gc();
	do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

void Couts(const string &s) {
    for (char c : s) pc(c);
    pc('\n');
}


int main(){
    int N = Cin(), M = Cin();
    vector<vector<int>> g(N);
    vector<vector<char>> a(N, vector<char>(N, 0));
    vector<int> rcnt(N,0), ccnt(N,0);
    for(int i = 0; i < M; i++){
        int c = Cin()-1, r = Cin()-1;
        if(!a[r][c]){
            a[r][c] = 1;
            rcnt[r]++;
            ccnt[c]++;
            g[r].push_back(c);
        }
    }
    int zr = 0, zc = 0;
    for(int i = 0; i < N; i++) if(rcnt[i] == 0) zr++;
    for(int j = 0; j < N; j++) if(ccnt[j] == 0) zc++;

    int Q = Cin();
    while(Q--){
        int c = Cin() - 1, r = Cin() - 1;
        if(a[r][c]){
            a[r][c] = 0;
            rcnt[r]--;
            ccnt[c]--;
            if(rcnt[r] == 0) zr++;
            if(ccnt[c] == 0) zc++;
        } else {
            a[r][c] = 1;
            if(rcnt[r] == 0) zr--;
            if(ccnt[c] == 0) zc--;
            rcnt[r]++;
            ccnt[c]++;
            bool f = false;
            for(auto cc : g[r]){
                if(cc == c){ f = true; break; }
            }
            if(!f) g[r].push_back(c);
        }
        if(zr || zc){
            Couts("No");
            continue;
        }
        vector<int> mr(N, -1), mc(N, -1), d(N, -1);
        auto bfs = [&]() -> bool {
            deque<int> dq;
            fill(d.begin(), d.end(), -1);
            int inf = INT_MAX;
            for (int i = 0; i < N; i++){
                if(mr[i] == -1){
                    d[i] = 0;
                    dq.push_back(i);
                }
            }
            while(!dq.empty()){
                int i = dq.front();
                dq.pop_front();
                if(d[i] < inf){
                    for(auto j : g[i]){
                        if(!a[i][j]) continue;
                        int k = mc[j];
                        if(k == -1) inf = d[i] + 1;
                        else if(d[k] < 0){
                            d[k] = d[i] + 1;
                            dq.push_back(k);
                        }
                    }
                }
            }
            return inf != INT_MAX;
        };
        function<bool(int)> dfs = [&](int i) -> bool {
            for(auto j : g[i]){
                if(!a[i][j]) continue;
                int k = mc[j];
                if(k == -1 || (d[k] == d[i] + 1 && dfs(k))){
                    mr[i] = j;
                    mc[j] = i;
                    return true;
                }
            }
            d[i] = -1;
            return false;
        };
        int m = 0;
        while(bfs()){
            for (int i = 0; i < N; i++) if(mr[i] == -1 && dfs(i)) m++;
        }
        Couts(m == N ? "Yes" : "No");
    }
    return 0;
}
accuracy: 1
time_ms: 210
memory_kb: 28008
final_rank: 8

code_snippet: // AOJ #3198 Arrangement of pieces
// 2025.3.19

#include <bits/stdc++.h>
using namespace std;

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);

    int N, M;
    cin >> N >> M;
    vector<vector<int>> g(N);
    vector<vector<char>> a(N, vector<char>(N, 0));
    vector<int> rcnt(N,0), ccnt(N,0);
    for(int i = 0; i < M; i++){
        int x, y;
        cin >> x >> y;
        int c = x - 1, r = y - 1;
        if(!a[r][c]){
            a[r][c] = 1;
            rcnt[r]++;
            ccnt[c]++;
            g[r].push_back(c);
        }
    }
    int zr = 0, zc = 0;
    for(int i = 0; i < N; i++) if(rcnt[i] == 0) zr++;
    for(int j = 0; j < N; j++) if(ccnt[j] == 0) zc++;

    int Q;
    cin >> Q;
    while(Q--){
        int x, y;
        cin >> x >> y;
        int c = x - 1, r = y - 1;
        if(a[r][c]){
            a[r][c] = 0;
            rcnt[r]--;
            ccnt[c]--;
            if(rcnt[r] == 0) zr++;
            if(ccnt[c] == 0) zc++;
        } else {
            a[r][c] = 1;
            if(rcnt[r] == 0) zr--;
            if(ccnt[c] == 0) zc--;
            rcnt[r]++;
            ccnt[c]++;
            bool f = false;
            for(auto cc : g[r]){
                if(cc == c){ f = true; break; }
            }
            if(!f) g[r].push_back(c);
        }
        if(zr || zc){
            cout << "No" << endl;
            continue;
        }
        vector<int> mr(N, -1), mc(N, -1), d(N, -1);
        auto bfs = [&]() -> bool {
            deque<int> dq;
            fill(d.begin(), d.end(), -1);
            int inf = INT_MAX;
            for (int i = 0; i < N; i++){
                if(mr[i] == -1){
                    d[i] = 0;
                    dq.push_back(i);
                }
            }
            while(!dq.empty()){
                int i = dq.front();
                dq.pop_front();
                if(d[i] < inf){
                    for(auto j : g[i]){
                        if(!a[i][j]) continue;
                        int k = mc[j];
                        if(k == -1) inf = d[i] + 1;
                        else if(d[k] < 0){
                            d[k] = d[i] + 1;
                            dq.push_back(k);
                        }
                    }
                }
            }
            return inf != INT_MAX;
        };
        function<bool(int)> dfs = [&](int i) -> bool {
            for(auto j : g[i]){
                if(!a[i][j]) continue;
                int k = mc[j];
                if(k == -1 || (d[k] == d[i] + 1 && dfs(k))){
                    mr[i] = j;
                    mc[j] = i;
                    return true;
                }
            }
            d[i] = -1;
            return false;
        };
        int m = 0;
        while(bfs()){
            for (int i = 0; i < N; i++) if(mr[i] == -1 && dfs(i)) m++;
        }
        cout << (m == N ? "Yes" : "No") << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 210
memory_kb: 28260
final_rank: 9

code_snippet: // verify: https://blog.tiramister.net/posts/acpc2020-day1-f/
// https://onlinejudge.u-aizu.ac.jp/challenges/sources/VPC/TUATPC/3198
#include <iostream>
#include <vector>
#include <queue>
#include <string>
#include <unordered_set>

struct HopcroftKarp{
    const int INF = 1e9+7;
    std::vector<std::vector<int>> Graph;
    std::vector<int> match, dist;
    int N, M;
    int now_matching = 0;
    int now_max_matching = 0;
    int now_edge = 0;

    void init(int n, int m){
        N = n;
        M = m;
        Graph.resize(n+1);
        match.resize(n+m+2, 0);
        dist.resize(n+m+2);
    }
    // u: 0-indexed, v: 0-indexed
    void add_edge(int u, int v){
        u++; v++;
        Graph[u].push_back(N+v);
        now_edge++;
    }
    // u: 0-indexed, v: 0-indexed
    void delete_edge(int u, int v){
        u++; v++;
        now_edge--;
        for(int i = 0; i < Graph[u].size(); i++){
            if(Graph[u][i] == N+v){
                Graph[u].erase(Graph[u].begin()+i);
                break;
            }
        }
        if (match[u] == N+v){
            match[u] = 0;
            match[N+v] = 0;
            now_matching--;
        }
    }
    bool bfs(int n){
        std::queue<int> que;
        for (int i = 1; i <= N; i++){
            if (match[i]) dist[i] = INF;
            else dist[i] = 0, que.emplace(i);
        }
        dist[0] = INF;
        while (!que.empty()){
            int u = que.front(); que.pop();
            if (!u) continue;
            for (int v : Graph[u]){
                if (dist[match[v]] == INF){
                    dist[match[v]] = dist[u] + 1, que.emplace(match[v]);
                }
            }
        }
        return dist[0] != INF;
    }
    bool dfs(int u){
        if (!u) return true;
        for (int v : Graph[u]){
            if (dist[match[v]] == dist[u] + 1 && dfs(match[v])){
                match[u] = v, match[v] = u;
                return true;
            }
        }
        dist[u] = INF;
        return false;
    }
    int matching_from_scratch(){
        int res = 0;
        while (bfs(N)){
            for (int i = 1; i <= N; i++){
                res += !match[i] && dfs(i);
            }
        }
        now_matching = res;
        now_max_matching = std::max(now_max_matching, now_matching);
        return res;
    }

    int matching_again(){
        while (bfs(N)){
            for (int i = 1; i <= N; i++){
                now_matching += !match[i] && dfs(i);
            }
        }
        now_max_matching = std::max(now_max_matching, now_matching);
        return now_matching;
    }

    int matching_again_if_maximized_is_bigger(){
        if (now_edge < now_max_matching) return 0;

        return matching_again();
    }
};

int main() {
    int N,M; std::cin >> N >> M;

    HopcroftKarp hk;
    hk.init(N, M);

    std::unordered_set<long long> s;

    for (int i = 0; i < M; i++){
        int a,b; std::cin >> a >> b; a--; b--;
        hk.add_edge(a, b);
        s.insert((long long)a * (long long)N + (long long)b);
    }

    int Q; std::cin >> Q;

    hk.matching_from_scratch();

    for (int i = 0; i < Q; i++){
        int a,b; std::cin >> a >> b; a--; b--;
        if (s.find((long long)a * (long long)N + (long long)b) != s.end()){
            hk.delete_edge(a, b);
            s.erase((long long)a * (long long)N + (long long)b);
        }else{
            hk.add_edge(a, b);
            s.insert((long long)a * (long long)N + (long long)b);
        }
        if (hk.matching_again_if_maximized_is_bigger() == N){
            std::cout << "Yes" << std::endl;
        }else{
            std::cout << "No" << std::endl;
        }
    }
    
    return 0;
}
accuracy: 1
time_ms: 210
memory_kb: 4040
final_rank: 4

code_snippet: // verify: https://blog.tiramister.net/posts/acpc2020-day1-f/
// https://onlinejudge.u-aizu.ac.jp/challenges/sources/VPC/TUATPC/3198
#include <iostream>
#include <vector>
#include <queue>
#include <string>
#include <unordered_set>

struct HopcroftKarp{
    const int INF = 1e9+7;
    std::vector<std::vector<int>> Graph;
    std::vector<int> match, dist;
    int N, M;
    int now_matching = 0;
    int now_max_matching = 0;
    int now_edge = 0;

    void init(int n, int m){
        N = n;
        M = m;
        Graph.resize(n+m+2);
        match.resize(n+m+2, 0);
        dist.resize(n+m+2);
    }
    // u: 0-indexed, v: 0-indexed
    void add_edge(int u, int v){
        u++; v++;
        Graph[u].push_back(N+v);
        now_edge++;
    }
    // u: 0-indexed, v: 0-indexed
    void delete_edge(int u, int v){
        u++; v++;
        now_edge--;
        for(int i = 0; i < Graph[u].size(); i++){
            if(Graph[u][i] == N+v){
                Graph[u].erase(Graph[u].begin()+i);
                break;
            }
        }
        if (match[u] == N+v){
            match[u] = 0;
            match[N+v] = 0;
            now_matching--;
        }
    }
    bool bfs(int n){
        std::queue<int> que;
        for (int i = 1; i <= N; i++){
            if (match[i]) dist[i] = INF;
            else dist[i] = 0, que.emplace(i);
        }
        dist[0] = INF;
        while (!que.empty()){
            int u = que.front(); que.pop();
            if (!u) continue;
            for (int v : Graph[u]){
                if (dist[match[v]] == INF){
                    dist[match[v]] = dist[u] + 1, que.emplace(match[v]);
                }
            }
        }
        return dist[0] != INF;
    }
    bool dfs(int u){
        if (!u) return true;
        for (int v : Graph[u]){
            if (dist[match[v]] == dist[u] + 1 && dfs(match[v])){
                match[u] = v, match[v] = u;
                return true;
            }
        }
        dist[u] = INF;
        return false;
    }
    int matching_from_scratch(){
        int res = 0;
        while (bfs(N)){
            for (int i = 1; i <= N; i++){
                res += !match[i] && dfs(i);
            }
        }
        now_matching = res;
        now_max_matching = std::max(now_max_matching, now_matching);
        return res;
    }

    int matching_again(){
        while (bfs(N)){
            for (int i = 1; i <= N; i++){
                now_matching += !match[i] && dfs(i);
            }
        }
        now_max_matching = std::max(now_max_matching, now_matching);
        return now_matching;
    }

    int matching_again_if_maximized_is_bigger(){
        if (now_edge < now_max_matching) return 0;

        return matching_again();
    }
};

int main() {
    int N,M; std::cin >> N >> M;

    HopcroftKarp hk;
    hk.init(N, M);

    std::unordered_set<long long> s;

    for (int i = 0; i < M; i++){
        int a,b; std::cin >> a >> b; a--; b--;
        hk.add_edge(a, b);
        s.insert((long long)a * (long long)N + (long long)b);
    }

    int Q; std::cin >> Q;

    hk.matching_from_scratch();

    for (int i = 0; i < Q; i++){
        int a,b; std::cin >> a >> b; a--; b--;
        if (s.find((long long)a * (long long)N + (long long)b) != s.end()){
            hk.delete_edge(a, b);
            s.erase((long long)a * (long long)N + (long long)b);
        }else{
            hk.add_edge(a, b);
            s.insert((long long)a * (long long)N + (long long)b);
        }
        if (hk.matching_again_if_maximized_is_bigger() == N){
            std::cout << "Yes" << std::endl;
        }else{
            std::cout << "No" << std::endl;
        }
    }
    
    return 0;
}
accuracy: 1
time_ms: 200
memory_kb: 3996
final_rank: 1

code_snippet: // verify: https://blog.tiramister.net/posts/acpc2020-day1-f/
// https://onlinejudge.u-aizu.ac.jp/challenges/sources/VPC/TUATPC/3198
#include <iostream>
#include <vector>
#include <queue>
#include <string>
#include <unordered_set>

struct HopcroftKarp{
    const int INF = 1e9+7;
    std::vector<std::vector<int>> Graph;
    std::vector<int> match, dist;
    int N, M;
    int now_matching = 0;
    int now_max_matching = 0;
    int now_edge = 0;

    void init(int n, int m){
        N = n;
        M = m;
        Graph.resize(n+m+2);
        match.resize(n+m+2, 0);
        dist.resize(n+m+2);
    }
    // u: 0-indexed, v: 0-indexed
    void add_edge(int u, int v){
        u++; v++;
        Graph[u].push_back(N+v);
        now_edge++;
    }
    // u: 0-indexed, v: 0-indexed
    void delete_edge(int u, int v){
        u++; v++;
        now_edge--;
        for(int i = 0; i < Graph[u].size(); i++){
            if(Graph[u][i] == N+v){
                Graph[u].erase(Graph[u].begin()+i);
                break;
            }
        }
        if (match[u] == N+v){
            match[u] = 0;
            match[N+v] = 0;
            now_matching--;
        }
    }
    bool bfs(int n){
        std::queue<int> que;
        for (int i = 1; i <= N; i++){
            if (match[i]) dist[i] = INF;
            else dist[i] = 0, que.emplace(i);
        }
        dist[0] = INF;
        while (!que.empty()){
            int u = que.front(); que.pop();
            if (!u) continue;
            for (int v : Graph[u]){
                if (dist[match[v]] == INF){
                    dist[match[v]] = dist[u] + 1, que.emplace(match[v]);
                }
            }
        }
        return dist[0] != INF;
    }
    bool dfs(int u){
        if (!u) return true;
        for (int v : Graph[u]){
            if (dist[match[v]] == dist[u] + 1 && dfs(match[v])){
                match[u] = v, match[v] = u;
                return true;
            }
        }
        dist[u] = INF;
        return false;
    }
    int matching_from_scratch(){
        int res = 0;
        while (bfs(N)){
            for (int i = 1; i <= N; i++){
                res += !match[i] && dfs(i);
            }
        }
        now_matching = res;
        now_max_matching = std::max(now_max_matching, now_matching);
        return res;
    }

    int matching_again(){
        while (bfs(N)){
            for (int i = 1; i <= N; i++){
                now_matching += !match[i] && dfs(i);
            }
        }
        now_max_matching = std::max(now_max_matching, now_matching);
        return now_matching;
    }

    int matching_again_if_maximized_is_bigger(){
        //if (now_edge < now_max_matching) return matching_again();

        // matching
        return matching_again();
    }
};

int main() {
    int N,M; std::cin >> N >> M;

    HopcroftKarp hk;
    hk.init(N, M);

    std::unordered_set<long long> s;

    for (int i = 0; i < M; i++){
        int a,b; std::cin >> a >> b; a--; b--;
        hk.add_edge(a, b);
        s.insert((long long)a * (long long)N + (long long)b);
    }

    int Q; std::cin >> Q;

    hk.matching_from_scratch();

    for (int i = 0; i < Q; i++){
        int a,b; std::cin >> a >> b; a--; b--;
        if (s.find((long long)a * (long long)N + (long long)b) != s.end()){
            hk.delete_edge(a, b);
            s.erase((long long)a * (long long)N + (long long)b);
        }else{
            hk.add_edge(a, b);
            s.insert((long long)a * (long long)N + (long long)b);
        }
        if (hk.matching_again() == N){
            std::cout << "Yes" << std::endl;
        }else{
            std::cout << "No" << std::endl;
        }
    }
    
    return 0;
}
accuracy: 1
time_ms: 210
memory_kb: 3996
final_rank: 3

code_snippet: // verify: https://blog.tiramister.net/posts/acpc2020-day1-f/
// https://onlinejudge.u-aizu.ac.jp/challenges/sources/VPC/TUATPC/3198
#include <iostream>
#include <vector>
#include <queue>
#include <string>
#include <unordered_set>

struct HopcroftKarp{
    const int INF = 1e9+7;
    std::vector<std::vector<int>> Graph;
    std::vector<int> match, dist;
    int N, M;
    int now_matching = 0;
    int now_max_matching = 0;
    int now_edge = 0;
    std::vector<std::vector<bool>> delete_edge_flag;

    void init(int n, int m){
        N = n;
        M = m;
        Graph.resize(n+m+2);
        match.resize(n+m+2, 0);
        dist.resize(n+m+2);
        delete_edge_flag.resize(n+1, std::vector<bool>(m+1, false));
    }
    // u: 0-indexed, v: 0-indexed
    void add_edge(int u, int v){
        u++; v++;
        Graph[u].push_back(N+v);
        now_edge++;
    }
    // u: 0-indexed, v: 0-indexed
    void delete_edge(int u, int v){
        u++; v++;
        now_edge--;
        for(int i = 0; i < Graph[u].size(); i++){
            if(Graph[u][i] == N+v){
                Graph[u].erase(Graph[u].begin()+i);
                break;
            }
        }
        if (match[u] == N+v){
            match[u] = 0;
            match[N+v] = 0;
            now_matching--;
        }
    }
    void delete_edge_flag_on(int u, int v){
        u++; v++;
        now_edge--;
        delete_edge_flag[u][v] = true;
        if (match[u] == N+v){
            match[u] = 0;
            match[N+v] = 0;
            now_matching--;
        }
    }
    bool bfs(int n){
        std::queue<int> que;
        for (int i = 1; i <= N; i++){
            if (match[i]) dist[i] = INF;
            else dist[i] = 0, que.emplace(i);
        }
        dist[0] = INF;
        while (!que.empty()){
            int u = que.front(); que.pop();
            if (!u) continue;
            for (int v : Graph[u]){
                if (dist[match[v]] == INF){
                    dist[match[v]] = dist[u] + 1, que.emplace(match[v]);
                }
            }
        }
        return dist[0] != INF;
    }
    bool dfs(int u){
        if (!u) return true;
        for (int v : Graph[u]){
            if (dist[match[v]] == dist[u] + 1 && dfs(match[v])){
                match[u] = v, match[v] = u;
                return true;
            }
        }
        dist[u] = INF;
        return false;
    }
    int matching_from_scratch(){
        int res = 0;
        while (bfs(N)){
            for (int i = 1; i <= N; i++){
                res += !match[i] && dfs(i);
            }
        }
        now_matching = res;
        now_max_matching = std::max(now_max_matching, now_matching);
        return res;
    }

    int matching_again(){
        while (bfs(N)){
            for (int i = 1; i <= N; i++){
                now_matching += !match[i] && dfs(i);
            }
        }
        now_max_matching = std::max(now_max_matching, now_matching);
        return now_matching;
    }

    int matching_again_with_delete_edge_flag(){
        //if (now_edge < now_max_matching) return matching_again();

        // delete edge
        std::vector<std::vector<int>> new_Graph(N+M+2);
        for (int i = 1; i <= N; i++){
            for (int v : Graph[i]){
                if (!delete_edge_flag[i][v-N]){
                    new_Graph[i].emplace_back(v);
                }
            }
        }
        swap(Graph, new_Graph);

        // matching
        return matching_again();
    }
};

int main() {
    int N,M; std::cin >> N >> M;

    HopcroftKarp hk;
    hk.init(N, M);

    std::unordered_set<long long> s;

    for (int i = 0; i < M; i++){
        int a,b; std::cin >> a >> b; a--; b--;
        hk.add_edge(a, b);
        s.insert((long long)a * (long long)N + (long long)b);
    }

    int Q; std::cin >> Q;

    hk.matching_from_scratch();

    for (int i = 0; i < Q; i++){
        int a,b; std::cin >> a >> b; a--; b--;
        if (s.find((long long)a * (long long)N + (long long)b) != s.end()){
            hk.delete_edge(a, b);
            s.erase((long long)a * (long long)N + (long long)b);
        }else{
            hk.add_edge(a, b);
            s.insert((long long)a * (long long)N + (long long)b);
        }
        if (hk.matching_again() == N){
            std::cout << "Yes" << std::endl;
        }else{
            std::cout << "No" << std::endl;
        }
    }
    
    return 0;
}
accuracy: 1
time_ms: 200
memory_kb: 7112
final_rank: 6

code_snippet: // verify: https://blog.tiramister.net/posts/acpc2020-day1-f/
// https://onlinejudge.u-aizu.ac.jp/challenges/sources/VPC/TUATPC/3198
#include <iostream>
#include <vector>
#include <queue>
#include <string>
#include <unordered_set>

struct HopcroftKarp{
    const int INF = 1e9+7;
    std::vector<std::vector<int>> Graph;
    std::vector<int> match, dist;
    int N, M;
    int now_matching = 0;
    int now_max_matching = 0;
    int now_edge = 0;
    std::vector<std::vector<bool>> delete_edge_flag;

    void init(int n, int m){
        N = n;
        M = m;
        Graph.resize(n+m+2);
        match.resize(n+m+2, 0);
        dist.resize(n+m+2);
        delete_edge_flag.resize(n+1, std::vector<bool>(m+1, false));
    }
    // u: 0-indexed, v: 0-indexed
    void add_edge(int u, int v){
        u++; v++;
        Graph[u].push_back(N+v);
        delete_edge_flag[u][v] = false;
        now_edge++;
    }
    // u: 0-indexed, v: 0-indexed
    void delete_edge(int u, int v){
        u++; v++;
        now_edge--;
        for(int i = 0; i < Graph[u].size(); i++){
            if(Graph[u][i] == N+v){
                Graph[u].erase(Graph[u].begin()+i);
                break;
            }
        }
        if (match[u] == N+v){
            match[u] = 0;
            match[N+v] = 0;
            now_matching--;
        }
    }
    void delete_edge_flag_on(int u, int v){
        u++; v++;
        now_edge--;
        delete_edge_flag[u][v] = true;
        if (match[u] == N+v){
            match[u] = 0;
            match[N+v] = 0;
            now_matching--;
        }
    }
    bool bfs(int n){
        std::queue<int> que;
        for (int i = 1; i <= N; i++){
            if (match[i]) dist[i] = INF;
            else dist[i] = 0, que.emplace(i);
        }
        dist[0] = INF;
        while (!que.empty()){
            int u = que.front(); que.pop();
            if (!u) continue;
            for (int v : Graph[u]){
                if (dist[match[v]] == INF){
                    dist[match[v]] = dist[u] + 1, que.emplace(match[v]);
                }
            }
        }
        return dist[0] != INF;
    }
    bool dfs(int u){
        if (!u) return true;
        for (int v : Graph[u]){
            if (dist[match[v]] == dist[u] + 1 && dfs(match[v])){
                match[u] = v, match[v] = u;
                return true;
            }
        }
        dist[u] = INF;
        return false;
    }
    int matching_from_scratch(){
        int res = 0;
        while (bfs(N)){
            for (int i = 1; i <= N; i++){
                res += !match[i] && dfs(i);
            }
        }
        now_matching = res;
        now_max_matching = std::max(now_max_matching, now_matching);
        return res;
    }

    int matching_again(){
        while (bfs(N)){
            for (int i = 1; i <= N; i++){
                now_matching += !match[i] && dfs(i);
            }
        }
        now_max_matching = std::max(now_max_matching, now_matching);
        return now_matching;
    }

    int matching_again_with_delete_edge_flag(){
        //if (now_edge < now_max_matching) return matching_again();

        // delete edge
        std::vector<std::vector<int>> new_Graph(N+M+2);
        for (int i = 1; i <= N; i++){
            for (int v : Graph[i]){
                if (!delete_edge_flag[i][v-N]){
                    new_Graph[i].push_back(v);
                    delete_edge_flag[i][v-N] = false;
                }
            }
        }
        swap(Graph, new_Graph);

        // matching
        return matching_again();
    }
};

int main() {
    int N,M; std::cin >> N >> M;

    HopcroftKarp hk;
    hk.init(N, M);

    std::unordered_set<long long> s;

    for (int i = 0; i < M; i++){
        int a,b; std::cin >> a >> b; a--; b--;
        hk.add_edge(a, b);
        s.insert((long long)a * (long long)N + (long long)b);
    }

    int Q; std::cin >> Q;

    hk.matching_from_scratch();

    for (int i = 0; i < Q; i++){
        int a,b; std::cin >> a >> b; a--; b--;
        if (s.find((long long)a * (long long)N + (long long)b) != s.end()){
            hk.delete_edge_flag_on(a, b);
            s.erase((long long)a * (long long)N + (long long)b);
        }else{
            hk.add_edge(a, b);
            s.insert((long long)a * (long long)N + (long long)b);
        }
        if (hk.matching_again_with_delete_edge_flag() == N){
            std::cout << "Yes" << std::endl;
        }else{
            std::cout << "No" << std::endl;
        }
    }
    
    return 0;
}
accuracy: 1
time_ms: 1680
memory_kb: 7736
final_rank: 13

code_snippet: // verify: https://blog.tiramister.net/posts/acpc2020-day1-f/
// https://onlinejudge.u-aizu.ac.jp/challenges/sources/VPC/TUATPC/3198
#include <iostream>
#include <vector>
#include <queue>
#include <string>
#include <unordered_set>

struct HopcroftKarp{
    const int INF = 1e9+7;
    std::vector<std::vector<int>> Graph;
    std::vector<int> match, dist;
    int N, M;
    int now_matching = 0;
    int now_max_matching = 0;
    int now_edge = 0;
    std::vector<std::vector<bool>> delete_edge_flag;

    void init(int n, int m){
        N = n;
        M = m;
        Graph.resize(n+m+2);
        match.resize(n+m+2, 0);
        dist.resize(n+m+2);
        delete_edge_flag.resize(n+1, std::vector<bool>(m+1, false));
    }
    // u: 0-indexed, v: 0-indexed
    void add_edge(int u, int v){
        u++; v++;
        Graph[u].push_back(N+v);
        now_edge++;
    }
    // u: 0-indexed, v: 0-indexed
    void delete_edge(int u, int v){
        u++; v++;
        now_edge--;
        for(int i = 0; i < Graph[u].size(); i++){
            if(Graph[u][i] == N+v){
                Graph[u].erase(Graph[u].begin()+i);
                break;
            }
        }
        if (match[u] == N+v){
            match[u] = 0;
            match[N+v] = 0;
            now_matching--;
        }
    }
    void delete_edge_flag_on(int u, int v){
        u++; v++;
        now_edge--;
        delete_edge_flag[u][v] = true;
        if (match[u] == N+v){
            match[u] = 0;
            match[N+v] = 0;
            now_matching--;
        }
    }
    bool bfs(int n){
        std::queue<int> que;
        for (int i = 1; i <= N; i++){
            if (match[i]) dist[i] = INF;
            else dist[i] = 0, que.emplace(i);
        }
        dist[0] = INF;
        while (!que.empty()){
            int u = que.front(); que.pop();
            if (!u) continue;
            for (int v : Graph[u]){
                if (dist[match[v]] == INF){
                    dist[match[v]] = dist[u] + 1, que.emplace(match[v]);
                }
            }
        }
        return dist[0] != INF;
    }
    bool dfs(int u){
        if (!u) return true;
        for (int v : Graph[u]){
            if (dist[match[v]] == dist[u] + 1 && dfs(match[v])){
                match[u] = v, match[v] = u;
                return true;
            }
        }
        dist[u] = INF;
        return false;
    }
    int matching_from_scratch(){
        int res = 0;
        while (bfs(N)){
            for (int i = 1; i <= N; i++){
                res += !match[i] && dfs(i);
            }
        }
        now_matching = res;
        now_max_matching = std::max(now_max_matching, now_matching);
        return res;
    }

    int matching_again(){
        while (bfs(N)){
            for (int i = 1; i <= N; i++){
                now_matching += !match[i] && dfs(i);
            }
        }
        now_max_matching = std::max(now_max_matching, now_matching);
        return now_matching;
    }

    int matching_again_with_delete_edge_flag(){
        if (now_edge < now_max_matching) return 0;

        // delete edge
        std::vector<std::vector<int>> new_Graph(N+M+2);
        for (int i = 1; i <= N; i++){
            for (int v : Graph[i]){
                if (!delete_edge_flag[i][v-N]){
                    new_Graph[i].emplace_back(v);
                }
            }
        }
        swap(Graph, new_Graph);

        // matching
        return matching_again();
    }
};

int main() {
    int N,M; std::cin >> N >> M;

    HopcroftKarp hk;
    hk.init(N, M);

    std::unordered_set<long long> s;

    for (int i = 0; i < M; i++){
        int a,b; std::cin >> a >> b; a--; b--;
        hk.add_edge(a, b);
        s.insert((long long)a * (long long)N + (long long)b);
    }

    int Q; std::cin >> Q;

    hk.matching_from_scratch();

    for (int i = 0; i < Q; i++){
        int a,b; std::cin >> a >> b; a--; b--;
        if (s.find((long long)a * (long long)N + (long long)b) != s.end()){
            hk.delete_edge(a, b);
            s.erase((long long)a * (long long)N + (long long)b);
        }else{
            hk.add_edge(a, b);
            s.insert((long long)a * (long long)N + (long long)b);
        }
        if (hk.matching_again() == N){
            std::cout << "Yes" << std::endl;
        }else{
            std::cout << "No" << std::endl;
        }
    }
    
    return 0;
}
accuracy: 1
time_ms: 200
memory_kb: 7176
final_rank: 7

code_snippet: #include <iostream>
#include <vector>
#include <queue>
#include <string>
#include <unordered_set>

struct HopcroftKarp{
    const int INF = 1e9+7;
    std::vector<std::vector<int>> Graph;
    std::vector<int> match, dist;
    int N, M;
    int now_matching = 0;

    void init(int n, int m){
        N = n;
        M = m;
        Graph.resize(n+m+2);
        match.resize(n+m+2, 0);
        dist.resize(n+m+2);
    }
    // u: 0-indexed, v: 0-indexed
    void add_edge(int u, int v){
        u++; v++;
        Graph[u].push_back(N+v);
    }
    // u: 0-indexed, v: 0-indexed
    void delete_edge(int u, int v){
        u++; v++;
        for(int i = 0; i < Graph[u].size(); i++){
            if(Graph[u][i] == N+v){
                Graph[u].erase(Graph[u].begin()+i);
                break;
            }
        }
        if (match[u] == N+v){
            match[u] = 0;
            match[N+v] = 0;
            now_matching--;
        }
    }
    bool bfs(int n){
        std::queue<int> que;
        for (int i = 1; i <= N; i++){
            if (match[i]) dist[i] = INF;
            else dist[i] = 0, que.emplace(i);
        }
        dist[0] = INF;
        while (!que.empty()){
            int u = que.front(); que.pop();
            if (!u) continue;
            for (int v : Graph[u]){
                if (dist[match[v]] == INF){
                    dist[match[v]] = dist[u] + 1, que.emplace(match[v]);
                }
            }
        }
        return dist[0] != INF;
    }
    bool dfs(int u){
        if (!u) return true;
        for (int v : Graph[u]){
            if (dist[match[v]] == dist[u] + 1 && dfs(match[v])){
                match[u] = v, match[v] = u;
                return true;
            }
        }
        dist[u] = INF;
        return false;
    }
    int matching_from_scratch(){
        int res = 0;
        while (bfs(N)){
            for (int i = 1; i <= N; i++){
                res += !match[i] && dfs(i);
            }
        }
        now_matching = res;
        return res;
    }

    int matching_again(){
        while (bfs(N)){
            for (int i = 1; i <= N; i++){
                now_matching += !match[i] && dfs(i);
            }
        }
        return now_matching;
    }
};

int main() {
    int N,M; std::cin >> N >> M;

    HopcroftKarp hk;
    hk.init(N, N);

    std::unordered_set<long long> s;

    for (int i = 0; i < M; i++){
        int a,b; std::cin >> a >> b; a--; b--;
        hk.add_edge(a, b);
        s.insert((long long)a * (long long)N + (long long)b);
    }

    int Q; std::cin >> Q;

    hk.matching_from_scratch();

    for (int i = 0; i < Q; i++){
        int a,b; std::cin >> a >> b; a--; b--;
        if (s.find((long long)a * (long long)N + (long long)b) != s.end()){
            hk.delete_edge(a, b);
            s.erase((long long)a * (long long)N + (long long)b);
        }else{
            hk.add_edge(a, b);
            s.insert((long long)a * (long long)N + (long long)b);
        }
        if (hk.matching_again() == N){
            std::cout << "Yes" << std::endl;
        }else{
            std::cout << "No" << std::endl;
        }
    }
    
    return 0;
}
accuracy: 1
time_ms: 210
memory_kb: 3928
final_rank: 2

code_snippet: #include <iostream>
#include <vector>
#include <queue>
#include <string>
#include <unordered_set>

struct HopcroftKarp{
    const int INF = 1e9+7;
    std::vector<std::vector<int>> Graph;
    std::vector<int> match, dist;
    int N, M;
    int now_matching = 0;

    void init(int n, int m){
        N = n;
        M = m;
        Graph.resize(n+m+2);
        match.resize(n+m+2, 0);
        dist.resize(n+m+2);
    }
    // u: 1-indexed, v: 1-indexed
    void add_edge(int u, int v){
        Graph[u].push_back(N+v);
    }
    // u: 1-indexed, v: 1-indexed
    void delete_edge(int u, int v){
        for(int i = 0; i < Graph[u].size(); i++){
            if(Graph[u][i] == N+v){
                Graph[u].erase(Graph[u].begin()+i);
                break;
            }
        }
        if (match[u] == N+v){
            match[u] = 0;
            match[N+v] = 0;
            now_matching--;
        }
    }
    bool bfs(int n){
        std::queue<int> que;
        for (int i = 1; i <= N; i++){
            if (match[i]) dist[i] = INF;
            else dist[i] = 0, que.emplace(i);
        }
        dist[0] = INF;
        while (!que.empty()){
            int u = que.front(); que.pop();
            if (!u) continue;
            for (int v : Graph[u]){
                if (dist[match[v]] == INF){
                    dist[match[v]] = dist[u] + 1, que.emplace(match[v]);
                }
            }
        }
        return dist[0] != INF;
    }
    bool dfs(int u){
        if (!u) return true;
        for (int v : Graph[u]){
            if (dist[match[v]] == dist[u] + 1 && dfs(match[v])){
                match[u] = v, match[v] = u;
                return true;
            }
        }
        dist[u] = INF;
        return false;
    }
    int matching_from_scratch(){
        int res = 0;
        while (bfs(N)){
            for (int i = 1; i <= N; i++){
                res += !match[i] && dfs(i);
            }
        }
        now_matching = res;
        return res;
    }

    int matching_again(){
        while (bfs(N)){
            for (int i = 1; i <= N; i++){
                now_matching += !match[i] && dfs(i);
            }
        }
        return now_matching;
    }
};

int main() {
    int N,M; std::cin >> N >> M;

    HopcroftKarp hk;
    hk.init(N, N);

    std::unordered_set<long long> s;

    for (int i = 0; i < M; i++){
        int a,b; std::cin >> a >> b;
        hk.add_edge(a, b);
        s.insert((long long)a * (long long)N + (long long)b);
    }

    int Q; std::cin >> Q;

    hk.matching_from_scratch();

    for (int i = 0; i < Q; i++){
        int a,b; std::cin >> a >> b;
        if (s.find((long long)a * (long long)N + (long long)b) != s.end()){
            hk.delete_edge(a, b);
            s.erase((long long)a * (long long)N + (long long)b);
        }else{
            hk.add_edge(a, b);
            s.insert((long long)a * (long long)N + (long long)b);
        }
        if (hk.matching_again() == N){
            std::cout << "Yes" << std::endl;
        }else{
            std::cout << "No" << std::endl;
        }
    }
    
    return 0;
}
accuracy: 1
time_ms: 220
memory_kb: 4096
final_rank: 5

code_snippet: #line 1 "test/onlinejudge.u-aizu.ac.jp/Arrangement_of_pieces.0.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/challenges/sources/VPC/TUATPC/3198"
#line 2 "graph/dinic.hpp"

#line 2 "template.hpp"

#include <bits/stdc++.h>
using namespace std;

#define all(a) (a).begin(), (a).end()
#define uniq(a) (a).erase(unique(all(a)), (a).end())
using ll = long long;
using ull = unsigned long long;
using pll = pair<ll, ll>;
using vll = vector<ll>;
constexpr ll dy[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0};
constexpr ll dx[9] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
constexpr ll sign(ll a) { return (a > 0) - (a < 0); }
constexpr ll fdiv(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); }
constexpr ll cdiv(ll a, ll b) { return -fdiv(-a, b); }
constexpr ull bit(int n) { return 1ull << n; }
template <typename T> constexpr T sq(const T &a) { return a * a; }
template <typename T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
template <typename T, typename U> bool chmax(T &a, const U &b) { return a < b ? a = b, true : false; }
template <typename T, typename U> bool chmin(T &a, const U &b) { return a > b ? a = b, true : false; }
template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &a) {
    os << "(" << a.first << ", " << a.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) {
    os << "(";
    for (auto itr = a.begin(); itr != a.end(); ++itr) os << *itr << (next(itr) != a.end() ? ", " : ")");
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &a) {
    os << "(";
    for (auto itr = a.begin(); itr != a.end(); ++itr) os << *itr << (next(itr) != a.end() ? ", " : ")");
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &a) {
    os << "(";
    for (auto itr = a.begin(); itr != a.end(); ++itr) os << *itr << (next(itr) != a.end() ? ", " : ")");
    return os;
}
template <typename T, typename U> ostream &operator<<(ostream &os, const map<T, U> &a) {
    os << "(";
    for (auto itr = a.begin(); itr != a.end(); ++itr) os << *itr << (next(itr) != a.end() ? ", " : ")");
    return os;
}
#ifdef ONLINE_JUDGE
#define dump(...) (void(0))
#else
void debug() { cerr << endl; }
template <typename Head, typename... Tail> void debug(Head &&head, Tail &&... tail) {
    cerr << head;
    if (sizeof...(Tail)) cerr << ", ";
    debug(tail...);
}
#define dump(...) cerr << __LINE__ << ": " << #__VA_ARGS__ << " = ", debug(__VA_ARGS__)
#endif
struct rep {
    struct itr {
        ll v;
        itr(ll v) : v(v) {}
        void operator++() { ++v; }
        ll operator*() const { return v; }
        bool operator!=(itr i) const { return v < *i; }
    };
    ll l, r;
    rep(ll l, ll r) : l(l), r(r) {}
    rep(ll r) : rep(0, r) {}
    itr begin() const { return l; };
    itr end() const { return r; };
};
struct per {
    struct itr {
        ll v;
        itr(ll v) : v(v) {}
        void operator++() { --v; }
        ll operator*() const { return v; }
        bool operator!=(itr i) const { return v > *i; }
    };
    ll l, r;
    per(ll l, ll r) : l(l), r(r) {}
    per(ll r) : per(0, r) {}
    itr begin() const { return r - 1; };
    itr end() const { return l - 1; };
};
struct io_setup {
    static constexpr int PREC = 20;
    io_setup() {
        cout << fixed << setprecision(PREC);
        cerr << fixed << setprecision(PREC);
    };
} iOS;
#line 4 "graph/dinic.hpp"

template <typename S> struct dinic {
    using C = typename S::cost_t;
    struct _edge {
        int to, rev;
        C cap;
    };
    struct edge {
        int from, to;
        C cap, flow;
    };
    vector<vector<_edge>> g;
    vector<int> level, iter;
    vector<pair<int, int>> pos;
    dinic(int n) : g(n), level(n), iter(n) {}
    int add_edge(int from, int to, C cap) {
        int from_id = g[from].size();
        int to_id = g[to].size();
        if (from == to) ++to_id;
        g[from].push_back({to, to_id, cap});
        g[to].push_back({from, from_id, S::zero()});
        pos.emplace_back(from, from_id);
        return pos.size() - 1;
    }
    void change_edge(int i, C new_cap, C new_flow) {
        _edge &e = g[pos[i].first][pos[i].second], &re = g[e.to][e.rev];
        e.cap = new_cap - new_flow;
        re.cap = new_flow;
    }
    C flow(int s, int t, C lim = S::inf()) {
        auto bfs = [&](int s) -> void {
            fill(level.begin(), level.end(), -1);
            queue<int> q;
            level[s] = 0;
            q.push(s);
            while (!q.empty()) {
                int v = q.front();
                q.pop();
                for (_edge &e : g[v]) {
                    if (e.cap == S::zero() || level[e.to] >= 0) continue;
                    level[e.to] = level[v] + 1;
                    q.push(e.to);
                }
            }
        };
        auto dfs = [&](auto dfs, int v, int t, C lim) -> C {
            if (v == t) return lim;
            for (int &i = iter[v]; i < g[v].size(); ++i) {
                _edge &e = g[v][i];
                if (level[v] >= level[e.to] || e.cap == S::zero()) continue;
                C d = dfs(dfs, e.to, t, lim > e.cap ? e.cap : lim);
                if (d == S::zero()) continue;
                e.cap -= d;
                g[e.to][e.rev].cap += d;
                return d;
            }
            return S::zero();
        };
        C ret = S::zero();
        while (true) {
            bfs(s);
            if (level[t] < 0 || lim == S::zero()) return ret;
            fill(iter.begin(), iter.end(), 0);
            C f;
            while ((f = dfs(dfs, s, t, lim)) != S::zero()) {
                ret += f;
                lim -= f;
            }
        }
    }
    edge get_edge(int i) const {
        _edge e = g[pos[i].first][pos[i].second], re = g[e.to][e.rev];
        return {pos[i].first, e.to, e.cap + re.cap, re.cap};
    }
    vector<edge> edges() const {
        vector<edge> ret(pos.size());
        for (int i : rep(pos.size())) ret[i] = get_edge(i);
        return ret;
    }
    vector<bool> min_cut(int s) const {
        vector<bool> ret(g.size());
        auto dfs = [&](auto dfs, int v) -> void {
            if (ret[v]) return;
            ret[v] = true;
            for (_edge e : g[v]) {
                if (e.cap) dfs(dfs, e.to);
            }
        };
        dfs(dfs, s);
        return ret;
    }
};

struct ll_dinic {
    using cost_t = ll;
    static cost_t zero() { return 0; }
    static cost_t inf() { return numeric_limits<cost_t>::max(); }
};
#line 3 "test/onlinejudge.u-aizu.ac.jp/Arrangement_of_pieces.0.test.cpp"

#line 5 "test/onlinejudge.u-aizu.ac.jp/Arrangement_of_pieces.0.test.cpp"
using namespace std;

int main() {
    ll n, m;
    cin >> n >> m;
    dinic<ll_dinic> dinic(n * 2 + 2);
    vector id(n, vll(n, -1));
    ll s = n * 2, t = n * 2 + 1;
    for (ll i : rep(n)) {
        dinic.add_edge(s, i, 1);
        dinic.add_edge(i + n, t, 1);
    }
    for (ll i : rep(m)) {
        ll a, b;
        cin >> a >> b, --a, --b;
        id[a][b] = dinic.add_edge(a, b + n, 1);
    }
    ll q, f = dinic.flow(s, t);
    cin >> q;
    for (ll i : rep(q)) {
        ll x, y;
        cin >> x >> y, --x, --y;
        if (id[x][y] == -1) {
            id[x][y] = dinic.add_edge(x, y + n, 1);
        } else {
            auto e = dinic.get_edge(id[x][y]);
            if (e.flow > 0) {
                dinic.flow(x, s, 1);
                dinic.flow(t, y + n, 1);
                --f;
            }
            dinic.change_edge(id[x][y], 0, 0);
            id[x][y] = -1;
        }
        f += dinic.flow(s, t);
        if (f == n) {
            cout << "Yes" << endl;
        } else {
            cout << "No" << endl;
        }
    }
}
accuracy: 1
time_ms: 590
memory_kb: 199872
final_rank: 14

code_snippet: #line 1 "test/onlinejudge.u-aizu.ac.jp/Arrangement_of_pieces.0.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/challenges/sources/VPC/TUATPC/3198"
#line 2 "graph/dinic.hpp"

#line 2 "template.hpp"

#include <bits/stdc++.h>
using namespace std;

#define all(a) (a).begin(), (a).end()
using ll = long long;
using ull = unsigned long long;
using pll = pair<ll, ll>;
using vll = vector<ll>;
constexpr ll dy[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0};
constexpr ll dx[9] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
constexpr ll sign(ll a) { return (a > 0) - (a < 0); }
constexpr ll fdiv(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); }
constexpr ll cdiv(ll a, ll b) { return -fdiv(-a, b); }
constexpr ull bit(int n) { return 1ull << n; }
template <typename T> constexpr T sq(const T &a) { return a * a; }
template <typename T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
template <typename T, typename U> bool chmax(T &a, const U &b) { return a < b ? a = b, true : false; }
template <typename T, typename U> bool chmin(T &a, const U &b) { return a > b ? a = b, true : false; }
template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &a) {
    os << "(" << a.first << ", " << a.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) {
    os << "(";
    for (auto itr = a.begin(); itr != a.end(); ++itr) os << *itr << (next(itr) != a.end() ? ", " : ")");
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &a) {
    os << "(";
    for (auto itr = a.begin(); itr != a.end(); ++itr) os << *itr << (next(itr) != a.end() ? ", " : ")");
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &a) {
    os << "(";
    for (auto itr = a.begin(); itr != a.end(); ++itr) os << *itr << (next(itr) != a.end() ? ", " : ")");
    return os;
}
template <typename T, typename U> ostream &operator<<(ostream &os, const map<T, U> &a) {
    os << "(";
    for (auto itr = a.begin(); itr != a.end(); ++itr) os << *itr << (next(itr) != a.end() ? ", " : ")");
    return os;
}
#ifdef ONLINE_JUDGE
#define dump(...) (void(0))
#else
void debug() { cerr << endl; }
template <typename Head, typename... Tail> void debug(Head &&head, Tail &&... tail) {
    cerr << head;
    if (sizeof...(Tail)) cerr << ", ";
    debug(tail...);
}
#define dump(...) cerr << __LINE__ << ": " << #__VA_ARGS__ << " = ", debug(__VA_ARGS__)
#endif
struct rep {
    struct itr {
        ll v;
        itr(ll v) : v(v) {}
        void operator++() { ++v; }
        ll operator*() const { return v; }
        bool operator!=(itr i) const { return v < *i; }
    };
    ll l, r;
    rep(ll l, ll r) : l(l), r(r) {}
    rep(ll r) : rep(0, r) {}
    itr begin() const { return l; };
    itr end() const { return r; };
};
struct per {
    struct itr {
        ll v;
        itr(ll v) : v(v) {}
        void operator++() { --v; }
        ll operator*() const { return v; }
        bool operator!=(itr i) const { return v > *i; }
    };
    ll l, r;
    per(ll l, ll r) : l(l), r(r) {}
    per(ll r) : per(0, r) {}
    itr begin() const { return r - 1; };
    itr end() const { return l - 1; };
};
struct io_setup {
    static constexpr int PREC = 20;
    io_setup() {
        cout << fixed << setprecision(PREC);
        cerr << fixed << setprecision(PREC);
    };
} iOS;
#line 4 "graph/dinic.hpp"

template <typename S> struct dinic {
    using C = typename S::cost_t;
    struct _edge {
        int to, rev;
        C cap;
    };
    struct edge {
        int from, to;
        C cap, flow;
    };
    vector<vector<_edge>> g;
    vector<int> level, iter;
    vector<pair<int, int>> pos;
    dinic(int n) : g(n), level(n), iter(n) {}
    int add_edge(int from, int to, C cap) {
        int from_id = g[from].size();
        int to_id = g[to].size();
        if (from == to) ++to_id;
        g[from].push_back({to, to_id, cap});
        g[to].push_back({from, from_id, S::zero()});
        pos.emplace_back(from, from_id);
        return pos.size() - 1;
    }
    void change_edge(int i, C new_cap, C new_flow) {
        _edge &e = g[pos[i].first][pos[i].second], &re = g[e.to][e.rev];
        e.cap = new_cap - new_flow;
        re.cap = new_flow;
    }
    C flow(int s, int t, C lim = S::inf()) {
        auto bfs = [&](int s) -> void {
            fill(level.begin(), level.end(), -1);
            queue<int> q;
            level[s] = 0;
            q.push(s);
            while (!q.empty()) {
                int v = q.front();
                q.pop();
                for (_edge &e : g[v]) {
                    if (e.cap == S::zero() || level[e.to] >= 0) continue;
                    level[e.to] = level[v] + 1;
                    q.push(e.to);
                }
            }
        };
        auto dfs = [&](auto dfs, int v, int t, C lim) -> C {
            if (v == t) return lim;
            for (int &i = iter[v]; i < g[v].size(); ++i) {
                _edge &e = g[v][i];
                if (level[v] >= level[e.to] || e.cap == S::zero()) continue;
                C d = dfs(dfs, e.to, t, lim > e.cap ? e.cap : lim);
                if (d == S::zero()) continue;
                e.cap -= d;
                g[e.to][e.rev].cap += d;
                return d;
            }
            return S::zero();
        };
        C ret = S::zero();
        while (true) {
            bfs(s);
            if (level[t] < 0 || lim == S::zero()) return ret;
            fill(iter.begin(), iter.end(), 0);
            C f;
            while ((f = dfs(dfs, s, t, lim)) != S::zero()) {
                ret += f;
                lim -= f;
            }
        }
    }
    edge get_edge(int i) const {
        _edge e = g[pos[i].first][pos[i].second], re = g[e.to][e.rev];
        return {pos[i].first, e.to, e.cap + re.cap, re.cap};
    }
    vector<edge> edges() const {
        vector<edge> ret(pos.size());
        for (int i : rep(pos.size())) ret[i] = get_edge(i);
        return ret;
    }
    vector<bool> min_cut(int s) const {
        vector<bool> ret(g.size());
        auto dfs = [&](auto dfs, int v) -> void {
            if (ret[v]) return;
            ret[v] = true;
            for (_edge e : g[v]) {
                if (e.cap) dfs(dfs, e.to);
            }
        };
        dfs(dfs, s);
        return ret;
    }
};

struct ll_dinic {
    using cost_t = ll;
    static cost_t zero() { return 0; }
    static cost_t inf() { return numeric_limits<cost_t>::max(); }
};
#line 3 "test/onlinejudge.u-aizu.ac.jp/Arrangement_of_pieces.0.test.cpp"

#line 5 "test/onlinejudge.u-aizu.ac.jp/Arrangement_of_pieces.0.test.cpp"
using namespace std;

int main() {
    ll n, m;
    cin >> n >> m;
    dinic<ll_dinic> dinic(n * 2 + 2);
    vector id(n, vll(n, -1));
    ll s = n * 2, t = n * 2 + 1;
    for (ll i : rep(n)) {
        dinic.add_edge(s, i, 1);
        dinic.add_edge(i + n, t, 1);
    }
    for (ll i : rep(m)) {
        ll a, b;
        cin >> a >> b, --a, --b;
        id[a][b] = dinic.add_edge(a, b + n, 1);
    }
    ll q, f = dinic.flow(s, t);
    cin >> q;
    for (ll i : rep(q)) {
        ll x, y;
        cin >> x >> y, --x, --y;
        if (id[x][y] == -1) {
            id[x][y] = dinic.add_edge(x, y + n, 1);
            f += dinic.flow(s, t);
        } else {
            auto e = dinic.get_edge(id[x][y]);
            if (e.flow > 0 && dinic.flow(x, y + n, 1) == 0) {
                dinic.flow(x, s, 1);
                dinic.flow(t, y + n, 1);
                --f;
            }
            dinic.change_edge(id[x][y], 0, 0);
            id[x][y] = -1;
        }
        if (f == n) {
            cout << "Yes" << endl;
        } else {
            cout << "No" << endl;
        }
    }
}
accuracy: 1
time_ms: 590
memory_kb: 199988
final_rank: 15

code_snippet: #include <bits/stdc++.h>

using namespace std;

const int M = 100040;
const int N = 5010;

int id[N][N];
int on[N][N];

vector<int> g[M];
int v[M], u[M], cap[M], f[M];
int pnt[M], dist[M];
int sz;
int S, T;

void add_edge(int V, int U, int capVU)
{
    v[sz] = V, u[sz] = U, cap[sz] = capVU;
    g[V].push_back(sz);
    sz++;
    v[sz] = U, u[sz] = V, cap[sz] = 0;
    g[U].push_back(sz);
    sz++;
}

int dfs(int v, int want)
{
    if (want == 0)
        return 0;
    if (v == T)
        return want;
    for (; pnt[v] < g[v].size(); pnt[v]++)
    {
        int e = g[v][pnt[v]];
        if (dist[u[e]] == dist[v] + 1 && f[e] < cap[e])
        {
            int go = min(want, cap[e] - f[e]);
            int fnd = dfs(u[e], go);
            want -= fnd;
            f[e] += fnd;
            f[e ^ 1] -= fnd;
            if (fnd != 0)
                return fnd;
        }
    }
    return 0;
}

int flow()
{
    int res = 0;
    while (1)
    {
        fill(dist, dist + T + 1, -1);
        fill(pnt, pnt + T + 1, 0);
        dist[S] = 0;
        vector<int> q = {S};
        for (int i = 0; i < q.size(); i++)
        {
            for (auto e : g[q[i]])
            {
                if (dist[u[e]] == -1 && f[e] < cap[e])
                {
                    dist[u[e]] = dist[q[i]] + 1;
                    q.push_back(u[e]);
                }
            }
        }
        if (dist[T] == -1)
            break;
        int z = dfs(S, N);
        while (z != 0)
        {
            res += z;
            z = dfs(S, N);
        }
    }
    return res;
}

int to[N], fr[N];

int main()
{
    int n, m, q;
    cin >> n >> m;
    S = 2 * n, T = 2 * n + 1;
    for (int i = 0; i < n; i++)
    {
        to[i] = sz;
        add_edge(S, i, 1);
        fr[i] = sz;
        add_edge(i + n, T, 1);
    }
    for (int i = 0; i < m; i++)
    {
        int x, y;
        cin >> x >> y;
        x--, y--;
        id[x][y] = sz;
        on[x][y] = 1;
        add_edge(x, y + n, 1);
    }
    int ans = flow();
    cin >> q;
    for (int i = 0; i < q; i++)
    {
        int x, y;
        cin >> x >> y;
        x--, y--;
        if (on[x][y])
        {
            int j = id[x][y];
            if (f[j] == 1)
            {
                f[j] = 0;
                f[j ^ 1] = 0;
                f[to[x]] = 0;
                f[to[x] ^ 1] = 0;
                f[fr[y]] = 0;
                f[fr[y] ^ 1] = 0;
                ans--;
            }
            cap[j] = 0;
            on[x][y] = 0;
        }
        else
        {
            id[x][y] = sz;
            on[x][y] = 1;
            add_edge(x, y + n, 1);
        }
        ans += flow();
        if (ans == n)
        {
            cout << "Yes\n";
        }
        else
        {
            cout << "No\n";
        }
    }
}
accuracy: 1
time_ms: 600
memory_kb: 203224
final_rank: 16

code_snippet: #include <bits/stdc++.h>

using namespace std;

const int M = 40040;
const int N = 5010;

int id[N][N];
int on[N][N];

vector<int> g[N];
int v[M], u[M], cap[M], f[M];
int pnt[M], dist[M];
int sz;
int S, T;

void add_edge(int V, int U, int capVU)
{
    v[sz] = V, u[sz] = U, cap[sz] = capVU;
    g[V].push_back(sz);
    sz++;
    v[sz] = U, u[sz] = V, cap[sz] = 0;
    g[U].push_back(sz);
    sz++;
}

int dfs(int v, int want)
{
    if (want == 0)
        return 0;
    if (v == T)
        return want;
    for (; pnt[v] < g[v].size(); pnt[v]++)
    {
        int e = g[v][pnt[v]];
        if (dist[u[e]] == dist[v] + 1 && f[e] < cap[e])
        {
            int go = min(want, cap[e] - f[e]);
            int fnd = dfs(u[e], go);
            want -= fnd;
            f[e] += fnd;
            f[e ^ 1] -= fnd;
            if (fnd != 0)
                return fnd;
        }
    }
    return 0;
}

int flow()
{
    int res = 0;
    while (1)
    {
        fill(dist, dist + T + 1, -1);
        fill(pnt, pnt + T + 1, 0);
        dist[S] = 0;
        vector<int> q = {S};
        for (int i = 0; i < q.size(); i++)
        {
            for (auto e : g[q[i]])
            {
                if (dist[u[e]] == -1 && f[e] < cap[e])
                {
                    dist[u[e]] = dist[q[i]] + 1;
                    q.push_back(u[e]);
                }
            }
        }
        if (dist[T] == -1)
            break;
        int z = dfs(S, N);
        while (z != 0)
        {
            res += z;
            z = dfs(S, N);
        }
    }
    return res;
}

int to[N], fr[N];

int main()
{
    int n, m, q;
    cin >> n >> m;
    S = 2 * n, T = 2 * n + 1;
    for (int i = 0; i < n; i++)
    {
        to[i] = sz;
        add_edge(S, i, 1);
        fr[i] = sz;
        add_edge(i + n, T, 1);
    }
    for (int i = 0; i < m; i++)
    {
        int x, y;
        cin >> x >> y;
        x--, y--;
        id[x][y] = sz;
        on[x][y] = 1;
        add_edge(x, y + n, 1);
    }
    int ans = flow();
    cin >> q;
    for (int i = 0; i < q; i++)
    {
        int x, y;
        cin >> x >> y;
        x--, y--;
        if (on[x][y])
        {
            int j = id[x][y];
            if (f[j] == 1)
            {
                f[j] = 0;
                f[j ^ 1] = 0;
                f[to[x]] = 0;
                f[to[x] ^ 1] = 0;
                f[fr[y]] = 0;
                f[fr[y] ^ 1] = 0;
                ans--;
            }
            cap[j] = 0;
            on[x][y] = 0;
        }
        else
        {
            id[x][y] = sz;
            on[x][y] = 1;
            add_edge(x, y + n, 1);
        }
        ans += flow();
        if (ans == n)
        {
            cout << "Yes\n";
        }
        else
        {
            cout << "No\n";
        }
    }
}
accuracy: 0.48484848484848486
time_ms: 280
memory_kb: 88412
final_rank: 17

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 9223372036854775807
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define SIZE 10005

struct Info{

	int a,b;
	bool del_FLG;
};

struct Edge{
	Edge(int arg_to,int arg_capacity,int arg_rev_index){
		to = arg_to;
		capacity = arg_capacity;
		rev_index = arg_rev_index;
	}
	int to,capacity,rev_index;
};

int N,M;
int V,E;
vector<Edge> G[SIZE];
int dist[SIZE],PRE[SIZE];
int cheked_index[SIZE];
Info info[SIZE];
map<pair<int,int>,int> MAP;
map<int,int> INDEX[SIZE];


void add_edge(int from,int to,int capacity){

	INDEX[from][to] = G[from].size();
	G[from].push_back(Edge(to,capacity,G[to].size()));

	INDEX[to][from] = G[to].size();
	G[to].push_back(Edge(from,0,G[from].size()-1));
}

void bfs(int source){
	for(int i = 0; i < V; i++)dist[i] = -1;
	queue<int> Q;
	dist[source] = 0;
	Q.push(source);

	while(!Q.empty()){
		int node_id = Q.front();
		Q.pop();
		for(int i = 0; i < G[node_id].size(); i++){
			Edge &e = G[node_id][i];
			if(e.capacity > 0 && dist[e.to] < 0){
				dist[e.to] = dist[node_id]+1;
				Q.push(e.to);
			}
		}
	}
}

int dfs(int node_id,int sink,int flow){
	if(node_id == sink)return flow;

	for(int &i = cheked_index[node_id]; i < G[node_id].size(); i++){
		Edge &e = G[node_id][i];
		if(e.capacity > 0 && dist[node_id] < dist[e.to]){
			int tmp_flow = dfs(e.to,sink,min(flow,e.capacity));
			if(tmp_flow > 0){
				e.capacity -= tmp_flow;
				G[e.to][e.rev_index].capacity += tmp_flow;
				return tmp_flow;
			}
		}
	}
	return 0;
}


int max_flow(int source,int sink){
	int flow = 0,add;
	while(true){
		bfs(source);
		if(dist[sink] < 0)break;
		for(int i = 0; i < V; i++)cheked_index[i] = 0;
		while((add = dfs(source,sink,BIG_NUM)) > 0){
			flow += add;
		}
	}
	return flow;
}

//source→sinkに押し戻す
void back_flow(int source,int sink){
	for(int i = 0; i < V; i++)PRE[i] = -1;
	queue<int> Q;
	PRE[source] = -1;
	Q.push(source);

	while(!Q.empty()){
		int node_id = Q.front();
		Q.pop();
		for(int i = 0; i < G[node_id].size(); i++){
			Edge &e = G[node_id][i];
			if(e.capacity > 0 && G[e.to][e.rev_index].capacity == 0 && PRE[e.to] < 0){
				PRE[e.to] = node_id;
				Q.push(e.to);
			}
		}
	}

	int current = sink;
	while(current != source){
		int pre = PRE[current];
		for(int i = 0; i < G[current].size(); i++){
			if(G[current][i].to == pre){

				G[current][i].capacity = 1;
				G[pre][G[current][i].rev_index].capacity = 0;

				break;
			}
		}
		current = pre;
	}
}


int main(){

	scanf("%d %d",&N,&M);

	V = 2*N+2;

	int source = 2*N,sink = 2*N+1;

	for(int i = 0; i < N; i++){

		add_edge(source,i,1);
		add_edge(N+i,sink,1);
	}

	for(int i = 0; i < M; i++){

		scanf("%d %d",&info[i].a,&info[i].b);

		info[i].a--;
		info[i].b--;
		info[i].del_FLG = false;
		MAP[make_pair(info[i].a,info[i].b)] = i;

		add_edge(info[i].a,N+info[i].b,1);
	}


	int num_query;
	scanf("%d",&num_query);

	int x,y;

	int pre = max_flow(source,sink);

	for(int loop = 0; loop < num_query; loop++){

		scanf("%d %d",&x,&y);
		x--;
		y--;

		auto at = MAP.find(make_pair(x,y));

		if(at == MAP.end()){ //新たに張れる辺が増えた場合

			info[M].a = x;
			info[M].b = y;
			info[M].del_FLG = false;

			add_edge(info[M].a,N+info[M].b,1);

			MAP[make_pair(x,y)] = M++;

			if(pre == N){

				printf("Yes\n");
			}else{

				pre += max_flow(source,sink);

				if(pre == N){

					printf("Yes\n");
				}else{

					printf("No\n");
				}
			}

		}else{ //既出の辺

			int tmp_index = MAP[make_pair(x,y)];

			info[tmp_index].del_FLG = not info[tmp_index].del_FLG;

			if(info[tmp_index].del_FLG){ //可だった辺が不可になった

				//printf("可だった辺が不可になった\n");

				int work_index = INDEX[info[tmp_index].a][N+info[tmp_index].b];

				if(G[info[tmp_index].a][work_index].capacity == 0){ //a列とb行がマッチしていた場合

					//printf("マッチしていた\n");
					pre--;

					back_flow(info[tmp_index].a,source);
					back_flow(sink,N+info[tmp_index].b);
				}

				G[info[tmp_index].a][work_index].capacity = 0;

				work_index = INDEX[N+info[tmp_index].b][info[tmp_index].a];
				G[N+info[tmp_index].b][work_index].capacity = 0;

				pre += max_flow(source,sink);

				if(pre == N){

					printf("Yes\n");
				}else{

					printf("No\n");
				}

			}else{ //不可だった辺が可になった

				//printf("不可だった辺が可になった\n");

				int work_index = INDEX[info[tmp_index].a][N+info[tmp_index].b];
				G[info[tmp_index].a][work_index].capacity = 1;

				work_index = INDEX[N+info[tmp_index].b][info[tmp_index].a];
				G[N+info[tmp_index].b][work_index].capacity = 0;

				if(pre == N){

					printf("Yes\n");

				}else{

					pre += max_flow(source,sink);

					if(pre == N){

						printf("Yes\n");
					}else{

						printf("No\n");
					}
				}
			}
		}
	}
	return 0;
}
accuracy: 1
time_ms: 670
memory_kb: 7480
final_rank: 12

code_snippet: #include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <unordered_map>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <assert.h>
#include <unordered_map>
#include <fstream>
#include <ctime>
#include <complex>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 510000;
ll dy[8] = {1,-1,0,0,1,-1,1,-1};
ll dx[8] = {0,0,1,-1,1,-1,-1,1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    rep(i,a.size()) cout << a[i] << " ";
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){cout << a << " " << b << "\n";}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << a << " " << b << " " << c << "\n";
}
ll pcount(ll x) {return __builtin_popcountll(x);}
const int mod = 1e9 + 7;
//const int mod = 998244353;

struct Dinic{
    struct edge{
        int to, cap, rev;
    };
    vector<vector<edge>> G;
    vector<int> level, itr;
    ll n;
    static const ll INF = 1<<30;
 
    Dinic(ll n) : n(n){
        G.resize(n);
        level.resize(n);
        itr.resize(n);
    }
 
    void add_edge(int from, int to, int cap){
        for(auto &e : G[from]){
            if(e.to == to){
                e.cap += cap;
                return;
            }
        }
        G[from].push_back({to,cap,(int)G[to].size()});
        G[to].push_back({from,0,(int)G[from].size()-1});
    }
 
    void bfs(int s){
        fill(level.begin(), level.end(), -1);
        queue<int> q;
        level[s] = 0;
        q.push(s);
        while(!q.empty()){
            int u = q.front(); q.pop();
            for(auto &e : G[u]){
                if(e.cap > 0 && level[e.to] < 0){
                    level[e.to] = level[u] + 1;
                    q.push(e.to);
                }
            }
        }
    }

    int dfs(int u, int t, int f){
        if(u == t) return f;
        for(int &i = itr[u]; i < G[u].size(); i++){
            edge &e = G[u][i];
            if(e.cap > 0 && level[u] < level[e.to]){
                ll d = dfs(e.to, t, min(f, e.cap));
                if(d > 0){
                    e.cap -= d;
                    G[e.to][e.rev].cap += d;
                    return d;
                }
            }
        }
        return 0;
    }
 
    int max_flow(int s, int t, int lim = INF){
        int flow = 0;
        while(flow < lim){
            bfs(s);
            if(level[t] < 0) return flow;
            fill(itr.begin(), itr.end(), 0);
            int f;
            while(flow < lim){
                f = dfs(s, t, INF);
                if(!f) break;
                flow += f;
            }
        }
        return flow;
    }
};

bool exist[5005][5005];

int main(){
    int n,m; cin >> n >> m;
    Dinic g(2*n+2);
    int S = 2*n, T = 2*n+1;
    rep(i,n){
        g.add_edge(S,i,1);
        g.add_edge(i+n,T,1);
    }
    rep(i,m){
        int a,b; cin >> a >> b; a--; b--;
        g.add_edge(a,b+n,1);
        exist[a][b] = true;
    }
    int ans = g.max_flow(S,T);
    int q; cin >> q;
    while(q--){
        int x,y; cin >> x >> y; x--; y--;
        if(exist[x][y]){
            rep(v,g.G[x].size()){
                if(g.G[x][v].to == y+n){
                    auto &e = g.G[x][v];
                    if(e.cap == 0){
                        if(g.max_flow(x,y+n,1) == 0){
                            g.max_flow(T,y+n,1);
                            g.max_flow(x,S,1);
                            ans--;
                        }
                    }
                    g.G[e.to][e.rev].cap = 0;
                    e.cap = 0;
                    break;
                }
            }
            exist[x][y] = false;
        }else{
            g.add_edge(x,y+n,1);
            exist[x][y] = true;
        }
        ans += g.max_flow(S,T,1);
        if(ans == n) cout << "Yes" << "\n";
        else cout << "No" << "\n";
    }
}
accuracy: 1
time_ms: 510
memory_kb: 23584
final_rank: 11

code_snippet: #include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <unordered_map>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <assert.h>
#include <unordered_map>
#include <fstream>
#include <ctime>
#include <complex>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 510000;
ll dy[8] = {1,-1,0,0,1,-1,1,-1};
ll dx[8] = {0,0,1,-1,1,-1,-1,1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    rep(i,a.size()) cout << a[i] << " ";
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){cout << a << " " << b << "\n";}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << a << " " << b << " " << c << "\n";
}
ll pcount(ll x) {return __builtin_popcountll(x);}
const int mod = 1e9 + 7;
//const int mod = 998244353;

struct Dinic{
    struct edge{
        int to, cap, rev;
    };
    vector<vector<edge>> G;
    vector<int> level, itr;
    ll n;
    static const ll INF = 1<<30;
 
    Dinic(ll n) : n(n){
        G.resize(n);
        level.resize(n);
        itr.resize(n);
    }
 
    void add_edge(int from, int to, int cap){
        for(auto &e : G[from]){
            if(e.to == to){
                e.cap += cap;
                return;
            }
        }
        G[from].push_back({to,cap,(int)G[to].size()});
        G[to].push_back({from,0,(int)G[from].size()-1});
    }
 
    void bfs(int s){
        fill(level.begin(), level.end(), -1);
        queue<int> q;
        level[s] = 0;
        q.push(s);
        while(!q.empty()){
            int u = q.front(); q.pop();
            for(auto &e : G[u]){
                if(e.cap > 0 && level[e.to] < 0){
                    level[e.to] = level[u] + 1;
                    q.push(e.to);
                }
            }
        }
    }

    int dfs(int u, int t, int f){
        if(u == t) return f;
        for(int &i = itr[u]; i < G[u].size(); i++){
            edge &e = G[u][i];
            if(e.cap > 0 && level[u] < level[e.to]){
                ll d = dfs(e.to, t, min(f, e.cap));
                if(d > 0){
                    e.cap -= d;
                    G[e.to][e.rev].cap += d;
                    return d;
                }
            }
        }
        return 0;
    }
 
    int max_flow(int s, int t, int lim = INF){
        int flow = 0;
        while(flow < lim){
            bfs(s);
            if(level[t] < 0) return flow;
            fill(itr.begin(), itr.end(), 0);
            int f;
            while(flow < lim){
                f = dfs(s, t, INF);
                if(!f) break;
                flow += f;
            }
        }
        return flow;
    }
};

bool exist[5005][5005];

int main(){
    int n,m; cin >> n >> m;
    Dinic g(2*n+2);
    int S = 2*n, T = 2*n+1;
    rep(i,n){
        g.add_edge(S,i,1);
        g.add_edge(i+n,T,1);
    }
    rep(i,m){
        int a,b; cin >> a >> b; a--; b--;
        g.add_edge(a,b+n,1);
        exist[a][b] = true;
    }
    int ans = g.max_flow(S,T);
    int q; cin >> q;
    while(q--){
        int x,y; cin >> x >> y; x--; y--;
        if(exist[x][y]){
            rep(v,g.G[x].size()){
                if(g.G[x][v].to == y+n){
                    auto &e = g.G[x][v];
                    if(e.cap == 0){
                        g.G[S][x].cap = 1;
                        g.G[x][g.G[S][x].rev].cap = 0;
                        g.G[T][y].cap = 0;
                        g.G[y+n][g.G[T][y].rev].cap = 1;
                        ans--;
                    }
                    g.G[e.to][e.rev].cap = 0;
                    e.cap = 0;
                    break;
                }
            }
            exist[x][y] = false;
        }else{
            g.add_edge(x,y+n,1);
            exist[x][y] = true;
        }
        ans += g.max_flow(S,T,1);
        if(ans == n) cout << "Yes" << "\n";
        else cout << "No" << "\n";
    }
}
accuracy: 1
time_ms: 470
memory_kb: 23664
final_rank: 10

code_snippet: #include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <unordered_map>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <assert.h>
#include <unordered_map>
#include <fstream>
#include <ctime>
#include <complex>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 510000;
ll dy[8] = {1,-1,0,0,1,-1,1,-1};
ll dx[8] = {0,0,1,-1,1,-1,-1,1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    rep(i,a.size()) cout << a[i] << " ";
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){cout << a << " " << b << "\n";}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << a << " " << b << " " << c << "\n";
}
ll pcount(ll x) {return __builtin_popcountll(x);}
const int mod = 1e9 + 7;
//const int mod = 998244353;

struct Dinic{
    struct edge{
        int to, cap, rev;
    };
    vector<vector<edge>> G;
    vector<int> level, itr;
    ll n;
    static const ll INF = 1<<30;
 
    Dinic(ll n) : n(n){
        G.resize(n);
        level.resize(n);
        itr.resize(n);
    }
 
    void add_edge(int from, int to, int cap){
        G[from].push_back({to,cap,(int)G[to].size()});
        G[to].push_back({from,0,(int)G[from].size()-1});
    }
 
    void bfs(int s){
        fill(level.begin(), level.end(), -1);
        queue<int> q;
        level[s] = 0;
        q.push(s);
        while(!q.empty()){
            int u = q.front(); q.pop();
            for(auto &e : G[u]){
                if(e.cap > 0 && level[e.to] < 0){
                    level[e.to] = level[u] + 1;
                    q.push(e.to);
                }
            }
        }
    }

    int dfs(int u, int t, int f){
        if(u == t) return f;
        for(int &i = itr[u]; i < G[u].size(); i++){
            edge &e = G[u][i];
            if(e.cap > 0 && level[u] < level[e.to]){
                ll d = dfs(e.to, t, min(f, e.cap));
                if(d > 0){
                    e.cap -= d;
                    G[e.to][e.rev].cap += d;
                    return d;
                }
            }
        }
        return 0;
    }
 
    int max_flow(int s, int t, int lim = INF){
        int flow = 0;
        while(flow < lim){
            bfs(s);
            if(level[t] < 0) return flow;
            fill(itr.begin(), itr.end(), 0);
            int f;
            while(flow < lim){
                f = dfs(s, t, INF);
                if(!f) break;
                flow += f;
            }
        }
        return flow;
    }
};

bool exist[5005][5005];

int main(){
    int n,m; cin >> n >> m;
    Dinic g(2*n+2);
    int S = 2*n, T = 2*n+1;
    rep(i,n){
        g.add_edge(S,i,1);
        g.add_edge(i+n,T,1);
    }
    rep(i,m){
        int a,b; cin >> a >> b; a--; b--;
        g.add_edge(a,b+n,1);
        exist[a][b] = true;
    }
    int ans = g.max_flow(S,T);
    int q; cin >> q;
    while(q--){
        int x,y; cin >> x >> y; x--; y--;
        if(exist[x][y]){
            rep(v,g.G[x].size()){
                if(g.G[x][v].to == y+n){
                    auto &e = g.G[x][v];
                    if(e.cap == 0){
                        if(g.max_flow(x,y+n,1) == 0){
                            g.max_flow(T,y+n,1);
                            g.max_flow(x,S,1);
                            ans--;
                        }
                    }
                    g.G[e.to][e.rev].cap = 0;
                    e.cap = 0;
                    break;
                }
            }
            exist[x][y] = false;
        }else{
            g.add_edge(x,y+n,1);
            exist[x][y] = true;
        }
        ans += g.max_flow(S,T,1);
        if(ans == n) cout << "Yes" << "\n";
        else cout << "No" << "\n";
    }
}
accuracy: 0.12121212121212122
time_ms: 20
memory_kb: 3388
final_rank: 19

code_snippet: #include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <unordered_map>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <assert.h>
#include <unordered_map>
#include <fstream>
#include <ctime>
#include <complex>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 510000;
ll dy[8] = {1,-1,0,0,1,-1,1,-1};
ll dx[8] = {0,0,1,-1,1,-1,-1,1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    rep(i,a.size()) cout << a[i] << " ";
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){cout << a << " " << b << "\n";}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << a << " " << b << " " << c << "\n";
}
ll pcount(ll x) {return __builtin_popcountll(x);}
const int mod = 1e9 + 7;
//const int mod = 998244353;

struct Dinic{
    struct edge{
        int to, cap, rev;
    };
    vector<vector<edge>> G;
    vector<int> level, itr;
    ll n;
    static const ll INF = 1<<30;
 
    Dinic(ll n) : n(n){
        G.resize(n);
        level.resize(n);
        itr.resize(n);
    }
 
    void add_edge(int from, int to, int cap){
        G[from].push_back({to,cap,(int)G[to].size()});
        G[to].push_back({from,0,(int)G[from].size()-1});
    }
 
    void bfs(int s){
        fill(level.begin(), level.end(), -1);
        queue<int> q;
        level[s] = 0;
        q.push(s);
        while(!q.empty()){
            int u = q.front(); q.pop();
            for(auto &e : G[u]){
                if(e.cap > 0 && level[e.to] < 0){
                    level[e.to] = level[u] + 1;
                    q.push(e.to);
                }
            }
        }
    }

    int dfs(int u, int t, int f){
        if(u == t) return f;
        for(int &i = itr[u]; i < G[u].size(); i++){
            edge &e = G[u][i];
            if(e.cap > 0 && level[u] < level[e.to]){
                ll d = dfs(e.to, t, min(f, e.cap));
                if(d > 0){
                    e.cap -= d;
                    G[e.to][e.rev].cap += d;
                    return d;
                }
            }
        }
        return 0;
    }
 
    int max_flow(int s, int t, int lim = INF){
        int flow = 0;
        while(flow < lim){
            bfs(s);
            if(level[t] < 0) return flow;
            fill(itr.begin(), itr.end(), 0);
            int f;
            while(flow < lim){
                f = dfs(s, t, INF);
                if(!f) break;
                flow += f;
            }
        }
        return flow;
    }
};

bool exist[5005][5005];

int main(){
    int n,m; cin >> n >> m;
    Dinic g(2*n+2);
    int S = 2*n, T = 2*n+1;
    rep(i,n){
        g.add_edge(S,i,1);
        g.add_edge(i+n,T,1);
    }
    rep(i,m){
        int a,b; cin >> a >> b; a--; b--;
        g.add_edge(a,b+n,1);
        exist[a][b] = true;
    }
    int ans = g.max_flow(S,T);
    int q; cin >> q;
    while(q--){
        int x,y; cin >> x >> y; x--; y--;
        if(exist[x][y]){
            rep(v,g.G[x].size()){
                if(g.G[x][v].to == y+n){
                    if(g.G[x][v].cap == 0){
                        if(g.max_flow(x,y+n,1) == 0){
                            g.max_flow(T,y+n,1);
                            g.max_flow(x,S,1);
                            ans--;
                        }
                    }
                    g.G[y+n][g.G[x][v].rev].cap = 0;
                    g.G[x][v].cap = 0;
                    break;
                }
            }
            exist[x][y] = false;
        }else{
            g.add_edge(x,y+n,1);
            exist[x][y] = true;
        }
        ans += g.max_flow(S,T);
        if(ans == n) cout << "Yes" << "\n";
        else cout << "No" << "\n";
    }
}
accuracy: 0.12121212121212122
time_ms: 10
memory_kb: 3412
final_rank: 18

code_snippet: #include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <unordered_map>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <assert.h>
#include <unordered_map>
#include <fstream>
#include <ctime>
#include <complex>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 510000;
ll dy[8] = {1,-1,0,0,1,-1,1,-1};
ll dx[8] = {0,0,1,-1,1,-1,-1,1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    rep(i,a.size()) cout << a[i] << " ";
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){cout << a << " " << b << "\n";}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << a << " " << b << " " << c << "\n";
}
ll pcount(ll x) {return __builtin_popcountll(x);}
const int mod = 1e9 + 7;
//const int mod = 998244353;

struct Dinic{
    struct edge{
        int to, cap, rev;
    };
    vector<vector<edge>> G;
    vector<int> level, itr;
    ll n;
    static const ll INF = 1<<30;
 
    Dinic(ll n) : n(n){
        G.resize(n);
        level.resize(n);
        itr.resize(n);
    }
 
    void add_edge(int from, int to, int cap){
        G[from].push_back({to,cap,(int)G[to].size()});
        G[to].push_back({from,0,(int)G[from].size()-1});
    }
 
    void bfs(int s){
        fill(level.begin(), level.end(), -1);
        queue<int> q;
        level[s] = 0;
        q.push(s);
        while(!q.empty()){
            int u = q.front(); q.pop();
            for(auto &e : G[u]){
                if(e.cap > 0 && level[e.to] < 0){
                    level[e.to] = level[u] + 1;
                    q.push(e.to);
                }
            }
        }
    }

    int dfs(int u, int t, int f){
        if(u == t) return f;
        for(int &i = itr[u]; i < G[u].size(); i++){
            edge &e = G[u][i];
            if(e.cap > 0 && level[u] < level[e.to]){
                ll d = dfs(e.to, t, min(f, e.cap));
                if(d > 0){
                    e.cap -= d;
                    G[e.to][e.rev].cap += d;
                    return d;
                }
            }
        }
        return 0;
    }
 
    int max_flow(int s, int t, int lim = INF){
        int flow = 0;
        while(flow < lim){
            bfs(s);
            if(level[t] < 0) return flow;
            fill(itr.begin(), itr.end(), 0);
            int f;
            while(flow < lim){
                f = dfs(s, t, INF);
                if(!f) break;
                flow += f;
            }
        }
        return flow;
    }
};

bool exist[5005][5005];

int main(){
    int n,m; cin >> n >> m;
    Dinic g(2*n+2);
    int S = 2*n, T = 2*n+1;
    rep(i,n){
        g.add_edge(S,i,1);
        g.add_edge(i+n,T,1);
    }
    rep(i,m){
        int a,b; cin >> a >> b; a--; b--;
        g.add_edge(a,b+n,1);
        exist[a][b] = true;
    }
    int ans = g.max_flow(S,T);
    int q; cin >> q;
    while(q--){
        int x,y; cin >> x >> y; x--; y--;
        if(exist[x][y]){
            rep(v,g.G[x].size()){
                if(g.G[x][v].to == y+n){
                    g.G[y+n][g.G[x][v].rev].cap = 0;
                    if(g.G[x][v].cap == 0){
                        g.max_flow(T,y+n,1);
                        g.max_flow(x,S,1);
                        ans--;
                    }
                    g.G[x][v].cap = 0;
                    break;
                }
            }
            exist[x][y] = false;
        }else{
            g.add_edge(x,y+n,1);
            exist[x][y] = true;
        }
        ans += g.max_flow(S,T);
        if(ans == n) cout << "Yes" << "\n";
        else cout << "No" << "\n";
    }
}
accuracy: 0.12121212121212122
time_ms: 40
memory_kb: 3200
final_rank: 20