-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path10037-Decorating_the_Pastures.cpp
More file actions
92 lines (72 loc) · 1.41 KB
/
Copy path10037-Decorating_the_Pastures.cpp
File metadata and controls
92 lines (72 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
typedef struct Dat{
int node;
char sign;
}d;
char chk[50001];
vector<int> way[50001];
int N,M;
void Initialize(){
fill(&chk[0],&chk[50001],0);
}
void Input(){
cin>>N>>M;
int a,b;
for(int i=0;i<M;i++){
cin>>a>>b;
way[a].push_back(b);
way[b].push_back(a);
}
}
int bfs(int start){
int jcnt=0;
int fcnt=0;
queue<d> q;
q.push({start,'J'});
chk[start]='J';
while(!q.empty()){
int node = q.front().node;
char sign = q.front().sign;
q.pop();
if(sign=='F')
fcnt++;
else
jcnt++;
for(int i=0;i<way[node].size();i++){
int next = way[node][i];
if(chk[next]==sign)
return -1;
if(chk[next])
continue;
chk[next] = sign=='F'?'J':'F';
q.push({next,chk[next]});
}
}
return max(jcnt,fcnt);
}
void Solution(){
int ret=0;
for(int i=1;i<=N;i++){
if(chk[i])
continue;
int tmp = bfs(i);
if(tmp == -1){
cout<<tmp<<'\n';
return ;
}
ret+=tmp;
}
cout<<ret<<'\n';
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
Initialize();
Input();
Solution();
return 0;
}