#include <bits/stdc++.h>
using namespace std;
int main() {
int test;
cin>>test;
while(test--){
unsigned long long int sum=0,temper=0,valuer=1;
int n,m;
cin>>n>>m;
vector<long int>vec[n];
for(int i1=0;i1<m;i1++){
int a,b;
cin>>a>>b;
a--;b--;
vec[a].push_back(b);
vec[b].push_back(a);
}
vector<vector<long int> >result;
vector<bool>visited(n,false);
for(int i1=0;i1<n;i1++)
if(visited[i1]==false)
{
vector<long int>temp;
vector<int>dist(n,-1);
dist[i1]=0;//starting number is always 0..
queue<int>que;
temp.push_back(i1);
que.push(i1);//starting vertex....
visited[i1]=true;
while(!que.empty()){
int index=que.front();que.pop();
for(int i=0;i<vec[index].size();i++){
if(dist[vec[index][i]]==-1 ){
dist[vec[index][i]]=dist[index]+1;
que.push(vec[index][i]);
temp.push_back(vec[index][i]);
visited[vec[index][i]]=true;
}
}
}
result.push_back(temp);
}
pair<long int,long int>pr[result.size()];
for(int i=0;i<result.size();i++){
long int pp=0,qq=0,rr=0;
for(int j=0;j<result[i].size();j++){
pp+=vec[result[i][j]].size();
// cout<<result[i][j]<<" ";
}
// cout<<endl;
pr[i] = make_pair(pp,result[i].size());
}
sort(pr,pr+result.size());
reverse(pr,pr+result.size());
for(int i=0;i<result.size();i++){
pr[i].first = pr[i].first / 2;
//cout<<pr[i].first<<" "<<pr[i].second<<endl;
}
unsigned long long int temp1=0,temp2=0,glb=0,gman=0,presum=0,cnt=0;
for(int i=0;i<result.size();i++)
{
int flg=0;presum=0;
if(pr[i].first>0)
{
sum+=(pr[i].second-1)*gman;
}
for(int j=1;j<=pr[i].first;j++){
if(j<pr[i].second){
sum+=(j+1)*(j);
presum=j*(j+1);
}
else {
cnt++;
//sum+=presum;
}
}
gman+=presum;
//cout<<sum<<"\n";
}
sum+=cnt*gman;
cout<<sum<<endl;
}
return 0;
}
using namespace std;
int main() {
int test;
cin>>test;
while(test--){
unsigned long long int sum=0,temper=0,valuer=1;
int n,m;
cin>>n>>m;
vector<long int>vec[n];
for(int i1=0;i1<m;i1++){
int a,b;
cin>>a>>b;
a--;b--;
vec[a].push_back(b);
vec[b].push_back(a);
}
vector<vector<long int> >result;
vector<bool>visited(n,false);
for(int i1=0;i1<n;i1++)
if(visited[i1]==false)
{
vector<long int>temp;
vector<int>dist(n,-1);
dist[i1]=0;//starting number is always 0..
queue<int>que;
temp.push_back(i1);
que.push(i1);//starting vertex....
visited[i1]=true;
while(!que.empty()){
int index=que.front();que.pop();
for(int i=0;i<vec[index].size();i++){
if(dist[vec[index][i]]==-1 ){
dist[vec[index][i]]=dist[index]+1;
que.push(vec[index][i]);
temp.push_back(vec[index][i]);
visited[vec[index][i]]=true;
}
}
}
result.push_back(temp);
}
pair<long int,long int>pr[result.size()];
for(int i=0;i<result.size();i++){
long int pp=0,qq=0,rr=0;
for(int j=0;j<result[i].size();j++){
pp+=vec[result[i][j]].size();
// cout<<result[i][j]<<" ";
}
// cout<<endl;
pr[i] = make_pair(pp,result[i].size());
}
sort(pr,pr+result.size());
reverse(pr,pr+result.size());
for(int i=0;i<result.size();i++){
pr[i].first = pr[i].first / 2;
//cout<<pr[i].first<<" "<<pr[i].second<<endl;
}
unsigned long long int temp1=0,temp2=0,glb=0,gman=0,presum=0,cnt=0;
for(int i=0;i<result.size();i++)
{
int flg=0;presum=0;
if(pr[i].first>0)
{
sum+=(pr[i].second-1)*gman;
}
for(int j=1;j<=pr[i].first;j++){
if(j<pr[i].second){
sum+=(j+1)*(j);
presum=j*(j+1);
}
else {
cnt++;
//sum+=presum;
}
}
gman+=presum;
//cout<<sum<<"\n";
}
sum+=cnt*gman;
cout<<sum<<endl;
}
return 0;
}
No comments:
Post a Comment