#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) begin(a),end(a)
using ll = long long;
using vi = vector<int>;
using ar2 = array<int,2>;
const int mxN = (int)2e3+10;
const ll LINF = (ll)1e17;
int n, m;
int vis[mxN];
set<int> adj[mxN];
bool dfs(int s, int t){
	if(vis[s]) return 0; vis[s] = 1;
	if(s==t) return 1;
	for(auto u : adj[s]) 
		if(dfs(u,t)) return 1;
	return 0;
}
int main(){
	ios_base::sync_with_stdio(false); cin.tie(0);
	cin >> n >> m;
	for(int i = 1; i < n; i++) adj[i].insert(i+1);
	for(int i = n; i < n*2; i++) adj[i+1].insert(i);
	while(m--){
		char t; int x, y, ok=0; 
		cin >> t >> x >> y;
		if(t=='A'){
			adj[x].insert(y); adj[y].insert(x);
		}
		else if(t=='B'){
			if(x<=n and y<=n and x>y)swap(x,y);
			if(x>n and y>n and x<y)swap(x,y);
			adj[x].erase(adj[x].find(y));
		}
		else{
			for(int i = 1; i <= n*2; i++) vis[i]=0;
			cout << (dfs(x,y)?"DA":"NE") << "\n";
		}
	}
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |