-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaiza19.php
47 lines (38 loc) · 861 Bytes
/
paiza19.php
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
<?php
function dfs($i, $graph, &$colorArr){
$colorArr[$i] = 1;
$q = new SplQueue();
$q->enqueue($i);
while(!$q->isEmpty()) {
$u = $q->dequeue();
foreach($graph[$u] as $v) {
if($colorArr[$v] == -1) {
$colorArr[$v] = 1 - $colorArr[$u];
$q->enqueue($v);
} else if($colorArr[$v] == $colorArr[$u]) {
return false;
}
}
}
return true;
}
function checkBipartite($N, $graph) {
$colorArr = array_fill(1, $N, -1);
for($i = 1; $i <= $N; $i++) {
if($colorArr[$i] == -1) {
if(!dfs($i, $graph, $colorArr)) {
return 'No';
}
}
}
return 'Yes';
}
list($N, $M) = explode(' ', trim(fgets(STDIN)));
$graph = [];
for($i = 0; $i < $M; $i++) {
list($a, $b) = explode(' ', trim(fgets(STDIN)));
$graph[$a][] = $b;
$graph[$b][] = $a;
}
echo checkBipartite($N, $graph);
?>