Submission #547817

# Submission time Handle Problem Language Result Execution time Memory
547817 2022-04-11T19:50:46 Z blue Collapse (JOI18_collapse) C++17
100 / 100
10523 ms 27092 KB
#include "collapse.h"
#include <vector>
#include <algorithm>
#include <set>
#include <queue>
#include <iostream>
using namespace std;
 
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
#define sz(x) int(x.size())
 
const int bs = 450;
 
 
 
 
 
struct dss
{
	int Z;
	vi parent;
	vi subtree;
 
	dss()
	{
		;
	}
 
	dss(int Z_)
	{
		Z = Z_;
		parent = subtree = vi(Z);
		for(int i = 0; i < Z; i++)
		{
			parent[i] = i;
			subtree[i] = 1;
		}
	}
 
	int root(int u)
	{
		if(parent[u] == u) return u;
		else return (parent[u] = root(parent[u]));
	}
 
	bool join(int u, int v)
	{
		u = root(u);
		v = root(v);
		if(u == v) return 0;
		if(subtree[u] < subtree[v]) swap(u, v);
		parent[v] = u;
		subtree[u] += subtree[v];
		return 1;
	}
 
	bool connected(int u, int v)
	{
		return root(u) == root(v);
	}
};
 
 
 
 
 
 
vi simulateCollapse(int N, vi T, vi X, vi Y, vi W, vi P)
{	
	int C = sz(X);
	int Q = sz(W);
 
	for(int i = 0; i < C; i++)
	{
		if(X[i] > Y[i])
			swap(X[i], Y[i]);
	}
 
	vi queries[N];
	for(int q = 0; q < Q; q++)
		queries[P[q]].push_back(q);
 
	vi res(Q);
 
 
 
	set<pii> edgeset;
 
	for(int bl = 0; bl*bs < C; bl++)
	{
		int l = bl*bs, r = min((bl+1)*bs - 1, C-1);
 
		// cerr << "\n\n\n\n\n\n\n";
		// cerr << "l r = " << l << ' ' << r << '\n';
 
		set<pii> critical;
		for(int i = l; i <= r; i++)
		{
			critical.insert({X[i], Y[i]});
		}
 
 		if(bl != 0)
 		{
 			for(int i = bl*bs - bs; i <= bl*bs - 1; i++)
			{
				// if(critical.find({X[i], Y[i]}) != critical.end())
				// 	continue;
	 
				if(T[i] == 0)
					edgeset.insert({X[i], Y[i]});
				else
					edgeset.erase({X[i], Y[i]});
			}
 		}
		
 
		set<pii> yescritical;
 
		for(pii z : critical)
		{
			if(edgeset.find(z) != edgeset.end())
			{
				// edgeset.erase(z);
				yescritical.insert(z);
			}
		}
 
		vi fwdedge[N], backedge[N];
		for(auto e : edgeset)
		{
			if(critical.find(e) == critical.end())
			{
				fwdedge[e.first].push_back(e.second);
				backedge[e.second].push_back(e.first);
			}
		}
 
		// cerr << "critical edges\n";
		// for(pii f : critical) cerr << f.first << ' ' << f.second << '\n';
		// 	cerr << '\n';
		// cerr << '\n';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
		dss dsu1(N);
 
		int comps = 0;
 
		vi checkedge[N];
		vi vis(N, 0);
 
 
		for(int i = 0; i <= N-2; i++)
		{
			comps++;
 
			for(int j : backedge[i])
			{
				comps -= dsu1.join(i, j);
			}
 
			for(int q : queries[i])
			{
				int t = W[q];
 
				if(!(l <= t && t <= r)) continue;
 
				set<pii> currcritical;
 
				for(pii w : yescritical)
					if(w.second <= P[q])
						currcritical.insert(w);
 
				vi vertlist;
 
				for(int ei = l; ei <= t; ei++)
				{
					if(Y[ei] >= P[q] + 1) continue;
 
					if(T[ei] == 0)
						currcritical.insert({X[ei], Y[ei]});
					else
						currcritical.erase({X[ei], Y[ei]});
				}
 
				for(auto k : currcritical)
				{
					int rx = dsu1.root(k.first);
					int ry = dsu1.root(k.second);
 
					checkedge[rx].push_back(ry);
					checkedge[ry].push_back(rx);
 
					vertlist.push_back(rx);
					vertlist.push_back(ry);
				}
 
				int thiscomps = comps;
 
				for(int z : vertlist)
				{
					if(vis[z]) continue;
 
					queue<int> tbv;
					tbv.push(z);
					vis[z] = 1;
 
					while(!tbv.empty())
					{
						int u = tbv.front();
						tbv.pop();
 
						for(int v : checkedge[u])
						{
							if(vis[v]) continue;
							vis[v] = 1;
							thiscomps--;
							tbv.push(v);
						}
					}
				}
 
				for(int v : vertlist)
				{
					vis[v] = 0;
					checkedge[v].clear();
				}
 
				res[q] += thiscomps;
				// cerr << "thiscomps = " << thiscomps << '\n';
			}
		}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
		dss dsu2(N);
 
		comps = 0;
 
		for(int i = 0; i < N; i++)
			checkedge[i].clear();
 
		vis = vi(N, 0);
 
 
		for(int i = N-1; i >= 1; i--)
		{
			comps++;
 
			for(int j : fwdedge[i])
			{
				comps -= dsu2.join(i, j);
			}
 
			for(int q : queries[i-1])
			{
				int t = W[q];
				if(!(l <= t && t <= r)) continue;
 
				set<pii> currcritical;
 
				for(pii w : yescritical)
					if(w.first >= P[q] + 1)
						currcritical.insert(w);
 
				vi vertlist;
 
				for(int ei = l; ei <= t; ei++)
				{
					if(X[ei] <= P[q]) continue;
 
					if(T[ei] == 0)
						currcritical.insert({X[ei], Y[ei]});
					else
						currcritical.erase({X[ei], Y[ei]});
				}
 
				// cerr << "l = " << l << ", t = " << t << '\n';
 
				for(auto k : currcritical)
				{
					int rx = dsu2.root(k.first);
					int ry = dsu2.root(k.second);
 
					checkedge[rx].push_back(ry);
					checkedge[ry].push_back(rx);
 
					vertlist.push_back(rx);
					vertlist.push_back(ry);
				}
 
				int thiscomps = comps;
 
				for(int z : vertlist)
				{
					if(vis[z]) continue;
 
					queue<int> tbv;
					tbv.push(z);
					vis[z] = 1;
 
					while(!tbv.empty())
					{
						int u = tbv.front();
						tbv.pop();
 
						for(int v : checkedge[u])
						{
							if(vis[v]) continue;
							vis[v] = 1;
							thiscomps--;
							tbv.push(v);
						}
					}
				}
 
				for(int v : vertlist)
				{
					vis[v] = 0;
					checkedge[v].clear();
				}
 
				res[q] += thiscomps;
				// cerr << "thiscomps = " << thiscomps << '\n';
			}
		}
	}
 
 
 
 
	return res;
}
# Verdict Execution time Memory Grader output
1 Correct 8 ms 468 KB Output is correct
2 Correct 3 ms 340 KB Output is correct
3 Correct 7 ms 456 KB Output is correct
4 Correct 9 ms 452 KB Output is correct
5 Correct 48 ms 468 KB Output is correct
6 Correct 118 ms 916 KB Output is correct
7 Correct 3 ms 1108 KB Output is correct
8 Correct 6 ms 1128 KB Output is correct
9 Correct 66 ms 1316 KB Output is correct
10 Correct 175 ms 1444 KB Output is correct
11 Correct 181 ms 1644 KB Output is correct
12 Correct 158 ms 1796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 2764 KB Output is correct
2 Correct 48 ms 2748 KB Output is correct
3 Correct 1016 ms 5132 KB Output is correct
4 Correct 170 ms 2988 KB Output is correct
5 Correct 1774 ms 5564 KB Output is correct
6 Correct 1408 ms 3436 KB Output is correct
7 Correct 3061 ms 11640 KB Output is correct
8 Correct 2288 ms 6556 KB Output is correct
9 Correct 52 ms 14668 KB Output is correct
10 Correct 112 ms 14672 KB Output is correct
11 Correct 1986 ms 15084 KB Output is correct
12 Correct 3221 ms 17232 KB Output is correct
13 Correct 4560 ms 20720 KB Output is correct
14 Correct 8566 ms 25340 KB Output is correct
15 Correct 6373 ms 25568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 2764 KB Output is correct
2 Correct 55 ms 2780 KB Output is correct
3 Correct 107 ms 2844 KB Output is correct
4 Correct 188 ms 2772 KB Output is correct
5 Correct 1933 ms 2752 KB Output is correct
6 Correct 2066 ms 3332 KB Output is correct
7 Correct 3457 ms 9284 KB Output is correct
8 Correct 5603 ms 13108 KB Output is correct
9 Correct 77 ms 17124 KB Output is correct
10 Correct 3014 ms 16288 KB Output is correct
11 Correct 8058 ms 26776 KB Output is correct
12 Correct 10443 ms 26796 KB Output is correct
13 Correct 8266 ms 26712 KB Output is correct
14 Correct 10523 ms 26916 KB Output is correct
15 Correct 7875 ms 26616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 468 KB Output is correct
2 Correct 3 ms 340 KB Output is correct
3 Correct 7 ms 456 KB Output is correct
4 Correct 9 ms 452 KB Output is correct
5 Correct 48 ms 468 KB Output is correct
6 Correct 118 ms 916 KB Output is correct
7 Correct 3 ms 1108 KB Output is correct
8 Correct 6 ms 1128 KB Output is correct
9 Correct 66 ms 1316 KB Output is correct
10 Correct 175 ms 1444 KB Output is correct
11 Correct 181 ms 1644 KB Output is correct
12 Correct 158 ms 1796 KB Output is correct
13 Correct 34 ms 2764 KB Output is correct
14 Correct 48 ms 2748 KB Output is correct
15 Correct 1016 ms 5132 KB Output is correct
16 Correct 170 ms 2988 KB Output is correct
17 Correct 1774 ms 5564 KB Output is correct
18 Correct 1408 ms 3436 KB Output is correct
19 Correct 3061 ms 11640 KB Output is correct
20 Correct 2288 ms 6556 KB Output is correct
21 Correct 52 ms 14668 KB Output is correct
22 Correct 112 ms 14672 KB Output is correct
23 Correct 1986 ms 15084 KB Output is correct
24 Correct 3221 ms 17232 KB Output is correct
25 Correct 4560 ms 20720 KB Output is correct
26 Correct 8566 ms 25340 KB Output is correct
27 Correct 6373 ms 25568 KB Output is correct
28 Correct 28 ms 2764 KB Output is correct
29 Correct 55 ms 2780 KB Output is correct
30 Correct 107 ms 2844 KB Output is correct
31 Correct 188 ms 2772 KB Output is correct
32 Correct 1933 ms 2752 KB Output is correct
33 Correct 2066 ms 3332 KB Output is correct
34 Correct 3457 ms 9284 KB Output is correct
35 Correct 5603 ms 13108 KB Output is correct
36 Correct 77 ms 17124 KB Output is correct
37 Correct 3014 ms 16288 KB Output is correct
38 Correct 8058 ms 26776 KB Output is correct
39 Correct 10443 ms 26796 KB Output is correct
40 Correct 8266 ms 26712 KB Output is correct
41 Correct 10523 ms 26916 KB Output is correct
42 Correct 7875 ms 26616 KB Output is correct
43 Correct 2319 ms 7224 KB Output is correct
44 Correct 3635 ms 13336 KB Output is correct
45 Correct 3224 ms 8840 KB Output is correct
46 Correct 4791 ms 15236 KB Output is correct
47 Correct 73 ms 17868 KB Output is correct
48 Correct 116 ms 16816 KB Output is correct
49 Correct 2621 ms 17204 KB Output is correct
50 Correct 3953 ms 18468 KB Output is correct
51 Correct 4877 ms 21248 KB Output is correct
52 Correct 7804 ms 23256 KB Output is correct
53 Correct 6515 ms 22728 KB Output is correct
54 Correct 8105 ms 25224 KB Output is correct
55 Correct 6813 ms 24664 KB Output is correct
56 Correct 7252 ms 26300 KB Output is correct
57 Correct 7511 ms 25404 KB Output is correct
58 Correct 9573 ms 25696 KB Output is correct
59 Correct 8457 ms 27092 KB Output is correct
60 Correct 10481 ms 27024 KB Output is correct