最小生成树
题解
题解
考虑使用 kruscal,发现加边是 的,于是考虑优化。
发现一次加边相当于是区间 和 间的连边,其中
我们使用类 st 表维护区间 与 是否已经全合成一个点了。若全合成一个点就 skip。期望
std
std
/* Use the slash-star style comments or the system won't see your
identification information */
/*
ID: zhuheng1
TASK:
LANG: C++
*/
/* LANG can be C++11 or C++14 for those more recent releases */
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=998244353;
const int N=100005;
#undef _GLIBCXX_DEBUG
template <typename A, typename B>string to_string(pair<A, B> p);
template <typename A, typename B, typename C>string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) { return '"' + s + '"';}
string to_string(const char* s) { return to_string((string) s);}
string to_string(bool b) { return (b ? "true" : "false");}
string to_string(vector<bool> v) { bool first = true; string res = "{"; for (int i = 0; i < static_cast<int>(v.size()); i++) { if (!first) { res += ", "; } first = false; res += to_string(v[i]); } res += "}"; return res;}
template <size_t N> string to_string(bitset<N> v) { string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast<char>('0' + v[i]); } return res; }
template <typename A> string to_string(A v) {bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res;}
template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; }
template <typename A, typename B, typename C>string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; }
template <typename A, typename B, typename C, typename D>string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; }
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); }
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
int n;
struct node{
int dis,sum;
friend bool operator<(node a,node b){
return a.dis<b.dis;
}
}nd[400005];
struct dsu{
int f[200005*20*2],sz[200005*20*2];
void init(){
for(int i=0;i<200005*20*2;i++){
f[i]=i;
sz[i]=1;
}
}
int getf(int x) {
if(f[x]==x)return x;
return f[x]=getf(f[x]);
}
void merge(int u,int v){
int p=getf(u),q=getf(v);
if(p==q)return;
if(sz[p]>sz[q])swap(p,q);
f[p]=q;
sz[q]+=sz[p];
}
bool check(int u,int v){
int p=getf(u),q=getf(v);
// cout<<p<<" "<<q<<endl;
if(p==q)return 1;
return 0;
}
}t;
int lg2[400005];
int pt(int x,int y,int z){
if(y==0)z=0;
return (x-1)*20*2+(y-1)*2+z;
}
int merge(int l1,int r1,int l2,int r2){
// printf("%d %d %d %d\n",l1,r1,l2,r2);
if(l1>r1||l2>r2)return 0;
if(l1==r1&&l2==r2){
// printf("%d %d\n",pt(l1,0,0),pt(l2,0,1));
if(l1!=l2&&(!t.check(pt(l1,0,0),pt(l2,0,1)))){
t.merge(pt(l1,0,0),pt(l2,0,1));
// cout<<"qwk\n";
return 1;
}
return 0;
}
int ct=lg2[r1-l1+1];
int ret=0;
if(!t.check(pt(l1,ct,0),pt(r2-(1<<ct)+1,ct,1))){
ret+=merge(l1,l1+(1<<ct-1)-1,r2-(1<<ct-1)+1,r2);
ret+=merge(l1+(1<<ct-1),l1+(1<<ct)-1,r2-(1<<ct)+1,r2-(1<<ct-1));
t.merge(pt(l1,ct,0),pt(r2-(1<<ct)+1,ct,1));
}
ret+=merge(l1+(1<<ct),r1,l2,r2-(1<<ct));
return ret;
}
void work(){
/*fill your code here*/
t.init();
cin>>n;
for(int i=2;i<=400000;i++){
lg2[i]=lg2[i/2]+1;
}
int m=0;
for(int i=3;i<=2*n-1;i++){
int a;
cin>>a;m++;
nd[m].dis=a;
nd[m].sum=i;
}
sort(nd+1,nd+1+m);
ll ans=0;
for(int i=1;i<=m;i++){
if(nd[i].sum<=n+1){
ans+=(ll)nd[i].dis*merge(1,nd[i].sum-1,1,nd[i].sum-1);
}
else {
// cout<<nd[i].dis<<" "<<nd[i].sum<<" "<<n<<endl;
ans+=(ll)nd[i].dis*merge(nd[i].sum-n,n,nd[i].sum-n,n);
}
// cout<<ans<<endl;
}
cout<<ans;
return;
}
#ifdef int
signed main()
#else
int main()
#endif
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
// ios::sync_with_stdio(0);
// cin.tie(0);cout.tie(0);
int T=1;
// cin>>T; /*If there's only one test case, comment this line.*/
while(T--){
work();
}
return 0;
}
/* Code written by
gala_xk (MarchWind)*/
Meeting for Meals
题解
题解
先跑一遍 Dijkstra,找出所有点到点 1 的距离,然后注意到,对于每一条边,只会对离它最近的在它两边的两个点造成影响(在其上面相遇),否则多余的那个点会和这两个点中离他近的一个走,相遇点不在这条边上(因为在同侧)。
然后我们需要求离当前点最近的点。这个只需要把所有点丢进去跑一遍 Dijkstra 即可。
std
std
/* Use the slash-star style comments or the system won't see your
identification information */
/*
ID: zhuheng1
TASK:
LANG: C++
*/
/* LANG can be C++11 or C++14 for those more recent releases */
#include <bits/stdc++.h>
#define int ll
using namespace std;
typedef long long ll;
const int mod=998244353;
const int N=100005;
#undef _GLIBCXX_DEBUG
template <typename A, typename B>string to_string(pair<A, B> p);
template <typename A, typename B, typename C>string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) { return '"' + s + '"';}
string to_string(const char* s) { return to_string((string) s);}
string to_string(bool b) { return (b ? "true" : "false");}
string to_string(vector<bool> v) { bool first = true; string res = "{"; for (int i = 0; i < static_cast<int>(v.size()); i++) { if (!first) { res += ", "; } first = false; res += to_string(v[i]); } res += "}"; return res;}
template <size_t N> string to_string(bitset<N> v) { string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast<char>('0' + v[i]); } return res; }
template <typename A> string to_string(A v) {bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res;}
template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; }
template <typename A, typename B, typename C>string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; }
template <typename A, typename B, typename C, typename D>string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; }
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); }
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
int n,m,k;
struct point{
int u,dis,frm;
friend bool operator < (point x,point y){
if(x.dis==y.dis)return x.u>y.u;
return x.dis>y.dis;
}
};
vector<point> ed[300005];
priority_queue<point> q;
int dis[300005];
int u[1000005],v[1000005],w[1000005];
int dist[300005],pt[300005],vv[300005];
double ans[300005];
int frd[300005];
//int ans=0;
void work(){
/*fill your code here*/
cin>>n>>m>>k;
for(int i=1;i<=n;i++){
ed[i].clear();ans[i]=0;
}
for(int i=1;i<=k;i++){
cin>>frd[i];
}
for(int i=1;i<=m;i++){
cin>>u[i]>>v[i]>>w[i];
ed[u[i]].push_back({v[i],w[i]});
ed[v[i]].push_back({u[i],w[i]});
}
q.push({1,0,0});
for(int i=1;i<=n;i++){
dis[i]=-1;
vv[i]=0;
dist[i]=-1;
}
while(!q.empty()){
point x=q.top();q.pop();
if(dis[x.u]!=-1) continue;
dis[x.u]=x.dis;
for(int i=0;i<ed[x.u].size();i++){
if(dis[ed[x.u][i].u]==-1){
q.push({ed[x.u][i].u,x.dis+ed[x.u][i].dis,0});
}
}
}
int ccc=0;
for(int i=1;i<=k;i++){
ccc=max(ccc,dis[frd[i]]);
// cout<<dis[i]<<" ";
}
// cout<<ccc<<endl;
for(int i=1;i<=k;i++)q.push({frd[i],0,frd[i]});
while(!q.empty()){
point x=q.top();q.pop();
if(dist[x.u]!=-1) continue;
// if(x.frm!=x.u){
dist[x.u]=x.dis;
pt[x.u]=x.frm;
// }
// else {
// if(vv[x.u])
// continue;
// vv[x.u]=1;
// }
for(int i=0;i<ed[x.u].size();i++){
if(dist[ed[x.u][i].u]==-1){
q.push({ed[x.u][i].u,x.dis+ed[x.u][i].dis,x.frm});
}
}
}
// puts("----");
// for(int i=1;i<=n;i++){
// printf("%d %d\n",dist[i],pt[i]);
// }
for(int i=1;i<=m;i++){
for(int p=1;p<=2;p++){
if(pt[u[i]]!=pt[v[i]]&&(dist[v[i]]+w[i]+dis[u[i]]<=ccc||dist[u[i]]+w[i]+dis[v[i]]<=ccc)){
// cout<<pt[u[i]]<<" "<<pt[v[i]]<<endl;
double tm=ccc-(dist[v[i]]+dist[u[i]]+w[i])/2.0;
ans[pt[u[i]]]=max(ans[pt[u[i]]],tm);
ans[pt[v[i]]]=max(ans[pt[v[i]]],tm);
}swap(u[i],v[i]);
}
}
for(int i=1;i<=k;i++){
printf("%.1lf ",ans[frd[i]]);
}
puts("");
return;
}
#ifdef int
signed main()
#else
int main()
#endif
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
// ios::sync_with_stdio(0);
// cin.tie(0);cout.tie(0);
int T=1;
cin>>T; /*If there's only one test case, comment this line.*/
while(T--){
work();
}
return 0;
}
/* Code written by
gala_xk (MarchWind)*/
/*
1
5 5 2
4 2
5 1 10
2 5 7
3 1 10
1 4 3
4 2 6
*/
题解
题解
std
std
Case of Computer Network
题解
题解
考虑哪些边是必走的。我们发现,每个(边)双联通分量只需要指定一个流向,即可从一点走向任意一点(边双的性质)。于是我们考虑对答案有影响的仅有割边。缩点之后跑树上差分即可。
std
std
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n,m,val[200005],q[200005],vis[200005],cnt,to,inq[200005],dfn[200005],minm[200005],col[200005],tim,dp[200005],ccc[200005];
vector<int> ed[200005],ed2[200005];
void dfs(int x,int fa){
// printf("%d %d\n",cnt,x);
q[to++]=x;inq[x]=1;
dfn[x]=++tim;
minm[x]=dfn[x];
//printf("%d %d %d\n",x,dfn[x],minm[x]);
for(int i=0;i<ed[x].size();i++){
int nx=ed[x][i];
if(nx==fa){
fa=0;
continue;
}
if(dfn[nx]){
if(inq[nx]){
minm[x]=min(minm[x],dfn[nx]);
}
}
else{
dfs(nx,x);
minm[x]=min(minm[x],minm[nx]);
}
}
if(dfn[x]==minm[x]){
// cout<<x<<endl;
cnt++;
int nx=0;
do{
nx=q[--to];
col[nx]=cnt;
inq[nx]=0;
}while(minm[nx]!=dfn[nx]&&to);
}
}
int f[200005][20],dep[200005];
void init_tree(int x,int fa){
f[x][0]=fa;dep[x]=dep[fa]+1;
for(auto v:ed2[x]){
if(v==fa)continue;
init_tree(v,x);
}
}
int LCA(int u,int v){
if(dep[u]<dep[v])swap(u,v);
for(int i=19;i>=0;i--){
if(dep[f[u][i]]>=dep[v])u=f[u][i];
}
for(int i=19;i>=0;i--){
if(f[u][i]!=f[v][i]){
u=f[u][i];
v=f[v][i];
}
}
if(u!=v)return f[u][0];
return u;
}
int tag1[200005],tag2[200005];
void sizuru_cute(int x,int f){
vis[x]=1;
for(auto v:ed2[x]){
if(v==f)continue;
sizuru_cute(v,x);
tag1[x]+=tag1[v];
tag2[x]+=tag2[v];
}
// cout<<tag1[x]<<" "<<tag2[x]<<endl;
if(tag1[x]&&tag2[x]){
puts("No");
exit(0);
}
}
signed main()
{
int q;
cin>>n>>m>>q;
for(int i=1;i<=m;i++){
int u,v;
scanf("%d%d",&u,&v);
ed[u].push_back(v);
ed[v].push_back(u);
}
for(int i=1;i<=n;i++)if(!dfn[i])dfs(i,i);
for(int i=1;i<=n;i++){
for(int j=0;j<ed[i].size();j++){
if(col[i]!=col[ed[i][j]])
ed2[col[i]].push_back(col[ed[i][j]]);
}
}
for(int i=1;i<=cnt;i++){
sort(ed2[i].begin(),ed2[i].end());
ed2[i].erase(unique(ed2[i].begin(),ed2[i].end()),ed2[i].end());
}
for(int i=1;i<=n;i++)
if(!f[i][0])
init_tree(i,i);
for(int i=1;i<20;i++){
for(int j=1;j<=n;j++){
f[j][i]=f[f[j][i-1]][i-1];
}
}
while(q--){
int u,v;
cin>>u>>v; u=col[u];v=col[v];
int lca=LCA(u,v);
// cout<<lca<<endl;
tag1[lca]+=-1;
tag1[u]+=1;
tag2[lca]+=-1;
tag2[v]+=1;
}
for(int i=1;i<=n;i++)
if(!vis[i])
sizuru_cute(i,i);
puts("Yes");
// cout<<970+369+910+889+470+106+658+659+916+964;
return 0;
}
/*
5 4
1 2 4 8 16
1 2
1 3
3 4
4 5
*/Cycle sort
题解
题解
将每个点向其排序好的位置连边,然后跑欧拉回路,一个回路上的所有点就是一次排序的交换。
考虑合并 个回路,代价为 ,需要先全转一遍,这时每个环上都有一个点错位了,此时再往回转一遍即可。
然后就是同色,建一个超级点压点即可。
std
std
/* Use the slash-star style comments or the system won't see your
identification information */
/*
ID: zhuheng1
TASK:
LANG: C++
*/
/* LANG can be C++11 or C++14 for those more recent releases */
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=998244353;
const int N=100005;
#undef _GLIBCXX_DEBUG
template <typename A, typename B>string to_string(pair<A, B> p);
template <typename A, typename B, typename C>string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) { return '"' + s + '"';}
string to_string(const char* s) { return to_string((string) s);}
string to_string(bool b) { return (b ? "true" : "false");}
string to_string(vector<bool> v) { bool first = true; string res = "{"; for (int i = 0; i < static_cast<int>(v.size()); i++) { if (!first) { res += ", "; } first = false; res += to_string(v[i]); } res += "}"; return res;}
template <size_t N> string to_string(bitset<N> v) { string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast<char>('0' + v[i]); } return res; }
template <typename A> string to_string(A v) {bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res;}
template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; }
template <typename A, typename B, typename C>string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; }
template <typename A, typename B, typename C, typename D>string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; }
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); }
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
int a[500005],b[500005];
int n,s;
int vis[500005],cur[500006];
vector<int>vt[500005];
vector<pair<int,int> >ed[500005];
int cnt;
void dfs(int x){
for(int i=cur[x];i<ed[x].size();i++){
i=max(i,cur[x]);
cur[x]++;
if(i>=ed[x].size())break;
dfs(ed[x][i].first);
vt[cnt].push_back(ed[x][i].second);
// cout<<ed[x][i].second<<endl;
}
}map<int,int>bk;
void work(){
/*fill your code here*/
cin>>n>>s;
for(int i=1;i<=n;i++){
cin>>a[i];b[i]=a[i];
}
sort(b+1,b+1+n);
int ttt=0;
for(int i=1;i<=n;i++){
if(b[i]!=b[i-1])bk[b[i]]=++ttt;
}
int res1=0;
for(int i=n;i>=1;i--){
if(a[i]!=b[i])
ed[bk[a[i]]].push_back({bk[b[i]],i});
// else res1++;
}
for(int i=1;i<=ttt;i++){
if(ed[i].size()>cur[i]){
// cout<<i<<endl;
cnt++;
dfs(i);
res1+=vt[cnt].size();
}
}//cout<<s<<" "<<res1<<endl;
if(s<res1){
puts("-1");
exit(0);
}
int rem=min(cnt,s-res1);//剩余代价
if(rem<2){//买不起
cout<<cnt<<endl;
for(int i=1;i<=cnt;i++){
cout<<vt[i].size()<<endl;
for(auto v:vt[i]){
cout<<v<<" ";
}cout<<endl;
}
}
else {
cout<<cnt-rem+2<<endl;
vector<int>nvt,vt2;
for(int i=1;i<=rem;i++){
for(auto v:vt[i]){
nvt.push_back(v);
}
}
for(int i=rem;i>=1;i--)
vt2.push_back(vt[i][0]);
cout<<nvt.size()<<endl;
for(auto v:nvt){
cout<<v<<" ";
}cout<<endl;
cout<<vt2.size()<<endl;
for(auto v:vt2){
cout<<v<<" ";
}cout<<endl;
for(int i=rem+1;i<=cnt;i++){
cout<<vt[i].size()<<endl;
for(auto v:vt[i]){
cout<<v<<" ";
}cout<<endl;
}
}
return;
}
#ifdef int
signed main()
#else
int main()
#endif
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
// ios::sync_with_stdio(0);
// cin.tie(0);cout.tie(0);
int T=1;
// cin>>T; /*If there's only one test case, comment this line.*/
while(T--){
work();
}
return 0;
}
/* Code written by
gala_xk (MarchWind)*/
Making It Bipartite
题解
见这篇博客
[AGC029F] Construction of a tree
题解
题解
个集合内,有 条树边,于是 都在这个集合内,即一个集合对应了一个 的 和一个非根节点。考虑二分图匹配,剩下的一个点就是根节点,从所有含根节点的集合出发,找出这些节点的匹配(也就是对于每一个 ,找出),然后递归下去处理完整棵树即可。
std
std
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int inf=0x3f3f3f3f3f3f3f3f;
struct node{
int u,dis,num;
};
vector<node> ed[2000005];
int n,m,s,t,mk[2000005],now[2000005];
bool bfs(){
queue<node>q;
memset(mk,-1,sizeof(mk));
q.push({s,0,0});
while(!q.empty()){
node x=q.front();q.pop();
if(mk[x.u]>=0)continue;
if(x.u==s&&x.dis>0)continue;
mk[x.u]=x.dis;
if(x.u==t)break;
for(int i=0;i<ed[x.u].size();i++){
node v=ed[x.u][i];
if(v.dis)q.push({v.u,x.dis+1,0});
}
}
return mk[t]>-1;
}
int dfs(int x,int fl){
if(x==t){
return fl;
}
int ret=0;
for(int i=now[x];i<ed[x].size();i++){
now[x]=i;
node v=ed[x][i];
if(!v.dis)continue;
if(mk[v.u]!=mk[x]+1)continue;
int k=dfs(v.u,min(fl,v.dis));
ed[x][i].dis-=k;
ed[v.u][v.num].dis+=k;
fl-=k;
ret+=k;
if(!fl)break;
}
return ret;
}
void add_edge(int u,int v,int w){
int s1=ed[u].size();
int s2=ed[v].size();
ed[u].push_back({v,w,s2});
ed[v].push_back({u,0,s1});
}
queue<int>q;
vector<int>bel[2000005];
int vis[2000005],too[2000005];
struct nodee{
int key,u,v;
friend bool operator <(nodee a,nodee b){
return a.key<b.key;
}
};
signed main(){
cin>>n;
int ans=0;
s=0;
t=n*2;
for(int i=1;i<n;i++){
int p;
cin>>p;
for(int j=1;j<=p;j++){
int u;
cin>>u;
bel[u].push_back(i);
add_edge(u,i+n,1);
}
}
for(int i=1;i<=n;i++)
add_edge(0,i,1);
for(int i=1;i<n;i++)
add_edge(i+n,t,1);
int cnt=0;
while(bfs()){
memset(now,0,sizeof(now));
ans+=dfs(s,inf);
}
if(ans!=n-1){
puts("-1");return 0;
}//cout<<";;";
int st;
for(int i=0;i<n;i++){
if(ed[0][i].dis==1){
st=i+1;
}
else{
for(auto v:ed[i+1]){
if(v.dis==0){
too[v.u-n]=i+1;
}
}
// cout<<i+1<<" "<<too[i+1]<<"\n";
}
}
q.push(st);
vis[st]=1;
vector<nodee>aa;
while(!q.empty()){
int cc=q.front();
q.pop();
for(auto v:bel[cc]){
if(!vis[too[v]]){
q.push(too[v]);
vis[too[v]]=1;
aa.push_back({v,cc,too[v]});
}
}
}
sort(aa.begin(),aa.end());
if(aa.size()==n-1){
for(auto v:aa){
printf("%d %d\n",v.u,v.v);
}
}
else{
puts("-1");
}
return 0;
}P13356 [GDCPC 2024] 图
题解
题解
注意到是无向图连通性问题,并且 的计算含有 ,可以想到这个和并查集有点关系。
具体地,我们建 个并查集,二分查找该条边可以贡献的并查集,将其加入。如果第 个并查集中被加入了一条边,这条边就是答案(因为前 个并查集中都给出了一种构造)。输出路径使用 dfs 即可。
std
std
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=998244353;
const int N=100005;
#undef _GLIBCXX_DEBUG
template <typename A, typename B>string to_string(pair<A, B> p);
template <typename A, typename B, typename C>string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) { return '"' + s + '"';}
string to_string(const char* s) { return to_string((string) s);}
string to_string(bool b) { return (b ? "true" : "false");}
string to_string(vector<bool> v) { bool first = true; string res = "{"; for (int i = 0; i < static_cast<int>(v.size()); i++) { if (!first) { res += ", "; } first = false; res += to_string(v[i]); } res += "}"; return res;}
template <size_t N> string to_string(bitset<N> v) { string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast<char>('0' + v[i]); } return res; }
template <typename A> string to_string(A v) {bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res;}
template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; }
template <typename A, typename B, typename C>string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; }
template <typename A, typename B, typename C, typename D>string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; }
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); }
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
namespace Rewrite{
int n,m,u[200005],v[200005];
vector<int>f[200005];
vector<int>ed[200005],eg[200005];
int getf(int x,int k){
if(f[k][x]==x)return x;
return getf(f[k][x],k);
}
int flag;vector<int>ans;
void dfs(int x,int f,int aim){
if(x==aim){
flag=1;ans.push_back(x);
return;
}
for(auto v:ed[x]){
if(v==f)continue;
dfs(v,x,aim);
if(flag)break;
}
if(flag) ans.push_back(x);
}
void Sizuru_Nakatsu(){
/*fill your code here*/
cin>>n>>m;
for(int i=1;i<=m;i++){
cin>>u[i]>>v[i];
}
int k=ceil(1.0*m/(n-1));
for(int i=1;i<=k;i++){
f[i].clear();eg[i].clear();
for(int j=0;j<=n;j++)f[i].push_back(j);
}
for(int i=1;i<=m;i++){
int l=1,r=k;
while(l<r){
int mid=(l+r)/2;
if(getf(u[i],mid)==getf(v[i],mid)){
l=mid+1;
}
else r=mid;
}
eg[l].push_back(i);
f[l][getf(u[i],l)]=getf(v[i],l);
if(l==k){//cout<<endl;
cout<<u[i]<<" "<<v[i]<<endl;
for(int j=1;j<=k;j++){
for(int ss=1;ss<=n;ss++)ed[ss].clear();
for(auto p:eg[j]){
ed[u[p]].push_back(v[p]);
ed[v[p]].push_back(u[p]);
}
flag=0;ans.clear();
dfs(v[i],v[i],u[i]);
cout<<ans.size()<<" ";
for(auto v:ans)cout<<v<<" ";
cout<<endl;
}
return;
}
}
cout<<"-1\n";
return;
}
}
#ifdef int
signed main()
#else
int main()
#endif
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
// ios::sync_with_stdio(0);
// cin.tie(0);cout.tie(0);
int T=1;
cin>>T; /*If there's only one test case, comment this line.*/
while(T--){
Rewrite::Sizuru_Nakatsu();
}
return 0;
}
/* Code written by
gala_xk(Shizuru_Nakatsu)*/