Submission #4071336
Source Code Expand
#include <stdio.h>
#include <stdlib.h>
#include <set>
#include <vector>
#define MAX_N (100000)
#define MAX_M (100000)
using namespace std;
int is_bipartite(const vector<vector<int>> &adj_lists, int cur,
vector<int> &gs) {
int ret = 1;
for (int nxt : adj_lists[cur]) {
if (gs[nxt] == 0) {
gs[nxt] = -gs[cur];
if (!is_bipartite(adj_lists, nxt, gs)) {
ret = 0;
break;
}
} else {
if (gs[nxt] == gs[cur]) {
ret = 0;
break;
}
}
}
return ret;
}
// read editorial
int main(int argc, char *argv[]) {
// read inputs
int n, m, as[MAX_M], bs[MAX_M];
scanf("%d %d", &n, &m);
for (int i = 0; i < m; i++) {
scanf("%d %d", &as[i], &bs[i]);
as[i]--; // NOTE : modified input
bs[i]--; // NOTE : modified input
}
// create adjacency lists
vector<vector<int>> adj_lists(n, vector<int>());
for (int i = 0; i < m; i++) {
const int a = as[i], b = bs[i];
adj_lists[a].push_back(b);
adj_lists[b].push_back(a);
}
// check whether the graph is bipartite
vector<int> gs(n, 0);
gs[0] = 1;
const int b = is_bipartite(adj_lists, 0, gs);
if (b) {
int cnt_b = 0, cnt_w = 0;
for (int g : gs) {
if (g > 0) {
cnt_b++;
} else {
cnt_w++;
}
}
// printf("cnt_b = %d, cnt_w = %d\n", cnt_b, cnt_w);
printf("%ld\n", (long) cnt_b * cnt_w - m);
} else {
printf("%ld\n", (long) (long) n * (n - 1) / 2 - m);
}
return 0;
}
Submission Info
Submission Time |
|
Task |
C - 3 Steps |
User |
khir |
Language |
C++14 (GCC 5.4.1) |
Score |
500 |
Code Size |
1569 Byte |
Status |
AC |
Exec Time |
40 ms |
Memory |
7296 KB |
Compile Error
./Main.cpp: In function ‘int main(int, char**)’:
./Main.cpp:35:25: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &m);
^
./Main.cpp:37:35: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &as[i], &bs[i]);
^
Judge Result
Set Name |
sample |
all |
Score / Max Score |
0 / 0 |
500 / 500 |
Status |
|
|
Set Name |
Test Cases |
sample |
sample-01.txt, sample-02.txt |
all |
sample-01.txt, sample-02.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 02-01.txt, 02-02.txt, 02-03.txt, 02-04.txt, 02-05.txt, 02-06.txt, 02-07.txt, 02-08.txt, 02-09.txt, 02-10.txt, 02-11.txt, 02-12.txt, 02-13.txt, 02-14.txt, sample-01.txt, sample-02.txt |
Case Name |
Status |
Exec Time |
Memory |
01-01.txt |
AC |
1 ms |
256 KB |
01-02.txt |
AC |
1 ms |
256 KB |
01-03.txt |
AC |
1 ms |
256 KB |
01-04.txt |
AC |
1 ms |
256 KB |
01-05.txt |
AC |
1 ms |
256 KB |
01-06.txt |
AC |
1 ms |
256 KB |
01-07.txt |
AC |
1 ms |
256 KB |
01-08.txt |
AC |
1 ms |
256 KB |
01-09.txt |
AC |
2 ms |
256 KB |
01-10.txt |
AC |
2 ms |
384 KB |
02-01.txt |
AC |
34 ms |
6400 KB |
02-02.txt |
AC |
37 ms |
6912 KB |
02-03.txt |
AC |
36 ms |
6912 KB |
02-04.txt |
AC |
40 ms |
6912 KB |
02-05.txt |
AC |
40 ms |
6912 KB |
02-06.txt |
AC |
37 ms |
6912 KB |
02-07.txt |
AC |
31 ms |
4608 KB |
02-08.txt |
AC |
34 ms |
6400 KB |
02-09.txt |
AC |
34 ms |
6912 KB |
02-10.txt |
AC |
18 ms |
2432 KB |
02-11.txt |
AC |
24 ms |
3072 KB |
02-12.txt |
AC |
34 ms |
5120 KB |
02-13.txt |
AC |
32 ms |
6272 KB |
02-14.txt |
AC |
39 ms |
7296 KB |
sample-01.txt |
AC |
1 ms |
256 KB |
sample-02.txt |
AC |
1 ms |
256 KB |