QOJ.ac

QOJ

IDSubmission IDProblemHackerOwnerResultSubmit timeJudge time
#991#634633#9453. Graph GeneratorQingyuucup-team4717Success!2024-10-14 16:52:222024-10-14 16:52:22

Details

Extra Test:

Time Limit Exceeded

input:

28
49999 99999
26903 5029
23317 26903
23317 5029
4470 26903
4470 5029
23317 31323
31323 26903
31323 5029
31323 17955
23317 17955
17955 26903
17955 5029
5029 3731
5029 4863
14611 26903
5029 14611
26238 5029
4470 2789
2789 26903
2789 5029
5029 33449
27707 23317
26903 27707
5029 27707
23317 7484
26903 ...

output:

Yes
1 0 
2 0 
3 0 
4 0 
5 0 
6 0 
7 0 
8 0 
9 0 
10 0 
11 0 
12 0 
13 0 
14 0 
15 0 
16 0 
17 0 
18 0 
19 0 
20 0 
21 0 
22 0 
23 0 
24 0 
25 0 
26 0 
27 0 
28 0 
29 0 
30 0 
31 0 
32 0 
33 0 
34 0 
35 0 
36 0 
37 0 
38 0 
39 0 
40 0 
41 0 
42 0 
43 0 
44 0 
45 0 
46 0 
47 0 
48 0 
49 0 
50 0 
51 0 ...

result:


IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#634633#9453. Graph Generatorucup-team4717#TL 996ms41648kbC++202.3kb2024-10-12 17:46:532024-10-14 16:53:54

answer

#include<bits/stdc++.h>
#define pii pair<int,int>
#define fi first
#define sd second
using namespace std;
const int N=2e5+5;
inline int read(){
	int x=0,f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch)){x=x*10+ch-48,ch=getchar();}
	return x*f;
}
int T,n,m,tot,in[N];
set<int> e[N];
set<pii,greater<pii > > s;
bool flg;

int vis[N],idx,siz,mx;
void dfs(int u){
	vis[u]=idx,siz++;
	for(int v:e[u]){
		if(vis[v]!=idx){
			dfs(v);
			if(siz-1>mx) return;
		}
	}
}
void dfs2(int u){
	vis[u]=idx;
	for(int v:e[u])
		if(vis[v]!=idx) 
			dfs2(v);
}

int id[N];
vector<int> ans[N];

int fa[N],sz[N],U[N],V[N],In[N];
inline int Find(int x){
	return x==fa[x]?x:fa[x]=Find(fa[x]);
}

map<int,int> mp;
map<pair<int,int> ,bool> lk;

inline void merge(int x,int y){
	x=Find(x),y=Find(y);
	if(x==y) return;
	fa[x]=y,sz[y]+=sz[x];
}

signed main(){	
	T=read();
	while(T--){
		n=read(),m=read(),flg=0;
		for(int u,v,i=1;i<=m;i++){
			u=read(),v=read();
			e[u].insert(v);
			e[v].insert(u);
			U[i]=u,V[i]=v;
			in[u]++,in[v]++;
		}
		idx=1,tot=0,s.clear();
		for(int i=1;i<=n;i++) s.insert({in[i],i});
		while(!s.empty()){
			int u=(*s.begin()).sd;
			s.erase(s.begin());

			for(int v:e[u]){
				s.erase({in[v],v});
				in[v]--;
				s.insert({in[v],v});
				e[v].erase(u);
			}			

			for(int v:e[u])
				ans[u].emplace_back(v);
			
			id[++tot]=u;
		}
		lk.clear();
		for(int i=1;i<=n;i++) fa[i]=i,sz[i]=1;
		for(int i=tot;i;i--){
			int f=id[i];
			mp.clear();
			vector<int> real;
			int sum=0;
			for(int x:ans[f]){
				lk[{x,f}]=1;
				if(!mp[Find(x)]) mp[Find(x)]=1,real.emplace_back(x),sum+=sz[Find(x)];
			}
			if(sum!=in[f]){
				flg=1;
				break;
			}
			ans[f]=real;
			for(int x:ans[f]) merge(x,f);
		}
		if(lk.size()!=m) flg=1;
		if(!flg){
			for(int i=1;i<=m;i++){
				if(!lk[{U[i],V[i]}]&&!lk[{V[i],U[i]}]){
					flg=1;
					break;
				}
			}			
		}

		if(flg){
			puts("No");
			for(int i=1;i<=n;i++) vis[i]=0,in[i]=0,e[i].clear(),ans[i].clear();
			continue;
		}
		puts("Yes");
		for(int i=tot;i;i--){
			int f=id[i];
			printf("%d ",f);
			printf("%d ",(int)ans[f].size());
			for(int x:ans[f]) printf("%d ",x);
			puts("");
		}
		for(int i=1;i<=n;i++) vis[i]=0,in[i]=0,e[i].clear(),ans[i].clear();
	}
	return 0;
}