QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#491466#8757. 图cppcppcpp3WA 65ms4420kbC++141.7kb2024-07-25 19:41:542024-07-25 19:41:56

Judging History

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

  • [2024-07-25 19:41:56]
  • 评测
  • 测评结果:WA
  • 用时:65ms
  • 内存:4420kb
  • [2024-07-25 19:41:54]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
const int inf=1e9+7;

static char buf[1000000],*p1=buf,*p2=buf;
#define getchar() p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++
inline int wrd(){int x=0,f=1;char c=getchar();while(c<'0' || c>'9'){if(c=='-') f=-1;c=getchar();}while(c>='0' && c<='9') {x=(x<<3)+(x<<1)+c-48;c=getchar();}return x*f;}
inline void write(int x){static char buf[20];static int len=-1;if(x<0)putchar('-'),x=-x;do buf[++len]=x%10,x/=10;while(x);while(len>=0)putchar(buf[len--]+48);}

int n,m,k;

struct Graph{
	vector<bool> vs;
	vector<int> f,ans;
	vector<vector<int>> g;
	Graph(){
		f.resize(n+1),vs.resize(n+1),g.resize(n+1);
		for(int i=1;i<=n;++i) f[i]=i;
		ans.clear();
	}
	int fd(int x){return x==f[x]?x:f[x]=fd(f[x]);}
	void uty(int x,int y){f[fd(x)]=fd(y),g[x].push_back(y),g[y].push_back(x);}
	bool dfs(int u,int t){
		if(u==t){ans.push_back(t);return 1;}
		for(int v:g[u]){
			if(vs[v]) continue;
			vs[v]=1;
			if(dfs(v,t)){ans.push_back(u);return 1;}
		}
		return 0;
	}
};

void solve(){
	n=wrd(),m=wrd(),k=ceil(1.*m/(n-1));
	vector<Graph> G(k);
	bool flg=0;
	for(int i=1;i<=m;++i){
		int u=wrd(),v=wrd();
		int l=0,r=k-1,p=k;
		while(l<=r){
			int md=(l+r)>>1;
			if(G[md].fd(u)^G[md].fd(v)) p=md,r=md-1;
			else l=md+1;
		}
		if(p<k) G[p].uty(u,v),flg|=(p==k-1);
	}
	if(!flg){puts("-1");return;}
	int u=0,v=0;
	for(u=1;u<=n;++u) if(G[k-1].g[u].size()){v=G[k-1].g[u][0];break;}
	write(u),putchar(' '),write(v),puts("");
	for(int i=0;i<k;++i){
		G[i].dfs(v,u),write(G[i].ans.size()),putchar(' ');
		for(int x:G[i].ans) write(x),putchar(' ');
		puts("");
	}
}

signed main(){
	int T=wrd();
	while(T--) solve();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 65ms
memory: 4420kb

input:

10000
2 20
1 2
1 2
2 1
1 2
1 2
2 1
1 2
2 1
1 2
1 2
1 2
1 2
2 1
1 2
1 2
2 1
1 2
1 2
1 2
2 1
2 20
2 1
2 1
2 1
2 1
2 1
1 2
1 2
1 2
1 2
2 1
1 2
1 2
2 1
1 2
1 2
2 1
1 2
1 2
2 1
1 2
2 20
1 2
2 1
1 2
1 2
2 1
2 1
1 2
1 2
2 1
2 1
1 2
1 2
1 2
1 2
2 1
1 2
1 2
1 2
2 1
2 1
2 20
1 2
2 1
2 1
1 2
1 2
1 2
2 1
1 2
2 ...

output:

1 2
2 1 2 
2 1 2 
2 1 2 
2 1 2 
2 1 2 
2 1 2 
2 1 2 
2 1 2 
2 1 2 
2 1 2 
2 1 2 
2 1 2 
2 1 2 
2 1 2 
2 1 2 
2 1 2 
2 1 2 
2 1 2 
2 1 2 
2 1 2 
1 2
2 1 2 
2 1 2 
2 1 2 
2 1 2 
2 1 2 
2 1 2 
2 1 2 
2 1 2 
2 1 2 
2 1 2 
2 1 2 
2 1 2 
2 1 2 
2 1 2 
2 1 2 
2 1 2 
2 1 2 
2 1 2 
2 1 2 
2 1 2 
1 2
2 1 2 
2...

result:

ok Answer correct. (10000 test cases)

Test #2:

score: -100
Wrong Answer
time: 28ms
memory: 4348kb

input:

10000
5 20
2 1
2 5
5 3
3 1
4 5
1 4
4 3
4 5
3 5
5 4
2 3
5 2
3 4
3 5
1 4
4 3
4 2
2 1
1 3
5 1
5 20
4 2
1 3
1 2
4 5
2 4
3 1
5 3
5 1
4 5
4 3
2 4
1 4
4 3
5 2
1 2
3 5
1 5
4 1
3 4
4 3
5 20
1 4
1 3
1 5
5 1
4 5
3 4
4 5
2 3
1 2
2 4
4 5
4 5
2 4
2 5
4 2
4 3
4 2
2 5
2 1
3 1
5 20
2 5
2 3
4 5
4 2
3 4
2 1
5 4
2 5
2 ...

output:

1 3
4 1 2 5 3 
2 1 3 
3 1 4 3 
4 1 2 4 3 
4 1 3 5 3 
1 5
4 1 2 4 5 
3 1 3 5 
2 1 5 
3 1 2 5 
2 1 5 
2 5
4 2 3 1 5 
3 2 1 5 
3 2 4 5 
3 2 4 5 
4 2 5 4 5 
1 2
4 1 2 5 2 
4 1 2 4 2 
4 1 3 5 2 
4 1 3 4 2 
4 1 2 4 2 
1 5
4 1 5 4 5 
4 1 5 4 5 
2 1 5 
2 1 5 
4 1 5 3 5 
1 3
4 1 4 5 3 
3 1 5 3 
2 1 3 
2 1 3 ...

result:

FAIL No edge cross. (test case 4)