-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11080.cpp
executable file
·87 lines (80 loc) · 1.65 KB
/
11080.cpp
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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <bitset>
#include <queue>
using namespace std;
#define MAX 205
int v,e,ff,tt;
std::vector< std::vector<int> > graph;
bitset< MAX > visited;
bitset< MAX > color;
int bfs( int n )
{
int now,next;
queue< int > q;
q.push( n );
visited[ n ] = true;
color[ n ] = false;
ff = tt = 0;
++ff;
while( !q.empty() )
{
now = q.front() ; q.pop();
// cout << "entre " << now << endl;
// cout << color << endl;
for( int i = 0; i < graph[ now ].size() ; ++i )
{
next = graph[ now ][ i ];
if( now == next )
continue;
if( !visited[ next ] )
{
// cout << "cambie " << now << " " << next << endl;
q.push( next );
visited[ next ] = 1;
color[ next ] = ~color[ now ];
if( !color[next] )
++ff;
else
++tt;
}
else if( color[ now ] == color[ next ] ){
// cout << now << " " << next << " " << color[ now ] << " " << color[ next ] << endl;
return -1;
}
}
}
if( !tt )
return ff;
return min( ff , tt );
}
int main()
{
int t,a,b;
scanf("%d",&t);
while( t-- )
{
scanf("%d %d",&v,&e);
graph.assign( v , std::vector< int > () );
for( int i = 0 ; i < e ; ++i )
{
scanf("%d %d",&a,&b );
graph[ a ].push_back( b );
graph[ b ].push_back( a );
}
int res = 0,temp = 0;
visited.reset();
for( int i = 0 ; i < v && res != -1 ; ++i )
{
if( !visited[ i ] )
{
temp = bfs( i );
res = temp == -1 ? -1 : res + temp;
}
}
printf("%d\n", res );
}
return 0;
}