QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#635775#9453. Graph Generatorucup-team5177#TL 960ms187816kbC++233.1kb2024-10-12 20:55:322024-10-14 16:54:19

Judging History

你现在查看的是最新测评结果

  • [2024-10-14 16:54:19]
  • 自动重测本题所有获得100分的提交记录
  • 测评结果:TL
  • 用时:960ms
  • 内存:187816kb
  • [2024-10-14 16:52:42]
  • hack成功,自动添加数据
  • (/hack/991)
  • [2024-10-12 20:55:32]
  • 评测
  • 测评结果:100
  • 用时:946ms
  • 内存:187676kb
  • [2024-10-12 20:55:32]
  • 提交

answer

#include <bits/stdc++.h>
static char buf[1000000],*paa=buf,*pd=buf;
static char buf2[1000000],*pp=buf2;
#define getchar() paa==pd&&(pd=(paa=buf)+fread(buf,1,1000000,stdin),paa==pd)?EOF:*paa++
inline void pc(char ch){
	if(pp-buf2==1000000) fwrite(buf2,1,1000000,stdout),pp=buf2;
	*pp++=ch;
}
inline void pcc(){
	fwrite(buf2,1,pp-buf2,stdout);
	pp=buf2;
}
inline int read(void){
	int w=1;
	register int x(0);register char c(getchar());
	while(c<'0'||c>'9'){if(c=='-') w=-1;c=getchar();}
	while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();
	return w*x;
}
void write(int x){
	static int sta[20];
	int top=0;
	do{
		sta[top++]=x%10,x/=10;
	}while(x);
	while(top) pc(sta[--top]+48);
}
void we(int x){
	write(x);
	pc('\n');
}
int T,N,M,ans[100009],dg[100009],pa[100009],id;
struct n_t{
	int a,b;
};
std::vector<int> as[100009];
inline int fr(int x) {
	while(x!=pa[x]) x=pa[x]=pa[pa[x]];
	return x;
}
inline void co(int x,int y) {
	x=fr(x),y=fr(y);
	if(x!=y) pa[x]=y;
}
bool cmp1(int m,int n) {
	return fr(m)<fr(n);
}
bool cmp2(n_t m,n_t n) {
	return fr(m.a)<fr(n.a);
}
bool sv(std::vector<int> x,std::vector<n_t> y) {
	if(x.size()==1) {
		ans[++id]=x[0];
		as[id].clear();
		return 1;
	}
	for(int i=0;i<x.size();i++) {
	//	printf("%d ",x[i]);
		dg[x[i]]=0;
		pa[x[i]]=x[i];
	}
	for(int i=0;i<y.size();i++) {
	//	printf("| %d %d",y[i].a,y[i].b);
		dg[y[i].a]++;
		dg[y[i].b]++;
	}
//	printf("\n");
	int pt=0;
	for(int i=0;i<x.size();i++) {
		if(dg[x[i]]==x.size()-1) {
			pt=x[i];
			break;
		}
	}
	if(pt==0) {
		return 0;
	}
	ans[++id]=pt;as[id].clear();
	std::vector<n_t> yy;
	std::vector<int> xx,rt,rt2;
	for(int i=0;i<y.size();i++) {
		if(y[i].a==pt||y[i].b==pt) continue;
		yy.push_back(y[i]);
		co(y[i].a,y[i].b);
	}
	y=yy;
	for(int i=0;i<x.size();i++) {
		if(x[i]!=pt) xx.push_back(x[i]);
	}
	x=xx;
	std::sort(y.begin(),y.end(),cmp2);
	std::sort(x.begin(),x.end(),cmp1);
	int i1=0,i2=0;
	int cur=id;
	for(int i=0;i<x.size();i++) {
		rt.push_back(fr(x[i]));
	}
	for(int i=0;i<y.size();i++) {
		rt2.push_back(fr(y[i].a));
	}
	for(int i=0;i<x.size();i++) {
		if(rt[i]!=x[i]) continue;
		int st=i1;
		while(i1<x.size()&&rt[i1]<=x[i]) {
			i1++;
		}
		int ed=i1-1;
		std::vector<int> xa;
		std::vector<n_t> ya;
		for(int j=st;j<=ed;j++) xa.push_back(x[j]);
		st=i2;
		while(i2<y.size()&&rt2[i2]<=x[i]) {
			if(rt2[i2]<x[i]) st=i2+1;
			i2++;
		}
		ed=i2-1;
		for(int j=st;j<=ed;j++) ya.push_back(y[j]);
		as[cur].push_back(x[i]);
		if(!sv(xa,ya)) return 0;
	}
	return 1;
}
signed main(void) {
	T=read();
	while(T--) {
		N=read();M=read();
		std::vector<int> x;
		std::vector<n_t> y;
		x.push_back(N+1);
		for(int i=1;i<=N;i++) x.push_back(i),y.push_back((n_t){i,N+1});
		for(int i=1;i<=M;i++) {
			int a=read(),b=read();
			y.push_back((n_t){a,b});
		}
		id=0;
		if(!sv(x,y)) {
			pc('N');pc('o');pc('\n');
			continue;
		}
		pc('Y');pc('e');pc('s');pc('\n');
		for(int i=id;i>=2;i--) {
			write(ans[i]),pc(' ');
			write(as[i].size()),pc(' ');
			for(int j=0;j<as[i].size();j++) write(as[i][j]),pc(' ');
			pc('\n');
		}
	}
	pcc();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 7852kb

input:

3
3 0
4 4
1 2
2 3
3 4
2 4
5 5
1 2
2 3
3 4
4 5
2 4

output:

Yes
3 0 
2 0 
1 0 
Yes
4 0 
3 1 4 
1 0 
2 2 1 4 
No

result:

ok 3 cases passed

Test #2:

score: 0
Accepted
time: 960ms
memory: 187816kb

input:

4176
10 15
1 4
9 1
3 7
8 1
2 1
5 4
5 1
10 1
7 1
10 7
10 3
6 4
3 1
6 1
9 2
7 10
6 7
1 7
1 4
7 2
4 2
5 2
3 1
1 6
2 6
5 1
6 8
5 2
3 1
4 6
2 1
5 1
1 4
6 2
6 1
9 15
5 1
4 2
7 1
1 8
2 3
5 8
1 9
4 3
6 5
9 2
3 1
4 1
7 2
9 7
1 6
6 11
1 2
3 5
3 1
3 2
4 3
1 6
4 2
1 4
5 4
5 1
5 2
6 6
1 6
6 3
1 3
1 5
4 2
2 1
10 ...

output:

Yes
8 0 
10 0 
7 1 10 
3 1 7 
6 0 
5 0 
4 2 5 6 
9 0 
2 1 9 
1 4 2 4 7 8 
No
No
No
Yes
6 0 
5 0 
4 1 5 
3 1 4 
2 1 5 
1 2 2 6 
No
No
Yes
6 0 
5 1 6 
4 1 5 
7 0 
3 1 7 
2 0 
1 3 2 3 5 
Yes
9 0 
8 0 
7 0 
5 0 
10 0 
6 0 
3 2 6 10 
2 5 3 5 7 8 9 
4 0 
1 2 4 7 
Yes
9 0 
8 0 
7 0 
5 2 7 8 
6 0 
3 2 6 7 
...

result:

ok 4176 cases passed

Extra Test:

score: -3
Extra Test Failed : Time Limit Exceeded on 2

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
49999 0 
49998 0 
49997 0 
49996 0 
49995 0 
49994 0 
49993 0 
49992 0 
49991 0 
49990 0 
49989 0 
49988 0 
49987 0 
49986 0 
49985 0 
49984 0 
49983 0 
49982 0 
49981 0 
49980 0 
49979 0 
49978 0 
49977 0 
49976 0 
49975 0 
49974 0 
49973 0 
49972 0 
49971 0 
49970 0 
49969 0 
49968 0 
49967 0 ...

result: