QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#615269#9449. New School Termucup-team5062#WA 0ms3624kbC++173.3kb2024-10-05 17:58:112024-10-05 17:58:12

Judging History

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

  • [2024-10-05 17:58:12]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3624kb
  • [2024-10-05 17:58:11]
  • 提交

answer

#include <vector>
#include <iostream>
using namespace std;

const int MOD = 986802277;

class modint {
private:
	int x;
public:
	modint() : x(0) {}
	modint(long long x_) : x(x_ >= 0 ? x_ % MOD : (x_ + 1) % MOD + (MOD - 1)) {}
	int val() const { return x; }
	modint& operator+=(const modint& m) { x = (x + m.x) % MOD; return *this; }
	modint& operator-=(const modint& m) { x = (x - m.x + MOD) % MOD; return *this; }
	modint& operator*=(const modint& m) { x = 1LL * x * m.x % MOD; return *this; }
	modint operator+(const modint& m) const { return modint(*this) += m; }
	modint operator-(const modint& m) const { return modint(*this) -= m; }
	modint operator*(const modint& m) const { return modint(*this) *= m; }
	modint pow(long long b) const {
		modint res(1), a(*this);
		while (b) {
			if (b & 1) {
				res *= a;
			}
			a *= a;
			b >>= 1;
		}
		return res;
	}
	modint inv() const { return pow(MOD - 2); }
};

#include <string>

string to_string(const vector<int>& arr) {
	string res = "[";
	for (int i = 0; i < int(arr.size()); i++) {
		if (i != 0) {
			res += ", ";
		}
		res += to_string(arr[i]);
	}
	res += "]";
	return res;
}

string to_string(const vector<modint>& arr) {
	string res = "[";
	for (int i = 0; i < int(arr.size()); i++) {
		if (i != 0) {
			res += ", ";
		}
		res += to_string(arr[i].val());
	}
	res += "]";
	return res;
}

int main() {
	// step #1. input
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	int N, M;
	cin >> N >> M;
	vector<int> A(M), B(M);
	for (int i = 0; i < M; i++) {
		cin >> A[i] >> B[i];
		A[i]--; B[i]--;
	}
	
	// step #2. initialize
	vector<int> col(2 * N), g(2 * N), c0(2 * N), c1(2 * N);
	for (int i = 0; i < 2 * N; i++) {
		col[i] = 0;
		g[i] = i;
		c0[i] = 1;
		c1[i] = 0;
	}

	// step #3. dynamic programming
	int sum = 0;
	vector<modint> dp(2 * N + 1);
	dp[0] = 1;
	auto add = [&](int x) -> void {
		if (x == 0) {
			return;
		}
		for (int i = sum; i >= 0; i--) {
			dp[i + x] += dp[i];
		}
		sum += x;
	};
	auto rollback = [&](int x) -> void {
		if (x == 0) {
			return;
		}
		sum -= x;
		for (int i = 0; i <= sum; i++) {
			dp[i + x] -= dp[i];
		}
	};
	for (int i = 0; i < 2 * N; i++) {
		add(1);
	}

	// step #4. processing
	for (int i = M - 1; i >= 0; i--) {
		if (g[A[i]] == g[B[i]]) {
			continue;
		}
		int ga = g[A[i]], gb = g[B[i]];
		int da = c0[ga] - c1[ga];
		int db = c0[gb] - c1[gb];
		if (da < db) {
			swap(A[i], B[i]);
			swap(ga, gb);
			swap(da, db);
		}
		int f = int(col[A[i]] == col[B[i]]);
		rollback(da);
		rollback(db);
		add(f == 0 ? da + db : da - db);
		if (dp[sum / 2].val() != 0) {
			for (int j = 0; j < 2 * N; j++) {
				if (g[j] == gb) {
					g[j] = ga;
					col[j] ^= f;
				}
			}
			if (f == 0) {
				c0[ga] += c0[gb];
				c1[ga] += c1[gb];
			} else {
				c0[ga] += c1[gb];
				c1[ga] += c0[gb];
			}
		} else {
			rollback(f == 0 ? da + db : da - db);
			rollback(f == 0 ? da - db : da + db);
			for (int j = 0; j < 2 * N; j++) {
				if (g[j] == gb) {
					g[j] = ga;
					col[j] ^= f ^ 1;
				}
			}
			if (f == 1) {
				c0[ga] += c0[gb];
				c1[ga] += c1[gb];
			} else {
				c0[ga] += c1[gb];
				c1[ga] += c0[gb];
			}
		}
	}

	// step #5. output
	for (int i = 0; i < 2 * N; i++) {
		cout << col[i];
	}
	cout << endl;

	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3536kb

input:

2 4
1 3
2 4
1 4
1 2

output:

1010

result:

ok Output is valid. OK

Test #2:

score: 0
Accepted
time: 0ms
memory: 3624kb

input:

3 7
2 5
1 3
4 6
2 6
4 5
2 4
5 6

output:

001101

result:

ok Output is valid. OK

Test #3:

score: -100
Wrong Answer
time: 0ms
memory: 3608kb

input:

1 0

output:

00

result:

wrong answer The number of 0s must be equal to that of 1s.