QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#259946#1957. Friendship Graphsucup-team288#WA 47ms7392kbC++201.8kb2023-11-21 16:45:162023-11-21 16:45:17

Judging History

你现在查看的是最新测评结果

  • [2023-11-21 16:45:17]
  • 评测
  • 测评结果:WA
  • 用时:47ms
  • 内存:7392kb
  • [2023-11-21 16:45:16]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using i64 = long long;
#define pb emplace_back
#define AI(i) begin(i), end(i)
template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true); }
template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true); }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() { cerr << endl; }
template <class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ', kout(b...); }
template <class T> void debug(T l, T r) { while (l != r) cerr << *l << " \n"[next(l) == r], ++l; }
#else
#define DE(...) 0
#define debug(...) 0
#endif

int main() {
	cin.tie(nullptr)->sync_with_stdio(false);

	auto solve = [&]() {
		int n, m;
		cin >> n >> m;

		vector adj(n, vector<int>(n));
		while (m--) {
			int u, v;
			cin >> u >> v;
			u--, v--;
			adj[u][v] = adj[v][u] = true;
		}

		vector<int> col(n, -1);
		vector<int> dp(n + 1);
		dp[0] = true;

		for (int i = 0; i < n; i++) {
			if (col[i] != -1) {
				continue;
			}
			vector<int> q{i};
			col[i] = 0;
			for (int j = 0; j < int(q.size()); j++) {
				int u = q[j];
				for (int v = 0; v < n; v++) {
					if (u == v || adj[u][v]) {
						continue;
					}
					if (col[v] == -1) {
						q.push_back(v);
						col[v] = col[u] ^ 1;
					} else if (col[u] == col[v]) {
						cout << "-1\n";
						return;
					}
				}
			}
			int c0 = count_if(q.begin(), q.end(), [&](auto u) {
					return col[u] == 0; });
			int c1 = int(q.size()) - c0;
			for (int i = n; i >= 0; i--) {
				if (i >= c0) {
					dp[i] |= dp[i - c0];
				}
				if (i >= c1) {
					dp[i] |= dp[i - c1];
				}
			}
		}

		for (int i = n / 2; i >= 0; i--) {
			if (dp[i]) {
				cout << n - 2 * i << '\n';
				return;
			}
		}
	};

	solve();
}


Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 47ms
memory: 7392kb

input:

1000 499000
20 615
260 390
779 37
13 563
650 605
358 684
783 370
162 90
379 662
88 208
458 371
178 3
590 762
455 514
738 641
270 805
205 881
205 315
837 54
976 579
519 532
982 669
563 804
648 274
268 293
182 392
337 772
961 603
294 607
546 772
189 218
702 266
515 610
691 965
643 235
509 193
184 302
...

output:

0

result:

ok single line: '0'

Test #2:

score: -100
Wrong Answer
time: 46ms
memory: 7352kb

input:

1000 498999
35 65
880 835
501 309
590 758
882 857
356 493
43 623
644 637
361 785
58 317
26 11
595 521
723 629
611 36
789 29
30 650
426 475
852 862
667 137
677 173
656 44
457 279
680 567
789 989
368 873
510 721
128 584
835 956
419 779
607 568
317 790
932 580
336 400
74 265
772 855
939 816
448 883
381...

output:

0

result:

wrong answer 1st lines differ - expected: '2', found: '0'