Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.3 kB
5
Indexable
Never
#include<iostream>
using namespace std;
//Queue.h
#define MAX_QUEUE 100000
template <typename T>
class Queue{
public:
	T Q[MAX_QUEUE];
	int front; 
	int rear;

	Queue()
	{
		front  = rear = -1;
	}
	void reset()
	{
		front  = rear = -1;
	}
	bool isEmpty()
	{
		return front == -1 || front > rear;
	}
	bool isFull()
	{
		return front == 0 && rear == MAX_QUEUE - 1;
	}
	void enQueue(T data)
	{
		if(!isFull())
		{
			if(front == -1) front++;
			rear++;
			Q[rear] = data;
		}
	}
	T deQueue()
	{
		if(!isEmpty())
		{
			T ele = Q[front];
			front++;
			return ele;
		}
	}
	T head()
	{
		if(!isEmpty())
			return Q[front];
	}
	T tail()
	{
		if(!isEmpty())
			return Q[rear];
	}
};
//
#define MAX_N 105
int M, N, u, v;
int cnt[MAX_N];
int adj[MAX_N][MAX_N];
int temp[MAX_N][MAX_N];
int visited[MAX_N];
int ans;
Queue<int> q = Queue<int>();
void resetVisit()
{
	for(int i = 1; i <= N; i++)
	{
		visited[i] = 0;
	}
}
void clearCol(int u)
{
	for(int j = 1; j <= N; j++)
	{
		if(temp[j][u] == 1)
			temp[j][u] = 0;
	}
}
void returnCol(int u)
{
	for(int j = 1; j <= N; j++)
	{
		if(adj[j][u] == 1)
			temp[j][u] = 1;
	}
}
int main()
{
	int tc;
	int T, x, y;
	freopen("input_2.txt", "r", stdin);
	cin >> T;
	for(tc = 1; tc <= T; ++tc)
	{
		cin>> N >> M >> u >> v;
		for(int i = 1; i <= N; i++)
		{
			cnt[i] = 0;
			visited[i] = 0;
			for(int j = 1; j <= N; j++)
			{
				adj[i][j] = 0;
				temp[i][j] = 0;
			}
		}
		for(int i = 0; i < M; i++)
		{
			cin>>x>>y;
			adj[x][y] = 1;
			temp[x][y] = 1;
		}
		ans = 0;
		int cp, np;
		bool check;
		for(int i = 1; i <= N; i++)
		{
			if(i != u && i != v)
			{
				resetVisit();
				clearCol(i);
				q.reset();
				q.enQueue(u);
				visited[u] = 1;
				check = false;
				while(!q.isEmpty())
				{
					cp = q.deQueue();
					for(int k = 1; k <= N; k++)
					{
						if(temp[cp][k] == 1 && visited[k] == 0)
						{
							q.enQueue(k);
							visited[k] = 1;
							if(k == v)
							{
								check = true;
								goto next;
							}
						}
					}
				}
			next:
				if(!check)
					ans++;
				returnCol(i);
			}
		}
		cout<<ans<<endl<<endl;
	}
	return 0;
}

Output:

0

3

2

0

1


Leave a Comment