QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#375770#6830. Just Some Bad Memorywjh213WA 1ms6472kbC++141.4kb2024-04-03 15:43:342024-04-03 15:43:34

Judging History

This is the latest submission verdict.

  • [2024-04-03 15:43:34]
  • Judged
  • Verdict: WA
  • Time: 1ms
  • Memory: 6472kb
  • [2024-04-03 15:43:34]
  • Submitted

answer

#include<bits/stdc++.h>
using namespace std;
#define pr pair<int,int>
#define fi first
#define se second
int const MAX=1e5+10;
vector<int> V[MAX];
bool ji,ou;
int dep[MAX],fa[MAX];
bool fl[MAX];
set<pr> S;
void maketag(int now,int top){
	if(now==top)return ;
	if(S.count({now,fa[now]})){
		ou=true;
		return ;
	}
	S.insert({now,fa[now]});
	maketag(fa[now],top);
}
void dfs(int t1,int last){
	if(fl[t1]){
		int tp=dep[last]-dep[t1];
		if(tp==1)return;
		if(tp%2==1)ou=true;
		if(tp%2==0){
			ji=true;
			maketag(last,t1);
		}
		return;
	}
	fa[t1]=last;
	dep[t1]=dep[last]+1;
	fl[t1]=true;
	for(auto it:V[t1]){
		dfs(it,t1);
	}
	return;
}
int maxx=0;
void dfs2(int now,int last){
	maxx=max(maxx,dep[now]);
	if(fl[now])return;
	fl[now]=true;
	for(auto it:V[now]){
		dep[it]=max(dep[it],dep[now]+1);
		dfs2(it,now);
	}
	return;
}
signed main(){
	int n,m;
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		int u,v;
		cin>>u>>v;
		V[u].push_back(v);
		V[v].push_back(u);
	}
	if(n<=3){
		cout<<-1;
		return 0;
	}
	if(m<=2){
		cout<<5-m;
		return 0;
	}
	for(int i=1;i<=n;i++){
		if(!fl[i])dfs(i,0);
	}
	if(ji&&ou){
		cout<<0;
		return 0;
	}
	memset(dep,0,sizeof(dep));
	memset(fl,0,sizeof(fl));
	for(int i=1;i<=n;i++){
		V[0].push_back(i);
	}
	dfs2(0,-1);
	if(ji){
		if(maxx>=4)cout<<1;
		else cout<<2;
		return 0;
	}
	if(ou){
		cout<<1;
		return 0;
	}
	if(maxx>=4)cout<<2;
	else cout<<3;
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3 3
1 2
2 3
1 3

output:

-1

result:

ok "-1"

Test #2:

score: 0
Accepted
time: 1ms
memory: 6076kb

input:

4 0

output:

5

result:

ok "5"

Test #3:

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

input:

5 4
1 2
2 3
3 4
4 5

output:

2

result:

ok "2"

Test #4:

score: 0
Accepted
time: 1ms
memory: 6004kb

input:

4 6
1 2
1 3
1 4
2 3
2 4
3 4

output:

0

result:

ok "0"

Test #5:

score: 0
Accepted
time: 1ms
memory: 6436kb

input:

4 4
1 2
2 3
3 4
4 1

output:

1

result:

ok "1"

Test #6:

score: 0
Accepted
time: 1ms
memory: 6152kb

input:

7 7
1 2
2 3
3 4
4 1
5 6
6 7
7 5

output:

0

result:

ok "0"

Test #7:

score: -100
Wrong Answer
time: 1ms
memory: 5948kb

input:

4 3
1 2
2 3
3 1

output:

0

result:

wrong answer 1st words differ - expected: '2', found: '0'