QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#818952#9875. Don't Detect CycleNKheyuxiangWA 10ms4020kbC++141.0kb2024-12-18 11:05:522024-12-18 11:05:53

Judging History

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

  • [2024-12-18 11:05:53]
  • 评测
  • 测评结果:WA
  • 用时:10ms
  • 内存:4020kb
  • [2024-12-18 11:05:52]
  • 提交

answer

#include<bits/stdc++.h>
#define N 4005
using namespace std;
int n,m,tp,tq;
int U[N],V[N],R[N],P[N],Q[N];
bool vis[N];
void del(int i,int o){
	vis[i]=1;
	if(o) Q[++tq]=i;
	else P[++tp]=i;
	R[U[i]]--,R[V[i]]--;
}
void solve(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++) R[i]=0;
	for(int i=1;i<=m;i++){
		vis[i]=0;
		scanf("%d%d",&U[i],&V[i]);
		R[U[i]]++;
		R[V[i]]++;
	}
	tp=0;tq=0;
	while(1){
		int u=0,v=0;
		for(int i=1;i<=n;i++)
			if(R[i]==1) u=i;
		if(u){
			for(int i=1;i<=m;i++)
				if(!vis[i]&&(U[i]==u||V[i]==u)) del(i,1);
			continue;
		}
		for(int i=1;i<=m;i++){
			if(vis[i]) continue;
			if(R[U[i]]==2&&R[V[i]]==2){del(i,0),u=U[i],v=V[i];break;}
		}
		if(!u) break;
		for(int i=1;i<=m;i++)
			if(!vis[i]&&(U[i]==u||U[i]==v||V[i]==u||V[i]==v)) del(i,1);
	}
	if(tp+tq!=m) puts("-1");
	else{
		for(int i=1;i<=tq;i++) printf("%d ",Q[i]);
		for(int i=tp;i>=1;i--) printf("%d ",P[i]);
		printf("\n");
	}
}
int main(){
	int t;
	scanf("%d",&t);
	while(t--) solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3888kb

input:

1
4 4
1 2
2 3
3 4
4 2

output:

1 3 4 2 

result:

ok Correct

Test #2:

score: 0
Accepted
time: 0ms
memory: 3832kb

input:

4
4 5
1 2
2 3
3 4
3 1
1 4
5 3
1 2
2 3
3 4
9 10
3 5
1 8
5 8
4 9
6 7
7 9
1 2
1 4
2 4
4 6
8 10
1 4
3 8
2 5
3 4
1 5
5 8
2 8
5 7
4 5
3 7

output:

-1
3 2 1 
1 3 2 6 10 4 8 9 7 5 
-1

result:

ok Correct

Test #3:

score: -100
Wrong Answer
time: 10ms
memory: 4020kb

input:

50
3214 2907
970 1929
2860 3033
1322 2296
931 1192
861 2505
831 2469
231 2549
1 2306
1765 1842
999 3171
177 2007
1798 1894
827 3180
673 1738
1163 1573
2213 2781
2766 3200
1663 2197
1797 2281
315 2637
442 2689
558 2874
1520 2591
651 1923
1133 2920
1747 2412
1104 1528
313 2487
632 3124
660 2182
1581 2...

output:

2453 945 673 17 696 1626 2761 98 661 1491 13 2210 1206 575 2383 759 358 1298 824 2858 2591 2677 1682 1915 530 1833 959 2026 1324 879 2583 2869 1815 1004 723 472 1228 54 2586 1462 2298 185 2653 2814 2822 649 1141 2142 150 1189 961 804 2743 617 2219 2382 1988 2050 1717 1483 1026 835 2293 2150 1852 765...

result:

wrong answer Wrong - you can add all edges, but found: -1