QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#723949#9449. New School TermbeamishboysWA 1ms3596kbC++232.4kb2024-11-08 04:27:492024-11-08 04:27:49

Judging History

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

  • [2024-11-08 04:27:49]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3596kb
  • [2024-11-08 04:27:49]
  • 提交

answer

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

using ll = long long;
const ll mod = 1e9+7;
const int mn = 1e5+1;
int parent[mn], sz[mn];

void fix(ll &i) {
	if (i >= mod) i -= mod;
	if (i < 0) i += mod;
}
struct SSum {
	vector<ll> vals;
	ll hi = 0;
	SSum(ll hi) : vals(hi+1, 0), hi(hi) {vals[0] = 1;}
	void add(ll a, ll b) {
		if (a > b) swap(a,b);
		vector<ll> ndp(hi+1, 0);
		for (ll i = hi-a; i >= 0; i--) {
			ndp[i+a] = vals[i];
		}
		if (a!=b) {
			for (ll i = hi-b; i >= 0; i--) {
				ndp[i+b] += vals[i];
				fix(vals[i+b]);
			}
		}
		vals = ndp;
	}
	void remove(ll a, ll b) {
		if (a > b) swap(a,b);
		vector<ll> ndp(vals);
		if (a!=b) {
			for (ll i = 0; i+b-a <= hi; i++) {
				ndp[i+b-a] -= ndp[i];
				fix(ndp[i+b-a]);
			}
		}
		for (ll i = 0; i+a <= hi; i++) {
			ndp[i] = ndp[i+a];
		};
		vals = ndp;
	}
};

int find(int i) {
	if (parent[i] == i) return i;
	return (parent[i] = find(parent[i]));
}

vector<int> adj[mn];
void merge(int i, int j) {
	i = find(i);
	j = find(j);
	if (i == j) return;

	if (sz[i] < sz[j]) swap(i, j);
	sz[i] += sz[j];
	parent[j] = i;
}

int main() {
	int n, m; cin >> n >> m;
	vector<pair<int, int>> edge;
	SSum s(2*n);
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < i; j++) edge.push_back({i, j});
	}

	for (int i = 0; i < m; i++) {
		int u, v; cin >> u >> v;
		u--;v--;
		edge.push_back({u, v});
	}
	for (int i = 0; i < 2*n; i++) {
		sz[i] = 1;
		parent[i] = i;
	}
	for (int i = 0; i < 2*n; i++) s.add(0, 1);

	for (int i = edge.size()-1; i >= 0; i--) {
		auto [a, b] = edge[i];
		a = find(a); b = find(b);
		bool try1 = false;
	run:
		if (find(a) == find(b)) continue;

		int aOpp = adj[a].size() ? sz[find(adj[a][0])] : 0;
		s.remove(sz[a], aOpp);
		int bOpp = adj[b].size() ? sz[find(adj[b][0])] : 0;
		s.remove(sz[b], bOpp);

		s.add(sz[a] + bOpp, sz[b] + aOpp);
		if (s.vals[n] == 0) {
			//s.remove(sz[a] + bOpp, sz[b] + aOpp);
			//s.add(sz[a] + sz[b], aOpp + bOpp);
			if (adj[a].size()) a = adj[a][0];
			else if (adj[b].size()) b = adj[b][0];
			else continue;
			if (try1) continue;
			try1 = true;
			goto run;
		}

		if (adj[a].size()) merge(adj[a][0], b);
		if (adj[b].size()) merge(adj[b][0], a);
		adj[a].push_back(b);
		adj[b].push_back(a);
	}
	for (int i = 0; i < 2*n; i++) {
		cout << (find(i) != find(0));
	}
	cout << endl;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3596kb

input:

2 4
1 3
2 4
1 4
1 2

output:

0101

result:

ok Output is valid. OK

Test #2:

score: -100
Wrong Answer
time: 1ms
memory: 3520kb

input:

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

output:

011111

result:

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