VILLAGES - Villages by the River - SPOJ Solution C++

  Problem Link : VILLAGES 


👉 Hint : Use bitset in C++ STL

 


✅ C++ Solution :

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

#define ll long long int

int main()
{
	int n,m,a,b;
	ll ans=0,c;
	cin>>n>>m;
	bitset<1001> arr[n+1];
	bitset<1001> bt;
	while(m--)
	{
		cin>>a>>b;
		arr[a].set(b);
	}

	for(int i=1;i<n;i++)
	{
		for(int j=i+1;j<=n;j++)
		{
			bt=arr[i] & arr[j];
			c=bt.count();
			ans+=(c*(c-1))/2;
		}
	}
	cout<<ans;

}

 

Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook. 

😇Happy Learning!!