QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#375369 | #6830. Just Some Bad Memory | wsc2008 | WA | 4ms | 16424kb | C++14 | 1.9kb | 2024-04-03 09:51:27 | 2024-04-03 09:51:28 |
Judging History
answer
#include<bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define pii pair<ll,ll>
#define rep(i,a,b) for(ll i=(a);i<=(b);++i)
#define per(i,a,b) for(ll i=(a);i>=(b);--i)
using namespace std;
bool Mbe;
inline ll read(){
ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-f;ch=getchar();}
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return x*f;
}
inline void write(ll x){
if(x<0)putchar('-'),x=-x;
if(x>9)write(x/10);
putchar(x%10+'0');
}
const ll N=2e5+9;
ll n,m,dep[N],vis[N],fa[N];
vector<ll>to[N],es[N],p;
void dfs2(ll x,ll f){
vis[x]=1,fa[x]=f,dep[x]=dep[f]+1;
for(ll y:to[x]){
if(y==f||vis[y])continue;
es[x].push_back(y);
es[y].push_back(x);
dfs2(y,x);
}
}
void dfs3(ll x,ll f){
dep[x]=dep[f]+1;
vis[x]=1;
p.push_back(x);
for(ll y:to[x]){
if(y==f||vis[y])continue;
dfs3(y,x);
}
}
bool Med;
int main(){
cerr<<fabs(&Med-&Mbe)/1048576.0<<"MB\n";
n=read(),m=read();
if(n<=3)return puts("-1"),0;
if(m<=2)return write(5-m),0;
rep(i,1,m){
ll x=read(),y=read();
to[x].push_back(y);
to[y].push_back(x);
}
ll fl1=0,fl2=0;
rep(i,1,n){
if(!vis[i])dfs2(i,0);
}
rep(i,1,n){
for(ll j:to[i]){
if(fa[j]==i||fa[i]==j)continue;
ll d=dep[i]+dep[j];
if(d&1)fl2=1;
else fl1=1;
}
}
if(fl1&&fl2)return puts("0"),0;
if(fl2)return puts("1"),0;
ll mx=0;
rep(i,1,n){
for(ll j:to[i]){
if(fa[j]==i||fa[i]==j)continue;
mx=max(mx,dep[i]+dep[j]);
}
}
memset(vis,0,sizeof(vis));
rep(i,1,n){
if(!vis[i]){
p.clear();
dfs3(i,0);
ll S=0;
for(ll x:p){
if(dep[x]>dep[S])S=x;
vis[x]=0;
}
p.clear();
dfs3(S,0);
for(ll x:p)mx=max(mx,dep[x]-1);
}
}
if(mx>=3){
if(fl1)return puts("1"),0;
return puts("2"),0;
}
if(fl1)return puts("2"),0;
puts("3");
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 14868kb
input:
3 3 1 2 2 3 1 3
output:
-1
result:
ok "-1"
Test #2:
score: 0
Accepted
time: 2ms
memory: 14636kb
input:
4 0
output:
5
result:
ok "5"
Test #3:
score: 0
Accepted
time: 0ms
memory: 16424kb
input:
5 4 1 2 2 3 3 4 4 5
output:
2
result:
ok "2"
Test #4:
score: 0
Accepted
time: 2ms
memory: 14136kb
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: 3ms
memory: 13552kb
input:
4 4 1 2 2 3 3 4 4 1
output:
1
result:
ok "1"
Test #6:
score: 0
Accepted
time: 2ms
memory: 14672kb
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: 4ms
memory: 15336kb
input:
4 3 1 2 2 3 3 1
output:
1
result:
wrong answer 1st words differ - expected: '2', found: '1'