QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#814220#9875. Don't Detect Cycleucup-team3586#WA 1ms3880kbC++233.2kb2024-12-14 16:04:502024-12-14 16:04:50

Judging History

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

  • [2024-12-14 16:04:50]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3880kb
  • [2024-12-14 16:04:50]
  • 提交

answer

//Author: Kevin
#include<bits/stdc++.h>
//#pragma GCC optimize("O2")
using namespace std;
#define ll long long
#define ull unsigned ll
#define pb emplace_back
#define mp make_pair
#define ALL(x) (x).begin(),(x).end()
#define rALL(x) (x).rbegin(),(x).rend()
#define srt(x) sort(ALL(x))
#define rev(x) reverse(ALL(x))
#define rsrt(x) sort(rALL(x))
#define sz(x) (int)(x.size())
#define inf 0x3f3f3f3f
#define pii pair<int,int>
#define lb(v,x) (int)(lower_bound(ALL(v),x)-v.begin())
#define ub(v,x) (int)(upper_bound(ALL(v),x)-v.begin())
#define uni(v) v.resize(unique(ALL(v))-v.begin())
#define longer __int128_t
void die(string S){puts(S.c_str());exit(0);}
#include<bits/stdc++.h>
using namespace std;
int u[5005],v[5005],n,m,ban[5005];
vector<int> G[5005];
int dfn[5005],low[5005],tot;
set<pii> st;
vector<vector<int>> ans;
vector<int> cur;
void dfs(int u,int fa)
{
	cur.push_back(u);
	int tmp=fa;
	dfn[u]=low[u]=++tot;
	for(auto v:G[u])
		if(v!=fa)
		{
			if(dfn[v])
				low[u]=min(low[u],low[v]);
			else
			{
				dfs(v,u);
				low[u]=min(low[u],low[v]);
			}
		}
		else fa=0;
	if(low[u]==dfn[u])
	{
		vector<int> tmp;
		do
		{
			tmp.push_back(cur.back());
			cur.pop_back();
		}
		while(tmp.back()!=u);
		ans.push_back(tmp);
	}
}
vector<vector<int>> bcc(vector<int> vec)
{
	st.clear();
	ans.clear();
	cur.clear();
	tot=0;
	for(auto x:vec) dfn[x]=low[x]=0;
	for(auto x:vec) if(!dfn[x])
		dfs(x,0);
	return ans;
}
int ind[5005];
vector<int> Result;
void del(int u,int v)
{
	for(int i=1;i<=m;i++)
		if(u==::u[i]&&v==::v[i])
		{
			Result.pb(i);
			ban[i]=1;
		}
	for(int i=1;i<=m;i++)
		if(v==::u[i]&&u==::v[i])
		{
			Result.pb(i);
			ban[i]=1;
		}
	G[u].erase(find(G[u].begin(),G[u].end(),v));
	G[v].erase(find(G[v].begin(),G[v].end(),u));
}
int deg[5005];
int in[5005];
void solve(vector<int> vec)
{
	if(sz(vec)<=1) return ;
	for(auto x:vec) deg[x]=0;
	for(auto x:vec) in[x]=1;
	for(int i=1;i<=m;i++) if(!ban[i])
		if(in[u[i]]&&in[v[i]])
		{
			deg[u[i]]++;
			deg[v[i]]++;
		}
	for(int i=1;i<=m;i++) if(!ban[i])
		if(in[u[i]]&&in[v[i]])
			if(deg[u[i]]==deg[v[i]]&&deg[u[i]]==2)
			{
				del(u[i],v[i]);
				vector<vector<int>> B=bcc(vec);
				for(int j=0;j<sz(B);j++)
					for(auto u:B[j])
						ind[u]=j;
				for(int i=1;i<=m;i++) if(!ban[i])
					if(in[u[i]]&&in[v[i]])
						if(ind[u[i]]!=ind[v[i]])
							del(u[i],v[i]);
				for(auto x:vec)
					in[x]=0;
				for(auto x:B) solve(x);
				return ;
			}
	for(auto x:vec)
		in[x]=0;
	return ;
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t;
	cin>>t;
	while(t--)
	{
		cin>>n>>m;
		for(int i=1;i<=n;i++)
			G[i].clear();
		for(int i=1;i<=m;i++)
		{
			cin>>u[i]>>v[i];
			ban[i]=0;
			G[u[i]].pb(v[i]);
			G[v[i]].pb(u[i]);
		}
		vector<int> vec;
		for(int i=1;i<=n;i++)
			vec.pb(i);
		vector<vector<int>> vec2;
		vec2=bcc(vec);
		for(int i=0;i<sz(vec2);i++)
			for(auto x:vec2[i])
				ind[x]=i;
		Result.clear();
		for(int i=1;i<=m;i++)
			if(ind[u[i]]!=ind[v[i]])
				del(u[i],v[i]);
		for(auto arr:vec2)
			solve(arr);
		if(sz(Result)==m)
		{
			for(auto x:Result) cout<<x<<" ";
			cout<<'\n';
		}
		else
			cout<<"-1\n";
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

1
4 4
1 2
2 3
3 4
4 2

output:

1 2 3 4 

result:

ok Correct

Test #2:

score: -100
Wrong Answer
time: 0ms
memory: 3880kb

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
1 2 3 
1 2 3 5 4 6 10 7 8 9 
-1

result:

wrong answer Wrong - you detected cycles