QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#76564#2214. Link Cut DigraphSanguineChameleonWA 739ms371772kbC++204.0kb2023-02-10 20:00:402023-02-10 20:00:42

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-02-10 20:00:42]
  • 评测
  • 测评结果:WA
  • 用时:739ms
  • 内存:371772kb
  • [2023-02-10 20:00:40]
  • 提交

answer

// BEGIN BOILERPLATE CODE

#include <bits/stdc++.h>
using namespace std;

#ifdef KAMIRULEZ
	const bool kami_loc = true;
	const long long kami_seed = 69420;
#else
	const bool kami_loc = false;
	const long long kami_seed = chrono::steady_clock::now().time_since_epoch().count();
#endif

const string kami_fi = "kamirulez.inp";
const string kami_fo = "kamirulez.out";
mt19937_64 kami_gen(kami_seed);

long long rand_range(long long rmin, long long rmax) {
	uniform_int_distribution<long long> rdist(rmin, rmax);
	return rdist(kami_gen);
}

long double rand_real(long double rmin, long double rmax) {
	uniform_real_distribution<long double> rdist(rmin, rmax);
	return rdist(kami_gen);
}

void file_io(string fi, string fo) {
	if (fi != "" && (!kami_loc || fi == kami_fi)) {
		freopen(fi.c_str(), "r", stdin);
	}
	if (fo != "" && (!kami_loc || fo == kami_fo)) {
		freopen(fo.c_str(), "w", stdout);
	}
}

void set_up() {
	if (kami_loc) {
		file_io(kami_fi, kami_fo);
	}
	ios_base::sync_with_stdio(0);
	cin.tie(0);
}

void just_do_it();

void just_exec_it() {
	if (kami_loc) {
		auto pstart = chrono::steady_clock::now();
		just_do_it();
		auto pend = chrono::steady_clock::now();
		long long ptime = chrono::duration_cast<chrono::milliseconds>(pend - pstart).count();
		string bar(50, '=');
		cout << '\n' << bar << '\n';
		cout << "Time: " << ptime << " ms" << '\n';
	}
	else {
		just_do_it();
	}
}

int main() {
	set_up();
	just_exec_it();
	return 0;
}

// END BOILERPLATE CODE

// BEGIN MAIN CODE

vector<long long> R;

struct node {
	int id = 0;
	int sz = 0;
	bool f = false;
	vector<node*> adj1, adj2;
	node *cmp = nullptr;

	node() {

	}

	node(int _id, int _sz) {
		id = _id;
		sz = _sz;
	}
};

struct edge {
	node *u = nullptr;
	node *v = nullptr;

	int t = 0;

	edge() {

	}

	edge(node *_u, node *_v, int _t) {
		u = _u;
		v = _v;
		t = _t;
	}
};

struct graph {
	int lt = 0;
	int rt = 0;
	vector<edge> E;
	node *cur = nullptr;
	long long res = 0;

	deque<node*> topo;

	void dfs1(node *u) {
		u->f = true;
		for (auto v: u->adj1) {
			if (!v->f) {
				dfs1(v);
			}
		}
		topo.push_front(u);
	}

	void dfs2(node *u) {
		res -= 1LL * u->sz * (u->sz - 1) / 2;
		cur->sz += u->sz;
		u->cmp = cur;
		for (auto v: u->adj2) {
			if (!v->cmp) {
				dfs2(v);
			}
		}
	}

	void solve() {
		long long tmp = res;
		int mt = (lt + rt) / 2;
		for (auto e: E) {
			if (e.t <= mt) {
				e.u->adj1.push_back(e.v);
				e.v->adj2.push_back(e.u);
			}
		}
		for (auto e: E) {
			if (e.t <= mt) {
				if (!e.u->f) {
					dfs1(e.u);
				}
				if (!e.v->f) {
					dfs1(e.v);
				}
			}
		}
		for (auto u: topo) {
			if (!u->cmp) {
				cur = new node(u->id, 0);
				dfs2(u);
				res += 1LL * cur->sz * (cur->sz - 1) / 2;
			}
		}
		if (lt == rt) {
			R[lt] = res;
			return;
		}
		graph Gl, Gr;
		Gl.lt = lt;
		Gl.rt = mt;
		Gl.res = tmp;
		Gr.lt = mt + 1;
		Gr.rt = rt;
		Gr.res = res;
		vector<edge> L;
		for (auto e: E) {
			if (!e.u->cmp) {
				e.u->cmp = new node(e.u->id, 1);
			}
			if (!e.v->cmp) {
				e.v->cmp = new node(e.v->id, 1);
			}
			if (e.t <= mt && e.u->cmp == e.v->cmp) {
				L.push_back(e);
			}
			else {
				Gr.E.emplace_back(e.u->cmp, e.v->cmp, max(e.t, mt + 1));
			}
		}
		for (auto e: E) {
			e.u->cmp = nullptr;
			e.v->cmp = nullptr;
		}
		for (auto e: L) {
			if (!e.u->cmp) {
				e.u->cmp = new node(e.u->id, 1);
			}
			if (!e.v->cmp) {
				e.v->cmp = new node(e.v->id, 1);
			}
			Gl.E.emplace_back(e.u->cmp, e.v->cmp, e.t);
		}
		Gl.solve();
		Gr.solve();
	}
} G;

void just_do_it() {
	int n, m;
	cin >> n >> m;
	G.lt = 1;
	G.rt = m;
	vector<node*> a(n + 1);
	for (int i = 1; i <= n; i++) {
		a[i] = new node();
		a[i]->id = i;
		a[i]->sz = 1;
	}
	R.resize(m + 1);
	for (int i = 1; i <= m; i++) {
		int u, v;
		cin >> u >> v;
		G.E.emplace_back(a[u], a[v], i);
	}
	G.solve();
	for (int i = 1; i <= m; i++) {
		cout << R[i] << '\n';
	}
}

// END MAIN CODE

Details

Test #1:

score: 0
Wrong Answer
time: 739ms
memory: 371772kb