106 lines
2.6 KiB
C++
106 lines
2.6 KiB
C++
#include <algorithm>
|
|
#include <cstdint>
|
|
#include <functional>
|
|
#include <ios>
|
|
#include <iostream>
|
|
#include <map>
|
|
#include <ostream>
|
|
#include <utility>
|
|
|
|
using namespace std;
|
|
using ll = int64_t;
|
|
|
|
template<class T,class P>
|
|
ostream&operator<<(ostream&os,map<T,P>const&m){
|
|
os<<"map { ";
|
|
for(auto [k,v]:m){
|
|
os<<"{ "<<k<<", "<<v<<" }, ";
|
|
}
|
|
os<<"}";
|
|
return os;
|
|
}
|
|
template<class T,class P>
|
|
ostream&operator<<(ostream&os,pair<T,P>const&m){
|
|
os<<"pair { "<<m.first<<", "<<m.second<<" }";
|
|
return os;
|
|
}
|
|
|
|
|
|
ll const maxn{ll(1e5+5)};
|
|
ll t,n,m,inf{ll(1)<<60};
|
|
ll q[maxn],qs{};
|
|
pair<ll, ll>e[maxn];
|
|
|
|
struct DSU{
|
|
ll f[maxn];
|
|
DSU(){for(ll i{1};i<=n;i++){f[i]=i;}}
|
|
ll find(ll const&n){if(f[n]==n)return n; return f[n]=find(f[n]);}
|
|
bool merge(ll const&a,ll const&b){if(find(a)==find(b))return false;f[find(a)]=find(b);return true;}
|
|
friend ostream&operator<<(ostream&os,DSU const& d){
|
|
os<<"DSU { ";
|
|
for(ll i{1};i<=n;i++)os<<d.f[i]<<", ";
|
|
os<<"}";
|
|
return os;
|
|
}
|
|
};
|
|
char dmem[sizeof(DSU)+1024];
|
|
DSU &dsu = *(DSU*)dmem;
|
|
ll un[maxn];
|
|
|
|
int main(){
|
|
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
|
|
|
|
cin>>t;
|
|
while(t--){
|
|
cin>>n>>m;
|
|
// cout<<n<<" "<<m<<'\n';
|
|
for(ll i{1};i<=m;i++){
|
|
ll u,v;
|
|
cin>>u>>v;
|
|
if(u>v)swap(u,v);
|
|
// cout<<"uv "<<u<<' '<<v<<'\n';
|
|
e[i].first=u,e[i].second=v;
|
|
}
|
|
sort(e+1,e+1+m,greater());
|
|
new(dmem) DSU();
|
|
// cout<<dsu<<'\n';
|
|
ll uns{};
|
|
for(ll i{1};i<=m;i++){
|
|
// cout<<e[i]<<'\n';
|
|
if(!dsu.merge(e[i].first, e[i].second)){
|
|
un[++uns]=i;
|
|
}
|
|
}
|
|
// cout<<dsu<<'\n';
|
|
map<ll, ll> mm;
|
|
for(ll i{1};i<=n;i++){
|
|
mm[dsu.find(i)] = min(mm[dsu.find(i)]==0?inf:mm[dsu.find(i)],i);
|
|
}
|
|
// cout<<mm<<' '<<uns<<' '<<un[uns]<<' '<<e[un[uns]]<<'\n';
|
|
// cout<<mm.size()<<' '<<uns<<'\n';
|
|
if((ll)mm.size()-1>uns&&mm.size()!=1){
|
|
cout<<"-1"<<'\n';
|
|
continue;
|
|
}
|
|
if(mm.size()==1){
|
|
cout<<"0\n";
|
|
continue;
|
|
}
|
|
|
|
qs=0;
|
|
for(auto const[k,v]:mm){
|
|
q[++qs]=v;
|
|
}
|
|
sort(q+1,q+1+qs);
|
|
// cout<<mm<<'\n';
|
|
// for(ll i{1};i<=qs;i++)cout<<q[i]<<" ,";
|
|
// cout<<'\n';
|
|
// cout<<mm<<'\n';
|
|
cout<<mm.size()-1<<'\n';
|
|
for(ll i{2};i<=qs;i++){
|
|
cout<<e[un[uns]].first<<" "<<e[un[uns]].second<<" "<<q[1]<<" "<<q[i]<<'\n';
|
|
--uns;
|
|
}
|
|
}
|
|
}
|