#include <bits/stdc++.h>
#include
#include<stdio.h>
#include
#include<math.h>
#include
#include
#include
#include
#include
// #include < unordered_map >
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
#define ll long long
#define pb push_back
#define P pair<ll,ll>
#define F first
#define S second
const ll N = 100005;
struct Graph{
ll size;
vector<ll> *adjlist;
Graph(ll n){
size=n;
adjlist=new vector<ll>[size];
}
void addedge(ll a,ll b){
adjlist[a].pb(b);
adjlist[b].pb(a);
}
ll dfs_helper(bool *visited,ll node){
bool flag=false;
for(auto x : adjlist[node]){
if(!visited[x]){
flag=true;
}
}
if(flag==false) return 1;
ll val=1;
visited[node]=true;
for(auto neighbour : adjlist[node]){
if(!visited[neighbour]){
val+=dfs_helper(visited,neighbour);
}
}
// cout<<val<<endl;
return val;
}
ll dfs(){
bool *visited=new bool[size];
memset(visited,false,sizeof(visited));
ll ans=(size)*(size-1)/2;
for(ll i=0;i<size;i++){
ll node = i;
if(!visited[node]){
ll temp= dfs_helper(visited,node);
ans-=(temp)*(temp-1)/2;
}
}
return ans;
}
};
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
ll n,e;
cin>>n>>e;
if(n==2||n==1){
cout<<0<<endl;
return 0;
}
struct Graph g(n);
for(ll i=0;i<e;i++){
ll e1,e2;
cin>>e1>>e2;
g.addedge(e1,e2);
}
cout<<g.dfs();
}
This is my code for this question. Please tell why 2 test cases are not working